Professional Documents
Culture Documents
Notes of Dbms Unit-2
Notes of Dbms Unit-2
Notes of Dbms Unit-2
1) Concepts of Relation:-
PRIMARY KEY: - The primary key value is used to identify individual tuples in
a relation. Having NULL values for the primary key implies that we cannot identify
some tuples. For example, if two or more tuples had NULL for their primary keys,
we may not be able to distinguish them if we try to reference them from other
relations.
FOREIGN KEY: - The conditions for a foreign key, given below, specify a
referential integrity constraint between the two relation schemas R1 and R2. A set
of attributes FK in relation schema R1 is a foreign key of R1 that references relation
R2 if it satisfies the following rules:
1. The attributes in FK have the same domain(s) as the primary key attributes
PK of R2; the attributes FK are said to reference or refer to the relation R2.
2. A value of FK in a tuple t1 of the current state r1(R1) either occurs as a value
of PK for some tuple t2 in the current state r2(R2) or is NULL. In the former
case, we have t1[FK] = t2[PK], and we say that the tuple t1 references or refers
to the tuple t2.
In this definition, R1 is called the referencing relation and R2 is the referenced
Relation. If these two conditions hold, a referential integrity constraint from R1
to R2 is said to hold. In a database of many relations, there are usually many
referential integrity constraints.
In fig. below the EMPLOYEE relation, the attribute Dno refers to the
department for which an employee works; hence, we designate Dno to be a foreign
key of EMPLOYEE referencing the DEPARTMENT relation. This means that a
value of Dno in any tuple t1 of the EMPLOYEE relation must match a value of the
primary key of DEPARTMENT—the Dnumber attribute—in some tuple t2 of the
DEPARTMENT relation, or the value of Dno can be NULL if the employee does
not belong to a department or will be assigned to a department later.
Relational Calculus:
Relational calculus is a non-procedural query language that tells the system what
data to be retrieved but doesn’t tell how to retrieve it. Relational Calculus exists in
two forms:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)
Question: - Query to display the last name of those students where age is greater
than 30
Last_Name
---------
Singh
Question: - Query to display all the details of students where Last name is ‘Singh’
Output:
First_Name Last_Name Age
---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
Ex:
Table-1: Customer
Table-3: Account
Account Branch name Balance
number
1111 ABC 50000
1112 DEF 10000
1113 GHI 9000
1114 ABC 7000
Table-4: Loan
Loan number Branch name Amount
L33 ABC 10000
L35 DEF 15000
L49 GHI 9000
L98 DEF 65000
Table-5: Borrower
Customer name Loan number
Saurabh L33
Mehak L49
Ria L98
Table-6: Depositor
Customer name Account number
Saurabh 1111
Mehak 1113
Sumiti 1114
Queries-1: Find the loan number, branch, amount of loans of greater than or equal
to 10000 amount.
{t| t ∈ loan ∧ t[amount]>=10000}
Resulting Branch name Amount
relation: Loan
number
L33 ABC 10000
L35 DEF 15000
L98 DEF 65000
Domain Relational Calculus (DRC)
In domain relational calculus the records are filtered based on the domains. Again
we take the same table to understand how DRC works.
Table: Student
First_Name Last_Name Age
---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Question: Query to find the first name and age of students where student age is
Greater than 27
Output:
First_Name Age
---------- ----
Ajeet 30
Chaitanya 31
Carl 28
Solution: -
TABLES:-
i) Retrieve average salary of all female employees.
ii) Retrieve the names & addresses of all employees who work in
“Research Department”.
ename, Add
John 731 Fondren, Houston, TX
Franklin 638 Voss, Houston, TX
Ramesh 975 Fire Oak, Humble, TX
Joyce 5631 Rice, Houston, TX
iii) For each project, List the project name and total hours per week
Spent on that project.
Soln: - Proj_hours (p.no,total_hours) = p.no Fsum hours (Works_on)
Result= P.name, total_hours (Proj_hours X p no = pnumber (Project)
iv) Find all employee in dept. No. 4 who work for more than 12
Hours per week.
Soln: - a) Join first
Emp_all=employee X ssn=essn Works_on X p.no=p number Project
emp_dept_4= d.no=4(employee)
{STUD_NO->STUD_NAME, STUD_NO->STUD_PHONE,
STUD_NO->STUD_STATE, STUD_NO->STUD_COUNTRY,
STUD_NO -> STUD_AGE, STUD_STATE->STUD_COUNTRY}
Attribute Closure: Attribute closure of an attribute set can be defined as
set of attributes which can be functionally determined from it.
How to find attribute closure of an attribute set?
To find attribute closure of an attribute set:
Example:
Id → Name,
Name → DOB
Examples:
AB → BC,
AD → DC