Tuesday 27 November 2012

PRIMARY KEY CONSTRAINT




            Primary key constraint doesn’t allow duplicate and null values insertion. We can create only one primary key on a table.


Applying Primary Key constraint on a new table:

Syntax:-

CREATE TABLE <Table name>
(<Column 1> <Data type>,
<Column 2> <Data type>,
.
.
.
<Column N> <Data type>,
CONSTRAINT <Constraint name> <Constraint type> (Column 1, Column 2,...,Column N)
)

Example 1: Applying constraint on single column with constraint name.

CREATE TABLE ConstraintTBL_PK1
(
ID INT
,Name VARCHAR(10)
,CONSTRAINT ID_PK1 PRIMARY KEY(ID)
)

Output: SP_HELP ConstraintTBL_PK1



Example 2: Applying constraint on single column with default constraint name.

CREATE TABLE ConstraintTBL_PK2
(
ID INT
,Name VARCHAR(10)
,PRIMARY KEY (ID)
)

Note: If we are not mentioned constraint name, it will set some default name as constraint name.

Output: SP_HELP ConstraintTBL_PK2



Example 3: Applying constraint on multiple columns.

CREATE TABLE ConstraintTBL_PK3
(
ID INT
,Name VARCHAR(10)
,CONSTRAINT ID_PK3 UNIQUE(ID,Name)
)

Output: SP_HELP ConstraintTBL_PK3




Apply Primary key constraint on existing table:

  • The specific column(s) should not have any duplicate or null values then only we can apply Primary Key constraint.

  • We cannot define primary key constraint on nullable columns. First we should define not null constraint.

Syntax: 
 ALTER TABLE <Table name> ADD CONSTRAINT <Constraint name> <Constraint type> (<Column 1>, <Column 2>,…,<Column N>)

Example:
Table creation:
CREATE TABLE ConstraintTBL_PK4
(
ID INT
,Name VARCHAR(10)
)

Not null constraint:
ALTER TABLE ConstraintTBL_PK4 ALTER COLUMN id INT NOT NULL;

Primary key constraint:
ALTER TABLE ConstraintTBL_PK4 ADD CONSTRAINT ID_PK4 PRIMARY KEY(ID);

Output: SP_HELP ConstraintTBL_PK4



Drop constraint:

Syntax:
ALTER TABLE <Table name> DROP CONSTRAINT <Constraint name>

Example:
ALTER TABLE ConstraintTBL_PK4 DROP CONSTRAINT ID_PK4

Output:




Monday 19 November 2012

UNIQUE CONSTRAINT




            Unique constraint doesn’t allow inserting duplicate values in specific column(s); it allows only one null value. Multiple Unique constraints we can define on a table.

Applying Unique constraint on a new table:

Syntax:-

CREATE TABLE <Table name>
(<Column 1> <Data type>,
<Column 2> <Data type>,
.
.
.
<Column N> <Data type>,
CONSTRAINT <Constraint name> <Constraint type> (Column 1, Column 2,...,Column N)
)

Example 1: Applying constraint on single column with constraint name.
CREATE TABLE ConstraintTBL_UC1
(
ID INT
,Name VARCHAR(10)
,CONSTRAINT ID_UC UNIQUE(ID)
)

Output: SP_HELP ConstraintTBL_UC1


Example 2: Applying constraint on single column with default constraint name.
CREATE TABLE ConstraintTBL_UC2
(
ID INT
,Name VARCHAR(10)
,UNIQUE (ID)
)

Note: If we are not mentioned constraint name, it will set some default name as constraint name.

Output: SP_HELP ConstraintTBL_UC2


 


Example 3: Applying constraint on multiple columns.
CREATE TABLE ConstraintTBL_UC3
(
ID INT
,Name VARCHAR(10)
,CONSTRAINT ID_UC3 UNIQUE(ID,Name)
)

Output: SP_HELP ConstraintTBL_UC3



Apply Unique constraint on existing table:

Note: The specific column(s) should not have any duplicate values and should not have more than one null value then only we can apply unique constraint.

Syntax: 
 ALTER TABLE <Table name> ADD CONSTRAINT <Constraint name> <Constraint type> (<Column 1>, <Column 2>,…,<Column N>)

Example:
ALTER TABLE ConstraintTBL_UC1 ADD CONSTRAINT ID_UC4 UNIQUE (Name)
  
Output: SP_HELP ConstraintTBL_UC1
 



Drop constraint:

Syntax:
ALTER TABLE <Table name> DROP CONSTRAINT <Constraint name>

Example:
ALTER TABLE ConstraintTBL_UC1 DROP CONSTRAINT ID_UC4

Output:





Friday 9 November 2012

NOT NULL




            It does not allow null values in the specified column.

Apply not null constraint while the time of new table creation:-

Syntax:

Create table <Table name>
(
<Column 1> <Data type> NOT NULL,
<Column 2> <Data type>,
.
.
.
<Column n> <Data type>
)


Example:

CREATE TABLE ConstraintTBL_1
(
ID INT NOT NULL,
Name VARCHAR(10)
)

Syntax to verify the constraint:
SP_HEPL <Table name>

Example:
SP_HELP ConstraintTBL_1

 
    • In the above screen ID column is having Nullable value is ‘NO’, means we cannot insert null values into ID column.
    • If we try to insert a null value it will throw a message.
 INSERT INTO ConstraintTBL_1 VALUES (NULL,'A')

Message:-

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ID', table 'kiran.dbo.ConstraintTBL_1'; column does not allow nulls. INSERT fails.
The statement has been terminated.


Apply not null constraint on existing table column:-

            The particular column (which we are going to apply constraint) should not have null values then only we can apply not null constraint.

Syntax:

ALTER TABLE <Table name> ALTER COLUMN <Column name> <Data type> NOT NULL

Example:
ALTER TABLE ConstraintTBL_1 ALTER COLUMN Name varchar(10) NOT NULL


Result:
SP_HELP ConstraintTBL_1



  Remove\Drop Not null constraint:

Syntax:
ALTER TABLE <Table name> ALTER COLUMN <Column name> <Data type>

Example:
ALTER TABLE ConstraintTBL_1 ALTER COLUMN Name varchar(10)

Result:
SP_HELP ConstraintTBL_1






CONSTRAINTS



            Constraint is a condition it can be defined on the selected columns in order to avoid invalid data.

Types of constraints: