Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

SQL Create Table

The CREATE TABLE command creates a new table in the database.

ALTER TABLE modifies an existing table.

To delete a table use DROP TABLE.

Example

#

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

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.


More Examples

ALTER TABLE

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: Add an email column to the Customer table.
ALTER TABLE Customer
ADD Email NVARCHAR(50)
Result:   1 column added

DROP TABLE

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: Remove the Customer table.
DROP TABLE Customer
Result:  table dropped

TRUNCATE TABLE

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: Quickly remove all data from the Customer table.
TRUNCATE TABLE Customer

For more details on TRUNCATE see our TRUNCATE function reference.

Result:  table truncated

You may also like



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.