You are on page 1of 16

SQL QUERIES

CONSTRAINTS
• Sometimes the user wants to impose certain
restrictions on the data being stored in a table.
• These restrictions are called constraints.
• Constraints control the data being entered in a table.
• Once the data constraints are part of a table column
specification ,the oracle engine checks the data being
entered into that column against the data
constraints.
• If the data satisfy the constraint check,it is stored in
the column,otherwise the data is rejected.
• There are five types of constraints:
• Primary Key Constraint
• Unique Constraint
• Not Null Constraint
• Foreign Key Constraint
• Check Constraint
1.PRIMARY KEY CONSTRAINT
• Primary Key is applied to a column so that this
column can be used to uniquely identify each row
in the table.
• A table can have only one primary key.
• A primary key has following features:
• a) Primary key does not allow duplicate values.
• b)Primary Key does not allow null values
• Syntax:
• Columnname datatype(size) primary key
Example:
• Create table student(id number(10) primary
key,name varchar(10),city varchar(10));
or
• Create table student(id number(10),name
char(20),city varchar(10),primary key(id));
2.UNIQUE CONSTRAINT
• In a table there may be more than one column which forms the
candidate key.
• The unique constraint is used for the candidate key columns so
that they contain the unique values for different entities.
• Unique constraint will not allow the duplicate values in the
column to which they are attached.
• A unique key has following characteristics:
1. Unique key will not allow duplicate values.
2. Unique key can accept null value.
3. A table can have many unique constraints.
Syntax:
•Column datatype(size) unique

Example:
Create table student(id number(10) primary
key,name varchar(20) unique, city varchar(10));
3.NOT NULL CONSTRAINT
• Not null column constraint ensures that a table column cannot
be left empty.
• When a Not Null constraint is attached to a column then ,that
column becomes a mandatory column i.e. a value must be
entered into this column entire record will be rejected.
Syntax: columnname datatype(size) not null;
Eg: create table student(id number(10) not null);
5.FOREIGN KEY CONSTRAINT
• Foreign key is a column whose values are derived from the
primary key of other table.
• Foreign key enables us to establish the relationship between the
tables.
• The table in which the foreign key is defined is called as
foreign table or detail table
• The table whose primary key is referenced by the foreign table
is called as primary table or master table.
Foreign key has following characteristics:
1.Foreign key can contain only those values that are present in the
primary key of the master table
2.The values in the foreign key can be null or duplicate values.
3.In foreign key , if we insert a value that is not present in the
primary key of master table then that record will be rejected.
4.A record in the master table cannot be deleted if the
corresponding record in the foreign table exists.
Syntax: column_name datatype(size) references
master_table_name(column name)
Example: let department and course table as master and detail
tables
• Create table department(did number(10) primary key,
dname varchar(10));
After creating the master table, the detail table can be created as
following:
• Create table course(cid number(10) primary key, d_id
number(10) references department(did));
6.CHECK CONSTRAINT
• Check constraint is used to specify the data validation for a
column.
• Check constraint is a Boolean expression that is evaluated to
either true or false.
• Before inserting a value into the column(to which the check
constraint is attached),the oracle evaluates the Boolean
condition.
• If the condition is evaluated to true then the value is accepted
by the database.
• If the condition is evaluated to false then the value is rejected
by the database and an error message is displayed
• Syntax:
• Columnname datatype(size) check (boolean
expression)
• Eg:suppose that in the employee table the age of
the employee must be between 22 and 30
• Create table employee(emp_id char(10) primary
key,ename varchar(20),dept_name varchar(20)
check (dept_name in (‘cse’,’ece’,’me’)),age
number(10) check(age between 22 and 30));
• Q: Which of the following is not a class of constraint
in SQL Server?
a) NOT NULL
b) CHECK
c) NULL
d) UNIQUE
• Which of the constraint can be enforced one per
table?
a) Primary key constraint
b) Not Null constraint
c) Foreign Key constraint
d) Check constraint
• How can a SQL developer add a key on a table?
a) While creating a table
b) With Alter table command
c) both A and B

You might also like