DDL Statements
Home ] Up ] DML Statements ] [ DDL Statements ] Transaction Statements ]

 

 

Data Definition Language

Schemas and Schema Objects

A database consists of a set of schemas, each of which contains, or owns, a set of schema objects, such as tables, etc.

Each schema object is created using a CREATE statement.

Creating Schema Objects

Each schema object typically has an associated CREATE statement which is used to create an instance of that object.

Creating a Table

To create a table, you use the CREATE TABLE statement. For example:

CREATE TABLE employees
{
  employeeID   INTEGER,
  name         CHARACTER(40),
  department   INTEGER,
  age          INTEGER,
  salary       DECIMAL(7,2)
};

which creates the Employees table specified earlier. Each column is specified using a name and a data type.

SQL Data Types

Here are some of the valid SQL data types:

Data Type Description
CHARACTER(n) A character field containing exactly n characters
CHARACTER VARYING(n) A character field containing up to n characters
INTEGER An integer field
SMALLINT An integer field of smaller size than INTEGER
NUMERIC(p, s) A numeric field specifying precision p, and scale s
DECIMAL(p, s) A decimal field specifying precision p, and scale s
REAL A single precision floating point value
DOUBLE PRECISION A double precision floating point value
FLOAT(p) A floating point value with precision p
DATE Year, month and day values of a date.
TIME Hour, minute, second values of a time
TIMESTAMP Year, month, day, hour, minute and second values

Changing Schema Objects

Once a schema object has been created, it may be possible to change its attributes (depending on the type of object, and the support from the database vendor), using an ALTER statement.

Changing the Attributes of a Table

You can change some (usually not all) of the attributes of a table using the ALTER TABLE statement:

ALTER TABLE employees
ADD COLUMN last_review DATE;
ALTER TABLE departments
DROP COLUMN manager;

Removing Schema Objects

You can use a DROP statement to remove an existing schema object.

Removing a Table

You use the DROP TABLE statement to remove an existing table:

DROP TABLE employees;

Note: Do not confuse the DROP statement with the DELETE statement!

 
The page was last updated February 19, 2008