SQL Create Table

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

SQL - CREATE TABLE

http://www.tuto rialspo int.co m/sql/sql-cre ate -table .htm


Co pyrig ht tuto rials po int.co m

Creating a basic table involves naming the table and defining its columns and each column's data type. T he SQL CREAT E T ABLE statement is used to create a new table.

Syntax:
Basic syntax of CREAT E T ABLE statement is as follows:
CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) );

CREAT E T ABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. T he unique name or identifier for the table follows the CREAT E T ABLE statement. T hen in brackets comes the list defining each column in the table and what sort of data type it is. T he syntax becomes clearer with an example below. A copy of an existing table can be created using a combination of the CREAT E T ABLE statement and the SELECT statement. You can check complete details at Create T able Using another T able.

Example:
Following is an example, which creates a CUST OMERS table with ID as primary key and NOT NULL are the constraints showing that these fields can not be NULL while creating records in this table:
SQL> CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );

You can verify if your table has been created successfully by looking at the messag e displayed by the SQL server, otherwise you can use DESC command as follows:
SQL> DESC CUSTOMERS; +---------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+---------+-------+ | ID | int(11) | NO | PRI | | | | NAME | varchar(20) | NO | | | | | AGE | int(11) | NO | | | | | ADDRESS | char(25) | YES | | NULL | | | SALARY | decimal(18,2) | YES | | NULL | | +---------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)

Now, you have CUST OMERS table available in your database which you can use to store required information related to customers.

You might also like