CONSTRAINTS used to specify rules for
the data in a table
WHAT IS CONSTRAINTS IN SQL?
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 IN SQL CAN BE
CATEGORIZED INTO TWO
TYPES:
Column Level Constraint:
Column Level Constraint is used to apply a constraint on a single
column.
Example;
create table dep1(
dep_id number constraint dep_id_pk primary key,
dep_name varchar2(20) constraint dep_name_uni unique,
manager_id number constraint man_not_null not null,
location_id number constraint loc_id_chq check(location_id between
1000 and 2500));
TABLE LEVEL CONSTRAINT:
Table Level Constraint is used to apply a constraint on multiple columns.
Example;
create table dep1(
dep_id number,
dep_name varchar2(20),
manager_id number constraint man_not_null not null,
location_id number,
constraint dep_id_pk primary key(dep_id),
constraint dep_name_uni unique(dep_name),
constraint loc_id_chq check(location_id between 1000 and 2500));
TYPES OF CONSTRAINS
KEY CONSTRAINTS
a.Primary key constraints
b. Unique constraints
c. NOT NULL constraints
DOMAIN CONSTRAINTS
a. Check constraints
INTEGRITY CONSTRAITS
a.Foreign key (referential) constraints
PRIMARY KEY CONSTRAINTS
The PRIMARY KEY constraint is a combination of both NOT NULL and
UNIQUE constraints. This means that when you define a column with
PRIMARY KEY, it will not accept any null or duplicate values.
A table can have only ONE primary key.
A composite primary key designates a combination of columns as the primary key.
Example;
create table dep1(
dep_id number constraint dep_id_pk primary key,
dep_name varchar2(20));
--insert into dep1 values(100,'balaji’);
--insert into dep1 values(200,'balaji’);
Output;
--insert into dep1 values(100,'balaji’);
--insert into dep1 values(null,'balaji’);
Output;
UNIQUE CONSTRAINTS
A unique key is the set of fields or columns of a table that helps us uniquely identify
records. The unique key guarantees the uniqueness of the columns in the database. It is
similar to the primary key but can accept a null value.
Example;
create table dep1(
dep_id number constraint dep_id_pk primary key,
dep_name varchar2(20) constraint dep_name_uni unique);
--insert into dep1 values(100,'balaji’);
--insert into dep1 values(200,'vijay’);
Output;
---insert into dep1 values(300,'vijay’);
---insert into dep1 values(300,null);
NOT NULL CONSTRAINTS
The NOT NULL constraint is used to ensure that a given column of a table is never
assigned the null value. Once a NOT NULL constraint has been defined for a particular
column, any insert or update operation that attempts to place a null value in that column
will fail.
Example;
create table dep1(
dep_id number constraint dep_id_notnull not null,
dep_name varchar2(20));
---insert into dep1 values(100,'balaji’);
---insert into dep1 values(100,'balaji’);
Output;
---insert into dep1 values(null,'vijay’);
CHECK CONSTRAINTS
The CHECK constraint is used to limit the value range that can be placed in a column. If
you define a CHECK constraint on a column it will allow only certain values for this column. If
you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
Example;
create table dep1(
dep_id number constraint dep_id_chq
check(dep_id between 100 and 200),
dep_name varchar2(20));
--insert into dep1 values(100,'balaji’);
--insert into dep1 values(120,’balaji’);
--insert into dep1 values(190,'vijay’);
--insert into dep1 values(200,'vijay’);
Output;
--insert into dep1 values(210,'vijay’);
FOREIGN KEY (REFERENTIAL)
CONSTRAINTS
A FOREIGN KEY is a field (or collection of fields) in one table, that
refers to the PRIMARY OR UNIQUE KEY in another table. The table with
the foreign key is called the child table, and the table with the primary key is
called the referenced or parent table.
A foreign key is generally used to build a relationship between the two
tables.
Example;
create table dep1(
dep_id number,
dep_name varchar2(20),
CONSTRAINT DEP1_ID_FK FOREIGN KEY(DEP_ID)REFERENCES
DEP(DEPARTMENT_ID));
--insert into dep1 values(10,'balaji’);
---insert into dep1 values(20,'vijay’);
Output;
---select*from dep;
---insert into dep1 values(280,'vijay’);
Output;
ON DELETE CASCADE AND ON DELETE SET NULL;
ON DELETE CASCADE;
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a
child table when corresponding rows are deleted in the parent table. If you do not specify
cascading deletes, the default behavior of the database server prevents you from deleting data
in a table if other tables reference it.
Syntax;
create table dep1(
dep_id number,
dep_name varchar2(20),
CONSTRAINT DEP1_ID_FK FOREIGN KEY(DEP_ID)REFERENCES
DEP(DEPARTMENT_ID)on delete cascade);
ON DELETE SET NULL:
A foreign key with "set null on delete" means that if a record in the
parent table is deleted, then the corresponding records in the child
table will have the foreign key fields set to NULL. The records in the child
table will not be deleted in SQL Server.
SYNTAX:
create table dep1(
dep_id number,
dep_name varchar2(20),
CONSTRAINT DEP1_ID_FK FOREIGN KEY(DEP_ID)REFERENCES
DEP(DEPARTMENT_ID)on delete set null
);
1.How to add constraint in table?
Syntax:
Alter table table_name ADD constraint constraint_name constraint
key(column_name);
2.How to drop constraint in table?
Alter table table_name drop constraint constraint_name;
3.Enable and disable;
Alter table table_name enable validateconstraint constraint_name;
Alter table table_name enable novalidate constraint constraint_name;
Alter table table_name disable constraint constraint_name;
S.No. Primary Key Foreign Key
1 A primary key generally focuses on the uniqueness of the table.
It assures the value in the specific column is unique.
A foreign key is generally used to
build a relationship between the two
tables.
2 Table allows only one primary key. Tables can allow more than one
foreign key.
3 The primary key doesn’t allow null values. Foreign key accepts multiple null
values.
4 It can identify the record uniquely in the database table. A foreign key is a field in the table
that is the primary key in another
table.
5 In the primary key, the value cannot be removed from the parent
table.
In this, the value can be deleted
from the child table.
6 Its restriction can be completely defined on the temporary
tables.
Its restriction cannot be defined on
the global or local temporary tables.
Difference Between Primary Key and Foreign Key
CONSTRAINTS PPT.pptx

CONSTRAINTS PPT.pptx

  • 1.
    CONSTRAINTS used tospecify rules for the data in a table
  • 2.
    WHAT IS CONSTRAINTSIN SQL? 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.
  • 3.
    CONSTRAINTS IN SQLCAN BE CATEGORIZED INTO TWO TYPES: Column Level Constraint: Column Level Constraint is used to apply a constraint on a single column. Example; create table dep1( dep_id number constraint dep_id_pk primary key, dep_name varchar2(20) constraint dep_name_uni unique, manager_id number constraint man_not_null not null, location_id number constraint loc_id_chq check(location_id between 1000 and 2500));
  • 4.
    TABLE LEVEL CONSTRAINT: TableLevel Constraint is used to apply a constraint on multiple columns. Example; create table dep1( dep_id number, dep_name varchar2(20), manager_id number constraint man_not_null not null, location_id number, constraint dep_id_pk primary key(dep_id), constraint dep_name_uni unique(dep_name), constraint loc_id_chq check(location_id between 1000 and 2500));
  • 5.
    TYPES OF CONSTRAINS KEYCONSTRAINTS a.Primary key constraints b. Unique constraints c. NOT NULL constraints DOMAIN CONSTRAINTS a. Check constraints INTEGRITY CONSTRAITS a.Foreign key (referential) constraints
  • 6.
    PRIMARY KEY CONSTRAINTS ThePRIMARY KEY constraint is a combination of both NOT NULL and UNIQUE constraints. This means that when you define a column with PRIMARY KEY, it will not accept any null or duplicate values. A table can have only ONE primary key. A composite primary key designates a combination of columns as the primary key. Example; create table dep1( dep_id number constraint dep_id_pk primary key, dep_name varchar2(20));
  • 7.
    --insert into dep1values(100,'balaji’); --insert into dep1 values(200,'balaji’); Output; --insert into dep1 values(100,'balaji’); --insert into dep1 values(null,'balaji’); Output;
  • 8.
    UNIQUE CONSTRAINTS A uniquekey is the set of fields or columns of a table that helps us uniquely identify records. The unique key guarantees the uniqueness of the columns in the database. It is similar to the primary key but can accept a null value. Example; create table dep1( dep_id number constraint dep_id_pk primary key, dep_name varchar2(20) constraint dep_name_uni unique);
  • 9.
    --insert into dep1values(100,'balaji’); --insert into dep1 values(200,'vijay’); Output; ---insert into dep1 values(300,'vijay’); ---insert into dep1 values(300,null);
  • 10.
    NOT NULL CONSTRAINTS TheNOT NULL constraint is used to ensure that a given column of a table is never assigned the null value. Once a NOT NULL constraint has been defined for a particular column, any insert or update operation that attempts to place a null value in that column will fail. Example; create table dep1( dep_id number constraint dep_id_notnull not null, dep_name varchar2(20));
  • 11.
    ---insert into dep1values(100,'balaji’); ---insert into dep1 values(100,'balaji’); Output; ---insert into dep1 values(null,'vijay’);
  • 12.
    CHECK CONSTRAINTS The CHECKconstraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row. Example; create table dep1( dep_id number constraint dep_id_chq check(dep_id between 100 and 200), dep_name varchar2(20));
  • 13.
    --insert into dep1values(100,'balaji’); --insert into dep1 values(120,’balaji’); --insert into dep1 values(190,'vijay’); --insert into dep1 values(200,'vijay’); Output; --insert into dep1 values(210,'vijay’);
  • 14.
    FOREIGN KEY (REFERENTIAL) CONSTRAINTS AFOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY OR UNIQUE KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table. A foreign key is generally used to build a relationship between the two tables.
  • 15.
    Example; create table dep1( dep_idnumber, dep_name varchar2(20), CONSTRAINT DEP1_ID_FK FOREIGN KEY(DEP_ID)REFERENCES DEP(DEPARTMENT_ID)); --insert into dep1 values(10,'balaji’); ---insert into dep1 values(20,'vijay’); Output;
  • 16.
  • 17.
    ---insert into dep1values(280,'vijay’); Output;
  • 18.
    ON DELETE CASCADEAND ON DELETE SET NULL; ON DELETE CASCADE; Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it. Syntax; create table dep1( dep_id number, dep_name varchar2(20), CONSTRAINT DEP1_ID_FK FOREIGN KEY(DEP_ID)REFERENCES DEP(DEPARTMENT_ID)on delete cascade);
  • 19.
    ON DELETE SETNULL: A foreign key with "set null on delete" means that if a record in the parent table is deleted, then the corresponding records in the child table will have the foreign key fields set to NULL. The records in the child table will not be deleted in SQL Server. SYNTAX: create table dep1( dep_id number, dep_name varchar2(20), CONSTRAINT DEP1_ID_FK FOREIGN KEY(DEP_ID)REFERENCES DEP(DEPARTMENT_ID)on delete set null );
  • 20.
    1.How to addconstraint in table? Syntax: Alter table table_name ADD constraint constraint_name constraint key(column_name); 2.How to drop constraint in table? Alter table table_name drop constraint constraint_name; 3.Enable and disable; Alter table table_name enable validateconstraint constraint_name; Alter table table_name enable novalidate constraint constraint_name; Alter table table_name disable constraint constraint_name;
  • 21.
    S.No. Primary KeyForeign Key 1 A primary key generally focuses on the uniqueness of the table. It assures the value in the specific column is unique. A foreign key is generally used to build a relationship between the two tables. 2 Table allows only one primary key. Tables can allow more than one foreign key. 3 The primary key doesn’t allow null values. Foreign key accepts multiple null values. 4 It can identify the record uniquely in the database table. A foreign key is a field in the table that is the primary key in another table. 5 In the primary key, the value cannot be removed from the parent table. In this, the value can be deleted from the child table. 6 Its restriction can be completely defined on the temporary tables. Its restriction cannot be defined on the global or local temporary tables. Difference Between Primary Key and Foreign Key