Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

SQL Primary Key

The PRIMARY KEY uniquely identifies each record in a database table; main goal is to prevent duplicate values for columns and provides unique identifier to each column.

1. Primary key creates clustered index on the columns.
2. Primary keys must contain UNIQUE values.
3. A primary key column cannot contain NULL values.

Example:

Column level:

USE [WebShop]
CREATE TABLE [dbo].[Department](
            [DepartmentId] [smallint] IDENTITY(1,1) NOT NULL Primary Key,
            [DeptName] [nvarchar](100) NOT NULL,
            [Description] [nvarchar](2000) NULL,
            [CreatedBy] [uniqueidentifier] NOT NULL,
            [CreatedDate] [datetime] NULL,
            [UpdateBy] [nvarchar](256) NULL,
            [UpdateDate] [datetime] NULL
 )

Table Level:

USE [WebShop]
CREATE TABLE [dbo].[Department](
            [DepartmentId] [smallint] IDENTITY(1,1) NOT NULL,
            [DeptName] [nvarchar](100) NOT NULL,
            [Description] [nvarchar](2000) NULL,
            [CreatedBy] [uniqueidentifier] NOT NULL,
            [CreatedDate] [datetime] NULL,
            [UpdateBy] [nvarchar](256) NULL,
            [UpdateDate] [datetime] NULL
            CONSTRAINT PK_Department  PRIMARY KEY(DepartmentId)
 

Invalid entry,please enter valid data.

Loading