0% found this document useful (0 votes)
2 views

Unit-II

The document covers the relational data model, including concepts like integrity constraints, keys, and the structure of relations. It introduces SQL, detailing its characteristics, commands, and operations such as joins and aggregate functions. Additionally, it explains relational algebra and its operations, providing examples and applications in a banking context.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-II

The document covers the relational data model, including concepts like integrity constraints, keys, and the structure of relations. It introduces SQL, detailing its characteristics, commands, and operations such as joins and aggregate functions. Additionally, it explains relational algebra and its operations, providing examples and applications in a banking context.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit-II

Relational data Model and Language: Relational data model concepts, integrity constraints, entity integrity,
referential integrity, Keys constraints, Domain constraints, relational algebra, relational calculus, tuple and domain
calculus.

Introduction on SQL: Characteristics of SQL, advantage of SQL. SQl data type and literals. Types of SQL commands.
SQL operators and their procedure. Tables, views and indexes. Queries and sub queries. Aggregate functions. Insert,
update and delete operations, Joins, Unions, Intersection, Minus, Cursors, Triggers, Procedures in SQL/PL SQL

Relational Data Model Concepts


 Formally, given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn
 Thus, a relation is a set of n-tuples (a1, a2, …, an) where each aiDi

Example: If

 customer_name = {Jones, Smith, Curry, Lindsay, …} /* Set of all customer names */


 customer_street = {Main, North, Park, …} /* set of all street names*/
 customer_city = {Harrison, Rye, Pittsfield, …} /* set of all city names */

Then r = { (Jones, Main, Harrison),

(Smith, North, Rye),

(Curry, North, Rye),

(Lindsay, Park, Pittsfield) }

is a relation over customer_name x customer_street x customer_city

Attribute Types

 Each attribute of a relation has a name


 The set of allowed values for each attribute is called the domain of the attribute
 Attribute values are (normally) required to be atomic; that is, indivisible
o E.g. the value of an attribute can be an account number, but cannot be a set of account numbers
 Domain is said to be atomic if all its members are atomic
 The special value null is a member of every domain
 The null value causes complications in the definition of many operations
 We shall ignore the effect of null values in our main presentation and consider their effect later

Relation Schema

 A1, A2, …, An are attributes


 R = (A1, A2, …, An ) is a relation schema

Example:

Customer_schema = (customer_name, customer_street, customer_city)

 r(R) denotes a relation r on the relation schema R

Example:

customer (Customer_schema)

Relation Instance

 The current values (relation instance) of a relation are specified by a table

1|Page PRAMOD KUMAR (ASST.PROF.)


 An element t of r is a tuple, represented by a row in a table

Relations are Unordered

 Order of tuples is irrelevant (tuples may be stored in an arbitrary order)

Example: account relation with unordered tuples

Database
 A database consists of multiple relations
 Information about an enterprise is broken up into parts, with each relation storing one part of the
information

account : stores information about accounts

depositor : stores information about which customer owns which account

customer : stores information about customers

 Storing all information as a single relation such as

bank(account_number, balance, customer_name, ..)

results in

o repetition of information

2|Page PRAMOD KUMAR (ASST.PROF.)


 e.g.,if two customers own an account

o the need for null values


 e.g., to represent a customer without an account

The customer Relation

The depositor Relation

Keys
Superkey

 Let K Í R
 K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R)
o by “possible r ” we mean a relation r that could exist in the enterprise we are modeling.

3|Page PRAMOD KUMAR (ASST.PROF.)


o Example: {customer_name, customer_street} and {customer_name} are both superkeys of Customer,
if no two customers can possibly have the same name
 In real life, an attribute such as customer_id would be used instead of customer_name to
uniquely identify customers, but we omit it to keep our examples small, and instead assume
customer names are unique.

Candidate Key

 K is a candidate key if K is minimal


Example: {customer_name} is a candidate key for Customer, since it is a superkey and no subset of it is a
superkey.

Primary Key

 a candidate key chosen as the principal means of identifying tuples within a relation
o Should choose an attribute whose value never, or very rarely, changes.
o E.g. email address is unique, but may change

Foreign Keys

 A relation schema may have an attribute that corresponds to the primary key of another relation. The
attribute is called a foreign key.
o E.g. customer_name and account_number attributes of depositor are foreign keys to customer and
account respectively.
o Only values occurring in the primary key attribute of the referenced relation may occur in the
foreign key attribute of the referencing relation.

Query Languages

 Language in which user requests information from the database.


 Categories of languages
o Procedural
o Nonprocedural, or declarative
 “Pure” languages:
o Relational algebra
o Tuple relational calculus
o Domain relational calculus
 Pure languages form underlying basis of query languages that people use.

Relational Algebra
The relational algebra defines a set of operations on relations, paralleling the usual algebraic operations such
as addition, subtraction or multiplication, which operate on numbers.
4|Page PRAMOD KUMAR (ASST.PROF.)
 Just as algebraic operations on numbers take one or more numbers as input and return a number as output,
the relational algebra operations typically take one or two relations as input and return a relation as output.
 It is procedural language
 Six basic operators
o select: (Return rows of the input relation that satisfy the predicate.)
o project: π (Output specified attributes from all rows of the input relation. Remove duplicate tuples
from the output.)
o union: U ( Output the union of tuples from the two input relations.)
o set difference: –
o Cartesian product: x (Output all pairs of rows from the two input relations (regardless of whether or
not they have the same values on common attributes)
o rename: 
 The operators take one or two relations as inputs and produce a new relation as a result.

Select Operation – Example


Relation r

A=B ^ D > 5 (r)

Project Operation – Example

Relation r:

5|Page PRAMOD KUMAR (ASST.PROF.)


Union Operation – Example
Relations r, s:

 For r U s to be valid.
o r, s must have the same arity (same number of attributes)

6|Page PRAMOD KUMAR (ASST.PROF.)


o The attribute domains must be compatible (example: 2nd column of r deals with the same type of
values as does the 2nd column of s)

 Set differences must be taken between compatible relations.


o r and s must have the same arity (same number of attributes)
o attribute domains of r and s must be compatible

Cartesian Product Operation – Example

7|Page PRAMOD KUMAR (ASST.PROF.)


Banking Example

branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)

1. Find all loans of over $1200


2. Find the loan number for each loan of an amount greater than $1200
3. Find the names of all customers who have a loan, an account, or both, from the bank
4. Find the names of all customers who have a loan at the Perryridge branch.
5. Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any
branch of the bank.

Set Intersection Operation – Example

Natural Join Operation


 Notation: r s
 Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R È S obtained as follows:
o Consider each pair of tuples tr from r and ts from s.
 If tr and ts have the same value on each of the attributes in R Ç S, add a tuple t to the result, where
o t has the same value as tr on r
o t has the same value as ts on s
Example:
R = (A, B, C, D)
S = (E, B, D)
 Result schema = (A, B, C, D, E)
 r s is defined as:

8|Page PRAMOD KUMAR (ASST.PROF.)


Division Operation

 Notation: r ÷s
 Suited to queries that include the phrase “for all”.
 Let r and s be relations on schemas R and S respectively
where
 R = (A1, …, Am , B1, …, Bn )
 S = (B1, …, Bn)
The result of r ¸ s is a relation on schema
R – S = (A1, …, Am)

Where tu means the concatenation of tuples t and u to produce a single tuple

9|Page PRAMOD KUMAR (ASST.PROF.)

You might also like