Relational Database
Relational Database
relation)
Degree of a relation (No. of attributes in a
relation)
Domain (Set of all possible values that an attribute
my validly contain)
Constraints are the rules enforced on data
columns on table. These are used to limit the
type of data that can go into a table. This ensures
the accuracy and reliability of the data in the
database.
The whole purpose of constraints is to maintain
the data integrity during an update/delete/insert
into a table.
Constraints could be column level or table level.
Column level constraints are applied only to one
column, whereas table level constraints are
applied to the whole table.
the values of individual columns are valid.
In a table, rows have a valid primary key or unique key
values.
in a dependent table, rows have valid foreign key values
Example-
Consider the following Student table-
Here, value ‘A’ is not allowed since only integer values can be taken by the age attribute.
Tuple Uniqueness Constraint
Tuple Uniqueness constraint specifies that all the tuples must be
necessarily unique in any relation.
Example-01:
This relation satisfies the tuple uniqueness constraint since here all the tuples are
unique.
Example-02:
Consider the following Student table-
This relation does not satisfy the tuple uniqueness constraint since here all the tuples
are not unique.
Key Constraint
Key constraint specifies that in any relation-
•All the values of primary key must be unique.
•The value of primary key must not be null.
Example-
This relation does not satisfy the key constraint as here all the values of primary key are
not unique.
Entity Integrity Constraint
Entity integrity constraint specifies that no attribute of primary key must
contain a null value in any relation.
This is because the presence of null value in the primary key violates the
uniqueness property.
Example-
This relation does not satisfy the entity integrity constraint as here the primary key
contains a NULL value.
Referential Integrity Constraint
This constraint is enforced when a foreign key references the primary key of a relation.
It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.
Important Results-
•The following two important results emerges out due to referential integrity
constraint-
•We can not insert a record into a referencing relation if the corresponding record
does not exist in the referenced relation.
•We can not delete or update a record of the referenced relation if the
corresponding record exists in the referencing relation.
•We can not insert a record into a referencing relation if the corresponding record
does not exist in the referenced relation.
•We can not delete or update a record of the referenced relation if the
corresponding record exists in the referencing relation.
Example-
The relation ‘Student’ does not satisfy the referential integrity constraint.
This is because in relation ‘Department’, no value of primary key specifies
department no. 14.
Cascade –
Here if a record in the parent table(referencing relation) is
deleted/Updated, then the corresponding records in the
child table(referenced relation) will automatically be
deleted/Updated.
table level.
If CUSTOMERS table has already been created, then to add a UNIQUE
constraint to AGE column, you would write a statement similar to the
following:
ALTER table customer add UNIQUE ( AGE);
A primary key is a field in a table which uniquely identifies
each row/record in a database table. Primary keys must
contain unique values. A primary key column cannot have
NULL values.
A table can have only one primary key, which may consist of
single or multiple fields. When multiple fields are used as a
primary key, they are called a composite key.
If a table has a primary key defined on any field(s), then you
can not have two records having the same value of that
field(s).
A foreign key is a key used to link two tables
together. This is sometimes called a referencing
key.
Foreign Key is a column or a combination of
columns whose values match a Primary Key in a
different table.
The relationship between 2 tables matches the
Primary Key in one of the tables with a Foreign Key
in the second table.
The CHECK Constraint enables a condition to check
the value being entered into a record. If the
condition evaluates to false, the record violates the
constraint and isn't entered into the table.
DEFAULT Constraint
The DEFAULT constraint is used to set a default value for
a column.
Super Key
Candidate Key
Primary Key
Alternate Key
Composite Key
Foreign Key
Super Key-
•A super key is a set of attributes that can identify each tuple
uniquely in the given relation.
•A super key is not restricted to have any specific number of
attributes.
•Thus, a super key may consist of any number of attributes
NOTE-
All the attributes in a super key are definitely sufficient to identify each
tuple uniquely in the given relation but all of them may not be necessary.
Candidate Key-
NOTE-