Monday, 14 September 2015

How to Create a table in sql server ?

The table can be created in the database using the following syntax.

Create Syntax :

CREATE TABLE  [SCHEMA NAME] [TABLE NAME]
(
   [COLUMN NAME1] [DATA TYPE] [CONSTRAINT],
   [COLUMN NAME2] [DATA TYPE] [CONSTRAINT],
   .
   .
   .
   .
 [COLUMN NAME(N)] [DATA TYPE] [CONSTRAINT],
)

Example :

CREATE TABLE DBO.EMPLOYEE
(
   [EmployeeID] [INT] IDENTITY (1,2) NOT NULL,
   [ForeName] [NVARHCAR] (50) NOT NULL,
   [SurName] [NVARHCAR] (50) NULL,
   [EmployeeDep] [NVARHCAR] (50) NOT NULL,
   [Salary] [FLOAT] NOT NULL
)

Now we have created a new table called Employee.
  • EmployeeID is int column and not nullable.
  • ForeName is string column and not nullable.
  • SurName is string column and nullable
  • EmployeeDep is string column and not nullable
  • Salary is float column and not nullable.

Identity (1,2) refers to start value and seed value.

Here 1 refers to the starting value of the EmployeeID column.
2 refers to the incremental value of the EmployeeID column.

So the EmployeeID column first value will be 1 and next value will be 3 and 5 and goes on..
We can  also change the start value and seed value.(Eg : Identity(1000,1)).So the starting value will be 1000 and next value will be 1001,1002....(n).

Note : 


Nullable column will allow Null value.
Not Nullable column will not allow Null value.


No comments:

Post a Comment