Relation:-A Relation /table Is Set of Records in The Form of Two
Relation:-A Relation /table Is Set of Records in The Form of Two
•To find just names and logins, replace the first line:
DELETE
FROM Students S
WHERE S.name = ‘Smith’
Integrity Constraints (ICs) over relations
• An integrity constraint is a condition that is specified on a data
base schema and resticts the data that can be stored in an
instance of the database.ICs help to prevent incorrect information
into the database.
• IC: condition that must be true for any instance of the database;
– ICs are specified when schema is defined.
– ICs are checked when relations are modified.
Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Reggae203 B 53666 Jones jones@cs 18 3.4
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8
General constraints-primary and foreign key constraints
are fundamental part of the relational model.but some times we need
to specify more general constraints
Update employee E
Set E.eid=12345263
Where E.eid=12345267;
It violates the rules and rejected if employee
with 12345263 already exists.
Enforcing Referential Integrity
since
name dname
ssn lot did budget
• In translating a relationship
set to a relation, attributes
CREATE TABLE Works_In(
of the relation must include:
ssn CHAR(11),
– Keys for each
did number(2),
participating entity set
since DATE,
(as foreign keys).
PRIMARY KEY (ssn, did),
• This set of attributes
FOREIGN KEY (ssn)
forms a superkey for
REFERENCES Employees,
the relation.
FOREIGN KEY (did)
– All descriptive attributes.
REFERENCES Departments)
Review of Key Constraints
Translation to
relational model?
since
name dname
ssn lot did budget
Works_In
since
Participation Constraints in SQL
name
cost pname age
ssn lot
name
As in C++, or other ssn lot
PLs, attributes are
inherited. Employees
If we declare A ISA B,
hourly_wages hours_worked
every A entity is also ISA
contractid
considered to be a B
entity. Contract_Emps
Hourly_Emps
• General approach:
– 3 relations: Employees, Hourly_Emps and Contract_Emps.
• Hourly_Emps: Every employee is recorded in
Employees. For hourly emps, extra info recorded in
Hourly_Emps (hourly_wages, hours_worked, ssn); must
delete Hourly_Emps tuple if referenced Employees tuple
is deleted).
• Queries involving all employees easy, those involving
just Hourly_Emps require a join to get some attributes.
• Alternative: Just Hourly_Emps and Contract_Emps.
– Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked.
– Each employee must be in one of these two subclasses.
Review: Binary vs. Ternary Relationships
name
ssn lot pname age
• What are the
additional Employees Covers
constraints in the
Dependents
2nd diagram? Bad design Policies
policyid cost
Purchaser
Beneficiary
policyid cost
Binary vs. Ternary Relationships
CREATE TABLE Policies (
• The key
constraints allow policyid number(10),
us to combine cost number(9,2),
Purchaser with ssn CHAR(11) NOT NULL,
Policies and PRIMARY KEY (policyid).
Beneficiary with FOREIGN KEY (ssn) REFERENCES Employees,
Dependents. ON DELETE CASCADE)
• Deletion of an CREATE TABLE Dependents (
employee leads to pname CHAR(20),
deletion of all
age number(2),
policies owned by
employee policyid number(10),
PRIMARY KEY (pname, policyid).
• And all
dependents who FOREIGN KEY (policyid) REFERENCES Policies,
are beneficiaries ON DELETE CASCADE)
• Of those policies
Views
sname rating
yuppy 9
rusty 10
S1 S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 31 lubber 8 55.5
58 rusty 10 35.0
S1 S2
S1 S2
Cross-Product(or cartesian product)
• R X S returns a relation instance whose schema
contains all fields of R followed by all fields of S and
each tuple of R is paired with each tuple of S.
1.Natural join(⋈ )
2.Outer join
i) left outer join
ii) right outer join
iii) full outer join
3.Theta join(conditional join).
Employee_works
Emp name Branch salary
coyote Mesa 1500
Rabbit Mesa 1300
Gates Redmond 5300
Williams Redmomd 1500
The natural join of these two relations employee ⋈
employee-works is
Notice that we lost the street and city info about smith, since
smith tuple is not there in emp-works.and also lost the branch
name and salary info about gates since gates tuple is absent in the
employe relation
To over come the drawbacks of natural join, we use outer join
operation
Left outer join-it takes all tuples in left relation that did not match
with any tuples in the right relation ,adds the tuples with null values for
all other columns from right relation and adds them to the result.
the following is added to the result of natural join
R c S c ( R S)
• Condition Join:
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96
S1 R1
S1. sid R1. sid
S1 R1
sid
• Result schema similar to cross-product, but only one
copy of fields for which equality is specified.
Division√