Constraints
Constraints
SQL constraints are used to specify rules for the data in a table.
• Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any
violation between the constraint and the data action, the action is aborted.
• Constraints can be specified when the table is created with the CREATE
TABLE statement, or after the table is created with the ALTER TABLE
statement.
Constraints in SQL can be categorized into two types:
• Column Level Constraint:
Column Level Constraint is used to apply a constraint on a single column.
• Table Level Constraint:
Table Level Constraint is used to apply a constraint on multiple columns.
Some of the real-life examples of constraints are as
follows:
• Every person has a unique email id. This simply means that no two
users can have the same email ids on the same email providing
service.
• Whenever we set a password for any system, there are certain
constraints that are to be followed. These constraints may include the
following:
• There must be one uppercase character in the password.
• Password must be of at least eight characters in length.
• Password must contain at least one special symbol.
The following constraints are
commonly used in SQL:
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
• FOREIGN KEY - Prevents actions that would destroy links between
tables
• CHECK - Ensures that the values in a column satisfies a specific
condition
• DEFAULT - Sets a default value for a column if no value is specified
1. NOT NULL
• Example:
CREATE TABLE student
(StudentID number(4) NOT NULL,
Student_Name varchar2(20));
Desc student
Create NOT NULL constraint - Using
an ALTER TABLE statement
Syntax
ALTER TABLE table_name
MODIFY(column1 NOT NULL);
Example
ALTER TABLE mytable
MODIFY (mynumber NUMBER(4) NOT NULL);
2. UNIQUE Constraint
• Duplicate values are not allowed in the columns to which the UNIQUE
constraint is applied.
• The column with the unique constraint will always contain a unique value.
• This constraint can be applied to one or more than one column of a table,
which means more than one unique constraint can exist on a single table.
• Using the UNIQUE constraint, you can also modify the already created
tables.
CREATE TABLE Persons (
ID number(4) NOT NULL UNIQUE, // column level
LastName varchar2(25) NOT NULL,
FirstName varchar2(25),
Age number(2)
)
Example
ALTER TABLE supplier
ADD CONSTRAINT supplier_unique UNIQUE (supplier_id);
3. PRIMARY KEY
Example:
CREATE TABLE Persons
(
ID number(5) PRIMARY KEY,
LastName varchar2(25) NOT NULL,
FirstName varchar2(25),
Age number(2)
What is Composite Key in Oracle?
• Primary key is a column that uniquely identifies column in a table. But sometimes it happens
that a single column is not sufficient to identify tuples in a table. This is where Composite key
comes into consideration. Using two or more column as a Primary key so as to make it unique
• For example, we have Subject ID, that can never be considered as Primary key if we have lot of
student having same subject. But, it will work if we consider Student ID as well as Subject ID
together as a Primary key. It will be nothing but a composite key.
create table stud
(
Name varchar2(20),
Section Varchar2(10),
Roll_no Number (20) ,
Subject_ID Number(20),
Subject Varchar2(20),
PRIMARY KEY(Roll_no,Subject_ID)
);
CREATE TABLE supplier
(
supplier_id number(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
In this example, we've created a primary key on the supplier table called supplier_pk. It
consists of only one field - the supplier_id field.
We could also create a primary key with more than one field as in the example below:
Example
ALTER TABLE supplier
ADD CONSTRAINT supplier_pk PRIMARY KEY (supplier_id);
In this example, we've created a primary key on the existing supplier table called supplier_pk. It
consists of the field called supplier_id.
We could also create a primary key with more than one field as in the example below:
Syntax:
Example
Let's look at an example of how to drop a primary key using the ALTER TABLE
statement in Oracle.
A foreign key is a way to enforce referential integrity within your Oracle database.
A foreign key means that values in one table must also appear in another table.
The referenced table is called the parent table while the table with the foreign
key is called the child table. The foreign key in the child table will generally
reference a primary key in the parent table.
• A REFERENTIAL INTEGRITY is a database concept that is used to build and maintain logical
relationships between tables to avoid logical corruption of data. It is a very useful and
important part in DBMS.
• Usually, referential integrity is made up of the combination of a primary key and a foreign key.
• The main concept of REFERENTIAL INTEGRITY is that it does not allow to add any record in a
table that contains the foreign key unless the reference table containing a
corresponding primary key.
• If any record in referenced table (i.e. the table who contain primary key) is deleted, all the
corresponding records in the referencing table will be deleted for the referential integrity.
Referential integrity is a term used in database design to describe the relationship between two
tables. It is important because it ensures that all data in a database remains consistent and up
to date. It helps to prevent incorrect records from being added, deleted, or modified.
Referential integrity is a constraint on the database design that makes certain that each foreign
key in a table point to a unique primary key value in another table.
• At Column level
Syntax
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype REFERENCES parent_table (column));
Example
Create table emp( empid number(3) primary key,empname varchar(15),
birthdate date);
select * from e1
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
);
CREATE TABLE e2 ( empid NUMBER PRIMARY KEY, ename VARCHAR2(100) NOT
NULL );
CREATE TABLE d2 ( did NUMBER PRIMARY KEY, dname VARCHAR2(100) NOT NULL,
empid NUMBER, CONSTRAINT fk_empid FOREIGN KEY (empid) REFERENCES
e2(empid) );
Syntax:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
Example:
ALTER TABLE products
DROP CONSTRAINT fk_supplier;