The Data Management Center

SQL CREATE TABLE


Google
 
Web infogoal.com
HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX


SQL Book Picks

SQL Cookbook
SQL Cookbook by Anthony Molinaro


SQL Visual Quick Start
SQL: Visual QuickStart Guide (2nd Edition) by Chris Fehily

SQL Bible
SQL Bible by Alex Kriegel, Boris M. Trukhnov

SQL Complete Reference
SQL: The Complete Reference, Second Edition by James R Groff, Paul N. Weinberg

SQL for Smarties
Joe Celko's SQL for Smarties: Advanced SQL Programming Third Edition by Joe Celko

Murach SQL Server
Murach's SQL for SQL Server by Bryan Syverson

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)
)
 

infogoal.com HOME
SQL OVERVIEW

SQL BASICS
SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE

SQL ADMINISTRATION
SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW

SQL ADVANCED
SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

SQL SYNTAX

Data Model Resource Book Vol 1
Data Model Resource Book Vol 2
HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX
Copyright© 1999-2006, First Place Software, Inc.