Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

TABLE

CREATE TABLE

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:

Example

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
);

CREATE TABLE Using Another Table

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:

Example

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;  

ALTER TABLE

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:

Example

ALTER TABLE Customers
ADD Email varchar(255);

The subsequent SQL removes the “Email” column from the “Customers” table:

Example

ALTER TABLE Customers
DROP COLUMN Email;

DROP TABLE

The DROP TABLE command deletes a table from the database.

The following SQL deletes the table named “Shippers”:

Example

DROP TABLE Shippers;
Exercise caution before deleting a table, as doing so will result in the loss of all information stored in the table!

TRUNCATE TABLE

The TRUNCATE TABLE command removes all data inside a table while preserving the table structure.

The following SQL truncates the table named “Categories”:

Example

TRUNCATE TABLE Categories;