Vous êtes sur la page 1sur 19

Advance data base

Lecture #:2 Course Code: CSC 610 Topic: Data Base Integrity Semester: III Program: MSC Dept. Computer Science Lecture Prepared by: Miss Nasreen Anjum

What is database integrity


Integrity constraints are used to ensure accuracy and consistency of data in a relational database. Ensuring changes do not result in loss of consistency.

A DBMS should provide capabilities for defining and enforcing these constraints.
What we mean by enforcing constraints is new entered information may be wrong and DBMS should guarantee that wrong operation will not effect accuracy and consistency.

Types of integrity constraints


Entity integrity Referential integrity(not included) Domain integrity User defined integrity

Domain Integrity Constraints


A domain defines the possible values of an attribute. Domain Integrity rules govern these values. In a database system, the domain integrity is defined by: The data type and the length The NULL value acceptance The allowable values, through techniques like constraints or rules The default value

For example, if you define the attribute of Age, of an Employee entity, is an integer, the value of every instance of that attribute must always be numeric and an integer. If you also define that this attribute must always be positive, the a negative value is forbidden

Domain Integrity Constraints: contd..


This type of data integrity warrants the following:
the identity and purpose of a field is clear and all of the tables in which it appears are properly identified.

Each attribute in the model should be assigned domain information which includes:
Data Type - Basic data types are integer, decimal, or character. Most data bases support variants of these plus special data types for date and time. Length - This is the number of digits or characters in the value. For example, a value of 5 digits or 40 characters. Null support - Indicates whether the attribute can have null values. Default value (if any) - The value an attribute instance will have if a value is not entered.

Domain Integrity Constraints: contd..


A column definition can specify NOT NULL. This indicates that the table column must contain a value for each row. By implication a column definition that does not specify NOT NULL do not have to contain an actual value

Examples Not Null: CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10) ); In the above example we see that name field cant be null , if we try to give null values ,dbms won`t accept it.

Check Constraint
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 single column it allows only certain values for this column. Example 1: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), gender char(1) CHECK (gender in ('M','F')), salary number(10), location char(10) ); Example 2: CREATE TABLE Persons ( P_Id int NOT NULL CHECK (P_Id>0), LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) Example above indicates that persons id is an integer and it cant be null and it is bigger than 0;

Check Constraint: contd..


Example3: CREATE TABLE Persons ( P_Id int NOT NULL,LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes') ) The above example is to allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns

DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified.

Example Default Constraint


CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255),City varchar(255) DEFAULT 'Sandnes')

Unique Constraint
The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

Example Unique
CREATE TABLE Persons(P_Id int NOT NULL UNIQUE LastName varchar(255) NOT NULL, FirstName varchar(255),Address varchar(255),City varchar(255)). The above example guarantees P_id is unique for all columns.

CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName) ) The above example is to to allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns

Entity Integrity
In the relational data model, entity integrity is one of the three inherent integrity rules. Entity Integrity ensures that there are no duplicate records within the table and that the field that identifies each record within the table is unique and never null.

The existence of the Primary Key is the core of the entity integrity. If you define a primary key for each entity, they follow the entity integrity rule. So Entity integrity rule ensures that primary key cannot contain null values. It is also called row integrity.

13

Entity Integrity: contd..


Entity Integrity ensures two properties for primary keys: The primary key for a row is unique; it does not match the primary key of any other row in the table. The primary key is not null, no component of the primary key may be set to null. The uniqueness property ensures that the primary key of each row uniquely identifies it; there are no duplicates. The second property ensures that the primary key has meaning, has a value; no component of the key is missing. Within relational databases using SQL, entity integrity is enforced by adding a primary key clause to a schema definition. The system enforces Entity Integrity by not allowing operations (INSERT, UPDATE) to produce an invalid primary key. Any operation that is likely to create a duplicate primary key or one containing nulls is rejected.

Entity Integrity: contd..


Example: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), age number(2), location char(10) ); Insert into employee values(123,Mehmet Yilmaz,28,Istanbul) Insert into employee values(124,Ali Er,24,Istanbul) Insert into employee values(123,Ahmet Yildiz,24,Istanbul) Insert into employee values(,,28,Ankara)

Referential Integrity constraint


It define the relationship between tables when records are added or deleted. It ensures that key values are consistent across the table. A value can not be added in foreign key if it has no corresponding values in primary key field of the relation Table. Such consistency require that if a key value changes then all reference to its should be changed accordingly. The value in the foreign key column must be same as the value in the corresponding primary key column in reference table.
16

2. Referential Integrity constraint (contd..)


Registration No 1940 1989 1991 Name Nadeem Ejaz Nauman Master Table Registration No Subject Marks Class MSc Msc Msc

1940 1940
1940 1989 1989 1991 1991 1991

Oop DBMS
Html Oop DBMS Oop DBMS Html

34 67
89 90 78 57 22 90
17

Child Table

2. Referential Integrity(cont.)
A value cannot be entered in child table before entering the corresponding value in master table. if the user want to enter the result of std with registration no 1940 he has to enter the record in master Table. Similarly if the record has to deleted from master table, it is necessary to delete the corresponding records in child table first.

18

References
http://www.data-eeducation.com/E113_Domain_Integrity_Constrai nts.html http://en.wikipedia.org/wiki/Database_integrity http://www.gc.maricopa.edu/business/sylvester/ cis164/session4.htm http://www.data-eeducation.com/E113_Domain_Integrity_Constrai nts.html

Vous aimerez peut-être aussi