DDL Commands Notes
1. CREATE TABLE
- Purpose: Used to create a new table in the database.
- Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
- Example:
CREATE TABLE Students (
ID INT,
Name VARCHAR(50),
Age INT
);
2. ALTER TABLE
- Purpose: Used to modify an existing table structure (e.g., adding/removing columns).
- Syntax:
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
- Example:
ALTER TABLE Students ADD Address VARCHAR(100);
3. DROP TABLE
- Purpose: Used to delete an existing table and its data.
- Syntax:
DROP TABLE table_name;
- Example:
DROP TABLE Students;
4. CREATE INDEX
- Purpose: Used to create an index on a table column to speed up queries.
- Syntax:
CREATE INDEX index_name ON table_name (column_name);
- Example:
CREATE INDEX idx_name ON Students (Name);
5. DROP INDEX
- Purpose: Used to delete an index from a table.
- Syntax:
DROP INDEX index_name;
- Example:
DROP INDEX idx_name;
6. TRUNCATE TABLE
- Purpose: Used to remove all records from a table without deleting the table itself.
- Syntax:
TRUNCATE TABLE table_name;
- Example:
TRUNCATE TABLE Students;