Top Banner
Constraints In SQL Constraints In SQL Presented By Presented By Priyanka Kumari Priyanka Kumari
13

Constraints In Sql

Jul 15, 2015

Download

Education

anuragr anuragr
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Constraints In Sql

Constraints In SQLConstraints In SQL

Presented ByPresented By

Priyanka KumariPriyanka Kumari

Page 2: Constraints In Sql

Topics to be CoveredTopics to be Covered

What is a constraint?What is a constraint? Column Level ConstraintsColumn Level Constraints Table Level ConstraintsTable Level Constraints Not Null ConstraintNot Null Constraint Unique Key ConstraintUnique Key Constraint Default ConstraintDefault Constraint Check ConstraintCheck Constraint Primary Key ConstraintPrimary Key Constraint Foreign Key ConstraintForeign Key Constraint Defining Constraint in Alter Table CommandDefining Constraint in Alter Table Command

Page 3: Constraints In Sql

What is a Constraint?What is a Constraint?

Integrity Constraints are the rules in real life which are to be Integrity Constraints are the rules in real life which are to be imposed on the data.imposed on the data.

Besides the cell name, cell length and the cell data type, there are Besides the cell name, cell length and the cell data type, there are other parameters that is other data constraints that can be other parameters that is other data constraints that can be passed to the DBA at cell creation time.passed to the DBA at cell creation time.

These data constraints are connected to a cell by the DBA as a These data constraints are connected to a cell by the DBA as a flag. Whenever a user attempts to load a cell with data, DBA flag. Whenever a user attempts to load a cell with data, DBA will check the data being loaded into the cell against the data will check the data being loaded into the cell against the data constraint defined at the cell creation time. If the data being constraint defined at the cell creation time. If the data being loaded fails to satisfy any of the data constraint checks fired loaded fails to satisfy any of the data constraint checks fired by the DBA, the DBA will not load the data into the cell, by the DBA, the DBA will not load the data into the cell, reject the entered record and will flash an error message.reject the entered record and will flash an error message.

Page 4: Constraints In Sql

The constraints can either be placed at the column level or The constraints can either be placed at the column level or at the table level.at the table level.

Column Level Constraint-Column Level Constraint- These constraints are defined These constraints are defined along with the column definition. These constraints can along with the column definition. These constraints can be applied to any one column at a time. If the constraints be applied to any one column at a time. If the constraints spans across multiple columns ,then the table level spans across multiple columns ,then the table level constraints are used.constraints are used.

Table Level Constraints-Table Level Constraints- If the data constraint attached to a If the data constraint attached to a specific cell in a table references the content of another specific cell in a table references the content of another cell in the table then the table level constraint is used.cell in the table then the table level constraint is used.

Page 5: Constraints In Sql

Types Of ConstraintsTypes Of ConstraintsNot Null ConstraintNot Null Constraint – – When a column name is defined as not When a column name is defined as not

null, then that column becomes a mandatory column. It null, then that column becomes a mandatory column. It implies that the user is enforced to enter data into that column.implies that the user is enforced to enter data into that column.

Principles of Null Values :Principles of Null Values :1.Setting the null value is appropriate when the actual value is 1.Setting the null value is appropriate when the actual value is

unknown or a value would not be meaningful.unknown or a value would not be meaningful.2.A null value would not be equivalent to a value of zero.2.A null value would not be equivalent to a value of zero.3.A null value would evaluate to null in any expression.3.A null value would evaluate to null in any expression. Ex-Null multiplied by 10 is null.Ex-Null multiplied by 10 is null. CREATE TABLE studentCREATE TABLE student (rollNo varchar2(4) NOT NULL,(rollNo varchar2(4) NOT NULL,

name varchar2(20) NOT NULL,name varchar2(20) NOT NULL,address varchar2(30) ,address varchar2(30) ,marks number(5,2));marks number(5,2));

Page 6: Constraints In Sql

Unique Key ConstraintUnique Key Constraint – The purpose of a unique key is – The purpose of a unique key is to ensure that information in the column for each record to ensure that information in the column for each record is unique.is unique.

Unique Key as a column constraintUnique Key as a column constraint : :CREATE TABLE studentCREATE TABLE student

(rollNo varchar2(4) UNIQUE ,(rollNo varchar2(4) UNIQUE ,name varchar2(20) ,address varchar2(30) ,name varchar2(20) ,address varchar2(30) ,marks number(5,2));marks number(5,2));

Unique Key as a table constraintUnique Key as a table constraint::CREATE TABLE studentCREATE TABLE student

(rollNo varchar2(4) ,name varchar2(20) ,(rollNo varchar2(4) ,name varchar2(20) ,address varchar2(30) ,marks number(5,2)address varchar2(30) ,marks number(5,2)CONSTRAINT roll_key UNIQUE(rollNo));CONSTRAINT roll_key UNIQUE(rollNo));

Page 7: Constraints In Sql

Default Key ConstraintDefault Key Constraint – At the time of cell creation a – At the time of cell creation a‘‘default value’ can be assigned to it.When the user is loading a default value’ can be assigned to it.When the user is loading a

‘record’ with values and leaves this cell empty,the DBA ‘record’ with values and leaves this cell empty,the DBA will automatically load this cell with the default value will automatically load this cell with the default value specified. The data type of the default value should match specified. The data type of the default value should match the data type of the column.the data type of the column.

CREATE TABLE student CREATE TABLE student (rollNo varchar2(4) ,name varchar2(20) ,(rollNo varchar2(4) ,name varchar2(20) ,address varchar2(30) ,address varchar2(30) ,marks number(5,2) DEFAULT 0);marks number(5,2) DEFAULT 0);

Page 8: Constraints In Sql

Check ConstraintCheck Constraint – It is used when we need to enforce – It is used when we need to enforce integrity rules that can be evaluated based on a logical integrity rules that can be evaluated based on a logical expression.expression.

CREATE TABLE student CREATE TABLE student

(rollNo varchar2(4) CHECK(rollNo like ‘C%’), (rollNo varchar2(4) CHECK(rollNo like ‘C%’),

name varchar2(20) CONSTRAINT chk_nm name varchar2(20) CONSTRAINT chk_nm CHECK(name = upper(name)),CHECK(name = upper(name)),

address varchar2(30) ,address varchar2(30) ,

marks number(5,2) CHECK(marks > 40));marks number(5,2) CHECK(marks > 40));

Page 9: Constraints In Sql

Primary Key Constraint Primary Key Constraint – It is used to uniquely identify each – It is used to uniquely identify each row in the table. Primary key values must not be null and must row in the table. Primary key values must not be null and must be unique across the column.be unique across the column.

Primary Key as a column constraintPrimary Key as a column constraint – – CREATE TABLE student CREATE TABLE student

(rollNo varchar2(4) PRIMARY KEY,(rollNo varchar2(4) PRIMARY KEY,name varchar2(20) , address varchar2(30) ,name varchar2(20) , address varchar2(30) ,marks number(5,2));marks number(5,2));

Primary Key as a table constraint –Primary Key as a table constraint –CREATE TABLE student CREATE TABLE student

(rollNo varchar2(4) ,name varchar2(20) , (rollNo varchar2(4) ,name varchar2(20) , address varchar2(30) ,marks number(5,2)address varchar2(30) ,marks number(5,2)PRIMARY KEY(rollNo));PRIMARY KEY(rollNo));

Page 10: Constraints In Sql

Foreign Key Constraint Foreign Key Constraint – Foreign keys represent relationships between tables.A – Foreign keys represent relationships between tables.A foreign key is a column(or a group of columns)whose values are derived from foreign key is a column(or a group of columns)whose values are derived from the primary key of the same or some other table.the primary key of the same or some other table.

The existence of a foreign key implies that the table with the foreign key is The existence of a foreign key implies that the table with the foreign key is related to the primary key table from which the foreign key is derived.A related to the primary key table from which the foreign key is derived.A foreign key must have a correponding primary key value in the primary key foreign key must have a correponding primary key value in the primary key table to have a meaning.table to have a meaning.

Foreign Key/References constraintForeign Key/References constraint::

1.Rejects an INSERT or UPDATE of a value if a corresponding value does not 1.Rejects an INSERT or UPDATE of a value if a corresponding value does not currently exist in the primary key table.currently exist in the primary key table.

2.Rejects a DELETE,if it would invalidate a REFERENCES constraint.2.Rejects a DELETE,if it would invalidate a REFERENCES constraint.3.Must reference a PRIMARY KEY or UNIQUE column in primary key table.3.Must reference a PRIMARY KEY or UNIQUE column in primary key table.4.Will reference the PRIMARY KEY of the primary key table if no column or 4.Will reference the PRIMARY KEY of the primary key table if no column or

group of columns is specified in the constraints.group of columns is specified in the constraints.5.Must refer a table , not a view.5.Must refer a table , not a view.6.Requires that a FOREIGN KEY column and the CONSTRAINT column have 6.Requires that a FOREIGN KEY column and the CONSTRAINT column have

matching data types.matching data types.

Page 11: Constraints In Sql

Foreign Key as a column constraint- Foreign Key as a column constraint- CREATE TABLE report CREATE TABLE report

(slNo number(2) PRIMARY KEY,(slNo number(2) PRIMARY KEY,roll varchar2(4) REFERENCES student(rollNo),roll varchar2(4) REFERENCES student(rollNo),grade char(1));grade char(1));

Foreign Key as a table constraint- Foreign Key as a table constraint- CREATE TABLE report CREATE TABLE report

(slNo number(2) PRIMARY KEY,(slNo number(2) PRIMARY KEY,roll varchar2(4) ,grade char(1),roll varchar2(4) ,grade char(1),FOREIGN KEY(roll) REFERENCES FOREIGN KEY(roll) REFERENCES

student(rollNo);student(rollNo);

Page 12: Constraints In Sql

Defining Constraint in the Alter tableDefining Constraint in the Alter tableAdd PRIMARY KEYAdd PRIMARY KEYALTER TABLE studentALTER TABLE student

ADD PRIMARY KEY(rollNo);ADD PRIMARY KEY(rollNo);

Add FOREIGN KEYAdd FOREIGN KEYALTER TABLE reportALTER TABLE report

ADD CONSTRAINT rk REFERENCES ADD CONSTRAINT rk REFERENCES student(roll);student(roll);

Add NOT NULLAdd NOT NULLALTER TABLE studentALTER TABLE student

MODIFY(name varchar2(20) NOT NULL);MODIFY(name varchar2(20) NOT NULL);

Page 13: Constraints In Sql

THANK YOUTHANK YOU