Introduction
This post will explain constraints in Microsoft SQL Server, specifically PRIMARY KEY and UNIQUE Constraints.
All constraints are categorized into 3 types:
- Entity Integrity Constraints.
- Domain Integrity Constraints.
- Referential Integrity Constraints.
A constraint is nothing but an additional condition that is associated with the column of a table.
In a database, we store data in a table. We want to store data that is consistent, accurate, and reliable in a table. In order to make the data consistent, accurate, and reliable, we need to associate these constraint(s) with the column of a table.
Entity Integrity Constraint
We can use any of the following two constraints as a part of this category
An entity integrity constraint is used to identify/recognize the row or record of a table uniquely.
Primary Key Constraint
When this constraint is associated with the column of a table it will not allow NULL values into the column and it will maintain unique values as part of the table.
We can add only one Primary Key constraint on a table.
We can say that a Primary Key constraint is a combination of Unique and NOT NULL constraints.
Unique Constraint
When this constraint is associated with the column(s) of a table it will not allow us to store a repetition of data/values in the column, but Unique constraint allows ONE NULL value. More than one NULL value is also considered as repetition, hence it does not store a repetition of NULL values also.
General syntax to add a constraint on the column of a table
CREATE TABLE <TableName>
(<Col-1> <DataType> [<ConstraintType-1> <Constraint-2> .... <ConstraintType-n>],
<Col-2> <DataType> [<ConstraintType-1> <Constraint-2> .... <ConstraintType-n>],
<Col-3> <DataType> [<ConstraintType-1> <Constraint-2> .... <ConstraintType-n>],
...
...
<Col-N> <DataType> [<ConstraintType-1> <Constraint-2> .... <ConstraintType-n>],
CONSTRAINT <ConstraintName-1> <ConstraintType>(<ColName-1>[,<ColName-2>,...,<ColName-N>]),
CONSTRAINT <ConstraintName-2> <ConstraintType>(<ColName-1>[,<ColName-2>,...,<ColName-N>]),
...
...
CONSTRAINT <ConstraintName-N> <ConstraintType>(<ColName-1>[,<ColName-2>,...,<ColName-N>])
)
In the above syntax please note the following points
A constraint that is added immediately next to the column definition is known as a Column-level constraint. All constraints that are added after the columns definition of the table is completed are known as Table-level constraints.
What is the difference between column-level constraints and table-level constraints?
We know that a constraint means an additional condition associated with the column of the table. Whenever we want to associate multiple conditions on a single column it is better to add the constraint as a column-level constraint.
Whenever we want to associate multiple conditions/constraints on a single column of a table rather than defining a single constraint multiple times as part of the table for each and every column, it is better to add the single constraint on a group of columns collectively as a table-level constraint.
When we define a table-level constraint we must begin with the keyword "CONSTRAINT" followed by Constraint-Name. The name of each constraint must be unique, as constraints are also objects in a Database. When adding a table-level constraint, we must mention the column name which we want to associate the constraint with.
Advantage with an Entity Integrity Constraint (Primary Key or Unique)
When there are no duplicate values in the roll number column of the table, we can easily identify each and every student record in the stud table uniquely based on the RNO column. That is the reason why Primary Key/Unique constraints are known as Entity Integrity Constraints. Similarly, we can define a product table using a product_id column with a Primary Key constraint. In the Customers table of a bank table, each customer account_number value is unique. This is how we can distinguish one customer from another. Therefore, our responsibility must be to identify/recognize that column in the table with which the data can be uniquely recognized and associate that column in the table with Primary Key constraint.
Note
When we add a constraint as a column-level constraint it will be SQL Server which will give the name to the constraint. However, when we add the constraint as a table-level constraint, we need to give the name of the constraint. It is our choice to give the name of the constraint (we must follow the same rules that we follow when we give a name to the table/column when giving name to a constraint).
- create database mytestdb1
- use mytestdb1
- CREATE TABLE stud
- (rno int,
- sname varchar(20),
- age numeric(2,0),
- gen varchar(6))
-
- INSERT INTO stud VALUES(1,'Amit',21,'Male')
- SELECT * FROM stud
- INSERT INTO stud VALUES(1,'Anil',21,'Male')
-
- SELECT * FROM stud
- DROP TABLE stud
-
- CREATE TABLE stud
- (rno int PRIMARY KEY,
- sname varchar(20),
- age numeric(2,0),
- gen varchar(6))
-
- sp_help stud
- INSERT INTO stud VALUES(1,'Amit', 21, 'Male')
- SELECT * FROM stud
- INSERT INTO stud VALUES(2,'Anil',22,'Male')
- SELECT * FROM stud
-
- INSERT INTO stud VALUES(1,'Sunil', 22,'Male')
-
- INSERT INTO stud(sname,age,gen) VALUES('Sunil', 22,'Male')
- /*The reason for the error is that the column rno in the table stud is defined with Primary key constraint and Primary Key constraint doesn't allow NULL values, not even one null value.*/
- DROP TABLE stud
-
- CREATE TABLE stud
- (rno int,
- sname varchar(20),
- age numeric(2,0),
- gen varchar(6),
- CONSTRAINT pk1 PRIMARY KEY(rno))
- sp_help stud
- INSERT INTO stud VALUES(1,'Amit', 21,'Male')
- SELECT * FROM stud
-
- INSERT INTO stud VALUES(1,'Anil',21,'Male')
-
- INSERT INTO stud(sname,age,gen) VALUES('Anil',21,'Male')
- sp_help stud
- DROP TABLE stud
-
-
- CREATE TABLE stud
- (rno int UNIQUE,
- sname varchar(20),
- age numeric(2,0),
- gen varchar(6))
- sp_help stud
- INSERT INTO stud VALUES(1,'Amit',21,'Male')
- SELECT * FROM stud
-
- INSERT INTO stud VALUES(1,'Anil', 22,'Male')
- INSERT INTO stud VALUES(NULL,'Sunil',22,'Male')
- SELECT * FROM stud
-
- INSERT INTO stud VALUES(NULL,'Hari',23,'Male')
- DROP TABLE stud
-
- CREATE TABLE stud
- (rno int,
- sname varchar(20),
- age numeric(2,0),
- gen varchar(6),
- CONSTRAINT unq1 UNIQUE(rno))
- sp_help stud
- INSERT INTO stud VALUES(1,'Amit',21,'Male')
- SELECT * FROM stud
-
- INSERT INTO stud VALUES(1,'Anil',22,'Male')
- INSERT INTO stud(sname,age,gen) VALUES('Anil',22,'Male')
- SELECT * FROM stud
-
- INSERT INTO stud VALUES(null,'Harry',22,'Male')
-
- INSERT INTO stud VALUES(NULL,'Harry',22,'Male')
-
- INSERT INTO stud(sname,age,gen) VALUES('Harry',22,'Male')