SQL CREATE TABLE

SQL CREATE TABLE statement to create a new table with the given name. SQL TABLE columns name must be unique with specified built-in data type. TABLE COLUMN support various attributes such as column data type, NULL, NOT NULL, CONSTRAINTS, DEFAULT VALUE et cetera.

Following is SQL CREATE TABLE syntax, but now you just learn basic CREATE TABLE syntax. Once you know SQL CONSTRAINT concept you can learn this syntax.

Syntax

CREATE TABLE [ IF NOT EXISTS ] table_name(
  column_name datatype[(size)] [ NULL | NOT NULL ],
  column_name datatype[(size)] [ NULL | NOT NULL ],
  [ CONSTRAINT constraint_name 
    PRIMARY KEY ( col1, col2, ... ) |
    FOREIGN KEY ( col1, col2, ... ) REFERENCES table_2 [ ( col1, col2, ... ) 
      [ ON UPDATE | ON DELETE 
        [ NO ACTION | SET NULL | SET DEFAULT | CASCADE ] 
      ] 
    ] |
    UNIQUE ( col1, col2, ... ) |
    CHECK ( expression )
  ]
  ...
);

Basic Syntax:

CREATE TABLE table_name(
  column_name1 datatype(size),
  column_name2 datatype(size)
  ...
);

Keep in Mind... : You can not create new table if table already exist in selected database. and another things table name must have unique within database.

Example

Here we create new table in SQL. 4 columns specify with data type and size.

SQL> CREATE TABLE users_info(
  no NUMBER(3,0),
  name VARCHAR(30),
  address VARCHAR(70),
  contact_no VARCHAR(12)
);

Table created.

Run it...   »