relational d bs3
DESCRIPTION
TRANSCRIPT
CIS3465: Professor Kirs Slide Number: 1
Chapter 2:The Relational Database Model
(With Considerable Modifications)
RDBMS Basics
CIS3465: Professor Kirs Slide Number: 2
RDBMS Basics
We already know that a Relational DataBase Management System (RDBMS) is:
• A DBMS Approach which manages data (logically) as a collection of tables where data, and data relationships, are represented by common values in related tables
• A Table (file) consists of records, which consist of attributes (fields), which relies on the storage of various data types
CIS3465: Professor Kirs Slide Number: 3
Consider some information the University maintains: Name Address SSN
Major Courses Taken Grades Received
Tuition Paid Tuition Owed Grants/Scholarships
HOW is this information stored?
You are an entity with attributes which vary. Within the University, different areas have different interests in you (i.e., the Registrar, the Bursar, etc.). Nonetheless, you are still part of the University as a whole.
HOW does this relate to a database?
RDBMS Basics
CIS3465: Professor Kirs Slide Number: 4
You are an entity Recordwith attributes Fieldswhich vary Fields can contain characters,
numbers, symbols, etc.Within the University, different areas, have different interests in you
Files
(i.e,. The Registrar, Bursar, etc.)
Nonetheless, you are still part of the University Database
RDBMS Basics
Database Components:
CIS3465: Professor Kirs Slide Number: 5
Database Components:
A Database consists of
Files, which contain
Records, which contain
Fields, which may consist of a variety of data types
Jones, Mary 234567890 102 3.87
Hernandez, Juan 123456789 72 2.42
• • •
• • •
• • •
• • •
• • • • •
• • • • •
• • • • •
• • • • •
• •
• •
• •
• •
• • •
• • •
• • •
• • •
• •
• •
• •
• •
• • •
• • •
• • •
• • •
• • •
• • •
• • •
• • •
• •
• •
• •
• •
• • • •
• • • •
• • • •
• • • •
Notice that there should always be a Key (Unique) Field
RDBMS Basics
CIS3465: Professor Kirs Slide Number: 6
RDBMS Basics
RDBMS Restrictions/Conventions: Each Relation MUST have a unique name
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
234567890 Chung, Mei 37 5th St. INFOSYS
345678901 Adams, John 54B Hague Accounting
456789012 Elam, Mary 123-22 E St. Accounting
•••••• •••••• •••••• ••••••
Table Student
Student Owed Department
103456678 1,502.36 Marketing
123456789 COBA219 Finance
456789012 COBA232 Accounting
•••••• •••••• ••••••
Table BalanceSID •••••• Depart
987654321 •••••• Finance
876543210 •••••• INFOSYS
765432109 •••••• Accounting
•••••• •••••• ••••••
Table Student
NOT Allowed
CIS3465: Professor Kirs Slide Number: 7
RDBMS Basics
RDBMS Restrictions /Conventions : Each Relation MUST have a unique name
Table Student
All Columns (Tuples) MUST have Unique names
StudentID Name Address MajorAddress
123456789 Saenz, Lupe 123 Mesa Arlington Finance
234567890 Chung, Mei 37 5th St. INFOSYSNew York
345678901 Adams, John 54B Hague Dallas Accounting
456789012 Elam, Mary 123-22 E St. Ft. Worth INFOSYS
One of the names MUST be changed
* NOTE: The same field names CAN be used in different Relations
CIS3465: Professor Kirs Slide Number: 8
RDBMS Basics
RDBMS Restrictions /Conventions : Each Relation MUST have a unique name
Table Student
All Columns (Tuples) MUST have Unique names
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa 24.34
234567890 Chung, Mei 37 5th St. INFOSYS
345678901 Adams, John 54B Hague Accounting
456789012 Elam, Mary 123-22 E St. INFOSYS
Unless this is stored as Character String “24.34” (and NOT as the real number 24.34), it MUST be changed
All Column Elements MUST be of the same data type
CIS3465: Professor Kirs Slide Number: 9
RDBMS Basics
RDBMS Restrictions /Conventions : Each Relation MUST have a unique name
Table Student
All Columns (Tuples) MUST have Unique names
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
All Column Elements MUST be of the same data type• Notice that this means each record requires the
SAME number of Bytes of Storage
A Character String Requiring 9 Bytes of StorageA Character String Requiring 20 Bytes of StorageA Character String Requiring 25 Bytes of StorageA Character String Requiring 10 Bytes of Storage
A Total 64 Bytes
of Storage
CIS3465: Professor Kirs Slide Number: 10
RDBMS Basics
RDBMS Restrictions /Conventions : Each Relation MUST have a unique name All Columns (Tuples) MUST have Unique names
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
234567890 Chung, Mei 37 5th St. INFOSYS
345678901 Adams, John 54B Hague Accounting
All Column Elements MUST be of the same data type The order of Rows is NOT important
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finace
345678901 Adams, John 54B Hague Accounting
234567890 Chung, Mei 37 5th St. INFOSYS
Is the Same as
CIS3465: Professor Kirs Slide Number: 11
RDBMS Basics
RDBMS Restrictions /Conventions : Each Relation MUST have a unique name All Columns (Tuples) MUST have Unique names
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
234567890 Chung, Mei 37 5th St. INFOSYS
345678901 Adams, John 54B Hague Accounting
All Column Elements MUST be of the same data type The order of Rows is NOT important
Each contains 64 Bytes
The Number of Bytes/Record Must be the same
CIS3465: Professor Kirs Slide Number: 12
RDBMS Basics
RDBMS Keys: Purpose
Define entity relationships Determination
Knowing the value of a key field means you also know (Determine) the values of the other fields
E.g., knowing StudentID means you know StudentName, StudentAddress, etc.
StudentID StudentName, StudentAddress(StudentID Determines StudentName and StudentAddress)
CIS3465: Professor Kirs Slide Number: 13
RDBMS Basics
RDBMS Keys: Purpose
Define entity relationships Determination
An attribute is functionally dependent on another if can be determined by that attribute
Functional Dependence
StudentIDStudentAddress
(StudentID Determines Student Address)
NOTE:
StudentID StudentAddress
(Two Students MAY live at the same address)
CIS3465: Professor Kirs Slide Number: 14
RDBMS Basics
RDBMS Keys: Each Relation MUST have a unique identifier or
PRIMARY KEYPRIMARY KEY
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
234567890 Chung, Mei 37 5th St. INFOSYS
345678901 Adams, John 54B Hague Accounting
456789012 Elam, Mary 123-22 E St. INFOSYS
•••••• •••••• •••••• ••••••
Table Student
No two students can have the same StudentID
CIS3465: Professor Kirs Slide Number: 15
RDBMS Basics
RDBMS Keys: Each Relation MUST have a unique identifier or
PRIMARY KEYPRIMARY KEY A COMPOSITE KEYCOMPOSITE KEY is a combination of keys (Multi-
key attributes) used to produce uniqueness
StudentName,StudentAddress StudentMajor(StudentName AND StudentAddress Determines StudentMajor)
NOTE: If Student Major is functionally dependent upon StudentName AND StudentAddress , BUT not on either StudentName or StudentName it is FULLY FUNCTIONAL DEPENDENT on the Concatenated key
(Attributes are fully functionally dependent on PRIMARY KEYS)
CIS3465: Professor Kirs Slide Number: 16
RDBMS Basics
RDBMS Keys: Each Relation MUST have a unique identifier or
PRIMARY KEYPRIMARY KEY A is COMPOSITE KEYCOMPOSITE KEY is a combination of keys
(Multi-key attributes) used to produce uniqueness A is SUPER KEYSUPER KEY is either a PRIMARY or
COMPOSITE KEY that uniquely identifies an entity
StudentID StudentAddressAND
StudentName,StudentAddress StudentMajor
Are BOTH Superkeys
CIS3465: Professor Kirs Slide Number: 17
RDBMS Basics
RDBMS Keys: Each Relation MUST have a unique identifier or
PRIMARY KEYPRIMARY KEY A is COMPOSITE KEYCOMPOSITE KEY is a combination of keys
(Multi-key attributes) used to produce uniqueness A is SUPER KEYSUPER KEY is either a PRIMARY or
COMPOSITE KEY that uniquely identifies an entity A is CANDIDATE KEYCANDIDATE KEY is any key or group of keys
that could become a SUPER KEY• StudentID is a candidate key• StudentName,StudentAddress is a candidate key• StudentID,StudentAddress is NOT a candidate key
(StudentID by itself is a CANDIDATE KEY)
CIS3465: Professor Kirs Slide Number: 18
RDBMS Basics
RDBMS Keys: Each Relation MUST have a unique identifier or
PRIMARY KEYPRIMARY KEY A is COMPOSITE KEYCOMPOSITE KEY is a combination of keys
(Multi-key attributes) used to produce uniqueness A is SUPER KEYSUPER KEY is either a PRIMARY or
COMPOSITE KEY that uniquely identifies an entity A is CANDIDATE KEYCANDIDATE KEY is any key or group of keys
that could become a SUPER KEY
A is SECONDARY KEYSECONDARY KEY is any field, or combination of fields, which does NOT yield a unique value• Used for retrieval/Narrowing purposes only • StudentName,StudentZip may yield several records
CIS3465: Professor Kirs Slide Number: 19
RDBMS Basics
RDBMS Keys: Each Relation MUST have a unique identifier or
PRIMARY KEYPRIMARY KEY A is COMPOSITE KEYCOMPOSITE KEY is a combination of keys
(Multi-key attributes) used to produce uniqueness A is SUPER KEYSUPER KEY is either a PRIMARY or
COMPOSITE KEY that uniquely identifies an entity A is CANDIDATE KEYCANDIDATE KEY is any key or group of keys
that could become a SUPER KEY
A is SECONDARY KEYSECONDARY KEY is any field, or combination of fields, which does NOT yield a unique value• Used for retrieval/Narrowing purposes only • StudentName,StudentZip may yield several records
CIS3465: Professor Kirs Slide Number: 20
RDBMS Basics
RDBMS Keys: In Order to relate two (or more) tables FOREIGN FOREIGN
KEYS KEYS must be used
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
234567890 Chung, Mei 37 5th St. INFOSYS
345678901 Adams, John 54B Hague Accounting
Table Student
Table DepartmentDeptName Chairman Telephone
INFOSYS Sircar, Sumit 555-1234
Marketing McDaniel, C. 555-2345
Accounting Courtney, H. 555-3456
A FOREIGN KEY in one tableis a PRIMARY/SUPER KEY In another
CIS3465: Professor Kirs Slide Number: 21
RDBMS Basics
Database Integrity: Maintaining wholeness and Unity Entity Integrity:
All Entries MUST be Unique No NULL values in primary key fields
StudentID Name Address Major
123456789 Saenz, Lupe 123 Mesa Finance
234567890 Chung, Mei 37 5th St. INFOSYS
123456789 Adams, John 54B Hague Accounting
456789012 Elam, Mary 123-22 E St. INFOSYS
Bush, G.W. 555 Austin Marketing
Table Student
Illegal Entries
CIS3465: Professor Kirs Slide Number: 22
RDBMS Basics
Database Integrity: Maintaining wholeness and Unity Entity Integrity Relational Integrity:
Foreign Key MUST have a valid entry in the corresponding table (or be NULL)
StudentID Major
123456789 Scamming
234567890 INFOSYS
345678901
Table Student Table Department
DeptName Chairman Telephone
INFOSYS Sircar, Sumit 555-1234
Marketing McDaniel, C. 555-2345
Accounting Courtney, H. 555-3456
???
NOT Allowed
CIS3465: Professor Kirs Slide Number: 23
RDBMS Basics
Database Integrity: Maintaining wholeness and Unity Entity Integrity Relational Integrity:
Foreign Key MUST have a valid entry in the corresponding table (or be NULL)
Table Student Table DepartmentDeptName Chairman Telephone
INFOSYS Sircar, Sumit 555-1234
Marketing McDaniel, C. 555-2345
StudentID Major
123456789 Accounting
234567890 INFOSYS
345678901 Finance
Primary Key entry CAN NOT be deleted if a foreign key refers to it
INFOSYS can NOT be deleted
CIS3465: Professor Kirs Slide Number: 24
RDBMS Basics
Basic RDBMS Operators: SELECT
Choose All or Specific values from a table
SELECT ALLYields
Table Student
StudentID Name Address
123456789 Saenz, Lupe 123 Mesa
234567890 Chung, Mei 37 5th St.
345678901 Adams, John 54B Hague
456789012 Elam, Mary 123-22 E St.
567890123 Bush, G.W 555 Austin
678901234 Clinton, B. 89 Congress
789012345 Bush, J. 555 Austin
Given:
StudentID Name Address
123456789 Saenz, Lupe 123 Mesa
234567890 Chung, Mei 37 5th St.
345678901 Adams, John 54B Hague
456789012 Elam, Mary 123-22 E St.
567890123 Bush, G.W 555 Austin
678901234 Clinton, B. 89 Congress
789012345 Bush, J. 555 Austin
CIS3465: Professor Kirs Slide Number: 25
RDBMS Basics
Basic RDBMS Operators: SELECT
Choose All or Specific values from a table
SELECT WHERE ADDRESS = “555 Austin”Yields
Table Student
StudentID Name Address
123456789 Saenz, Lupe 123 Mesa
234567890 Chung, Mei 37 5th St.
345678901 Adams, John 54B Hague
456789012 Elam, Mary 123-22 E St.
567890123 Bush, G.W 555 Austin
678901234 Clinton, B. 89 Congress
789012345 Bush, J. 555 Austin
Given:
StudentID Name Address
567890123 Bush, G.W 555 Austin
789012345 Bush, J. Austin
CIS3465: Professor Kirs Slide Number: 26
RDBMS Basics
Basic RDBMS Operators: PROJECT
Produce list of all values for a selected attribute
PROJECT STUDENTIDYields
Table Student
StudentID Name Address
123456789 Saenz, Lupe 123 Mesa
234567890 Chung, Mei 37 5th St.
345678901 Adams, John 54B Hague
456789012 Elam, Mary 123-22 E St.
567890123 Bush, G.W 555 Austin
678901234 Clinton, B. 89 Congress
789012345 Bush, J. 555 Austin
Given:
StudentID
123456789
234567890
345678901
456789012
567890123
678901234
789012345
CIS3465: Professor Kirs Slide Number: 27
RDBMS Basics
Basic RDBMS Operators: PROJECT
Produce list of of all values for a selected attribute
PROJECT STUDENTID, NAMEYields
Table Student
StudentID Name Address
123456789 Saenz, Lupe 123 Mesa
234567890 Chung, Mei 37 5th St.
345678901 Adams, John 54B Hague
456789012 Elam, Mary 123-22 E St.
567890123 Bush, G.W 555 Austin
678901234 Clinton, B. 89 Congress
789012345 Bush, J. 555 Austin
Given:
StudentID Name
123456789 Saenz, Lupe
234567890 Chung, Mei
345678901 Adams, John
456789012 Elam, Mary
567890123 Bush, G.W
678901234 Clinton, B.
789012345 Bush, J.
CIS3465: Professor Kirs Slide Number: 28
RDBMS Basics
Basic RDBMS Operators: JOIN
Links 2 different tables together (Producing a different table)
Each Table MUST have attributes that are common to both tables
123CID
124125126127
CNAABBCCBBDD
CA6
20129
18
CRAABBCC
CAdd123 A234 B345 C
TableA TableB
CIS3465: Professor Kirs Slide Number: 29
RDBMS Basics
Basic RDBMS Operators: JOIN
Consider the relationship between a Physician and his/her Patients
123456789
PhysID
Physician Patient
234567890
345678901
Physname
Smith, Mary
Brown, John
Jones, Jerry
PatID Patname PhysID
987654321 Washington, G. 234567890
876543210
765432109
654321098
543210987
Adams, J.Q.
Jefferson, T.
XXX
YYY
ZZZ
AAA
432109876
321098765
345678901
123456789
123456789
234567890
345678901
123456789
CIS3465: Professor Kirs Slide Number: 30
RDBMS Basics
Basic RDBMS Operators: JOIN
The Product of the tables is:Physician Patient
123456789 Smith, Mary
234567890 Brown, John
345678901 Jones, Jerry
PatnamePatID Phys
Washington, G.987654321 234567890
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
PhysID Physname PhysID Physname PatID Patname Phys
Adams, J.Q.876543210 345678901123456789 Smith, Mary
123456789 Smith, Mary Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
123456789 Smith, Mary Jefferson, T.765432109 123456789
123456789 Smith, Mary XXX654321098 123456789
123456789 Smith, Mary YYY543210987 234567890
123456789 Smith, Mary ZZZ432109876 345678901
=*
234567890
234567890
234567890
234567890
234567890
234567890
Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Brown, John
Brown, John
Brown, John
Brown, John
Brown, John
Brown, John
Washington, G.987654321 234567890
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Adams, J.Q.876543210 345678901
345678901
345678901
345678901
345678901
345678901
345678901
Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Washington, G.987654321 234567890
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Adams, J.Q.876543210 345678901
The product of a 3 (row) x 2 (column) Table and a 6 (row) x 3 (column) Table would be a 18 (row) by 5 (column) Table
CIS3465: Professor Kirs Slide Number: 31
RDBMS Basics
Basic RDBMS Operators: JOIN
Now, entering the command: Select Patname … Where PhysID = Phys would yield:
PhysID Physname PatID Patname Phys
Adams, J.Q.876543210 345678901123456789 Smith, Mary
123456789 Smith, Mary Washington, G.987654321 234567890
123456789 Smith, Mary Jefferson, T.765432109 123456789
123456789 Smith, Mary XXX654321098 123456789
123456789 Smith, Mary YYY543210987 234567890
123456789 Smith, Mary ZZZ432109876 345678901
234567890
234567890
234567890
234567890
234567890
234567890
Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Brown, John
Brown, John
Brown, John
Brown, John
Brown, John
Brown, John
345678901
345678901
345678901
345678901
345678901
345678901
Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
PhysID Physname PatID Patname Phys
Adams, J.Q.876543210 345678901123456789 Smith, Mary
Smith, Mary Washington, G.987654321123456789 234567890
123456789 Smith, Mary Jefferson, T.765432109 123456789
123456789 Smith, Mary XXX654321098 123456789
123456789 Smith, Mary YYY543210987 234567890
123456789 Smith, Mary ZZZ432109876 345678901
234567890
234567890
234567890
234567890
234567890
234567890
Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Brown, John
Brown, John
Brown, John
Brown, John
Brown, John
Brown, John
345678901
345678901
345678901
345678901
345678901
345678901
Washington, G.987654321 234567890
Adams, J.Q.876543210 345678901
Jefferson, T.765432109 123456789
XXX654321098 123456789
YYY543210987 234567890
ZZZ432109876 345678901
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
Jones, Jerry
CIS3465: Professor Kirs Slide Number: 32
RDBMS Basics
Basic RDBMS Operators: JOIN
123CID
124125126127
CNAABBCCBBDD
CA6
20129
18
TableA
CRAABBCC
CAdd123 A234 B345 C
TableB
• Natural Joins (Also called Inner Joins)• ONLY the rows with common values in their
join columns are retained.
TableC
CID CN CA CAdd123 AA 6 123 A
124 BB 20 234 B
125 CC 12 345 C126 BB 9 234 B
123 AA 6 123 A
CIS3465: Professor Kirs Slide Number: 33
RDBMS Basics
How??? A Simple PRODUCT of the Tables is First created
123CID
124125126127
CNAABBCCBBAA
CA6
20129
18
TableA
CRAABBCC
CAdd123 A234 B345 C
TableBCN CR CAddCID CA
123 6 AA 123 AAA124 20 AA 123 ABB125 12 AA 123 ACC126 9 AA 123 ABB127 18 AA 123 AAA123 6 BB 234 BAA124 20 BB 234 BBB125 12 BB 234 BCC126 9 BB 234 BBB127 18 BB 234 BAA123 6 CC 345 CAA124 20 CC 345 CBB125 12 CC 345 CCC126 9 CC 345 CBB127 18 CC 345 CAA
CR AA Joins
CR BB Joins
CR CC Joins
CIS3465: Professor Kirs Slide Number: 34
RDBMS Basics
How??? A Simple PRODUCT of the Tables is First created
CN CR CAddCID CA123 6 AA 123 AAA124 20 AA 123 ABB125 12 AA 123 ACC126 9 AA 123 ABB127 18 AA 123 AAA123 6 BB 234 BAA124 20 BB 234 BBB125 12 BB 234 BCC126 9 BB 234 BBB127 18 BB 234 BAA123 6 CC 345 CAA124 20 CC 345 CBB125 12 CC 345 CCC126 9 CC 345 CBB127 18 CC 345 CAA
A SELECT command where CN = CR is issuedTableC
CID CN CA CAdd123 AA 6 123 A124 BB 20 234 B
125 CC 12 345 C126 BB 9 234 B
CIS3465: Professor Kirs Slide Number: 35
RDBMS Basics
ADDITIONAL RDBMS Operators: UNION
(NOT always available)
May use single single table
E.G., Creating a phone directory from 2 separate tables
There are no common values so a join can not be performed
Separate queries will produce separate tables
Duplicate records NOT displayed
Combines the results of two unrelated queries into a single output
CIS3465: Professor Kirs Slide Number: 36
RDBMS Basics
ADDITIONAL RDBMS Operators: INTERSECT
(NOT always available)
Similar to UNION (Combines the results of two unrelated queries into a single output)
There MUST be matching rows in the tables
E.G., Creating a phone directory of Faculty with offices in BUS who also teach in the Business Bldg. Some Faculty Have offices in BUS Some Faculty Teach in the Business Building Merge those who do both into one listing
CIS3465: Professor Kirs Slide Number: 37
RDBMS Basics
ADDITIONAL RDBMS Operators: DIFFERENCE
(NOT always available)
Yields all rows in one table NOT found in the other
Produces list of all possible pairings
PRODUCT (Seen Earlier)
DIVIDE
CIS3465: Professor Kirs Slide Number: 38
RDBMS Basics
RDBMS Software Classifications Fully Relational
Supports all relational algebra functions Enforces Entity and Referential Integrity Rules
Relationally Complete Supports all relational algebra functions Does NOT support Integrity Rules
Minimally Relational Supports only SELECT, PROJECT, and JOIN
Tabular Supports only SELECT, PROJECT, and JOIN User MUST define all access paths
CIS3465: Professor Kirs Slide Number: 39
RDBMS Basics