The CREATE TABLE command establishes a new table in the database.
The provided SQL creates a table named “Persons” comprising five columns: PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) ); |
You can also create a copy of an existing table using CREATE TABLE.
The following SQL creates a new table named “TestTables” as a copy of the “Customers” table:
CREATE TABLE TestTable AS SELECT customername, contactname FROM customers; |
The ALTER TABLE command facilitates adding, deleting, or modifying columns in a table, as well as adding and deleting various constraints.
The following SQL adds an “Email” column to the “Customers” table:
ALTER TABLE Customers ADD Email varchar(255); |
The subsequent SQL removes the “Email” column from the “Customers” table:
ALTER TABLE Customers DROP COLUMN Email; |
The DROP TABLE command deletes a table from the database.
The following SQL deletes the table named “Shippers”:
DROP TABLE Shippers; |
Exercise caution before deleting a table, as doing so will result in the loss of all information stored in the table! |
The TRUNCATE TABLE command removes all data inside a table while preserving the table structure.
The following SQL truncates the table named “Categories”:
TRUNCATE TABLE Categories; |