Constraints in SQL Server
Constraints in SQL Server
• 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 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.
• 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.