ITD 256 02
description
Transcript of ITD 256 02
ITD 256 Database Concepts
Lecture 02
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Database System
• 2 main Database System Component
– Database
– DBMS
Database
• 1st Database System Component
• Database
– Self-describing collection of related tables logically stored together to facilitate integration and sharing
• Self-describing – Description of structure contained within database itself
• Related tables – Interrelated data
Database
– Database Contents
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
DBMS
• 2nd Database System Component
• DBMS (Database Management System) – Computer Program or SOFTWARE to Create,
Process, & Administer the Database
– Receive requests encoded in SQL and translate those into actions on the Database
– Examples are SQL Server, Oracle, Teradata, mySQL, DB2, etc
DBMS
• Functions of a DBMS
DBMS
• Functions of a DBMS
• Enforce rules – Data Types
– Limited list or choices
– Business driven rules
– Referential Integrity Constraints • Rules to control data referenced by related values in
related tables
DBMS
• Functions of a DBMS
• Concurrency
– Ensuring one user’s work does not interfere with another user’s work
DBMS
• Functions of a DBMS
• Security
– Authentication
– Authorization
– Encrypted Information
– Audit Trail
– Transaction Log
DBMS
• Functions of a DBMS
• Backup/Recovery
– Ensure no loss of Data in event of errors (hardware, software, natural, or human)
– Backup strategies
– Restore from multiple media & sites
DBMS
• Functions of a DBMS
• High Availability aka HA
– High Availability solutions like clustering, mirroring, replication, etc.
– HA solutions aim for 99.999% system availability
– Is HA interchangeable with Backup/Recovery?
DBMS
• Enterprise-Class MS SQL Server 2012
DBMS
• Enterprise-Class Oracle 11g
DBMS
• MySQL 5.6
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Database System
• Personal Database System
– Individual users
– Include few tables
Database System
• Enterprise-Class Database System
– Enormous Databases supporting organizations
– Hundreds of Databases, millions of records
– Thousands of concurrent users
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
HW 1
1. Figure above shows a list that is used by a veterinary office. Give examples of three modification problems that are likely to occur when using this list. (3pts)
2. Break the list in figure above into two tables, each with data for a single theme. Assume that owners have a unique OwnerEmail and pets have unique PetName column. Indicate the unique identifiers and linking column. (5pts)
3. Describe how the tables you created in Q2 solves the problems you identified in Q1. (3pts)
4. Why is it a bad idea to assume PetName as a unique identifier? (2pts)
5. What is the difference between data and information? (2pts)
6. How is relational model better than traditional file processing? (2pts)
7. Describe in your own words how relationships between single themed tables are represented in a relational database. (2pts)
8. Define the term database. (2pts)
9. What is a DBMS? (2pts)
10. Define the term “Data Anomaly” and give an example. (2pts)
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Relations
• Relational model developed in 1970s by E.F. Codd
• Referred tables as “Relations” hence the term RDBMS
• Single theme in a table is referred as an “entity”
• Relation is two-dimensional “table” consisting of “rows” & “columns”
• A schema is the organization of data as a conceptual framework; how the database is constructed
Relations
• Characteristics of a Relation
Relations
• Are all relational characteristics satisfied?
Relations
• What relational characteristics are violated?
Relations
• What relational characteristics are violated?
Relations
• What relational characteristics are violated?
Relations
• Presenting Relation Structure
– RELATION_NAME(Col01, Col02, … , … , LastColumn)
– Table or relation name is all uppercase
– Column names have first letter capitalized for each word
– No blank spaces in Relation or Table names
– Relationships are inherent within data, there are no explicit linkage between tables
Relations
• Presenting Relation Structure
• EMPLOYEE table is represented as
EMPLOYEE (EmployeeNumber, FirstName, LastName, Department, Email, Phone)
Relations
• Equivalent Sets of Terms
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Keys
• Key
–One or more columns of a table that is used to identify a row
–A key can be
• Unique
• Nonunique
Keys
• Unique Key – Query produces single row
– Unique keys?
Keys
• Nonunique Key – Query produces multiple rows
– Nonunique keys?
Keys
• Composite Keys
• A key that contains two or more attributes to uniquely identify any record
- OR -
• Unique key is formed by combining two or more attributes
• STUDENT (FirstName, LastName, City, State, Zip, Phone) – Assume none of the individual fields are unique
– What are potential composite keys?
Keys
• Candidate Keys
• Keys that uniquely identify each row in a table
• Can be single or composite
• EMPLOYEE (EmployeeID, SSN, FirstName, LastName, Department, WorkEmail, HomePhone, CellPhone, ManagerID)
– What are potential candidate keys?
Keys
• Primary Key
• The candidate key that is chosen as the key that DBMS will use to uniquely identify each row in a relation
• Best practice states that PRIMARY keys are short, numeric, and never change the value
• If EmployeeID is chosen as the PRIMARY KEY:
• EMPLOYEE (EmployeeID, SSN, FirstName, LastName, Department, WorkEmail, HomePhone, CellPhone, ManagerID)
Keys
• Surrogate Key
• A column with a unique, DBMS-assigned identifier that has been added to a
table to be the primary key.
• Unique values are assigned each time a row is created, like auto-number
• Recall that ideal primary keys are short, numeric, & never changes
• An example of when surrogate key may be the answer
PROPERTY(Street, City, State, ZIP, OwnerID)
PROPERTY(PropertyID, Street, City, State, ZIP, OwnerID)
Keys
• Foreign Key
• When an attribute in a table A is a Primary key in table B, then that attribute in table A is called a Foreign key to table B.
• EMPLOYEE (EmployeeNumber, FirstName, LastName, “Department”, Email, Phone)
• DEPARTMENT (DepartmentName, BudgetCode, OfficeNumber, DepartmentPhone)
• Department in EMPLOYEE is a foreign key to DEPARTMENT
Keys
• Foreign Key
• Recall: Relationships are inherent within the data – there are no explicit linkages between tables.
• Foreign Keys form relationship between related tables.
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Referential Integrity Constraint
• Referential Integrity Constraint
• Every value of a foreign key must match a value of the referred primary key
• EMPLOYEE (EmployeeNumber, FirstName, LastName, “Department”)
• DEPARTMENT (DepartmentName, BudgetCode, DepartmentPhone)
• What is the Referential Integrity Statement?
Referential Integrity Constraint
• Referential Integrity Constraint
• EMPLOYEE (EmployeeNumber, FirstName, LastName, “Department”)
• DEPARTMENT (DepartmentName, BudgetCode, DepartmentPhone)
Department in EMPLOYEE must exist in DepartmentName in DEPARTMENT
– Note: Foreign Key in CHILD must exist in Primary Key in PARENT
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Exercise
• A __________ is the organization of data as a conceptual __________ of how the database is constructed.
• When an attribute in a table A is a __________ key in table B, then that attribute in table A is called a __________ key to table B.
• In relational model __________ are inherent within the data so there are no explicit linkages between tables.
• __________ ensures that every value of a foreign key must match a value of the referred primary key.
CUSTOMER
PRESCRIPTION
• What are Primary keys? Need for Surrogate key?
• Parent/Child tables, Foreign keys?
• Referential Integrity Constraint statement?
STUDENT DORM
• What are Primary keys? Need for Surrogate key?
• Parent/Child tables, Foreign keys?
• Referential Integrity Constraint statement?
EMPLOYEE DEPARTMENT
• What are Primary keys? Need for Surrogate key?
• Parent/Child tables, Foreign keys?
• Referential Integrity Constraint statement?
ATTORNEY_MEETING CLIENT
• What are Primary keys? Need for Surrogate key?
• Parent/Child tables, Foreign keys?
• Referential Integrity Constraint statement?
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
HW 2
1. Explain the difference between a primary key and a candidate key. (2pts)
2. What is a surrogate key and why would you use one? (2pt)
3. Explain the term foreign key. (2pt)
4. Define the term referential integrity constraint. (2pts)
5. Define the terms functional dependency and determinant. (2pts)
6. Define the term primary key in terms of functional dependencies. (2pts)
7. Draw Relations Equivalent set of terms for Table, Row, and Column. (2pts)
8. In your own words, describe the purpose of the normalization process. (2pts)
9. Apply the normalization process to figure above to develop a set of normalized relations (assume PetName is unique in the relation above). Show the results of each of the steps in the normalization process 1,2,3a,3b,3c,3d. (5pts)
10. Draw and populate the two normalized tables from Q9. (4pts)
Agenda
• Database
• DBMS
• Database System
• Recap HW1
• Relations
• Keys
• Referential Integrity Constraint
• Class Exercise
• HW2
• Quiz
Quiz Exercise
• CLIENT schema fields – ClientFirst
– ClientLast
– ClientID
– ClientEmail
– AttorneyName
– AttorneyEmail
– AttorneyLocation
Assume a client is assigned an Attorney but attorney serves many clients
Draw the single themed solution
Identify the themes
Quiz Exercise • CLIENT schema fields
– ClientFirst
– ClientLast
– ClientID
– ClientEmail
– AttorneyName
– AttorneyEmail
– AttorneyLocation
Assume a client is assigned an Attorney but attorney serves many clients
CLIENT
ATTORNEY
Identify the fields
Quiz Exercise • CLIENT schema fields
– ClientFirst
– ClientLast
– ClientID
– ClientEmail
– AttorneyName
– AttorneyEmail
– AttorneyLocation
Assume a client is assigned an Attorney but attorney serves many clients
CLIENT (ClientFirst, ClientLast, ClientID, ClientEmail)
ATTORNEY (AttorneyName, AttorneyEmail, AttorneyLocation)
Identify the unique identifiers
Quiz Exercise • CLIENT schema fields
– ClientFirst
– ClientLast
– ClientID
– ClientEmail
– AttorneyName
– AttorneyEmail
– AttorneyLocation
Assume a client is assigned an Attorney but attorney serves many clients
CLIENT (ClientFirst, ClientLast, ClientID, ClientEmail)
ATTORNEY (AttorneyName, AttorneyEmail, AttorneyLocation)
Identify the parent & child tables, add the linking field
Quiz Exercise • CLIENT schema fields
– ClientFirst
– ClientLast
– ClientID
– ClientEmail
– AttorneyName
– AttorneyEmail
– AttorneyLocation
Assume a client is assigned an Attorney but attorney serves many clients
CLIENT (ClientFirst, ClientLast, ClientID, ClientEmail, “AttorneyEmail”)
ATTORNEY (AttorneyName, AttorneyEmail, AttorneyLocation)
Quiz Exercise CLIENT (ClientFirst, ClientLast, ClientID, ClientEmail, “AttorneyEmail”)
ATTORNEY (AttorneyName, AttorneyEmail, AttorneyLocation)
– Which table is the PARENT? CHILD?
– Which table can benefit from Surrogate key?
– Candidate Keys in CLIENT besides chosen primary key?
– Candidate Keys in ATTORNEY besides chosen primary key?
– Non-unique keys in CLIENT?
– Non-unique keys in ATTORNEY?
– Referential Integrity Constraint statement?