Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Chapter 2: 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.

19/12/2023 Priyanka Singh Unit -2 1


Relational Data Model Concepts

HISTORY-
• First introduced by Ted Codd(in 1970).
• Uses the concept of mathematical relation (set theory).
• First commercial implementations of relational model-
Oracle DBMS, SQL/DS system(IBM)
• Current popular RDMSs – SQL Server & Access (Microsoft) DB2 & Infomix (IBM).
• Standard for commercial RDBMS – SQL Query.

19/12/2023 Priyanka Singh Unit -2 2


Terminologies
•Relational data represents data as a collection of tables.
•A table is also known as relation.
•Each row- tuple.
•Each column – attribute. Attribute

Relation Name
STUDENT
Roll. Name Age Phone City
No
Tuples
1 David 24 8654625868 Noida
2 John 25 9465356546 Delhi

19/12/2023 Priyanka Singh Unit -2 3


Terminologies
• Domain-
A set of atomic values allowed for an attribute.
Eg-a) Name- string of character that represents name of person.
b) Employee_age – possible ages of employees in a company.(values
between 20-70 yrs)
• Relation Schema-
• Describes a relation.
• Made up of a relation name R and a list of attributes A1,A2,A3,……….An.
• Degree (or arity) of a relation.
• Number of attributes in a relation schema.
STUDENT(Name, RollNo, Age, Address, Phone, Grade)
STUDENT(Name: string, RollNo: integer, Age: integer, Address:
string, Phone: string, Grade: real)

19/12/2023 Priyanka Singh Unit -2 4


Terminologies
• Cardinality-
• Total number of tuples present in a relation.

STUDENT ROLL_NO NAME AGE

1 HARRY 19

2 BEN 22
Cardinality = 3
3 KATHY 21

19/12/2023 Priyanka Singh Unit -2 5


Terminologies

Relational database schema-


Is a set of relation schemas and a set of integrity constraints.
Relation state( or relation instance)-
Set of tuples at a given time.

19/12/2023 Priyanka Singh Unit -2 6


Integrity Constraints
• Integrity Constraints are used to ensure accuracy and consistency of the data in
the relational database.
• Integrity Constraints are set of rules that the database is not permitted to violate.
• Constraints may apply to each attribute or they may apply to relationships
between tables.
• Integrity Constraints ensure that changes(update, deletion, insertion) made to
the database by authorized users do not result in a loss of data consistency. Thus,
Integrity Constraints guard against damage to the database.
• Example- a blood group must be ‘A’ OR ‘AB’ OR ‘O’ only (can not any other values
else).

19/12/2023 Priyanka Singh Unit -2 7


Types of Integrity Constraints

19/12/2023 Priyanka Singh Unit -2 8


Domain constraints
• Domain constraints defines or valid set of values for an attribute.
• The data type of domain includes string, character, integer, time, date,
currency, etc. The value of the attribute must be available in the
corresponding domain.

19/12/2023 Priyanka Singh Unit -2 9


Entity integrity constraints
• The entity integrity constraint states that primary key value can't be null.
• This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify
those rows.
• A table can contain a null value other than the primary key field.
• Example-

19/12/2023 Priyanka Singh Unit -2 10


Referential Integrity Constraints
• A referential integrity constraint is specified between two tables.
• In the Referential integrity constraints, if a foreign key in Table 1 refers to the
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be
null or be available in Table 2.
• The rules are-
• You can`t delete a record from a primary table if matching recorded exist in a
related table.
• You can`t change a primary key value in a primary table if that record has
related records.
• You can`t insert a value in a foreign key field of the related table that doesn`t
exist in the primary key of the primary table.
• However, You can enter a Null value in the foreign key, specifying that the
records are unrelated.

19/12/2023 Priyanka Singh Unit -2 11


Referential Integrity Constraints(cont…)

19/12/2023 Priyanka Singh Unit -2 12


Key constraints
• Keys are the entity set that is used to identify an entity within its entity set
uniquely.
• An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.

19/12/2023 Priyanka Singh Unit -2 13


Relational Algebra
• Relational algebra is a procedural query language.
• It collects instances of relations as input and gives occurrences of relations as
output.
• It uses various operations to perform this action.
• Relational algebra operations are performed recursively on a relation.
• The output of these operations is a new relation, which might be performed from
one or more input relations.

19/12/2023 Priyanka Singh Unit -2 14


Types of Relational operation

19/12/2023 Priyanka Singh Unit -2 15


Types of Relational operation(cont…)

• Selection(σ)
• Projection(∏)
• Union(∪)
• Set Difference(-)
• Rename
• Cartesian Product(X)

19/12/2023 Faculty Name Unit -2 16


Types of Relational operation(cont…)
STUDENT
EMPLOYEE
ROLL NAME AGE EMPLOYEE_N
NAME AGE
1 Aman 20 O
2 Atul 18 E-1 Anant 20
3 Baljeet 19 E-2 Ashish 23
4 Harsh 20 E-3 Baljeet 25
5 Prateek 21
E-4 Harsh 20
6 Prateek 23
E-5 Pranav 22

19/12/2023 Faculty Name Unit -2 17


Types of Relational operation(cont…)

• Select (σ)
• Select operation is done by Selection Operator which is represented
by "sigma"(σ).
• It is used to retrieve tuples(rows) from the table where the given condition is
satisfied.
• is a unary operator means it requires only one operand.
Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula

19/12/2023 Faculty Name Unit -2 18


Types of Relational operation(cont…)
• Let's understand this with an example:
Suppose we want the row(s) from STUDENT Relation where
"AGE" is 20
σ AGE=20 (STUDENT)

• This will return the following output:

ROLL NAME AGE


1 Aman 20
4 Harsh 20

19/12/2023 Faculty Name Unit -2 19


Types of Relational operation(cont…)

• Project (∏)
• Project operation is done by Projection Operator which is represented by "pi"(∏).
• It is used to retrieve certain attributes(columns) from the table.
• It is also known as vertical partitioning as it separates the table vertically.
• It is also a unary operator.
• Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list

19/12/2023 Faculty Name Unit -2 20


Types of Relational operation(cont…)

• Let's understand this with an example:


Suppose we want the names of all students from STUDENT Relation.
∏ NAME(STUDENT)

This will return the following output:

NAME
Aman
Atul
Baljeet
Harsh
Prateek

19/12/2023 Faculty Name Unit -2 21


Types of Relational operation(cont…)

• As you can see from the above output it eliminates duplicates.


For multiple attributes, we can separate them using a ",".

∏ ROLL,NAME(STUDENT)

Above code will return two columns, ROLL and NAME.


ROLL NAME
1 Aman
2 Atul
3 Baljeet
4 Harsh
5 Prateek
6 Prateek
19/12/2023 Faculty Name Unit -2 22
Types of Relational operation(cont…)

• Union (∪)
• Union operation is done by Union Operator which is represented by "union"(∪).
• It is the same as the union operator from set theory, i.e., it selects all tuples from
both relations but with the exception that for the union of two relations/tables
both relations must have the same set of Attributes.
• It is a binary operator as it requires two operands.
Notation: R ∪ S
• Where R is the first relation
S is the second relation

19/12/2023 Faculty Name Unit -2 23


Types of Relational operation(cont…)
• If relations don't have the same set of attributes, then the union of such relations will result
in NULL.
• Let's have an example to clarify the concept:
Suppose we want all the names from STUDENT and EMPLOYEE relation.

∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE)
NAME
Aman
Anant
Ashish
Atul
Baljeet
Harsh
Pranav
Prateek
As we can see from the above output it also eliminates duplicates.
19/12/2023 Faculty Name Unit -2 24
Types of Relational operation(cont…)
• Set Difference (-)
• Set Difference as its name indicates is the difference
between two relations (R-S).
• It is denoted by a "Hyphen"(-) and it returns all the
tuples(rows) which are in relation R but not in
relation S. It is also a binary operator.
• Notation : R – S
• Where R is the first relation
• S is the second relation
• Just like union, the set difference also comes with the
exception of the same set of attributes in both
relations.
Types of Relational operation(cont…)

• Let's take an example where we would like to know the names of students who are
in STUDENT Relation but not in EMPLOYEE Relation.
∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)

This will give us the following output:

NAME
Aman
Atul
Prateek

19/12/2023 Faculty Name Unit -2 26


Types of Relational operation(cont…)

• Cartesian product (X)


• Cartesian product is denoted by the "X" symbol. Let's say we have two relations R
and S.
• Cartesian product will combine every tuple(row) from R with all the tuples from
S.
• I know it sounds complicated, but once we look at an example, you'll see what I
mean.
Notation: R X S
• Where R is the first relation
• S is the second relation
• As we can see from the notation it is also a binary operator.
19/12/2023 Faculty Name Unit -2 27
Types of Relational operation(cont…)
• Let's combine the two relations STUDENT and EMPLOYEE.
STUDENT X EMPLOYEE

EMPLOYEE
ROLL NAME AGE NAME AGE
_NO
1 Aman 20 E-1 Anant 20
1 Aman 20 E-2 Ashish 23
1 Aman 20 E-3 Baljeet 25
1 Aman 20 E-4 Harsh 20
1 Aman 20 E-5 Pranav 22
2 Atul 18 E-1 Anant 20
2 Atul 18 E-2 Ashish 23
2 Atul 18 E-3 Baljeet 25
2 Atul 18 E-4 Harsh 20
2 Atul 18 E-5 Pranav 22

. . . And so on.
19/12/2023 Faculty Name Unit -2 28
Types of Relational operation(cont…)

• Rename (ρ)
• Rename operation is denoted by "Rho"(ρ).
• As its name suggests it is used to rename the output relation.
• Rename operator too is a binary operator.
Notation: ρ(R,S)
• Where R is the new relation name
• S is the old relation name
• Let's have an example to clarify this

19/12/2023 Faculty Name Unit -2 29


Types of Relational operation(cont…)
• Suppose we are fetching the names of students from STUDENT relation. We
would like to rename this relation as STUDENT_NAME.

ρ(STUDENT_NAME,∏ NAME(STUDENT))

STUDENT_NAME

NAME
Aman
Atul
Baljeet
Harsh
Prateek

As you can see, this output relation is named "STUDENT_NAME".

19/12/2023 Faculty Name Unit -2 30


Revision

• Select (σ) is used to retrieve tuples(rows) based on certain conditions.


• Project (∏) is used to retrieve attributes(columns) from the relation.
• Union (∪) is used to retrieve all the tuples from two relations.
• Set Difference (-) is used to retrieve the tuples which are present in R but not in
S(R-S).
• Cartesian product (X) is used to combine each tuple from the first relation with
each tuple from the second relation.
• Rename (ρ) is used to rename the output relation.

19/12/2023 Faculty Name Unit -2 31


Types of Relational operation(cont…)

• Derived Operations
• Also known as extended operations, these operations can be derived from basic
operations and hence named Derived Operations.
• These include three operations:
• Join Operations,
• Intersection operations, and
• Division operations.
• Let's study them one by one.

19/12/2023 Faculty Name Unit -2 32

You might also like