SlideShare a Scribd company logo
Relational Databases
The Relational
Model
Originally prepared by Jennifer Widom
The Relational Model
 The Relational Model is more than 35 years old, and
it's really the foundation of database management
systems
Used by all major commercial database systems
 Very simple model
 Query with high-level languages: simple yet expressive
 Efficient implementations
Lecture-2 - Relational Model.pptx
4
Alternative Terminology for
Relational Model
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptx
7
Examples of Attribute Domains
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptx
10
Instances of Branch and Staff
Relations
11
Mathematical Definition of Relation
• Consider two sets, D1 & D2, where D1 = {2, 4} and
D2 = {1, 3, 5}.
• Cartesian product, D1  D2, is set of all ordered
pairs, where first element is member of D1 and
second element is member of D2.
D1  D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
• Alternative way is to find all combinations of
elements with first from D1 and second from D2.
12
Mathematical Definition of Relation
• Consider three sets D1, D2, D3 with Cartesian
Product D1  D2  D3; e.g.
D1 = {1, 3} D2 = {2, 4} D3 = {5, 6}
D1  D2  D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6),
(3,2,5), (3,2,6), (3,4,5), (3,4,6)}
• Any subset of these ordered triples is a relation.
13
Database Relations
• Relation schema
– Named relation defined by a set of attribute
and domain name pairs.
• Relational database schema
– Set of relation schemas, each with a distinct
name.
14
Properties of Relations
• Relation name is distinct from all other relation
names in relational schema.
• Each cell of relation contains exactly one atomic
(single) value.
• Each attribute has a distinct name.
• Values of an attribute are all from the same domain.
15
Properties of Relations
• Each tuple is distinct; there are no
duplicate tuples.
• Order of attributes has no significance.
• Order of tuples has no significance,
theoretically.
Schema = structural description of relations in database
Instance = actual contents at given point in time
The Relational Model
ID Name GPA Photo Name City Enrollments
Student University
Schema = structural description of relations in database
Instance = actual contents at given point in time
The Relational Model
ID Name GPA Photo
1 Ahmed 3.7
2 Ali 3.4
3 Usman 3.1
Name City Enrollments
Comsats LHR 7000
FAST ISB 6000
NUST ISB 7000
Comsats ISB 8000
Student University
Database = set of named relations (or tables)
 Students, University
Each relation has a set of named attributes (or columns)
 (ID, Name, GPA, Photo) in Student relation
Each tuple (or row) has a value for each attribute
 (1, Ahmed, 3.7, ) is the first tuple in Student relation
Each attribute has a type (or domain)
The Relational Model
ID Name GPA Photo
1 Ahmed 3.7
2 Ali 3.4
3 Usman 3.1
Name City Enrollments
Comsats LHR 7000
FAST ISB 6000
NUST ISB 7000
Comsats ISB 8000
Student University
19
NULL
• Null
– Represents value for an attribute that is currently
unknown or not applicable for tuple.
– Deals with incomplete or exceptional data.
– Represents the absence of a value and is not the same
as zero or spaces, which are values.
Schema = structural description of relations in database
Instance = actual contents at given point in time
Database = set of named relations (or tables)
Each relation has a set of named attributes (or columns)
Each tuple (or row) has a value for each attribute
Each attribute has a type (or domain)
The Relational Model
Schema – structural description of relations in database
Instance – actual contents at given point in time
NULL – special value for “unknown” or “undefined”
Students with GPA greater than 3.5
Answer: Ahmed
Students with GPA less than or equal to 3.5
Answer: Ali
Students with GPA greater than 3.5 OR less than or equal to 3.5
Answer: Ahmed, Ali
ID Name GPA Photo
1 Ahmed 3.7
2 Ali 3.4 NULL
3 Usman NULL
Name City Enrollments
Comsats LHR 7000
FAST ISB 6000
NUST ISB 7000
Comsats ISB 8000
Student University
Keys
Lecture-2 - Relational Model.pptx
23
Relational Keys
• Superkey
– An attribute, or set of attributes, that uniquely identifies a
tuple within a relation.
• Candidate Key
– Superkey (K) such that no proper subset is a super key
within the relation.
– In each tuple of R, values of K uniquely identify that tuple
(uniqueness).
– No proper subset of K has the uniqueness property
(irreducibility).
24
Relational Keys
• Primary Key
– Candidate key selected to identify tuples uniquely within relation.
• Alternate Keys
– Candidate keys that are not selected to be primary key.
• Foreign Key
– Attribute, or set of attributes, within one relation that matches candidate
key of some (possibly same) relation.
Schema = structural description of relations in database
Instance = actual contents at given point in time
Database = set of named relations (or tables)
Each relation has a set of named attributes (or columns)
Each tuple (or row) has a value for each attribute
Each attribute has a type (or domain)
The Relational Model
Schema – structural description of relations in database
Instance – actual contents at given point in time
NULL – special value for “unknown” or “undefined”
Key – attribute whose value is unique in each tuple
Or set of attributes whose combined values are unique
ID Name GPA Photo
1 Ahmed 3.7
2 Ali 3.4 NULL
3 Usman NULL
Name City Enrollments
Comsats LHR 7000
FAST ISB 6000
NUST ISB 7000
Comsats ISB 8000
Student University
Lecture-2 - Relational Model.pptx
27
Instances of Branch and Staff
Relations
SQL
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL became a standard of the American National
Standards Institute (ANSI) in 1986.
Syntax for creating a table
• Create table <table_name>
• (
• Column1 name data type,
• Column1 name data type,
• Column1 name data type
• );
To display schema of relation
• Create table <table_name>
• (
• Column1 name data type,
• Column2 name data type,
• Column3 name data type
• );
• desc table_name ;
Example
• Create table emp
• (
• Id int,
• Name varchar2(10),
• Salary number(12)
• );
• desc emp;
The Relational Model
Creating relations (tables) in SQL
Create Table Student(ID integer, Name character (30), GPA float,
Photo character(100))
Create Table University(Name character(50), City char(3),
Enrollments integer)
Relational Databases
Querying Relational
Databases
Querying Relational Databases
Steps in creating and using a (relational) database
1. Design schema; create using DDL
2. “Bulk load” initial data
3. Repeat: execute queries and modifications
Examples: simple university admissions database
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
uName city enr sID sName GPA HS sID uName major dec
University Student Apply
Querying Relational Databases
Querying Relational Databases
Ad-hoc queries in high-level language
– All students with GPA > 3.7 applying to Comsats and NUST only
– All engineering departments in ISB with < 500 applicants
– University with highest average accept rate over last 5 years
 Some easy to pose; some a bit harder
 Some easy for DBMS to execute efficiently; some harder
 “Query language” also used to modify data
Querying Relational Databases
Queries return relations (“compositional”, “closed”)
 Closed: When you get back the same type of object that
you query upon, that's known as closure of the language.
 Composition: The ability to run a query over the result of
our previous query.
To display all the data from relation
• SELECT command
– SELECT * FROM table_name;
Querying Relational Databases
Query Languages
 Relational Algebra
∏ID (σGPA>3.7 ^ uName = ‘Comsats’ (Student ⋈ Apply) )
 SQL
IDs of students with GPA > 3.7 applying to Comsats
Select Student.ID
From Student, Apply
Where Student.ID=Apply.ID
And GPA>3.7 and uName=‘Comsats’
What is View
• A virtual relation that does not actually exist in the
database but is produced upon request, at time of request.
• Contents of a view are defined as a query on one or more
base relations.
• Views are dynamic, meaning that changes made to base
relations that affect view attributes are immediately
reflected in the view.
Lecture-2 - Relational Model.pptx
42
Purpose of Views
• Provides powerful and flexible security
mechanism by hiding parts of database from
certain users.
• Permits users to access data in a customized way,
so that same data can be seen by different users in
different ways, at same time.
• Can simplify complex operations on base relations.
43
Updating Views
• There are restrictions on types of modifications
that can be made through views:
– Updates are allowed if query involves a single base relation and
contains a candidate key of base relation.
– Updates are not allowed involving multiple base relations.
– Updates are not allowed involving aggregation or grouping
operations.
44
Updating Views
• All updates to a base relation should be
immediately reflected in all views that reference
that base relation.
• If view is updated, underlying base relation
should reflect change.
Lecture-2 - Relational Model.pptx

More Related Content

PDF
Lecture 02 ❘ Relational Data Model in Data Science.pdf
bleakstreak
 
PPT
Dbms relational model
Chirag vasava
 
PPTX
DBMS: Week 04 - Relational Model in a Database
RashidFaridChishti
 
PPT
Mca ii-dbms- u-iii-sql concepts
Rai University
 
PPTX
L4_Relational DatabasesDatabase Systems and Programming
ottrinari33
 
PPTX
L4_Relational Databases Database Systems and Programming
ottrinari33
 
PPTX
Relational model
Sabana Maharjan
 
PDF
Unit-2.pdf
fikadumola
 
Lecture 02 ❘ Relational Data Model in Data Science.pdf
bleakstreak
 
Dbms relational model
Chirag vasava
 
DBMS: Week 04 - Relational Model in a Database
RashidFaridChishti
 
Mca ii-dbms- u-iii-sql concepts
Rai University
 
L4_Relational DatabasesDatabase Systems and Programming
ottrinari33
 
L4_Relational Databases Database Systems and Programming
ottrinari33
 
Relational model
Sabana Maharjan
 
Unit-2.pdf
fikadumola
 

Similar to Lecture-2 - Relational Model.pptx (20)

PDF
19IS305_U2_LP4_LM4-22-23.pdf
GOWTHAMR721887
 
PPTX
Relational Model in DBMS detail explanation
keerthanaparath1
 
PDF
Relational Database Model Database Management system
soalteepaudel
 
PPT
Bsc cs ii-dbms- u-iii-data modeling using e.r. model (entity relationship model)
Rai University
 
PPTX
Relation Model Database Pertemuan k.pptx
titisarwindartii
 
PPT
Intro to relational model
ATS SBGI MIRAJ
 
PDF
3_Relational_Model.pdf
SrikanthS494888
 
PPTX
Relational database
SanthiNivas
 
PPTX
Quick Revision on DATA BASE MANAGEMENT SYSTEMS concepts.pptx
fouziasulthanak
 
PDF
3 relational model
Utkarsh De
 
PDF
RDBMS Model
Sarmad Ali
 
DOCX
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
Raj vardhan
 
PPTX
Cs 371-lecture-02
Iqra Tariq
 
PPTX
chapter2 Relational Model
MrsGAmudhaIT
 
PPT
Dbms Lec Uog 02
smelltulip
 
PDF
Database_Concepts_Final.pptx.pdf for class 12
pramodcoder1319
 
PPTX
The Relational Model
Bhandari Nawaraj
 
PPTX
Relational Model
Er. Nawaraj Bhandari
 
PPT
Database Technology Teaching Material For Learn
hermawatyrahma21
 
PPT
603s129
nsuthar2015
 
19IS305_U2_LP4_LM4-22-23.pdf
GOWTHAMR721887
 
Relational Model in DBMS detail explanation
keerthanaparath1
 
Relational Database Model Database Management system
soalteepaudel
 
Bsc cs ii-dbms- u-iii-data modeling using e.r. model (entity relationship model)
Rai University
 
Relation Model Database Pertemuan k.pptx
titisarwindartii
 
Intro to relational model
ATS SBGI MIRAJ
 
3_Relational_Model.pdf
SrikanthS494888
 
Relational database
SanthiNivas
 
Quick Revision on DATA BASE MANAGEMENT SYSTEMS concepts.pptx
fouziasulthanak
 
3 relational model
Utkarsh De
 
RDBMS Model
Sarmad Ali
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
Raj vardhan
 
Cs 371-lecture-02
Iqra Tariq
 
chapter2 Relational Model
MrsGAmudhaIT
 
Dbms Lec Uog 02
smelltulip
 
Database_Concepts_Final.pptx.pdf for class 12
pramodcoder1319
 
The Relational Model
Bhandari Nawaraj
 
Relational Model
Er. Nawaraj Bhandari
 
Database Technology Teaching Material For Learn
hermawatyrahma21
 
603s129
nsuthar2015
 
Ad

More from HanzlaNaveed1 (8)

PPTX
Lecture 25 Link Layer - Error detection and Multiple Access.pptx
HanzlaNaveed1
 
PPTX
Lecture 23 DHCP and NAT.pptx
HanzlaNaveed1
 
PPTX
Lecture 22 What inside the Router.pptx
HanzlaNaveed1
 
PPTX
Lecture-3 Relational Algebra I.pptx
HanzlaNaveed1
 
PPTX
Lecture 06 and 07.pptx
HanzlaNaveed1
 
PPTX
Lecture 26 Link Layer .pptx
HanzlaNaveed1
 
PPTX
Lecture 19 and 20 IP Addressing.pptx
HanzlaNaveed1
 
PPT
Lecture 05 OSI Model and IP Protocol Suite.ppt
HanzlaNaveed1
 
Lecture 25 Link Layer - Error detection and Multiple Access.pptx
HanzlaNaveed1
 
Lecture 23 DHCP and NAT.pptx
HanzlaNaveed1
 
Lecture 22 What inside the Router.pptx
HanzlaNaveed1
 
Lecture-3 Relational Algebra I.pptx
HanzlaNaveed1
 
Lecture 06 and 07.pptx
HanzlaNaveed1
 
Lecture 26 Link Layer .pptx
HanzlaNaveed1
 
Lecture 19 and 20 IP Addressing.pptx
HanzlaNaveed1
 
Lecture 05 OSI Model and IP Protocol Suite.ppt
HanzlaNaveed1
 
Ad

Recently uploaded (20)

PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Presentation about variables and constant.pptx
safalsingh810
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Exploring AI Agents in Process Industries
amoreira6
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 

Lecture-2 - Relational Model.pptx

  • 2. The Relational Model  The Relational Model is more than 35 years old, and it's really the foundation of database management systems Used by all major commercial database systems  Very simple model  Query with high-level languages: simple yet expressive  Efficient implementations
  • 10. 10 Instances of Branch and Staff Relations
  • 11. 11 Mathematical Definition of Relation • Consider two sets, D1 & D2, where D1 = {2, 4} and D2 = {1, 3, 5}. • Cartesian product, D1  D2, is set of all ordered pairs, where first element is member of D1 and second element is member of D2. D1  D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)} • Alternative way is to find all combinations of elements with first from D1 and second from D2.
  • 12. 12 Mathematical Definition of Relation • Consider three sets D1, D2, D3 with Cartesian Product D1  D2  D3; e.g. D1 = {1, 3} D2 = {2, 4} D3 = {5, 6} D1  D2  D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6), (3,2,5), (3,2,6), (3,4,5), (3,4,6)} • Any subset of these ordered triples is a relation.
  • 13. 13 Database Relations • Relation schema – Named relation defined by a set of attribute and domain name pairs. • Relational database schema – Set of relation schemas, each with a distinct name.
  • 14. 14 Properties of Relations • Relation name is distinct from all other relation names in relational schema. • Each cell of relation contains exactly one atomic (single) value. • Each attribute has a distinct name. • Values of an attribute are all from the same domain.
  • 15. 15 Properties of Relations • Each tuple is distinct; there are no duplicate tuples. • Order of attributes has no significance. • Order of tuples has no significance, theoretically.
  • 16. Schema = structural description of relations in database Instance = actual contents at given point in time The Relational Model ID Name GPA Photo Name City Enrollments Student University
  • 17. Schema = structural description of relations in database Instance = actual contents at given point in time The Relational Model ID Name GPA Photo 1 Ahmed 3.7 2 Ali 3.4 3 Usman 3.1 Name City Enrollments Comsats LHR 7000 FAST ISB 6000 NUST ISB 7000 Comsats ISB 8000 Student University
  • 18. Database = set of named relations (or tables)  Students, University Each relation has a set of named attributes (or columns)  (ID, Name, GPA, Photo) in Student relation Each tuple (or row) has a value for each attribute  (1, Ahmed, 3.7, ) is the first tuple in Student relation Each attribute has a type (or domain) The Relational Model ID Name GPA Photo 1 Ahmed 3.7 2 Ali 3.4 3 Usman 3.1 Name City Enrollments Comsats LHR 7000 FAST ISB 6000 NUST ISB 7000 Comsats ISB 8000 Student University
  • 19. 19 NULL • Null – Represents value for an attribute that is currently unknown or not applicable for tuple. – Deals with incomplete or exceptional data. – Represents the absence of a value and is not the same as zero or spaces, which are values.
  • 20. Schema = structural description of relations in database Instance = actual contents at given point in time Database = set of named relations (or tables) Each relation has a set of named attributes (or columns) Each tuple (or row) has a value for each attribute Each attribute has a type (or domain) The Relational Model Schema – structural description of relations in database Instance – actual contents at given point in time NULL – special value for “unknown” or “undefined” Students with GPA greater than 3.5 Answer: Ahmed Students with GPA less than or equal to 3.5 Answer: Ali Students with GPA greater than 3.5 OR less than or equal to 3.5 Answer: Ahmed, Ali ID Name GPA Photo 1 Ahmed 3.7 2 Ali 3.4 NULL 3 Usman NULL Name City Enrollments Comsats LHR 7000 FAST ISB 6000 NUST ISB 7000 Comsats ISB 8000 Student University
  • 21. Keys
  • 23. 23 Relational Keys • Superkey – An attribute, or set of attributes, that uniquely identifies a tuple within a relation. • Candidate Key – Superkey (K) such that no proper subset is a super key within the relation. – In each tuple of R, values of K uniquely identify that tuple (uniqueness). – No proper subset of K has the uniqueness property (irreducibility).
  • 24. 24 Relational Keys • Primary Key – Candidate key selected to identify tuples uniquely within relation. • Alternate Keys – Candidate keys that are not selected to be primary key. • Foreign Key – Attribute, or set of attributes, within one relation that matches candidate key of some (possibly same) relation.
  • 25. Schema = structural description of relations in database Instance = actual contents at given point in time Database = set of named relations (or tables) Each relation has a set of named attributes (or columns) Each tuple (or row) has a value for each attribute Each attribute has a type (or domain) The Relational Model Schema – structural description of relations in database Instance – actual contents at given point in time NULL – special value for “unknown” or “undefined” Key – attribute whose value is unique in each tuple Or set of attributes whose combined values are unique ID Name GPA Photo 1 Ahmed 3.7 2 Ali 3.4 NULL 3 Usman NULL Name City Enrollments Comsats LHR 7000 FAST ISB 6000 NUST ISB 7000 Comsats ISB 8000 Student University
  • 27. 27 Instances of Branch and Staff Relations
  • 28. SQL • SQL stands for Structured Query Language • SQL lets you access and manipulate databases • SQL became a standard of the American National Standards Institute (ANSI) in 1986.
  • 29. Syntax for creating a table • Create table <table_name> • ( • Column1 name data type, • Column1 name data type, • Column1 name data type • );
  • 30. To display schema of relation • Create table <table_name> • ( • Column1 name data type, • Column2 name data type, • Column3 name data type • ); • desc table_name ;
  • 31. Example • Create table emp • ( • Id int, • Name varchar2(10), • Salary number(12) • ); • desc emp;
  • 32. The Relational Model Creating relations (tables) in SQL Create Table Student(ID integer, Name character (30), GPA float, Photo character(100)) Create Table University(Name character(50), City char(3), Enrollments integer)
  • 34. Querying Relational Databases Steps in creating and using a (relational) database 1. Design schema; create using DDL 2. “Bulk load” initial data 3. Repeat: execute queries and modifications
  • 35. Examples: simple university admissions database University(uName,city,enrollment) Student(sID,sName,GPA,sizeHS) Apply(sID,uName,major,decision) uName city enr sID sName GPA HS sID uName major dec University Student Apply Querying Relational Databases
  • 36. Querying Relational Databases Ad-hoc queries in high-level language – All students with GPA > 3.7 applying to Comsats and NUST only – All engineering departments in ISB with < 500 applicants – University with highest average accept rate over last 5 years  Some easy to pose; some a bit harder  Some easy for DBMS to execute efficiently; some harder  “Query language” also used to modify data
  • 37. Querying Relational Databases Queries return relations (“compositional”, “closed”)  Closed: When you get back the same type of object that you query upon, that's known as closure of the language.  Composition: The ability to run a query over the result of our previous query.
  • 38. To display all the data from relation • SELECT command – SELECT * FROM table_name;
  • 39. Querying Relational Databases Query Languages  Relational Algebra ∏ID (σGPA>3.7 ^ uName = ‘Comsats’ (Student ⋈ Apply) )  SQL IDs of students with GPA > 3.7 applying to Comsats Select Student.ID From Student, Apply Where Student.ID=Apply.ID And GPA>3.7 and uName=‘Comsats’
  • 40. What is View • A virtual relation that does not actually exist in the database but is produced upon request, at time of request. • Contents of a view are defined as a query on one or more base relations. • Views are dynamic, meaning that changes made to base relations that affect view attributes are immediately reflected in the view.
  • 42. 42 Purpose of Views • Provides powerful and flexible security mechanism by hiding parts of database from certain users. • Permits users to access data in a customized way, so that same data can be seen by different users in different ways, at same time. • Can simplify complex operations on base relations.
  • 43. 43 Updating Views • There are restrictions on types of modifications that can be made through views: – Updates are allowed if query involves a single base relation and contains a candidate key of base relation. – Updates are not allowed involving multiple base relations. – Updates are not allowed involving aggregation or grouping operations.
  • 44. 44 Updating Views • All updates to a base relation should be immediately reflected in all views that reference that base relation. • If view is updated, underlying base relation should reflect change.

Editor's Notes

  • #3: The Relational Model is more than 35 years old, and it's really the foundation of database management systems. It's spawned a many billion dollar industry. The relational model underlies all commercial database systems at this point in time. It's actually an extremely simple model and that's one of its benefits. Furthermore, it can be queried. By that it means we can ask questions of databases in the model using High Level Languages. High Level Languages are simple, yet extremely expressive for asking questions over the database. And finally, very importantly there are extremely efficient implementations of the relational model and of the query languages on that model.
  • #17: The schema of a database is the structure of the relation. So the schema includes the name of the relation and the attributes of the relation and the types of those attributes. Where the instance is the actual contents of the table at a given point in time. So, typically you set up a schema in advance, then the instances of the data will change over time.
  • #21: There's also a special value that's in any type of any column and that's a special value known as null, and nulls are actually quite important in relational databases. Null values are used to denote that a particular value is maybe unknown or undefined. Let's say, student named Usman, for whatever reason, doesn't have a GPA. Maybe Usman is home schooled, maybe Usman doesn't want to reveal his GPA. So then the database would contain a null value for Usman. Or, for example, maybe Ali doesn't want to have his photo in the database, so then Ali would have a null value for his photo, again nulls can go anywhere. Now null values are useful but one has to be very careful in a database system when you run queries over relations that have null values. So, let's suppose we're asking a query over our student table of all students whose GPA is greater than 3.5. So when we run that query on our database obviously we'll get Ali out, obviously we won't get Ahmed out, but should we get Usman? The answer is No. We don't know for a fact that Usman's GPA is greater than 3.5, so we'll only get one student out from that query. Now let's suppose we had another query, where we were gonna ask for the GPA less than or equal to 3.5. So, similarly where we would not have Ahmed in result and we would certainly have Ali in the result and similarly would not have Usman in the result because we don't know that his GPA is less than or equal to 3.5. So far so good, but it gets a little weird is when we add an or here in our query, we say I want everyone who's GPA is greater than 3.5 or who's GPA is less than or equal to 3.5. And even though it looks like every tuple should satisfy this condition, that it's always true, that's not the case when we have null values. So, that's why one has to be careful when one uses null values in relational databases.
  • #26: Let's move on to our next concept which is the concept of Key. Key is again another important concept in relational databases. And, a key is an attribute in of a relation where every value for that attribute is unique. So if we look at the student relation, we can feel pretty confident that the ID is going to be a key. In other words, every tuple is going to have a unique for ID. Thinking about the University relation, it's a little less clear. You know what, we're allowed to have sets of attributes that are unique and that makes sense in the University relation. Most likely the combination of the name and city of a university is unique, and that's what we would identify as the key for the University relation. Now, you might wonder why it's even important to have attributes that are identified as keys. There's actually several uses for them. One of them is just to identify specific tuples. So if you want to run a query to get a specific tuple out of the database you would do that by asking for that tuple by its key. And related to that database systems for efficiency tend to build special index structures or store the database in a particular way. So it's very fast to find a tuple based on its key.
  • #33: Okay, just to wrap up, how one creates relations or tables in the SQL language. It's very simple, you just say "create table," give the name of the relation and a list of the attributes. And if you want to give types for the attributes. It's similar except you follow each attribute name with its type. So to wrap up, the relational model has been around a long time. Has started a huge industry. It's used by all database systems. As you've seen it's a very simple model and will shortly see that it can be queried with very nice languages. And, finally, it's been implemented very efficiently. Definitely
  • #35: So, the first step is to design the schema of the database and then create the schema using a data definition language. So as we discussed in previously in a relational database the schema consists of the structure of the relations and the attributes of those relations. So we set those up inside our big disk. Once that's ready, the next step is to load up the database with the initial data. So it's fairly common for the database to be initially loaded from data that comes from an outside source. Maybe the data is just stored in files of some type, and then that data could be loaded into the database. Once the data is loaded, then we have a bunch of tuples in our relation. Now, we're ready for the fun part which is to query and modify the data. And so that happens continuously over time as long as the database is in existence. So let's just say for now that we're going to have human users that are directly querying the database. In reality, that typically happens through say an application or a website. So, a user will come along and we'll ask a question of the database and we will get an answer. He might come along and ask another question Q2 and he'd get another answer back. The same human or maybe a different human might ask to modify the database. So, they might want to insert new data or update some of the data and the database will come back and say, "Okay, I made that change for you." So that's the basic paradigm of querying and updating relational databases.
  • #37: Relational databases support ad hoc queries and high-level languages. By ad hoc, we mean that you can pose queries that you didn't think of in advance. So it's not necessary to write long programs for specific queries. Rather the language can be used to pose a query as you think about what you want to ask. And as mentioned in previously the languages supported by relational systems are high level, meaning you can write in a fairly compact fashion rather complicated queries and you don't have to write the algorithms that get the data out of the database. So, let's look at an example of a few queries. Let's go to our imaginary database of students who are applying to universities. And here's just three examples of the types of things that you might ask of a relational database. You might want to get all students whose GPA is greater than 3.7 who are applying to Comsats and NUST only. You might want to get all engineering departments in ISB with fewer than 500 applicants or you might ask for the university with the highest average accept rate over the last five years. Now these might seem like a fairly complicated queries but all of these can be written in a few lines in say the SQL language or a pretty simple expression in relational algebra. So, some queries are easier to pose than others, that's certainly true. Though the 3 queries you see here are as I said pretty easy to pose. Now some queries are easier for the database system to execute efficiently than others. And interestingly it's not necessarily. These two things aren't necessarily correlated. There are some queries that are easy to post but hard to execute efficiently and some that are vice-versa. Now, just a bit about terminology. Frequently, people talk about the “query language” of the database system. That's usually used sort of synonymously with the DML or Data Manipulation Language which usually includes not only querying but also data modifications.
  • #38: In all relational query languages, when you ask a query over a set of relations, you get a relation as a result. So let's run a query Q say over these three relations shown here and what we'll get back is another relation. When you get back the same type of object that you query, that's known as closure of the language. And it really is a nice feature. For example, when I want to run another query, say Q2, that query could be posed over the answer of my first query and could even combine that answer with some of the existing relations in the database. That's known as compositionality, the ability to run a query over the result of our previous query. Now, let me talk briefly about two query languages. We'll be learning these languages in detail later, but I'm just going to give the basic flavor of the languages here. Relational algebra is a formal language. Well, it's an algebra as you can tell by its name. So it's very theoretically well-grounded. SQL by contrast is what I'll call an actual language or an implemented language. That 's the one you're going to run on an actual deployed database application. But the SQL language does have as its foundation relational algebra. That's how the semantics of the SQL language are defined.
  • #40: Now let me just give you a flavor of these two languages and I'm going to write one query in each of the two languages. Let's start in relational algebra. So we're looking for the ID's of students whose GPA is greater than 3.7 and they've applied to Comsats. In relational algebra, the basic operators language are Greek symbols. Again, we'll learn the details later, but this particular expression will be written by a Pi followed by a Sigma. The Pi says we're going to get the ID, the Sigma says we want students whose GPA is greater than 3.7 and the university that the students have applied to is Comsats. And then that will operate on what's called the natural join of the student relation with the apply relation. Again, we'll learn the details of that in later. Now, here's the same query in SQL. And this is something that you would actually run on a deployed database system, and the SQL query is, in fact, directly equivalent to the relational algebra query.
  • #41: View is the result set of stored queries .