DEV Community

Cover image for ✅ SQL Table Management 🧱🗄️
ssekabira robert sims
ssekabira robert sims

Posted on

✅ SQL Table Management 🧱🗄️

1️⃣ CREATE TABLEUsed to create a new table in the database.

CREATE TABLE Students (
  ID INT PRIMARY KEY,
  Name VARCHAR(50),
  Age INT,
  Grade VARCHAR(10)
);
Enter fullscreen mode Exit fullscreen mode

2️⃣ ALTER TABLEUsed to modify an existing table structure.

Add a new column

ALTER TABLE Students ADD Email VARCHAR(100);
Enter fullscreen mode Exit fullscreen mode

Rename a column (syntax may vary by SQL version)

ALTER TABLE Students RENAME COLUMN Name TO FullName;
Enter fullscreen mode Exit fullscreen mode

Change data type

ALTER TABLE Students MODIFY Age SMALLINT;
Enter fullscreen mode Exit fullscreen mode

Drop a column

ALTER TABLE Students DROP COLUMN Grade;
Enter fullscreen mode Exit fullscreen mode

3️⃣ DROP TABLEPermanently deletes the entire table and its data.

DROP TABLE Students;
Enter fullscreen mode Exit fullscreen mode

⚠️ Caution: This action cannot be undone.

4️⃣ TRUNCATE TABLERemoves all rows but keeps the table structure.

TRUNCATE TABLE Students;
Enter fullscreen mode Exit fullscreen mode

5️⃣ RENAME TABLEChange the table name.

RENAME TABLE Students TO Alumni;
Enter fullscreen mode Exit fullscreen mode

6️⃣ DESCRIBE TableView the structure of a table.

DESCRIBE Students;
Enter fullscreen mode Exit fullscreen mode

💡 Pro Tips:

  • Always back up data before making structural changes.
  • Use IF EXISTS or IF NOT EXISTS to avoid errors.
DROP TABLE IF EXISTS Students;
CREATE TABLE IF NOT EXISTS Teachers (...);
Enter fullscreen mode Exit fullscreen mode

💬 Double Tap ❤️ For More!

Top comments (0)