Chapter 3 - The Relational Database Model

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

Chapter 3 – The Relational Database Model

1
Engin Calisir University of Texas at Dallas
Learning Objectives
• After completing this chapter, you will be able to:
• Describe the relational database model’s logical structure
• Identify the relational model’s basic components and explain the structure,
contents, and characteristics of a relational table
• Use relational database operators to manipulate relational table contents
• Explain the purpose and components of the data dictionary and system
catalog
• Identify appropriate entities and then the relationships among the entities in
the relational database model
• Describe how data redundancy is handled in the relational database model
• Explain the purpose of indexing in a relational database

2
A Logical View of Data
• Relational database model enables logical representation of the data and its relationships
• Logical simplicity yields simple and effective database design methodologies
• The logical view is facilitated by the creation of data relationships based on a logical construct called a
relation
• No worries about hardware requirements at this stage

Example of 1:M
Example of 1:1

fills teaches

3
Tables and Their Characteristics
1 A table is perceived as a two-dimensional structure
composed of rows and columns.

2 Each table row (tuple) represents a single entity


occurrence within the entity set.

3 Each table column represents an attribute, and


each column has a distinct name.

4 Each table must have an attribute or combination


of attributes that uniquely identifies each row.

5 All values in a column must conform to the same


data format.
6 Each column has a specific range of values known
as the attribute domain.
7 The order of the rows and columns is immaterial to
the DBMS.

8 Each intersection of a row and column represents a


single data value.

4
Types of Keys (1/2)
• Keys consist of one or more attributes that determine other attributes
• Ensure that each row in a table is uniquely identifiable
• Establish relationships among tables and to ensure the integrity of the data
• Superkey: key that can uniquely identify any row in the table ( it can be one or more
attributes)
• Primary key (PK) is a super key
• ID or ID+SensorID ID SensorID Time Value
1 3 10 14.3
2 2 16 124
3 1 40 3
• Candidate key: minimal super key 4 3 70 14.4
• ID 5 1 100 8
• How about “Time” ? 6 2 116 186
• How about PK ?

5
Types of Keys (2/2)
• Simple Key :LastName or FirstName or ID
• Composite key: key that is composed of more than one attribute
• LastName+FirstName+StudentID or ID+SensorID etc..
• Compound key : more than one simple key’s combination
• StudentID+StudentEmail or StudentID+CourseID ;
• Difference between composite and component keys ;
• if CourceID is combination of more then one attribution then StudentID+CourseID became a
CompositeID
• Some DB vendors use both word is interchangeable
• Foreign key: primary key of one table that has been placed into another table to create a common
attribute
• Secondary key: key used strictly for data retrieval purposes
• Secondary key is another candidate key that is not selected as a PK.

6
Dependencies
• Determination
• State in which knowing the value of one attribute makes it possible to determine the value of
another
• Establishes the role of a key
• Based on the relationships among the attributes
• Functional dependence: value of one or more attributes determines the value of one or more
other attributes
• Determinant: attribute whose value determines another
• Dependent: attribute whose value is determined by the other attribute
• Full functional dependence: entire collection of attributes in the determinant is necessary for the
relationship <perhaps for OODB? >

7
Database Integrity
• Entity integrity: condition in which each row in the table has its own unique
identity
• All of the values in the primary key must be unique
• No key attribute in the primary key can contain a null

• Referential integrity: every reference to an entity instance by another entity


instance is valid

8
Integrity Rules
• Ways to handle nulls
• Flags
• Special codes used to indicate the absence of some value
• Constraints
• NOT NULL constraint: placed on a column to ensure that every row in the table has a value for
that column
• UNIQUE constraint: restriction placed on a column to ensure that no duplicate values exist for
that column

9
What is null ?
• Null: absence of any data value
• Unknown attribute value, known but missing attribute value, or inapplicable condition
• Ways to handle nulls
• Flags
• Special codes used to indicate the absence of some value
• Constraints
• NOT NULL constraint: placed on a column to ensure that every row in the table has a
value for that column
• UNIQUE constraint: restriction placed on a column to ensure that no duplicate values
exist for that column

10
Relational Algebra
• Theoretical way of manipulating table contents using relational operators
• Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output.
• It uses operators to perform queries.
• An operator can be either unary or binary.
• They accept relations as their input and yield relations as their output

11
Relational Set Operators (1 of 13)
• Select (Restrict) Single Table (Unary Operations)
• Project

• Union Multiple Table Operations (in Math)


• Intersect
• Difference
• Product
• Division

Joins Multiple Table Operations


• Natural Join with selection options
• Equijoin
• Theta Join (non Equijoin)
• Inner join
• Left Outer Join
• Right Outer Join
• Full Join
• Cross Join

12
Relational Set Operators (2 of 13)

13
Relational Set Operators (3 of 13)
• SELECT (Restrict)
• Unary operator that yields a horizontal subset of a table
• Only one table is a “input”
• Output is always subset of input table
• SELECT ( Condition) FROM Table;
Select ;
ID SensorID Time Value Select Time < 100 ID SensorID Time Value

1 3 10 14.3
Horizontal
1 3 10 14.3
2 2 16 124 • No limit
2 2 16 124
3 1 40 3
3 1 40 3
Select ID = 1 4 3 70 14.4
4 3 70 14.4
5 1 100 8 ID SensorID Time Value
6 2 116 186 1 3 10 14.3

14
Relational Set Operators (4 of 13)
Unary Operators Example
PROJECT

• Unary operator that yields a vertical subset of a table


• Only one table is a “input”
• Output is always subset of input table
SELECT (Column) from Table;

Project ; SensorID
3
ID SensorID Time Value Project SensorID 2
1 3 10 14.3 1
2 2 16 124 3
3 1 40 3 1 • Vertical
2 •
4 3 70 14.4 Time Value No limit
5 1 100 8 10 14.3
6 2 116 186 16 124
Project Time and Value 40 3
70 14.4
100 8
116 186

15
Relational Set Operators (5 of 13)
• UNION
• Combines all rows from two tables, excluding duplicate rows
• Union-compatible: tables share the same number of columns, and their corresponding
columns share compatible domains
• No conditions
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

16
Relational Set Operators (6 of 13)
• Full Outer Join
• Specifies a row from either left or right table
• Union-compatible: tables share the same number of columns, and their
corresponding columns share compatible domains
SELECT * FROM table_A
FULL OUTER JOIN table_B
ON table_A.A=table_B.A;

17
What we learned so far

 Select (Restrict) Single Table (Unary Operations)


 Project

 Union Multiple Table Operations (in Math)


• Intersect
• Difference
• Product
• Division

Joins Multiple Table Operations


• Natural Join with selection options
• Equijoin
• Theta Join (non Equijoin)
• Inner join
• Left Outer Join
• Right Outer Join
 Full (Other)Join
• Cross Join

18
Relational Set Operators (7 of 13)
• Equijoin
• The SQL EQUI JOIN is a simple SQL join uses the equal sign(=) as the comparison operator for
the condition.
• It has two types - SQL Outer join and SQL Inner join

SELECT * FROM Employee


FULL OUTER JOIN Departments ON Employee.EmpID = Departments.EmpID

• Theta ( non-Equijoin)
• The SQL NON EQUI JOIN is a join uses comparison operator other than the equal sign like >, <,
>=, <= with the condition

SELECT * FROM Employee


FULL OUTER JOIN Departments ON Employee.EmpID < 200
19
Relational Set Operators (8 of 13)
• Intersect
• Yields only the rows that appear in both tables A∩B
• Tables must be union-compatible to yield valid results

Inner join Left Join Right Join

20
Relational Set Operators (9 of 13)

21
Relational Set Operators (10 of 13)
• Natural Join
• It is similar like equijoin based on table level and all columns names and type must be same in both
tables .Repeated columns and rows are avoided.
• SQL use mostly inner join form with SELECT statement based on (ON predicate) columns -Eliminate
duplicated rows not columns

SELECT * FROM Table1 NATURAL JOIN Table2;


SELECT tb1.ssn FROM Table1, tb1 INNER JOIN Table2 ,tb2
ON tb1.ssn=tb2.ssn;
3 Steps process
PRODUCT >SELECT >PROJECT

22
Relational Set Operators (11 of 13)
• PRODUCT
• Yields all possible pairs of rows from two tables
• Cartesian or Cross Joins ( advanced class topic)
• DIVIDE
• Yields all rows in one table that are not found in the other table
• Tables must be union-compatible to yield valid results .
• SQL does not use DIVIDE relational operator in real life instead SQL use
either sub-query or “where …except “ type logical implication.
• Example :
SELECT * FROM suppliers as s
WHERE NOT EXISTS (( SELECT p.pid FROM parts as p )
EXCEPT
(SELECT sp.pid FROM supplies sp WHERE sp.sid = s.sid ) );

23
Relational Set Operators (12 of 13)
• DIFFERENCE
• It is Minus operator and Oracle use “MINUS” and SQL use “EXCEPT” command not “-”
• Yields all rows in one table that are not found in the second table.

SELECT column1 , column2 , ... columnN


FROM table_name
WHERE condition
MINUS (or EXCEPT)
SELECT column1 , column2 , ... columnN
FROM table_name
WHERE condition;
• Left outer Join and Difference are different do not confuse !!

24
Relational Set Operators (13 of 13)

25
What we learned so far
 Select (Restrict) Single Table (Unary Operations)
 Project

 Union Multiple Table Operations (in Math)


 Intersect
 Difference
 Product
 Division

Joins Multiple Table Operations


 Natural Join with selection options
 Equijoin
 Theta Join (non Equijoin)
 Inner join
 Left Outer Join
 Right Outer Join
 Full (Other)Join
• Cross Join

26
Confused ?

27
Data Dictionary /System Catalog
• Description of all tables, views etc., in the database created by the user and
designer ,it is vital for DB.

28
Microsoft System Catalog
• https://
docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/qu
erying-the-sql-server-system-catalog-faq?view=sql-server-2017

• Use generally ;
• Documentation
• Communicate with other users/programs about a common meaning of elements
• Troubleshooting

• Homonyms and synonyms must be avoided to lessen confusion


• Homonym: same name is used to label different attributes
• Synonym: different names are used to describe the same attribute

29
Relationships within the Relational Database (1 of 4)
• One-to-many (1:M)
• Most common norm for relational databases
• One-to-one (1:1)
• One entity can be related to only one other entity and vice versa , this kind of relationship is very rare
• Many-to-many (M:N)
• Implemented by creating a new entity in 1:M relationships with the original entities , most complex and centralized
tables have this kind of relationship
• Composite entity required (i.e., bridge or associative entity): helps avoid problems inherent to M:N relationships ,
• Logical 3rd table need to solve relationship with 1:M relationship with original entites
• Includes the primary keys of tables to be linked
• Self Referencing (AKA Recursive) Relationship
• This is used when a table needs to have relationship with itself
• Sponsorship , costumer referral ,assembly parts in part table etc..

• One-to-None
• Single tables with no relationship with anyone.
• Log tables, some security tables etc..

30
Relationships within the Relational Database (2 of 4)
One-to-many (1:M) ;Most common norm for relational databases

31
Relationships within the Relational Database (3 of 4)
One-to-one (1:1)
One entity can be related to only one other entity and vice versa , this kind of relationship is very
rare

32
Relationships within the Relational Database (4 of 4)
Many-to-many (M:N)

Always you need 3rd relationship !!


33
Indexes
• Orderly arrangement to logically access rows in a table
• Index key: index’s reference point that leads to data location identified by the
key
• Unique index: index key can have only one pointer value associated with it
• Each index is associated with only one table
• The index key can have multiple attributes
• They are not fee ! Specially they are very costly in Cloud environment
• Increase performance and table space
• Indexing is key element in RDBMS

34
Codd’s Relational Database Rules (1 of 2)
Rule Rule Name Description
1 Information All information in a relational database must be logically represented as column values in rows within
tables.
2 Guaranteed access Every value in a table is guaranteed to be accessible through a combination of table name, primary key
value, and column name.
3 Systematic treatment of nulls Nulls must be represented/supported and treated in a systematic way, independent of data type.
4 Dynamic online catalog based The metadata must be stored and managed as ordinary data—that is, in tables within the database; such
on the relational model data must be available to authorized users using the standard database relational language.
Data Dictionary/catalog must exist.
5 Comprehensive data The relational database may support many languages; however, it must
sublanguage support one well-defined, declarative language as well as data definition,
view definition, data manipulation (interactive and by program), integrity
constraints, authorization, and transaction management (begin, commit,
and rollback).it is SQL
6 View updating Any view that is theoretically updatable must be updatable through the
system. Logical view tables must be update according to system.
7 High-level insert, update, and The database must support set-level inserts, updates, and deletes.
delete

35
Codd’s Relational Database Rules (2 of 2)
Rule Rule Name Description

8 Physical data independence Application programs and ad hoc facilities are logically unaffected when physical access methods
or storage structures are changed.
9 Logical data independence Application programs and ad hoc facilities are logically unaffected when changes are made to the
table structures that preserve the original table values (changing order of columns or inserting
columns).

10 Integrity independence All relational integrity constraints must be definable in the relational language and stored in the
system catalog, not at the application level.
11 Distribution independence The end users and application programs are unaware of and unaffected by the data location
(distributed vs. local databases).
12 Nonsubversion If the system supports low-level access to the data, users must not be allowed to bypass the
integrity rules of the database. SQL is hig-level database query language.

13 Rule zero /Foundation Rule relational database management system must be able to use the relational model functionalities
to organize, store, retrieve and manipulate the data.

1.FoxPro database system follows a minimum of 7 Codd’s rules.


2.Microsoft SQL Server follows around 11 Codd’s rules.
3.Oracle database system follows 11 Codd’s rules as well.
36

You might also like