Referential Integrity

0

To establish a “parent-child” or a “master-detail” relationship between two tables having a common column, we make use of referential integrity constraints. To implement this, we should define the column in the parent table as a primary key and the same column in the child table as a foreign key referring to the corresponding parent entry.

A value that appears in one relation for a given set of attributes also appears for a certain group of attributes in another relation. This condition is called referential integrity.

It is a rule that maintains consistency among the rows of two relations. The rule states that if there is a foreign key in one relation, either each foreign key value must match a primary key value in another relation.

database-integrity
database-integrity

Create table Department

( Denptno number(2),

Dname varchar2(20),

HOD varchar2(10),

Constraint pk_deptno Primary Key(Deptno));

Create table Employee

( EmpNo number(3),

Ename varchar2(20),

Salary number(5),

Address varchar2(20),

Deptno number(2),

Constraint pk_empno Primay Key(EmpNo)

Constraint fk_deptno Foreign Key (Deptno) References Department(Deptno));

There may be a tuple tr in r(say Department Table) that does not join with any tuple in s (say Employee Table). Such tuples are called dangling tuples. Depending on the entity set or relationship set being modeled, dangling tuples may or may not be acceptable. Here as shown in the example above record of deptid 30 is exist whose corresponding employee record does not exist so that tuple is called dangling tuples. Dangling tuples in a relation are permitted where the primary key existed but dangling tupes into the foreign key columns contains does not permit.

Database modifications can cause violations of referential integrity. While performing database modification; referential-integrity constraint rules should not be violated.

Insert:-

If a tuple t2 is inserted into r2, the system must ensure that there must be a tuple t1 in r1.

This means that if you enter an employee record into the Employee table then his/her department must have existed in the Department table under which he/she is working.

i.e. we can enter employee records who are working under departments 10,20 or 30 but not 40 cause this is not present in the Department table.

Delete:-

If a tuple t1 is deleted from r1, the system must compute the set of tuples in r2 that reference t1.

If this set is not empty, either the delete command is rejected as an error, or the tuples that reference t1 must themselves be deleted. The latter solution may lead to cascading deletions, since tuples may reference tuples that reference t1, and so on.

In short; you can’t delete any department from the Department table till you delete all the employees from the Employee table working under that department. But here we can use on delete cascade macro for performing this task.

Update:-

We must consider two cases for the update: updates to the referencing relation (r2), and updates to the referenced relation (r1).

  • If a tuple t2 is updated in relation r2, and the update modifies values for the foreign key, then a test similar to the insert case is made.
  • If a tuple t1 is updated in r1, and the update modifies values for the primary key, then a test similar to the delete case is made.

SQL Syntax:

Create a table Employee (foreign key (Deptno) references Department(Deptno) on delete cascade on update cascade);

Foreign Key can be specified as part of the SQL create table statement by using the foreign key clause. (ie. SQL DDL statements). By default, a foreign key references the primary key attributes of the referenced table. SQL also supports a version of the references clause where a list of attributes of the referenced relation can be specified explicitly. Ie. We can simply write
Foreign key (Deptno) references Department);

When a referential integrity constraint is violated, the normal procedure is to reject the action that caused the violation, However, a foreign key clause can specify that if a delete or update action on the referenced relation violates the constraint, then, instead of rejecting the action, the system must take steps to change the tuple in the referencing relation to restoring the constraint.

Because of the clause on delete cascade associated with the foreign-key declaration, if a delete of a tuple in any Department table results in this referential-integrity constraint being violated, the system does not reject the delete. Instead, the delete “cascades to the Employee relation, deleting the tuple that refers to the Department that was deleted. Similarly, the system does not reject an update to a field referenced by the constraint if it violates it.

Referential Integrity (example from BOOK)

(referential-integrity constraints or sub-set dependencies)

A value that appears in one relation for a given set of attributes also appears for a certain set of attributes in another relation. This condition is called referential integrity.

create table customer

( customer_name char(20),

customer_street char(30),

customer_city char(30),

primary key (customer_name));

create table branch

( branch_name char(15),

branch_city char(30),

assets numeric(16,2),

primary key (branch_name),

check (assets>=0));

create table account

( account_number char(10),

branch_name char(15),

balance numeric(12,2),

primary key (account_number),

foreign key (branch_name) references the branch,

check (balance >=0));

create table depositor

( customer_name char(20),

account_number char(20),

 primary key (customer_name, account_number),

foreign key (customer_name) references the customer,

foreign key (account_name) references account);

Foreign keys can be specified as part of the SQL create table statement by using the foreign key clause. We illustrate foreign-key declarations by using the SQL DDL definition of part of our database (as shown above in SQL statements).

The definition of the account table has a declaration of a foreign key (branch_name) referencing the branch. This foreign-key declaration specifies that for each account tuple, the branch name specified in the tuple must exist in the branch relation.

By default, in SQL a foreign key references the primary key attributes of the referenced table. SQL also supports a version of the references clause where a list of attributes of the referenced relation can be specified explicitly.

Branch_name char(15) references the branch

When a referential integrity constraint is violated, the normal procedure is to reject the action that caused the violation. However, a foreign key  clause can specify that if a delete or update action on the referenced relation violates the constraint, then, instead of rejecting the action, the system must take steps to change the tuple in the referencing relation to restore the constraint. Consider this definition of an integrity constraint on the relation account

Assertions

An assertion is a predicate expressing a condition that we wish the database to always satisfy. Domain constraints and referential integrity constraints are special forms of assertions. We have paid substantial attention to these forms of assertion because they are easily tested and apply to a wide range of database applications. However, there are many constraints that we cannot express by using only these special forms. Two examples of such constraints are:

  • The sum of all loan amounts for each branch must be less than the sum of all account balances at the branch.
  • Every loan has at least one customer who maintains an account with a minimum balance of Rs. 1000.

An assertion in SQL takes the form

create assertion <assertion-name> check <predicate>

When an assertion is created, the system tests it for validity. If the assertion is valid, then any future modification to the database is allowed only if it does not cause that assertion to be violated. This testing may introduce a significant amount of overhead if complex assertions have been made.

Domain Constraints

0

(the principle behind attribute domains is similar to that behind typing of variables in programming languages.)

We have seen that a domain of possible values must be associated with every attribute. We know a no of standard domain types and data and time types defined in SQL. Declaring an attribute to be of a particular domain acts as a constraint on the values that it can take. The system tests them easily whenever a new data item is entered into the database.

A domain is a set of values that may be assigned to an attribute. A domain definition usually consists of the following components: domain name, meaning, data type , size(or length), and allowable values or allowable range.

As we know that every attribute must have a specific domain (in general data types) that accepts the associated values of its own kind. We know a number of standard domain types, such as integer types, character types, and date/time types defined in SQL. Declaring an attribute to be of a particular domain acts as a constraint on the values that it can take. Domain constraints are the most elementary form of integrity constraint. The system tests them easily whenever a new data item is entered into the database.

It is possible for several attributes to have the same domain. For example, the attributes customer-name and employee-name might have the same domain. At the implementation level, both customer names and branch names are character strings.

The create domain clause can be used to define new domains.

create domain <domain-name> <constraints-types>

For example, the statements

create domain dollars number(12,2);

create domain pounds number(10,2);

Here, these statements define the domains dollars and pounds to be decimal numbers with a total of 12 digits and 10 digits respectively, two of which are placed after the decimal point. An attempt to assign a value of type dollars to a variable of type pounds would result in a syntax error, although both are of the same numeric type.

The check clause in SQL permits domains to be restricted in powerful ways that most programming language-type systems do not permit. Specifically, the check clause permits the schema designer to specify a predicate (selection condition) that must be satisfied by any value assigned to a variable whose type is the domain.

For example, a check clause can ensure that an hourly wage domain allows only values greater than a specified values.

create domain hourly wage numeric(5,2)

constraint wage-value-test check(value>=4.00)

The domain hourly wage has a constraint that ensures that the hourly wage is greater than 4.00. the clause constraint wage-value-test is optional and is used to give the name wage-value-test to the constraint. The name is used to indicate which constraint an update violated.

The check clause can also be used to restrict a domain not to contain any null values.

create domain account char(10)

constraint account-no-null-test check(value not null)

Another example: here the domain can be restricted to contain only a specified set of values by using the in the clause.

create domain accounType char(10)

constraint account-type-test check(value in(Checking, Saving))

Integrity Constraints

0

(The rules that should not be violated while performing an operation on the database)

Integrity constraints ensure that changes made to the database by authorized users do not result in a loss of data consistency. Thus, integrity constraints guard against accidental damage to the database.

Simple Constraints are Key Constraints and forms of Relationships Constraints.

The relational data model includes several types of constraints, or business rules, whose purpose is to facilitate maintaining the accuracy and integrity of data in the database. The major types of integrity constraints are domain constraints, entity integrity, and referential integrity.

Integrity Constraint:

Database designers can specify integrity constraints that are enforced by the DBMS. A constraint is a rule that cannot be violated by database users (ie. Also called a business rule).

(controlling data integrity)

For many DBMS data, integrity constraints ( ie. Control on the possible value a field can assume) can be built into the physical structures of the fields. The data type enforces one form of data integrity control. Since it may limit the type of data (for eg:- numeric or character, data type) and length of a field value. Some typical integrity constraints control that a DBMS may support are:-

Default Value:- A default is a value a field will assume unless a user enters an explicit value. For an instance of that field. Assigning a default value to a field can reduce a data entry time since the entry of a value can be skipped and it can also help to reduce data entry errors for that most common value.

Range Control:- A range control limits the set of permitted values, a field may assume. The range may be a numeric lower to upper bound or a set of specific values. Range control must be used with caution since the limits of the range may change over time.

NULL value control:- Each primary key must have an integrity control that prohibits null value. Any other required fields may also have invalid value control placed on them if that is the policy of the organization.

Referential Integrity:- Referential Integrity on a field is a form of range control in which the value of that field must exist as the value in some field in another row of the same or different table. ie. The range of legitimate values comes from the dynamic contents of the field in a database table, not from the pre-specified setup values.

An integrity constraint is that the value of an attribute in one relation depends on the value of a primary key in the same or another relation.