DATABASE ESSENTIALS
Lecture 5
[email protected]
Relational database
• A relational database consists of a collection of tables, each of which
is assigned a unique name.
• A table is made up of columns and rows.
• A row is also referred as a tuple.
• A column is also referred as an attribute.
Relational database…
• A database schema is the logical design of the database.
• A relation schema consists of a list of attributes and their
corresponding domains.
• The schema of a relation rarely change.
• A database instance is a snapshot of the data in the database at a
given instant in time.
Relational database…
• The contents of a relation instance may change with time as the
relation is updated.
• Tuples of distinct relations are related using common attributes in
relation schemas.
• Example:
Consider the following schemas
student(student_id, name, age)
subject(subject_id, name, teacher)
scores(student_id, subject_id, marks)
Relational database…
• Example
KEYS
• In a relation, no two tuples in a relation are allowed to have
exactly the same value for all attributes.
• A key refers to an attribute or set of attributes that is used to
uniquely identify tuples in entity sets.
• The attributes which are used as key are known as key attributes.
• Rest of attributes are known as non-key attributes.
KEYS
• Types of Keys
1. Super key
2. Candidate key
3. Primary key
4. Alternate key
5. Foreign key
KEYS…
Super key
• A super key is one or more than one attributes that can identify
data uniquely.
• Any entity set has more than one super key.
• All combinations of attributes can identify data uniquely.
KEYS…
Super key
• Example
• Super keys include but not limited to:
• Reg. No, ID, Name, Salary, Dept-ID
• ID, Name, Salary
KEYS…
Candidate key
• A candidate key is a minimal super key, that is, a set of attributes
that forms a super key, but none of whose subsets is a super key.
• Example
• Candidate keys include:
• Reg. No, ID
• Reg. No
• ID
KEYS…
Primary key
• A primary key is an attribute which identifies data uniquely.
• One of the candidate keys of a relation is chosen as its primary
key.
• The primary key should be chosen such that its attribute values
are never, or very rarely, changed.
• From the previous relation; either of the following attributes can
be a primary key
• Reg. No
• ID
KEYS…
Alternate key
• An alternate key(s) refers to all the candidate key(s) other than
primary key.
• Example:
• If you use ID as a primary key. Then, Reg. No. is an alternate key
and vice versa.
KEYS…
Foreign key
• Foreign key refers to an attribute in a relation (r1) which is a
primary key in another relation (r2).
• The relation r1 is also called the referencing or child relation of the
foreign key dependency.
• The relation r2 is called the referenced or master relation of the
foreign key.
KEYS…
Foreign key
• Referential integrity constraint or foreign key constraint refers to
limitations on the foreign key.
• There are two referential integrity constraints:
• Insert constraint
Value cannot be inserted in child relation if the value is not lying in master
relation.
• Delete constraint
Value cannot be deleted from the master relation if the value is existing in
the child relation.
KEYS…
Foreign key
• Referential integrity constraint or foreign key constraint
It’s hard to beat a person
who never gives up. –
Babe Ruth