Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

RCS437E Lesson 7b

Understanding Integrity
Constraints in SQL

Dr. Patrick Wamuyu 74


Database Integrity
When you execute a DML statement (an
INSERT, UPDATE, or DELETE, for
example), the database ensures that the rows
in the tables maintain their integrity.
This means that any changes you make to
the rows in the tables must always be in
keeping with the primary key and foreign
key relationships set for the tables.

RCS437E 75
Entity Integrity
Entity Integrity:
The primary key attributes PK of each relation schema R
in S cannot have null values in any tuple of r(R).
This is because primary key values are used to identify the individual
tuples.
t[PK] ≠ null for any tuple t in r(R)
If PK has several attributes, null is not allowed in any of these attributes
Note:
Other attributes of R may be constrained to disallow null values,
even though they are not members of the primary key.

RCS437E 76
Entity Integrity
Entity Integrity:
ALTER TABLE [TABLE NAME] ADD PRIMARY KEY
[COLUMN NAME]
ALTER TABLE PK_DEMO ADD PRIMARY KEY (ID);
CREATE TABLE PK_DEMO_1 (
ID NUMBER,
NAME VARCHAR2(100),
CONSTRAINT PK_ID PRIMARY KEY(ID));

CREATE TABLE PK_DEMO_2 ( CREATE TABLE PK_DEMO_3 (


ID NUMBER, ID NUMBER (5) primary key,
NAME VARCHAR2(100), NAME VARCHAR2(100),
PRIMARY KEY(ID));
PRIMARY KEY(ID));
RCS437E 77
Entity Integrity
Primary key of a table must contain a unique, non-null
value for each row.
ISO standard supports FOREIGN KEY clause in
CREATE and ALTER TABLE statements:
PRIMARY KEY(staffNo)
PRIMARY KEY(clientNo, propertyNo)

Can only have one PRIMARY KEY clause per table.


Can still ensure uniqueness for alternate keys using
UNIQUE:
UNIQUE(telNo)

RCS437E 78
Entity Integrity
Enforcement of Primary Key Constraints
Let’s examine some examples that show the enforcement
of a primary key constraint.
The Employees table’s primary key is the emp_id
column, which means that every value stored in the
emp_id column must be unique.
If you try to insert/update a row with a duplicate value
for a primary key column, the database returns the error
ORA-00001
ERROR at line 1:
ORA-00001: unique constraint (STORE.EMPLOYEES_PK)
RCS437E
violated 78
Referential Integrity
FK is column or set of columns that links each
row in child table containing foreign FK to row
of parent table containing matching PK.
Referential integrity means that, if FK contains a
value, that value must refer to existing row in
parent table.
ISO standard supports definition of FKs with
FOREIGN KEY clause in CREATE and ALTER
TABLE:
FOREIGN KEY(branchNo) REFERENCES Branch

RCS437E 79
Referential Integrity
Tuples in the referencing relation R1 have
attributes FK (called foreign key attributes) that
reference the primary key attributes PK of the
referenced relation R2.
A tuple t1 in R1 is said to reference a tuple t2 in R2 if
t1[FK] = t2[PK].
A referential integrity constraint can be displayed
in a relational database schema as a directed arc
from R1.FK to R2.

RCS437E 80
Referential Integrity
Statement of the constraint
The value in the foreign key column (or columns) FK
of the the referencing relation R1 can be either:
(1) a value of an existing primary key value of a
corresponding primary key PK in the referenced relation
R2, or
(2) a null.
In case (2), the FK in R1 should not be a part of
its own primary key.

RCS437E 81
Referential Integrity
FOREIGN KEY (staffNo) REFERENCES Staff
ON DELETE SET NULL
FOREIGN KEY (ownerNo) REFERENCES Owner
ON UPDATE CASCADE

RCS437E 82
Referential Integrity
CREATE TABLE supplier(
supplier_id numeric(10) Primary Key,
supplier_name varchar2(50) not null,
contact_name varchar2(50));

CREATE TABLE products(


product_id numeric(10) Primary Key,
supplier_id numeric(10) References supplier,
supplier_name varchar2(50) not null );
RCS437E 83
Referential Integrity
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
PRIMARY KEY (supplier_id, supplier_name));

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name));
RCS437E 84
Referential Integrity
Enforcement of Foreign Key Constraints
A foreign key relationship is where a column from one
table is referenced in another.
The Emp_Dept column in the employees table
references the Dept_No column in the departments
table using a foreign key relationship.
The departments table is known as the parent table,
and the employees table is known as the child table
because the Emp_Dept column in the employees table
is dependent on the Dept_No column in the
departments table.
RCS437E 85
Database Integrity
If you try to insert a row into the employees table
with a non-existent emp_dept number, the
database will return the error ORA-02291.
This error indicates the database could not find a
matching parent key value (the parent key is the
dept_no colum
ERROR at line 1: ORA-02291: integrity constraint
(STORE.EMPLOYEES_FK_EMPLOYEE_TYPES)
violated - parent key not found).

RCS437E 86
Database Integrity
If you attempt to delete a row in the parent table
that already had dependent child rows, the
database returns error ORA-02292, which means a
child record was found.
For example, if you attempt to delete the row with
a emp_dept of 50 in the employees table, the
database will return error ORA-02292 because the
employees table contains rows with emp_dept
values equal to that value.

RCS437E 87
Other Types of Constraints
Semantic Integrity Constraints:
based on application semantics and cannot be
expressed by the model per se
Example: “the max. no. of hours per employee for all
projects he or she works on is 56 hrs per week”
A constraint specification language may have to
be used to express these
SQL-99 allows triggers and ASSERTIONS to
express for some of these

RCS437E 88
Integrity Enhancement Feature
Required Data
position VARCHAR(10) NOT NULL

Domain Constraints
(a) CHECK
Gender CHAR NOT NULL
CHECK (Gender IN (‘M’, ‘F’))
Quantity number(6) CHECK (quantity BETWEEN 100
AND 5000);

RCS437E 89
Possible violations for each operation
INSERT may violate any of the constraints:
Domain constraint:
if one of the attribute values provided for the new tuple is not of the
specified attribute domain
Key constraint:
if the value of a key attribute in the new tuple already exists in another
tuple in the relation
Referential integrity:
if a foreign key value in the new tuple references a primary key value
that does not exist in the referenced relation
Entity integrity:
if the primary key value is null in the new tuple

RCS437E 90
Possible violations for each operation
DELETE may violate only referential integrity:
If the primary key value of the tuple being deleted is
referenced from other tuples in the database
Can be remedied by several actions: RESTRICT, CASCADE, SET
NULL
– RESTRICT option: reject the deletion
– CASCADE option: propagate the new primary key value into the foreign
keys of the referencing tuples
– SET NULL option: set the foreign keys of the referencing tuples to NULL
One of the above options must be specified during database
design for each foreign key constraint

RCS437E 91
Possible violations for each operation
UPDATE may violate domain constraint and NOT
NULL constraint on an attribute being modified
Any of the other constraints may also be violated,
depending on the attribute being updated:
Updating the primary key (PK):
Similar to a DELETE followed by an INSERT
Need to specify similar options to DELETE
Updating a foreign key (FK):
May violate referential integrity
Updating an ordinary attribute (neither PK nor FK):
Can only violate domain constraints

RCS437E 92
Update Operations on Relations
In case of integrity violation, several actions can
be taken:
Cancel the operation that causes the violation
(RESTRICT or REJECT option)
Perform the operation but inform the user of the
violation
Trigger additional updates so the violation is
corrected (CASCADE option, SET NULL option)
Execute a user-specified error-correction routine

RCS437E 93
Referential Integrity
Any INSERT/UPDATE attempting to create FK
value in child table without matching CK value in
parent is rejected.
Action taken attempting to update/delete a CK
value in parent table with matching rows in child
is dependent on referential action specified using
ON UPDATE and ON DELETE subclauses:
CASCADE - SET NULL
SET DEFAULT - NO ACTION

RCS437E 94
Referential Integrity
CASCADE: Delete row from parent and delete matching
rows in child, and so on in cascading manner.
SET NULL: Delete row from parent and set FK
column(s) in child to NULL. Only valid if FK columns
are NOT NULL.
SET DEFAULT: Delete row from parent and set each
component of FK in child to specified default. Only valid
if DEFAULT specified for FK columns.
NO ACTION: Reject delete from parent. Default.

RCS437E 95
Database Integrity
Constraint Constraint Meaning
Type
CHECK C Specifies the value for a column, or group of columns, must satisfy
a certain condition.
NOT NULL C Specifies a column doesn't allow storage of null values. This is
actually enforced as a CHECK constraint. You can check NOT
NULL columns using the DESCRIBE command.
PRIMARY P Specifies the primary key of a table. A primary key is made up of
KEY one or more columns that uniquely identify each row in a table.
FOREIGN R Specifies a foreign key for a table. A foreign key references a
KEY column in another table, or a column in the same table in the case
of a self-reference.
UNIQUE U Specifies a column, or group of columns, can only store unique
values.
CHECK V Specifies that DML operations on a view must satisfy the subquery.
OPTION
READ ONLY O Specifies that a view may only be read from.
RCS437E 96
Database Integrity
Adding a CHECK Constraint
The following example uses ALTER TABLE to add a CHECK
constraint to the location column of the departments table:
ALTER TABLE departments ADD CONSTRAINT
departments_location_ck CHECK (location IN
(‘MOMBASA', ‘NAIROBI‘));
Notice the constraint enforces the status value is in the supplied
list of values in the IN clause
If you attempt to add a row that doesn’t satisfy a check
constraint, the database returns the error ORA-02290.
The following can also be used
CHECK (budget > 9000);
RCS437E 97
Database Transactions
A database transaction is a group of SQL statements that are a
logical unit of work.
You can think of a transaction as an inseparable set of SQL statements that should be
made permanent in the database (or undone) as a whole. An example of this would be
a transfer of money from one bank account to another.
One UPDATE statement would subtract from the total amount of
money from one account, and another UPDATE would add money
to the other account.
Both the subtraction and the addition must either be permanently
recorded in the database, or they both must be undone—otherwise
money will be lost.
This simple example uses only two UPDATE statements, but a
more realistic transaction may consist of many INSERT, UPDATE,
and DELETE statements.
RCS437E 98
Database Transactions
Committing and Rolling Back a Transaction
To permanently record the results of the SQL statements in a
transaction, you perform a commit with the COMMIT
statement.
To undo the results of the SQL statements, you perform a
rollback with the ROLLBACK statement, which resets all the
rows back to what they were originally.
Any changes you make prior to performing a rollback will be
undone, as long as you haven’t disconnected from the database
beforehand.
COMMIT;
ROLLBACK;
RCS437E 99
Database Transactions
Starting and Ending a Transaction
A transaction has both a beginning and an end; it begins
when one of the following events occurs:
You connect to the database and perform the first DML
statement.
A previous transaction ends and you enter another DML
statement.
A transaction ends when one of the following events occurs:
You perform a COMMIT or a ROLLBACK statement.
You perform a DDL statement, such as a CREATE TABLE
statement, in which case a COMMIT is automatically
performed.
RCS437E 100
Database Transactions
You perform a DCL statement, such as a GRANT statement, in
which case a COMMIT is automatically performed.
You disconnect from the database.
If you exit SQL*Plus normally by entering the EXIT command, a
COMMIT is automatically performed for you.
If SQL*Plus terminates abnormally—for example, if the
computer on which SQL*Plus was running were to crash—a
ROLLBACK is automatically performed.
This applies to any program that accesses a database. For example, if you
wrote a Java program that accessed a database and your program
crashed, a ROLLBACK would be automatically performed.
You perform a DML statement that fails, in which case a
ROLLBACK is automatically performed for that individual
DML statement.
RCS437E 101
Database Transactions
Savepoints
You can also set a savepoint at any point within a
transaction.
These allow you to roll back changes to that point.
This might be useful if you have a very long
transaction because if you make a mistake after you’ve
set a savepoint, you don’t have to roll back the
transaction all the way to the start.
You should use savepoints sparingly; you might be
better off restructuring your transaction into smaller
transactions instead.
RCS437E 102
Database Transactions
Savepoints
Set a savepoint named save1.
This will allow you to roll back any further DML
statements and preserve the previous changes to a
table:
SAVEPOINT save1;
To roll back the transaction to a savepoint
established earlier:
ROLLBACK TO SAVEPOINT save1;
RCS437E 103
Sequences
A sequence is a database item that generates a
sequence of integers.
You typically use the integers generated by a
sequence to populate a numeric primary key column.
Sequences are database objects from which multiple users
can generate unique integers.
The sequence generator generates sequential numbers,
which can help to generate unique primary keys
automatically, and to coordinate keys across multiple rows
or tables.
Without sequences, sequential values can only be produced
programmatically. 104
Sequences
Creating a Sequence
You create a sequence using the CREATE
SEQUENCE statement, which has the following
syntax:
CREATE SEQUENCE sequence_name
[START WITH start_num]
[INCREMENT BY increment_num]
[ { MAXVALUE maximum_num | NOMAXVALUE } ]
[ { MINVALUE minimum_num | NOMINVALUE } ]
[ { CYCLE | NOCYCLE } ]
[ { CACHE cache_num | NOCACHE } ]
[ { ORDER | NOORDER } ]; 105
Sequences
sequence_name specifies the name you assign to the sequence.
START WITH start_num specifies the integer to start the sequence.
The default start number is 1.
INCREMENT BY increment_num specifies the integer to
increment the sequence by.
The default increment number is 1. The absolute value of increment_num must be
less than the difference between maximum_num and minimum_num.
MINVALUE minimum_num specifies the maximum integer of the
sequence. minimum_num must be less than or equal to start_num,
and minimum_num must be less than maximum_num.
NOMINVALUE specifies the maximum is 1 for an ascending
sequence or -1026 for a descending sequence. NOMINVALUE is
the default.
RCS437E 106
Sequences
MAXVALUE maximum_num specifies the maximum integer of the sequence.
maximum_num must be greater than or equal to start_num, and maximum_num
must be greater than minimum_num.
NOMAXVALUE specifies the maximum is 1027 for an ascending sequence or –1
for a descending sequence.
NOMAXVALUE is the default.
CYCLE specifies the sequence generates integers even after reaching its
maximum or minimum value.
When an ascending sequence reaches its maximum value, the next value generated is the
minimum. When a descending sequence reaches its minimum value, the next value generated is
the maximum.
NOCYCLE specifies the sequence cannot generate any more integers after
reaching its maximum or minimum value.
NOCYCLE is the default.

RCS437E 107
Sequences
CACHE cache_num specifies the number of integers to keep in
memory.
The default number of integers to cache is 20. The minimum number of integers that
may be cached is 2.
The maximum integers that may be cached is determined by the formula
CEIL(maximum_num - minimum_num)/ABS(increment_num).
NOCACHE specifies no integers are to be stored.
ORDER guarantees the integers are generated in the order of the
request.
NOORDER doesn’t guarantee the integers are generated in the
order of the request.
NOORDER is the default.

RCS437E 108
Sequences
The following example creates a sequence named test_seq:
CREATE SEQUENCE test_seq;
Since this CREATE SEQUENCE statement omits the optional parameters,
the default values are used.
This means that parameters such as start number and the increment by
numbers are set to the default of 1.
The next example creates a sequence named test2_seq that supplies
values for the optional parameters:
CREATE SEQUENCE test2_seq
START WITH 10 INCREMENT BY 5
MINVALUE 10 MAXVALUE 20
CYCLE CACHE 2 ORDER;

RCS437E 109
Sequences
The final example creates a sequence named test3_seq
that starts at 10 and counts down to 1:
CREATE SEQUENCE test3_seq
START WITH 10 INCREMENT BY -1
MINVALUE 1 MAXVALUE 10
CYCLE CACHE 5;
Using a Sequence
A sequence generates a series of numbers. A sequence contains
two pseudo columns named currval and nextval that you use to
get the current value and the next value from the sequence.

RCS437E 110
Sequences
Using a Sequence
Before retrieving the current value you must initialize a
sequence by retrieving the next value. When you select
test_seq.nextval the sequence is initialized to 1.
For example, the following SELECT statement retrieves test_seq.nextval;
Notice you use dual in the FROM clause:

SELECT test_seq.nextval FROM dual;


The first value in test_seq sequence is 1. Once initialized, you
can get the current value from the sequence using currval.
For example:
SELECT test_seq.currval FROM dual;

RCS437E 111
Using a Sequence
CREATE TABLE EMPLOYEE(
ID NUMBER NOT NULL PRIMARY KEY,
EMP_NAME VARCHAR2(100) NOT NULL,
DESIGNATION VARCHAR2(50) NOT NULL);

CREATE SEQUENCE SEQ_EMPLOYEE_ID


INCREMENT BY 1
START WITH 1;

INSERT INTO EMPLOYEE VALUES(SEQ_EMPLOYEE_ID.nextval,


'Kanyi','Programmer');
INSERT INTO EMPLOYEE VALUES(SEQ_EMPLOYEE_ID.nextval,'Abdi','Programmer');
INSERT INTO EMPLOYEE
VALUES(SEQ_EMPLOYEE_ID.nextval,'Akinyi','Programmer');
UPDATE EMPLOYEE SET ID= SEQ_EMPLOYEE_ID.NEXTVAL WHERE ID= 12;
RCS437E 112
Sequences
Modifying a Sequence
You modify a sequence using the ALTER SEQUENCE
statement. There are some limitations on what you can
modify in a sequence, which include the following:
You cannot change the start value of a sequence.
The minimum value cannot be more than the
current value of the sequence ( currval ).
The maximum value cannot be less than the current
value of the sequence ( currval ).
ALTER SEQUENCE test_seq INCREMENT BY 2;
RCS437E 113
Sequences
Dropping a Sequence
You drop a sequence using DROP SEQUENCE.
The following example drops test3_seq:
DROP SEQUENCE test3_seq;

RCS437E 114
Domain Constraints Example
create table employees(
empno number(8),
empname varchar2(20) not null,
title varchar2(15) check(title in(‘manager’,
‘programmer’, ‘tester’),
dept number(6) check(dept between1 and 3),
salary number(8,2) check(salary>=30000),
allowance number(6,2),
primary key(empno));
RCS437E 115

You might also like