Free Web Development Online Tutorials, Learn to Code
Primary Key Foreign key Relationship, Learn SQL Database Development, MS SQL Tutorial

Create Primary key constraint in SQL Database Table

SQL PRIMARY KEY Constraint

A primary key in SQL Table helps to identify each row/record in a table uniquely.

Here is the characteristic of Primary Key in SQL Table

  • Primary keys must contain unique values
  • primary key column cannot have NULL values
  • Table can have only one primary key (can be composite with multiple fields)

How to create Primary key in SQL

ColumnName datatype not null identity(1,1) PRIMARY KEY, Note: identity(1,1) seed not Necessary while creating the primary key, but in general we set the identity seed for Primary Key column most of the time.

CREATE TABLE ClassStudentMap (
CSId	int not null identity(1,1)  PRIMARY KEY,
ClassId int NOT NULL,
StudentId bigint NOT NULL             
);

You may check the Foreign Key to understand the more insight about Primary Key.

Comment
Name
Email
Website
Subscribe