Content
1. CREATE TABLE
- Purpose : Creates a new table in the database.
-
Syntax
:
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ... );
-
Example
:
CREATE TABLE Employees ( EmployeeID int, FirstName varchar(255), LastName varchar(255), BirthDate date );
2. ALTER TABLE
- Purpose : Modifies an existing table (e.g., adding a column).
-
Syntax
:
ALTER TABLE table_name ADD column_name datatype;
-
Example
:
ALTER TABLE Employees ADD Email varchar(255);
3. DROP TABLE
- Purpose : Deletes an existing table in the database.
-
Syntax
:
DROP TABLE table_name;
-
Example
:
DROP TABLE Employees;
4. TRUNCATE TABLE
- Purpose : Removes all data from a table, but not the table itself.
-
Syntax
:
TRUNCATE TABLE table_name;
-
Example
:
TRUNCATE TABLE Employees;
5. CREATE INDEX
- Purpose : Creates an index on a table column for faster retrieval of data.
-
Syntax
:
CREATE INDEX index_name ON table_name (column1, column2, ...);
-
Example
:
CREATE INDEX idx_lastname ON Employees (LastName);
6. DROP INDEX
- Purpose : Deletes an index from a table.
-
Syntax
:
DROP INDEX index_name ON table_name;
- Note: Syntax may vary slightly depending on the database system.
-
Example
:
DROP INDEX idx_lastname ON Employees;
7. RENAME TABLE
- Purpose : Renames an existing table.
-
Syntax
:
ALTER TABLE table_name RENAME TO new_table_name;
-
Example
:
ALTER TABLE Employees RENAME TO Staff;
Remember, the exact syntax for these commands can vary slightly depending on the SQL database system (e.g., MySQL, PostgreSQL, SQL Server, etc.). Always refer to the specific documentation for your database system for the most accurate information.