Infogoal Logo
GOAL DIRECTED LEARNING
Master SQL

SQL TUTORIAL HOME

SQL OVERVIEW
SQL SYNTAX
SQL BOOKREVIEWS

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 CREATE TABLE Statement

Previous | Next

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

Previous | Next


Advertisements

Advertisements:


Infogoal.com is organized to help you gain mastery.
Examples may be simplified to facilitate learning.
Content is reviewed for errors but is not warranted to be 100% correct.
In order to use this site, you must read and agree to the terms of use, privacy policy and cookie policy.
Copyright 2006-2020 by Infogoal, LLC. All Rights Reserved.

Infogoal Logo