SQL COMMANDS -- CREATE TABLE1

In the previous blog, database creations is explained.
Now, We will see how to create tables in a database using SQL Command.

Let's assume that you want a store your friends names and their contact numbers in your SQL database.

Here is the command for the above scenario:

CREATE TABLE FRIENDS
(
FRIEND_NAME
VARCHAR(25),

CONTACT_NUMBER INT
)

Typical Create table Sytax is:

CREATE TABLE <Table Name>
(

[Column1]            [data type],
[Column2]            [data type],
[Column3]            [data type],
[Column4]            [data type],
.....................and so on
)


High level syntax for the table creation is :



CREATE TABLE
    [ database_name.[ owner ] . | owner. ] table_name
    ( { < column_definition >
        | column_name AS
computed_column_expression         | < table_constraint > } [ ,...n ]
    )

[ ON { filegroup | DEFAULT } ]
[ TEXTIMAGE_ON { filegroup | DEFAULT } ]

< column_definition > ::= column_name data_type
    [ COLLATE < collation_name > ]
    [ [ DEFAULT constant_expression ]
        | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
    ]
    [ ROWGUIDCOL]
    [ < column_constraint > ] [ ...n ]

< column_constraint > ::= [ CONSTRAINT constraint_name ]
    { [ NULL | NOT NULL ]
        | [ { PRIMARY KEY | UNIQUE }
            [ CLUSTERED | NONCLUSTERED ]
            [ WITH FILLFACTOR = fillfactor ]
            [ON {filegroup | DEFAULT} ] ]
        ]
        | [ [ FOREIGN KEY ]
            REFERENCES ref_table [ ( ref_column ) ]
            [ ON DELETE { CASCADE | NO ACTION } ]
            [ ON UPDATE { CASCADE | NO ACTION } ]
            [ NOT FOR REPLICATION ]
        ]
        | CHECK [ NOT FOR REPLICATION ]
        ( logical_expression )
    }
< table_constraint > ::= [ CONSTRAINT constraint_name ]
    { [ { PRIMARY KEY | UNIQUE }
        [ CLUSTERED | NONCLUSTERED ]
        { ( column [ ASC | DESC ] [ ,...n ] ) }
        [ WITH FILLFACTOR = fillfactor ]
        [ ON { filegroup | DEFAULT } ]
    ]
    | FOREIGN KEY
        [ ( column [ ,...n ] ) ]
        REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
        [ ON DELETE { CASCADE | NO ACTION } ]
        [ ON UPDATE { CASCADE | NO ACTION } ]
        [ NOT FOR REPLICATION ]
    | CHECK [ NOT FOR REPLICATION ]
        ( search_conditions )
    }

Above syntax includes creation of Primary, foreign keys ,constrains and indexes etc.,
All the above attributes of table will be discussed in further sessions.

In this blog, we learnt simple table creation steps so far.

This entry was posted in . Bookmark the permalink.

Leave a reply