Relational Databases - Amit Bhawnani & Nimesh Shah.
-
Upload
marsha-lamb -
Category
Documents
-
view
219 -
download
3
Transcript of Relational Databases - Amit Bhawnani & Nimesh Shah.
![Page 1: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/1.jpg)
Relational Databases
- Amit Bhawnani & Nimesh Shah
![Page 2: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/2.jpg)
Topics
• Relational Model• SQL • Indexes and execution plans• Normalization
![Page 3: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/3.jpg)
Databases vs Flat Files
• Data independence and efficient access• Reduced application development time• Data integrity and security• Protect data from inconsistency due to
multiple concurrent users• Security and access control• Etc etc
![Page 4: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/4.jpg)
Relational Model
• Dis-satisfaction with other models such as Network, hierarchical etc
• Proposed by Edgar. F. Codd in the early seventies
• Simple and elegant model with a mathematical basis – Set theory and first-order predicate logic.
![Page 5: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/5.jpg)
Relational Model
KEY DEFINITIONS
![Page 6: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/6.jpg)
Relation
• An association between two or more data elements• Resembles a “Table” of records.• Consider the relation EMPLOYEE represented by the
below
emp_id name ssn desig_id salary join_date dept_id1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 7: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/7.jpg)
Tuples of a Relation
• Each row here is a tuple• Relation is a set of tuples
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 8: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/8.jpg)
Cardinality of a RELATION
• Is the number of tuples in a relation at a point in time
1
23
4
Cardinality = 4
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 9: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/9.jpg)
Attributes
• A tuple consists of attribute values
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 10: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/10.jpg)
Arity / Degree
• Is the number of attributes in a relation
Arity / Degree = 6
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 11: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/11.jpg)
Domain
• Defines the space of possible values for an attribute
emp_id name ssn desig_id salary join_date dept_id1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 12: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/12.jpg)
Domain - Example
Emp_id integer Set of all 4 digital numbers
name Varhar(50) Set of alpha characters. Max length 50
ssn Varchar(10) Set of alpha numeric characters. Max length 10
desig_id smallint Set of all designation codes
salary decimal Decimal
join_date date date
dept_id smallint Set of all department ids
![Page 13: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/13.jpg)
Relation Schema
• Consists of name of relation + attributes (along with domain)
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
Employee
![Page 14: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/14.jpg)
DEFINITION SUMMARYFormal Terms Informal Terms
Relation Table
Attribute/Domain Column
Tuple Row
Domain Values in the column
Schema of a Relation Table definition
![Page 15: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/15.jpg)
Super Key
• Uniqueness property: No two tuples share the same value for the key
• Time-independent
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
Super Keys: emp_id, name, ssn emp_id_name,ssn_design_d,salary,join_date,dept_idemp_idssn
![Page 16: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/16.jpg)
Candidate Key
• Uniqueness property: No two tuples shave the same value for the key
• Minimality property: None of the attributes of the key can be discarded from the key without destroying the uniqueness property.
• Time-independent • Can be composite
emp_id name ssn desig_id salary join_date dept_id1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 17: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/17.jpg)
Primary Key
• One of the candidate keys is chosen to uniquely identify tuples in a relation, such a key is called the primary key.
• There can be only one primary key per relation• Primary key may be a compound key• A relation must have a primary key
emp_id name ssn desig_id salary join_date dept_id1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 18: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/18.jpg)
Alternate Key
• Any candidate Key which is not a primary key is an alternate key
• There can be more than one alternate keys for any relation.
emp_id name ssn desig_id salary join_date dept_id
1011 ABC a3232x3444 5 100000 01/01/11 2
![Page 19: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/19.jpg)
Entity Integrity
• Every Relation must have a primary key• No attribute participating in the primary key of
a base relation may accept null values• Guarantees that each entity will have a unique
identity.
![Page 20: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/20.jpg)
Foreign Key
• Some times a set of attributes in a relation may point to certain tuples in another relation
• A foreign key is a set of attributes in one relation whose values are required to match one of the values of the primary key of the same or different relation.
• There can be more than one foreign key in a given relation.
emp_id name ssn desig_id salary join_date dept_id1011 ABC a3232x3444 5 100000 01/01/11 2
dept_id dept_name Dept_location
2 Software Engineering 1B
Employee
Department
![Page 21: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/21.jpg)
Referential Integrity
• Values of the foreign key – Must be either null or – If non-null, must match with the primary key value
of some tuple of the ‘parent’ relation. The reference can be the same relation.
![Page 22: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/22.jpg)
Relational database schema
• A set S of relation schemas that belong to the same database
• S is the name of the whole database schema
![Page 23: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/23.jpg)
Schema Diagram for the COMPANY
![Page 24: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/24.jpg)
One possible database state for the COMPANYrelational database schema
![Page 25: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/25.jpg)
Relational Algebra
• The basic set of operations for the relational model is known as the relational algebra. These operations enable a user to specify basic retrieval requests.
• A set of operators (unary and binary) that take relation instances as arguments and return new relations.
• Laid the foundation for the development of Database standard SQL
• SQL queries are internally translated into RA expressions.
• Provides a framework for query optimization.
![Page 26: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/26.jpg)
Selection• SELECT Operation
SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition.
Example: To select the EMPLOYEE tuples whose salary is greater than 30,000 the following notation is used:
SALARY > 30,000 (EMPLOYEE)
<selection condition>(R)
![Page 27: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/27.jpg)
Select Operation – Example
• Relation r A B C D
1
5
12
23
7
7
3
10
• A=B & D > 5 (r))
• A=B ( D > 5 (r))A B C D
1
23
7
10
The degree of the new relation is the same.The cardinality is different.Commutative operator
![Page 28: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/28.jpg)
Projection• PROJECT Operation
This operation selects certain columns from the table and discards the other columns.
Example: To list each employee’s first name,last name and salary, the following is used:
FNAME, LNAME,SALARY(EMPLOYEE)
The general form of the project operation is <attribute list>(R)
The project operation removes any duplicate tuples.
![Page 29: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/29.jpg)
Project Operation – Example
• Relation r: A B C
10
20
30
40
1
1
1
2
A C
1
1
1
2
=
A C
1
1
2
A,C (r)
Output is another relation with a different schema.Non - Commutative
![Page 30: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/30.jpg)
Union• UNION Operation
The result of this operation, denoted by R S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated.
• For r s to be valid.1. r, s must have the same same number of attributes2. The attribute domains must be compatible
• E.g. to find all customers with either an deposit account or a loan account customer-name (depositor) customer-name (borrower)
![Page 31: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/31.jpg)
Union Operation – Example• Relations r, s:
r s:
A B
1
2
1
A B
2
3
rs
A B
1
2
1
3
![Page 32: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/32.jpg)
Chapter 6-32
Difference (or MINUS) Operation
• The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S.
• The two operands must be "type compatible”.
• Relations r, s:
r - s:
A B
1
2
1
A B
2
3
rs
A B
1
1
![Page 33: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/33.jpg)
Chapter 6-33
CARTESIAN (or cross product) Operation
Relations r, s:
r x s:
A B
1
2
A B
11112222
C D
1019201010102010
E
aabbaabb
C D
10102010
E
aabbr
s
![Page 34: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/34.jpg)
Composition of Operations
• Can build expressions using multiple operations• Example: A=C(r x s)• r x s
• A,D,E (A=C(r x s))
A B
11112222
C D
1019201010102010
E
aabbaabb
A B C D E
122
102020
aab
![Page 35: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/35.jpg)
Banking Examplebranch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
![Page 36: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/36.jpg)
Example Queries• Find all loans of over $1200• Find the loan number for each loan of an amount greater
than $1200• Find the names of all customers who have a loan, an
account, or both, from the bank• Find the names of all customers who have a loan and an
account at bank.• Find the names of all customers who have a loan at the
ABC branch.• Find the names of all customers who have a loan at the
ABC branch but do not have an account at any branch of the bank.
![Page 37: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/37.jpg)
Intersection• INTERSECTION OPERATION
The result of this operation, denoted by R S, is a relation that includes all tuples that are in both R and S. The two operands must be "type compatible"
• Relations r, s:
r s:
A B
1
2
1
A B
2
3
rs
A B
2
![Page 38: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/38.jpg)
Theta Join Operator
• Theta join is used to combine related tuples from two or more relations ( specified by the condition theta ), to form a single tuple.
• The general form of theta join is as follows :R (join condition) S.
• Tuples whose join attributes are null do not appear in the final result.
• Theta join where the only comparison operator used is the equals (=) sign, are called equijoins .
![Page 39: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/39.jpg)
Theta Join Operator – Example• Relations r, s:
A B
12412
C D
aabab
B
13123
D
aaabb
E
r
A B
11112
C D
aaaab
E
sr (r.b = s.b and r.d = s.d) s
![Page 40: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/40.jpg)
Outer Join
• An extension of the join operation that avoids loss of information.
• Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join.
![Page 41: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/41.jpg)
Outer Join – Exampleloan
loan-number amount
L-170L-230L-260
300040001700
borrowercustomer-name loan-number
JonesSmithHayes
L-170L-230L-155
branch-name
DowntownRedwoodPerryridge
![Page 42: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/42.jpg)
Outer Join – ExampleInner Join
loan (loan.loan_number = borrower.loan_number) Borrower
loan (loan.loan_number = borrower.loan_number) borrowerLeft Outer Join
loan-number amount
L-170L-230
30004000
customer-name
JonesSmith
branch-name
DowntownRedwood
loan-number amount
L-170L-230L-260
300040001700
customer-name
JonesSmithnull
branch-name
DowntownRedwoodPerryridge
![Page 43: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/43.jpg)
Outer Join – ExampleRight Outer Join loan borrower
loan-number amount
L-170L-230L-155
30004000null
customer-name
JonesSmithHayes
loan-number amount
L-170L-230L-260L-155
300040001700null
customer-name
JonesSmithnullHayes
loan borrowerFull Outer Join
branch-name
DowntownRedwoodnull
branch-name
DowntownRedwoodPerryridgenull
![Page 44: Relational Databases - Amit Bhawnani & Nimesh Shah.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649cd95503460f949a2d42/html5/thumbnails/44.jpg)
Questions ?