RDBMS
RDBMS
RDBMS
2.Relation Instance:
This is a table specifying the information. An instance of a relation is a set
of ‘tuples’, also called ‘records’, in which each tuple has the same number
of fields as the relation schemas. A relation instance can be thought of as
a table in which each tuple is a row and all rows have the same number of
fields.
sid Name login age Gross
1111 Dave dave@cs 19 1.2
2222 Jones Jones@cs 18 2.3
333 Smith smith@ee 18 3.4
4444 Smith smith@math 19 4
Relation
Degree
The number of fields/columns/attributes is called as ‘degree’. This is also
called as ‘arity’.
Cardinality:
The number of tuples/rows/records in a given relation is called as
cardinality of a relationsid Name login age Gross
Example
In the above example, the degree of the relation is 5 and the cardinality is 4
Relational database and schema
Relational database
It is a collection of relations with distinct relation names.
Relational database schema:
It is the collection of schemas for the relations in the
database.
Instance: An instance of a relational database is a
collection of relation instances, one per relation schema
in the database schema. Each relation instance must
satisfy the domain constraints in its schema.
Integrity constraints over relations
1.When the DBA or end user defines a database schema, he or she specifies
the ICs that must hold on any instance of this database.
2.When a data base application is run, the DBMS checks for violations and
disallows changes to the data that violate the specified ICs. Legal Instance: If
the database instance satisfies all the integrity constraints specified on the
database schema.
Integrity constraints
The constraints can be classified into 4 types as
below.
1. Domain Constraints.
2. Key Constraints.
3. Entity Integrity Constraints.
4. Referential Integrity Constraints.
Domain constraints
Domain constraints are the most elementary
form of integrity constraints.
They are tested easily by the system whenever
a new data item is entered into the database.
Domain constraints specify the set of possible
values that may be associated with an
attribute. Such constraints may also prohibit
the use of null values for particular attributes.
Key constraints
A key constraint is a statement that a certain
minimal subset of the fields of a relation is a
unique identifier for a tuple.
Example: The ‘students’ relation and the
constraint that no 2 students have the same
student id (sid).
Entity Integrity Constraints.
queries can be posed. A very attractive feature of the relational model is that it
Deferred Update
It is a technique for the maintenance of the transaction log files of the DBMS. It is also called NO-UNDO/REDO
technique. It is used for the recovery of the transaction failures which occur due to power, memory or OS
failures. Whenever any transaction is executed, the updates are not made immediately to the database. They
are first recorded on the log file and then those changes are applied once commit is done. This is called “Re-
doing” process. Once the rollback is done none of the changes are applied to the database and the changes in
the log file are also discarded. If commit is done before crashing of the system, then after restarting of the
system the changes that have been recorded in the log file are thus applied to the database.
Immediate Update
It is a technique for the maintenance of the transaction log files of the DBMS. It is also called UNDO/REDO
technique. It is used for the recovery of the transaction failures which occur due to power, memory or OS
failures. Whenever any transaction is executed, the updates are made directly to the database and the log file
is also maintained which contains both old and new values. Once commit is done, all the changes get stored
permanently into the database and records in log file are thus discarded. Once rollback is done the old values
get restored in the database and all the changes made to the database are also discarded. This is called “Un-
doing” process. If commit is done before crashing of the system, then after restarting of the system the
changes are stored permanently in the database.
Enforcing Integrity Constraints
•
• Given below are the examples of candidate keys since
each set consists of minimal attributes required to identify
each student uniquely in the Student table-
• ( class , section , roll )
Candidate key
• All the attributes in a candidate key are sufficient as well
as necessary to identify each tuple uniquely.
• Removing any attribute from the candidate key fails in
identifying each tuple uniquely.
• The value of candidate key must always be unique.
• The value of candidate key can never be NULL.
• It is possible to have multiple candidate keys in a relation.
• Those attributes which appears in some candidate key are
called as prime attributes.
•
Primary key
• It is the minimal of candidate key.
• The value of primary key can never be NULL.
• The value of primary key must always be unique.
• The values of primary key can never be changed
i.e. no updation is possible.
• The value of primary key must be assigned when
inserting a record.
• A relation is allowed to have only one primary key.
Foreign key
• It references the primary key in another
table.
Foreign key
An attribute ‘X’ is called as a foreign key to some other
attribute ‘Y’ when its values are dependent on the values of
attribute ‘Y’.
The attribute ‘X’ can assume only those values which are
assumed by the attribute ‘Y’.
Here, the relation in which attribute ‘Y’ is present is called as
the referenced relation.
The relation in which attribute ‘X’ is present is called as
the referencing relation.
The attribute ‘Y’ might be present in the same table or in
some other table.
Composite Key
budget
did
Departments
Translation to relational model?
Many-to-Many
1-to-1
1-to Many
Many-to-1
Key Constraints name
since
dname
ssn lot did budget
• Consider Works_In:
Employees Manages Departments
An employee can
Key Constraint
work in many
departments; a dept
can have many
employees.
• In contrast, each dept
has at most one
manager, according to
the key constraint on
Manages. 1-to-1 Many-to 1 1-to-Many Many-to-Many
3.Translating ER Diagrams with Key
Constraints
• Translating ER Diagrams with Key Constraints
• CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY
(did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did)
REFERENCES Departments)
• Map relationship to a table
• Note that did is the key now!
• Separate tables for Employees and Departments.
• Since each department has a unique manager, we
could instead combine Manages and Departments.
since
since
name
name
dname
dname
lot
budget
did
budget
did
ssn
Departments
Employees
Manages
Works_In
since
3.Translating ER Diagrams with participation
Constraints
• Participation Constraints in SQL We can capture
participation constraints involving one entity set in a
binary relationship, but little else (without resorting
to CHECK constraints).
name
cost
pname
age
ssn
lot
Dependents
Policy
Employees
Weak Entities
A weak entity can be identified uniquely only by
considering the primary key of another (owner)
entity.
Owner entity set and weak entity set must participate in
a one-to-many relationship set (1 owner, many weak
entity.
name
cost pname age
ssn lot
Primary Key
for weak entity
Employees Policy Dependents
contractid
Contract_Emps
Hourly_Emps Overlap constraints Can Joe be an Hourly_Emps
as well as a Contract_Emps entity?
(Allowed/disallowed)
• Covering constraints Does every Employees
entity also have to be an Hourly_Emps or a
Contract_Emps entity? (Yes/no)
name
ssn lot
Dependents
Covers
Bad design
pname
age
Dependents
Purchaser
Better design
name
ssn lot
Aggregation Employees