Académique Documents
Professionnel Documents
Culture Documents
Lecture #:2 Course Code: CSC 610 Topic: Data Base Integrity Semester: III Program: MSC Dept. Computer Science Lecture Prepared by: Miss Nasreen Anjum
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.
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
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.
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;
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.
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
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