DEV Community

Cover image for βœ… SQL Table Management πŸ§±πŸ—„οΈ
ssekabira robert sims
ssekabira robert sims

Posted on

βœ… SQL Table Management πŸ§±πŸ—„οΈ

1️⃣ CREATE TABLE – Used 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 TABLE – Used 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 TABLE – Permanently deletes the entire table and its data.

DROP TABLE Students;
Enter fullscreen mode Exit fullscreen mode

⚠️ Caution: This action cannot be undone.

4️⃣ TRUNCATE TABLE – Removes all rows but keeps the table structure.

TRUNCATE TABLE Students;
Enter fullscreen mode Exit fullscreen mode

5️⃣ RENAME TABLE – Change the table name.

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

6️⃣ DESCRIBE Table – View 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)