Logical Design:
Relational model
2
Introduction to Relational database
Files will be replaced with tables
Variables will be replaced with attributes
There will be records in the tables.
Tables will be related to another table(s).
There will be no duplicate records.
Slide
2-3
Relational Database schema
Slide
Relational Database state 1-4
Relational model
A model in which data is maintained in table which is also
known as relation.
Relation: A table with columns and rows.
Informal Formal
Table Relation
Columns / fields Attributes
Rows Tuples (1 record)
# of rows in a table Cardinality of a
relation
# of columns in a Degree of a relation
table
All possible values Domain of the
for a field attribute.
Properties of Relation
1. Relation must have a name. Relation Name must be
unique from all the names in relational schema.
2. Each cell contain exactly one value.
3. In a relation, each Column name must be unique.
4. No duplicate tuples are allowed in a relation.
5. Order does not matter in rows/ columns.
Foreign key
Student Course Instructor
ID Name Crdthr ID Nam Addre ID Name Address
s e ss
Grade
Sid Cid Iid Grade
• It is used to represent relationship between two tables.
• Primary key of one table becomes Foreign key of some other table.
• Foreign key can have duplicate or Null value.
• Foreign key must not have wrong value.
• It can have a same or different name as compared to that in parent table.
• The datatype must be same as that is in parent table.
Database Constraints
Database Constraint
Every relation has some
condition that must hold for it to
be a valid relation. These
conditions are called relational
integrity constraints.
10
1. Domain Constraint
It states that any value that is inserted/updated for an attribute should
belong to the data type that has been defined for the attribute.
Example an attribute with integer data type should not be given a
string as an input.
11
2. Key Constraint
There is a property of relational databases that there should be no
duplicate rows.
For this we have to consider one attribute that will never be same for two
persons/things.
Example : student, Book.
The unique attribute is called primary key.
It is underlined in the schema/state.
Key constraint states that there should be no duplicate value for
the primary key.
12
3. Entity Integrity Constraint
Entity Integrity constraint states that there should not be NULL value
for the primary key.
13
4. Null Constraint
We can design some attributes in a way that they cannot have a NULL
value ever.
Examples ?
This constraint says that an attribute declared as NOT NULL
cannot be given a NULL value.
14
5. Referential Integrity Constraint
This constraint says that the foreign key value should exist in the
parent table or the value can be NULL.
Properties of Foreign key:
An attribute is considered as a foreign key if it is a primary key in the parent
table.
A foreign key must have the same data type as the data type of the primary
key in the parent table.
A foreign key can have a different name from that of primary key in the
parent table.
Database Constraint
1. Key constraint : No duplicate primary key
2. Entity integrity Constraint: No Null primary key
3. Domain constraint : Every value / attribute is bound to have specific range of
value.
4. Null Constraint: Every required field is bound to have a value.
5. Referential integrity Constraint : foreign key value must exist in the primary
key of the parent table.
Identify violations
Identify violations
Database Constraints
For the database state and schema given to you
please specify whether the following operations will
be accepted or rejected. If they are rejected which
constraint is being violated in that case.
• Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’,
‘6357 Windy Lane, Katy, TX’, F, 28000, NULL, 4>
into EMPLOYEE.
• Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’,
‘6357 Windy Lane, Katy, TX’, F, 28000, ‘987654321’, 4>
into EMPLOYEE.
• Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-
05’, ‘6357 Windswept, Katy’, TX’, F, 28000,
Identify violations
•Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-
05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, NULL, 4>
into EMPLOYEE.
•Delete the WORKS_ON tuple with Essn = ‘999887777’
and Pno = 10.
•Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
Identify violations
•Delete the EMPLOYEE tuple with Ssn = ‘333445555’.
•Update the Dno of the EMPLOYEE tuple with Ssn =
‘999887777’ to 1.
•Update the Dno of the EMPLOYEE tuple with Ssn =
‘999887777’ to 7.
•Update the Ssn of the EMPLOYEE tuple with Ssn =
‘999887777’ to ‘987654321’.
Referential integrity constraint
Student (Base/ Reference
table) Sid Name Addr Grade (Referencing table )
ess
Cid Sid Grad
Insert e
Delete
Update
Solution of Foreign key violation
DELETE
On delete / Update Cascade
Automatically delete / update data
On delete / update Set NULL / Default
Automatically set Null.
On delete no action is performed by default
Class Activity:
Identify
violations
Domain Constraint
Key Constraint
Entity Integrity
Constraint
Referential Integrity
Constraint.