Unit2 DrCSYadav
Unit2 DrCSYadav
Content
RCS 501
Data Base Management System
Unit: 2
• Syllabus of Unit 2
• Relational Data Model- Basic Concepts
• Relational Schema
• Relational Instance
• Keys
– Super key
– Candidate Key
– Primary Key
– Alternate Key
– Foreign Key
• Relational Integrity Constraint
– Entity Integrity Constraint
– Referential Integrity Constraint
– Key Constraint
– Domain Constraint
Example: If
customer_name = {Rajeev, Sumit, Amit, Kaml, …}
customer_street = {S1, S2, S3, …}
customer_city = {Delhi, Kanpur, Noida, …}
Then r = {(Rajeev, S1, Delhi), (Sumit, S2, Kanpur),
(Amit, S2, Kanpur), (Kamal, S3 , Noida) }
is a relation over
customer_name x customer_street x customer_city
Dr C S Yadav KCS 501 DBMS Unit 2
09/13/2024 5
Relational Data Model- Basic Concepts
Content (CO1, CO2)
Degree:
The total number of attributes in the relation is
called the degree of the relation.
For example,
Student has four attributes, or degree four.
Cardinality:
Total number of rows(tuples) present in the Table.
Example:
Customer_schema = (customer_name, customer_street,
customer_city)
customer
Degree = 3, Cardinality =4
Super Key
Candidate Key
Primary Key
Alternate Key
Foreign key
KCS 501
Data Base Management System
Unit: 2
Relational Algebra
Dr C S Yadav
Prof & Head CSE
B.Tech. (CSE) 5th Sem
Procedural language
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two relations as inputs and
produce a new relation as a result.
Dr C S Yadav KCS 501 DBMS Unit 2
09/13/2024 27
Select Operation:
Content (CO1, CO2)
Relation r
A B C D
1 7
5 7
12 3
23 10
1 7
23 10
Notation: p(r)
p is called the selection predicate
Defined as:
branch_name=“Noida”(account)
Relation r: A B C
10 1
20 1
30 1
40 2
A,C (r) A C A C
1 1
1 = 1
1 2
2
Notation:
A1 , A2 ,, Ak (r )
• Notation: r s
• Defined as:
r s = {t | t r or t s}
• For r s to be valid.
1. r, s must have the same arity (same number of
attributes)
2. The attribute domains must be compatible
• Example: to find all customers with either an account or a
loan
customer_name (depositor) customer_name (borrower)
• Notation: r s
• Defined as:
r s = {t | t r or t s}
• For r s to be valid.
1. r, s must have the same arity (same number of
attributes)
2. The attribute domains must be compatible
• Example: to find all customers with either an account or a
loan
customer_name (depositor) customer_name (borrower)
• Notation r – s
• Defined as:
r – s = {t | t r and t s}
Relations r, s:
A B C D E
1 10 a
10 a
2 20 b
r 10 b
s
r x s:
A B C D E Defined as:
Notation 1 10 a
1 10 a
rxs 1 20 b r x s = {t q | t r and q s}
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
• Notation: rs A B A B
1 2
• Defined as: 2 3
1
r s
r s = { t | t r and t s }
• Assume:
– r, s have the same arity A B
• Note: r s = r – (r – s)
A=C(r x s)
A B C D E
1 10 a
2 10 a
2 20 b
customer_name(depositor)
Join
Inner Outer
Join Join
Notation: r s
• Let r and s be relations on schemas R and S respectively.
• A natural join is the set of tuples of all combinations in R
and S that are equal on their common attribute names.
• It is denoted by ⋈.
• Example:
R = (A, B, C, D)
S = (E, B, D)
– Result schema = (A, B, C, D, E)
– r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
Notation: r s
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
Dr C S Yadav KCS 501 DBMS Unit 2
09/13/2024 47
Theta (θ) Content
Join (CO1, CO2)
R1 ⋈θ R2
The join condition is denoted by the symbol θ.
Theta Join, Equijoin, and Natural Join are called inner joins
09/13/2024 50
Modification of the Database
Content (CO1, CO2)
DRC: { < en, city > |∃ sal ( < en , 'TCS' , sal>∈ works-
for
∧ ∃ st ( < en , st , city> ∈ employee )) }
Dr C S Yadav KCS 501 DBMS Unit 2
09/13/2024 57
TRC andContent
DRC (CO1, CO2)
TRC:
TRC:
TRC:
TRC:
DRC:
(i) RA: ()
DRC:
(i) RA:
DRC:
(i) RA:
DRC:
(i) RA:
DRC:
DRC:
{uv | ( q) ( r) ( s) ( t) ( w) ( x) ( y) ( z)
(EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and
s=’Smith’)} }
{ t | (tEMPLOYEE) }
This is UNSAFE expression in tuple relational calculus!
{<l, b, a> | }
KCS 501
Data Base Management System
Unit: 2
Introduction to SQL
Dr C S Yadav
Prof & Head CSE
B.Tech. (CSE) 5th Sem
• Introduction to SQL
• Data Definition
• Basic Query Structure
• Set Operations
• Aggregate Functions
• Null Values
• Nested Subqueries
• Complex Queries
• Views
• Modification of the Database
• Joined Relations
• Example:
create table branch
(branch_name char(15) not null,
branch_city char(30),
assets integer)
• not null
• primary key (A1, ..., An )
– Ai represents an attribute
– ri represents a relation
– P is a predicate.
• This query is equivalent to the relational algebra expression.
• Updatable view
• Non-updatable View
or equivalently
update account
set balance = balance 1.05
where balance 10000
– The order is important
– Can be done better using the case statement
Advantages:
Their use in queries usually results in much better performance.
They make it possible to quickly retrieve (fetch) data.
They can be used for sorting
Disadvantages:
There is some overhead to an index.
The index itself occupies space on disk and memory.
Too many indexes could be a problem.
The index needs to be maintained after performing DML
operations
Dr C S Yadav KCS 501 DBMS Unit 2
09/13/2024 112
PL/SQL Introduction
Content (CO3)
SQL PL/SQL
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
BEGIN
SQL statement;
END;
/
END;
/
Output:
I love INDIA
PL/SQL procedure successfully
completed.
SQL> DECLARE
END;
/
PL/SQL code to print sum of two numbers taken from the user.
SQL> DECLARE
END;
/
%FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected
one or more rows or a SELECT INTO statement returned one or more
rows. Otherwise, it returns FALSE.
%NOTFOUND
The logical opposite of %FOUND.
It returns TRUE if an INSERT, UPDATE, or DELETE statement
affected no rows, or a SELECT INTO statement returned no rows.
Otherwise, it returns FALSE.
%ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the
SQL cursor automatically after executing its associated SQL
statement.
%ROWCOUNT
Returns the number of rows affected by an INSERT, UPDATE, or
DELETE statement, or returned by a SELECT INTO statement.
update.
• The trigger is associated with a database table and is
fired when the triggering event takes place on the table.
SQL>
SQL> CREATE
CREATE TRIGGER
TRIGGER increase_salary_trg
increase_salary_trg
22 BEFORE
BEFORE UPDATE
UPDATE OF
OF sal
sal
33 ON
ON emp
emp
44 BEGIN
BEGIN
55 INSERT
INSERT INTO
INTO sal_hist(increased,
sal_hist(increased, changedOn)
changedOn)
66 VALUES
VALUES (‘YES’,
(‘YES’, SYSDATE);
SYSDATE);
77 END;
END;
88 //
3. Given the following data definition, write a query that returns the number
of students whose first name is John. String comparisons should be case
sensitive.
TABLE students
id INTEGER PRIMARY KEY,
firstName VARCHAR(30) NOT NULL,
lastName VARCHAR(30) NOT NULL
4.A table containing the students enrolled in a yearly course has incorrect
data in records with ids between 20 and 100 (inclusive).
TABLE enrollments
id INTEGER NOT NULL PRIMARY KEY
year INTEGER NOT NULL
studentId INTEGER NOT NULL
Write a query that updates the field 'year' of every faulty record to 2015.
3. Differentiate between a weak entity sets and strong entity sets. CO1,
CO2
4. Define with examples – CO1,CO2
Specialization
Generalization
Aggregation
5. We can convert weak entity set to strong entity set by simply adding
attributes. Then why do we need weak entity sets. CO1,CO2
1) Given
Project (projectno, projectname, chief_architect),
employee(empno, empname),
assigned_to(projectno,empno) CO2
Use the above database schema for following questions:
(iv) Find out the name of employees who have involved in at least one
project.
Table : itemmaster
Itemdesc char(20)
Rate number(8,2)
Table: itemtran
Quantity number(2)
09/13/2024 Dr C S Yadav KCS 501 DBMS Unit 2 139
Assignment 2.4 (CO1, CO 2)
Content
(ii) Itemid and ordid must be the primary key in the table itemtran
• https://drive.google.com/drive/u/0/folders/1g
WUEwo7Ztpxs4smy34fl9jWQrx4AKpkC
Thank You
09/13/2024 Dr C S Yadav KCS 501 DBMS Unit 2 143