0% found this document useful (0 votes)
21 views21 pages

SQL 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views21 pages

SQL 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

SQL Constraints

DBMS SQL
DATAINTEGRITY& CONSTRAINTS

• The constraint in SQL is used to specify the rule that allows or restricts
what values/data will be stored in the table.
• They provide a suitable method to ensure data accuracy and integrity
inside the table.
• It also helps to limit the type of data that will be inserted inside the
table
DATAINTEGRITY& CONSTRAINTS

• Database Systems offer three types of integrity constraints:-


 Entity Integrity- PRIMARY KEY
 Domain Integrity- DATA TYPES, CHECK CONSTRAINT
 Referential Integrity – FOREIGN KEY
Types of SQL Constraints

• Constraints in SQL is classified into two types:


 Column Level Constraints: These constraints are applied only to the
single column that limits the type of particular column data.
Column datatype [CONSTRAINT constraint_name] constraint_type
Example:
Building VARCHAR2(7) CONSTRAINT location_building_nn NOT NULL
Any constraint can be defined at the column level except for a FOREIGN KEY and COMPOSITE
primary key constraints.
 Table Level Constraints: These constraints are applied to the entire
table that limits the type of data for the whole table.
[CONSTRAINT constraint_name] constraint_typ (Column, . . .),
Example:
CONSTRAIN location_roomid_pk PRIMARY KEY(Roomid)
All constraints can be defined at the table level except for the NOT NULL constraint.
How to create constraints in SQL

• We can define the constraints during a table created by using the


CREATE TABLE statement.
• SQL also uses the ALTER TABLE statement to specify the constraints
in the case of the existing table schema.
Syntax to add constraints with CREATE Table Statement

• The following are the syntax to create a constraints in table:


CREATE TABLE new_table_name (
col_name1 datatype constraint,
col_name2 datatype constraint,
col_name3 datatype constraint,
.........
);
Syntax to add constraints with ALTER table Statement

• To add a constraint using ALTER TABLE, the syntax for table


level constraint is used. The general syntax of ALTER TABLE
is
ALTER TABLE tablename
ADD [CONSTRAINT constraint_name] constraint_type
(column, …),
Constraints used in SQL

• The following are the most common constraints used in the Oracle:
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
• AUTO_INCREMENT
• INDEX
• ENUM
NOT NULL Constraint

• This constraint specifies that the column cannot have NULL or empty
values.
• The NOT NULL constraint ensures that the column has a value and the
value is not a null value.
• A space or a numeric zero is not a null value
Example: Create a table Student with following schema.
Student(Id, First Name, Last Name, City) Apply Not Null constraint on
Student Name.
UNIQUE Constraint

• This constraint ensures that all values inserted into the column will be
unique.
• It means a column cannot store duplicate values.
• SQL allows us to use more than one column with UNIQUE constraints
in a table.
• Example: Create a table Product with the following schema.
Product(Id, Name, quantity, Price) Apply Unique constraint on Product
Id.
PRIMARY KEY Constraint

• The PRIMARY KEY constraint is also known as the entity


integrity constraint
• It creates a primary key for the table. A table can have only one
primary key constraint.
• This constraint is used to identify each record in a table uniquely. If the
column contains primary key constraints, then it cannot be null or
empty.
• It always contains unique value into a column.
• Defined at either the table level or the column level
PRIMARY KEY Constraint Example

• Crate table Person with following schema


• Person(Id, Name, Age,city)
• Id-Primary key
• Name, Age-Not Null
AUTO_INCREMENT Constraint

• This constraint automatically generates a unique number whenever we


insert a new record into the table.
• Generally, we use this constraint for the primary key field in a table.
• Example : Create table Subject(Subject_code ,Name) Apply
primary key and auto increment to Subject code and not null to
name.
Foreign Key Constraint

• This constraint is used to link two tables together. This is also known as referential integrity constraint.
 It is also known as the referencing key. A foreign key column matches the primary key field of another
table. At the table level ONLY.
• It uses a column or columns as a foreign key, and it establishes a relationship with the primary key of
the same or another table.
• Foreign key and referenced primary key columns need not have the same name, but a foreign key
value must match the value in the parent table’s primary key value or be NULL
• It means a foreign key field in one table refers to the primary key field of another table.
Foreign Key Constraint Example

• Consider the structure of these tables: Persons and Orders.


• Person(PId(PK),Name(Not Null),age(Not Null),City)
• Order(Order_Id(PK), Order_date(Not Null),Pid(FK))
Adding a Constraints

• Consider the structure of these tables Employee:


• Employee(empno(PK),ename(Not Null),salary, doj, mgr(FK))
• Add a FOREIGN KEY constraint to the EMP table indicating that
a manager must already exist as a valid employee in the EMP
table.
Dropping Constraint

• Remove the manager constraint from the EMP table.


Disabling Constraints

• Execute the DISABLE clause of the ALTER TABLE statement to


deactivate an integrity constraint.
• Apply the CASCADE option to disable dependent integrity constraints.
Enabling Constraints

• Activate an integrity constraint currently disabled in the table


definition by using the ENABLE clause.
The check Constraints

• Defines a condition that each row must satisfy.


• It controls the value in a particular column.
• It ensures that the inserted value in a column must be satisfied with
the given condition.
• In other words, it determines whether the value associated with the
column is valid or not with the given condition.
– Example:
– Voter(Id(PK), Name(Not Null),Age(age>=18))
• CREATE TABLE Persons ( ID int NOT NULL, Name varchar(45)
NOT NULL, Age int CHECK (Age>=18) );
Default Constraints

• A column can be given the default value by using DEFAULT option.


• The data type of column and default expression must be the same.
• This constraint is used to set the default value for the particular column
where we have not specified any value.
• It means the column must contain a value, including NULL.
• Example(Id(PK), Name, Age,City(default 'Delhi'))
• CREATE TABLE Persons ( ID int PRIMARY
KEY, Name varchar(45) NOT NULL, Age int, City varchar(25) D
EFAULT 'Delhi' );

You might also like