Sponser Link

How to Create table in SQL Server

You can create the table using design view or you can write the query for create the table. Using design view, you can create directly by right click on table->New Table. Now you can write the column names and data type then press save. It will ask the table name. Type table name and save.

You can create the table using CREATE TABLE statement. The syntax of Create table is given below.

CREATE TABLE [table_name]
(
  Col1 [datatype],
  Col2 [datatype]
  CONSTRAINT [PK_Name] PRIMARY KEY CLUSTERED 
  (
	[Col1] ASC
  )
)

In given below example, we will create customer table. After execute the script successfully the given below message will be show. This means that no error in query and table has been created.

Command(s) completed successfully.


CREATE TABLE [dbo].[Customers](
	[Cust_Id] [int] NOT NULL,
	[Cust_Name] [nvarchar](50) NULL,
	[Cust_Address] [nvarchar](100) NULL,
	[Cust_Phone] [nvarchar](50) NULL,
	[Cust_DOB] [datetime] NULL,
  CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
 (
	[Cust_Id] ASC
 )
) ON [PRIMARY]

GO

Here Cust_id is the primary key column. You can define composite keys using comma seperator.