SQL CREATE TABLE


David Haertzen David Haertzen, Tutorial Author
Check out our
Data Warehousing Tutorial.
Previous | Next

SQL > SQL Administration > SQL CREATE TABLE Statement

What is the SQL CREATE TABLE Statement?

The SQL CREATE TABLE statement is the SQL command that adds a new table to an SQL database. Tables are a basic unit of organization and storage of data in SQL. Each table tends to represent an entity such as a customer, product, code or event. A table is similar to a file in a non-database system.

Tables are organized into rows and columns. For example, a customer table would likely have a seperate row for each customer. Columns are attributes that describe a row. For example, a row in a customer table would have columns like customer_name, customer_nbr, account_balance_amt and established_date.

Why Use the SQL CREATE TABLE Statement?

Anytime you want to add a new table to the database you would use the SQL CREATE TABLE statement.

How To Use the SQL CREATE TABLE Statement

The SQL CREATE TABLE command is used as follows.

SQL CREATE TABLE Statement Syntax

 
CREATE TABLE <table_name> (
<column_name1> <datatype1> <constraint1>
<column_name2> <datatype2> <constraint2>
<constraint-list>
)  

The number of characters that can make up SQL table names and column names varies by DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic - not a number or special character. The name of a new table can not duplicate the name of an existing table and should not be the same as a SQL reserved word. The underscore character can be used to improve readability. The same column name can not be repeated within a table. List elements are seperated by commas.

Here are some example datatypes:

SQL DatatypeDescription
integer(size)
int(size)
smallint(size)
tinyint(size)
Integers
decimal(size,decimals)
numeric(size,decimals)
Numbers with decimals
char(size) Fixed length character string
varchar(size) Variable length character string
date A date in yyyymmdd format

SQL CREATE TABLE Statement Example

The following example creates a table named product with columns named product_nbr, product_name, product_status_code, start_date, end_date and raw_material_cost_amt.

This SQL CREATE TABLE Statement is executed:

 
CREATE TABLE product ( product_nbr int NOT NULL,
product_name varchar(40) NOT NULL,
product_status_code char(10) NOT NULL,
start_date date NULL,
end_date date NULL,
raw_material_cost decimal(12,2) NULL,
primary key (product_nbr)
)
 

Copyright© 1999-2015, First Place Software, Inc.