Wednesday, June 17, 2015

Create table script in SQL

In order to create table in SQL we have to use CREATE keyword.

To create a table we need to have below details in hand with us:
a. You should know what should be the table name
b. What are all the columns required in your table
c. Data types of all those columns
d. Constraints of every column, we will learn about different constraints available in SQL in separate post. As of now I will mention whether column accept NULL values or not

Syntax:
CREATE TABLE <<tableName>>
(
<<columnName>> <<dataType>> <<constraint>>
)

Find below Employee table creation script:
CREATE TABLE Employee
(
     EmpID INT NOT NULL
    ,FirstName NVARCHAR(250) NOT NULL
    ,LastName NVARCHAR(250) NULL
    ,Salary MONEY
)

On execution of above script table will be created in the database in below format:
EmpID
FirstName
LastName
Salary

If we see the above script, we have declared EmpID and FirstName as NOT NULL and other two columns as NULL that means those two fields are not mandatory to fill.

In SQL if we don't mention anything then the default constraint will be taken as NULL that is the reason for Salary field the default constraint will be NULL.



Labels: , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home