English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Usage and examples of the SQL TABLE keyword

SQL Keyword Reference

CREATE TABLE

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

The following SQL creates a table named "Persons" that contains 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) 
);

Create a table using another table

You can also use to create a copy of an existing table CREATE TABLE

The following SQL creates a new table called "TestTables" (a copy of the "Customers" table): 

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

ALTER TABLE (Modify Table)

The ALTER TABLE command adds, deletes, or modifies columns in the table.

The ALTER TABLE command also adds and removes various constraints in the table.

The following SQL adds a column named "Email" to the "Customers" table:

ALTER TABLE Customers
ADD Email varchar(255);

The following SQL deletes the "Email" column from the "Customers" table:

ALTER TABLE Customers
DROP COLUMN Email;

DROP TABLE (Delete Table)

The DROP TABLE command will delete the table from the database.

The following SQL deletes the table "Shippers":

  DROP TABLE Shippers;

Note:Be careful before deleting a table. Deleting a table will result in the loss of all information stored in the table!

TRUNCATE TABLE (Clear Table)

The TRUNCATE TABLE command will delete the data in the table but will not delete the table itself.

The following SQL clears the "Categories" table: 

TRUNCATE TABLE Categories;

SQL Keyword Reference