CREATE TABLE Statement

The CREATE TABLE statement names the table and usually lays down the structure for the table, naming and giving form to most if not all of the columns as well.

The basic syntax looks like this:

CREATE TABLE table
(
column1 data_type,
column2 data_type,
column3 data_type,
etc.
)

This is also a good time to add a bit more structure, such as the PRIMARY KEY which denotes the unique identifier for each row. It is often best to make this an ID number that is auto incremented. This is also a good time to add in CHECKs which restrict the input to individual cells within columns.

If we wished to set up a table of fictional characters with the primary key being the "id" and we did not wish to include characters from the "Flashman" series, we might use the following:

CREATE TABLE fiction_chars
(
id INT NOT NULL,
name VARCHAR (255),
description TEXT,
book VARCHAR (255) CHECK (book != 'Flashman'),
author VARCHAR (255),
PRIMARY KEY(id)
)

< CREATE INDEX SQL | Data Types in SQL >


Send notes in disappearing ink!

Interesting Pages