0% found this document useful (0 votes)
5 views

Constraints in SQL Server

Uploaded by

suresh p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Constraints in SQL Server

Uploaded by

suresh p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Constraints in SQL Server

• What is constraints in SQL server ?


Constraints are the predefined set of rules and restrictions applied on the tables or
columns for restricting unauthorised values to be inserted into the tables. They are
responsible for ensuring the column's data accuracy, integrity, and reliability inside the
table.

Types of Constraints used in SQL Server-

PRIMARY FOREIGN NOT


KEY KEY NULL

UNIQUE CHECK DEFAULT

• Primary Key
Primary Key is a type of constraint which makes each row unique. A column on which Primary
Key constraint is applied can’t have duplicate values.
Note-
o Primary Key does not accept the NULL value. That means you cannot assign NULL
value to the column(s) forming Primary Key.
o A table cannot have more than one Primary Key.

Syntax –

Method 1 (Not recommended) -


CREATE TABLE [Test] (
[ID] INT PRIMARY KEY
, [Name] VARCHAR (50)
)

- Name of the Primary key is assigned automatically.


Method 2 – In this method, you assign the Primary Key constraint along with the Primary Key
name in the column definition itself
CREATE TABLE [Test] (
[ID] INT
CONSTRAINT PK_Test_ID PRIMARY KEY
, [Name] VARCHAR (50)
)

Method 3 - In this method, you define the Primary Key constraint along with the Primary Key
name separately after column definition. (Recommended)
CREATE TABLE [Test] (
[ID] INT
, [Name] VARCHAR (50)
, CONSTRAINT PK_Test_ID PRIMARY KEY([ID])
)
Using this method, you can also create a composite Primary Key. Just add the required set of columns
in comma separated format within parenthesis after keyword PRIMARY KEY.

• Foreign Key
o The Foreign Key is a type of constraint which refers to another table. It makes sure that the
value being inserted or updated in the Foreign Key column exists in the referenced column
of another table (called Primary Key).
o Referenced table column should have Primary Key or UNIQUE constraint defined on it.
o The Foreign Key constraint column(s) can have the NULL value. But Primary Key
constraint column(s) cannot.
o Primary Key table (referenced table) is treated as a parent and Foreign Key (referencing
table) is treated as Child.

Note-

o If a table having Primary Key (parent) is referenced in another table as Foreign Key
(child) then parent table can’t be dropped. Similarly, the parent row can’t be
deleted unless the child is deleted. So first the child is to be deleted and then the
parent.

Syntax –
CREATE TABLE [Test] (
[ID] INT
, [Name] VARCHAR (50)
, CONSTRAINT FK_Test_Parent_ID FOREIGN KEY
([Parent_ID]) REFERENCES [Test] (ID)
)
• NOT NULL –
o Not Null constraint can be created in the same way the Primary Key constraint is
created.
o This constraint restricts the NULL values. It means you cannot specify the NULL value
in a column (using INSERT or UPDATE statement) if it is assigned a NOT NULL
constraint.
o In case a NULL value is assigned, the SQL Server will throw an error.

The following is an example to assign the NOT NULL constraint to a column while creating a
new table.
CREATE TABLE [Test] (
[ID] INT
, [Name] VARCHAR (50) NOT NULL
, CONSTRAINT PK_Test_ID PRIMARY KEY([ID])
)
The following is an example to assign the NOT NULL constraint to an existing column of a
table.
ALTER TABLE [Test]
ALTER COLUMN [Name] VARCHAR (50) NOT NULL

• UNIQUE
UNIQUE constraint It helps to maintain the data integrity.
UNIQUE constraint is used to enforce the uniqueness of the data on a column, or on the set of
columns.
This is how you can add UNIQUE constraints while creating a new table:
CREATE TABLE [Test] (
[ID] INT
, [Name] VARCHAR (50)
, CONSTRAINT UQ_Test_ID UNIQUE([ID])
)
This is how you can add UNIQUE constraint on an existing table:
ALTER TABLE [Test]
ADD CONSTRAINT UQ_Test_ID UNIQUE([ID])

NOTE- When you make column(s) Primary Key then NOT NULL constraint automatically gets
created on those set of columns(s), irrespective you specify it or not. Whereas, UNIQUE constraint doesn’t
create the NOT NULL constraint, and it accepts the NULL value.

Primary Key versus UNIQUE constraint

o The purpose of the Primary Key constraint is to uniquely identify each


record, whereas the UNIQUE Key constraint enforces uniqueness on a
column or set of columns.
o Only one Primary Key constraint can be created on a table, whereas a
table can have multiple UNIQUE Key constraints.
o Primary Key creates NOT NULL constraint on the column(s) forming
Primary Key, whereas UNIQUE constraint doesn’t create the NOT NULL
constraint.
o Primary Key column(s) do not accept the NULL values, whereas the
UNIQUE constraint column(s) allow the NULL values provided such
combination is unique.

• CHECK
CHECK constraint is another special kind of constraint that is used to specify
the rule of the accepted values in the column.
For example, if we want to restrict the Gender column to only accept the values ‘Male’,
‘Female’ then it can be achieved with the help of CHECK constraint as shown in the
following example. If any other value is supplied other than the ones specified, an error
will be thrown.

Adding CHECK constraint at the time of table creation then it can be done as follows:
CREATE TABLE [Test] (
[ID] INT
, [Name] VARCHAR (50)
, [Gender] varchar (25)
, CONSTRAINT PK_Test_ID PRIMARY KEY([ID])
, CONSTRAINT CK_Test_Gender CHECK
([Gender] IN (‘Male’,’Female’))
)

• DEFAULT
The DEFAULT constraint is a special kind of constraint that is used to assign a
default value to the column (if no value is assigned to it).

Note- If a column is Nullable, it means it accepts the NULL value, and it doesn’t have a
DEFAULT constraint. All the rows inserted in such a column will have NULL if it is not
part of the INSERT statement.

Syntax –
CREATE TABLE [Test] (
[ID] INT
, [Name] VARCHAR (50)
, [Gender] varchar (25)
, CONSTRAINT PK_Test_ID PRIMARY KEY([ID])
, CONSTRAINT DF_Test_Gender DEFAULT('Do
not wish to mention')
)
In the above example of the DEFAULT constraint, we assign ‘Do not wish to mention’ as a default value to the ‘Gender’ column.
So, if the Gender column is not included in the INSERT, then the default value Do not wish to mention will be assigned to the
respective rows being inserted.

You might also like