The CREATE TABLE command creates a new table in the database.
ALTER TABLE modifies an existing table.
To delete a table use DROP TABLE.
This example creates a table named Client.
CREATE TABLE Client (
Id INT IDENTITY,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Email VARCHAR(60),
Phone VARCHAR(20),
CONSTRAINT PK_Client PRIMARY KEY(Id)
)
This table is created with a primary key.
Syntax to create a table.
CREATE TABLE table-name ( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, CONSTRAINT pk-name PRIMARY KEY( one or more columns ) )
Syntax to add a new column.
ALTER TABLE table-name ADD column-name datatype
Syntax to remove a column.
ALTER TABLE table-name DROP COLUMN column_name
Dropping a column will also remove all its data.
Syntax to remove a table.
DROP TABLE table-name
Dropping a table will also remove all its data.
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
ALTER TABLE Customer
ADD Email NVARCHAR(50)
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
DROP TABLE Customer
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
TRUNCATE TABLE Customer
For more details on TRUNCATE see our TRUNCATE function reference.