Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model.

Similar presentations


Presentation on theme: "COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model."— Presentation transcript:

1 COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model

2 22 L3 Download from our website Individual work with your academic honesty declaration form Hardcopy or written work not electronic form Due date: 20/08/2007(Monday, week 5), late submission will not be accepted ER Diagram, relational schema and SQL DDL Assignment 1

3 33 L3 Relational Data Model Introduction to Conceptual Data Model Database Design Sequence Conceptual data model Types of conceptual data model Entity Relationship Model Entity, Relationship, Attribute Degree of relationship Cardinality and participation Ternary relationship Weak entity Review of Last Class

4 44 L3 Relational Data Model Defining the Schema Mathematics of Relations Logical Database Design Today’s Agenda

5 55 L3 Relational Data Model Review Relational database: a set of relations Relation: Instance : a table, with rows and columns. The value in the database at a given time. Schema : specifies name of relation, plus name and type of each column. E.G. Student (sid: string, name: string, login: string, age: integer, gpa: real). Can think of a relation as a set of rows or tuples (i.e., all rows are distinct).

6 66 L3 Relational Data Model Example Instance   All rows are distinct.   Do all columns in a relation instance have to be distinct? NO! Student Fields (Attributes, Columns) Tuples (Records, Rows)

7 77 L3 Relational Data Model Defining Schema in SQL Creates the Student relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Student (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL ) CREATE TABLE Enrolled (sid: CHAR(20), cid: CHAR(20), grade: CHAR (2))

8 88 L3 Relational Data Model Destroying and Altering Relations DROP TABLE Student Destroys the relation Student. The schema information and the tuples are deleted. ALTER TABLE Student ADD COLUMN firstYear: integer The schema of Student is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field.

9 99 L3 Relational Data Model Adding Tuples Can insert a single tuple using: INSERT INTO Student (sid, name, login, age, gpa) VALUES (53688, ‘ Smith ’, ‘ smith@ee ’, 18, 3.2) Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Student S WHERE S.name = ‘ Smith ’ Powerful variants of these commands are available; more later!

10 1010 L3 Relational Data Model Integrity Constraints Integrity Constraint (IC): condition that must be true for any instance of the database; e.g., domain constraints. ICs can be declared in the schema They are specified when schema is defined. All declared ICs are checked whenever relations are modified. A legal instance of a relation is one that satisfies all specified ICs. If ICs are declared, DBMS will not allow illegal instances. If the DBMS checks ICs, stored data is more faithful to real-world meaning. Avoids data entry errors, too!

11 1111 L3 Relational Data Model Non-Null Columns One domain constraint is to insist that no value in a given column can be null The value can’t be unknown; The concept can’t be inapplicable In SQL CREATE TABLE Student (sid: CHAR(20) NOT NULL, name: CHAR(20), login: CHAR(10) NOT NULL, age: INTEGER, gpa: REAL)

12 1212 L3 Relational Data Model Relational Keys Keys are special fields that serve two main purposes: Primary keys are unique not-null identifiers of the relation in question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique Foreign keys are where one table has a column that refers to the primary key of another table (to allow connections to be made) Keys can be simple (a single field) or composite (more than one field) Keys usually are used as indices to speed up the response to user queries (More on this later in the semester)

13 1313 L3 Relational Data Model Example: Relational Keys Primary Key Foreign Key (implements 1:N relationship between customer and order) Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product)

14 1414 L3 Relational Data Model Primary Key Constraints A set of fields is a key for a relation if : 1. No two distinct tuples can have same values in all key fields, and 2. This is not true for any subset of the key. Part 2 false? A superkey. If there’s >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key. E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.

15 1515 L3 Relational Data Model Student and Course studentcourse enroll title cid credits sid name grade CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid))

16 1616 L3 Relational Data Model Primary and Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key.   “For a given student and course, there is a single grade.” vs. “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.”   Used carelessly, an IC can prevent the storage of database instances that arise in practice! CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid)) CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid), UNIQUE (cid, grade)) vs

17 1717 L3 Relational Data Model Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer’. E.g. sid is a foreign key referring to Student: Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references. Can you name a data model w/o referential integrity? Links in HTML! sidname cidtitle Student Course credits grade Enrolled sidcid

18 1818 L3 Relational Data Model Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Student ) Enrolled Student

19 1919 L3 Relational Data Model Enforcing Referential Integrity Consider Student and Enrolled; sid in Enrolled is a foreign key that references Student. What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!) What should be done if a Student tuple is deleted? Also delete all Enrolled tuples that refer to it. Disallow deletion of a Student tuple that is referred to. Set sid in Enrolled tuples that refer to it to a default sid. (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’.) Similar if primary key of Student tuple is updated.

20 2020 L3 Relational Data Model Referential Integrity in SQL SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (also delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Student ON DELETE CASCADE ON UPDATE SET DEFAULT ) sidname cidtitle Student Course credits grade Enrolled sidcid

21 2121 L3 Relational Data Model Named Constraints Tip: give a name to constraints (this makes error messages clearer) CREATE TABLE Student ( sid INTEGER, …, CONSTRAINT Students_PK PRIMARY KEY (sid) ); CREATE TABLE Course ( cid CHAR(8), …, CONSTRAINT Courses_PK PRIMARY KEY (cid) ); CREATE TABLE Enroll ( sid INTEGER, cid CHAR(8), CONSTRAINT Enroll_FK1 FOREIGN KEY (sid) REFERENCES Students, CONSTRAINT Enroll_FK2 FOREIGN KEY (cid) REFERENCES Course, CONSTRAINT Enroll_PK PRIMARY KEY (sid,cid) ); studentcourse enroll title cid credits sid name

22 2222 L3 Relational Data Model Views A view is just a relation, but we store a definition, rather than a set of tuples. The definition is given by a query (see later lectures) which combine information from one or more tables Users can operate on the view as if it were a stored table DBMS calculates the value whenever it is needed CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21

23 2323 L3 Relational Data Model Defining the Schema Mathematics of Relations Logical Database Design Today’s Agenda

24 2424 L3 Relational Data Model Relation The model was first proposed by Dr. E.F. Codd of IBM in 1970 in the following paper: "A Relational Model for Large Shared Data Banks," Communications of the ACM, June 1970. The above paper caused a major revolution in the field of Database management and earned Ted Codd the coveted ACM Turing Award. The relational Model of Data is based on the mathematical concept of Relation. Studied in Discrete Mathematics The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

25 2525 L3 Relational Data Model Definition of Relation A Relation is a mathematical concept based on the ideas of sets. Formal definition Relation R Given sets D 1, D 2, …, D n, a relation R is a subset of D 1 x D 2 x…x D n. Thus, a relation is a set of n-tuples ( a 1, a 2, …, a n ) where a i  D i Example: If customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city = {Sydney, Brisbane, Perth} then R = { (Jones, Main, Sydney), (Smith, North, Perth), (Curry, North, Brisbane), (Lindsay, Park, Sydney) } is a relation over customer-name x customer-street x customer-city

26 2626 L3 Relational Data Model Relation Schema and Instance A relation R has a relation schema: specifies name of relation, plus name and data type of each attribute. A 1, A 2, …, A n are attributes R = (A 1, A 2, …, A n ) is a relation schema e.g. Students (sid: string, name: string, login: string, age: integer, gpa: real). an relation instance: a table, with rows and columns. D 1, D 2, …, D n are the domains each attribute corresponds to one domain: dom(A i ) = D i, 1 <= i <= n R  D 1 x D 2 x…x D n #rows = cardinality, #fields = degree.

27 2727 L3 Relational Data Model Defining the Schema Mathematics of Relations Logical Database Design Today’s Agenda

28 2828 L3 Relational Data Model Logical DB Design: ER to Relational Entity sets to tables: CREATE TABLE Employee (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Employee ssn name lot ssnnamelot Employee

29 2929 L3 Relational Data Model Relationship Sets to Tables In translating a relationship set to a relation, attributes of the relation must include: Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation. All descriptive attributes. CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employee, FOREIGN KEY (did) REFERENCES Department) ssnnamelot diddname Employee Department budget since Works_In ssndid

30 3030 L3 Relational Data Model Review: Key Constraints Each dept has at most one manager, according to the key constraint on Manages. Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1 dname budget did since lot name ssn Manages Employee Department

31 3131 L3 Relational Data Model Translating ER Diagrams with Key Constraints Map relationship to a table: Note that did is the key now! Separate tables for Employees and Departments. Since each department has a unique manager, we could instead combine Manages and Departments. CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employee, FOREIGN KEY (did) REFERENCES Department) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employee) OR

32 3232 L3 Relational Data Model Review: Participation Constraints Does every department have a manager? If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial). Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value!) lot name dname budgetdid since name dname budgetdid since Manages since Department Employee ssn Works_In

33 3333 L3 Relational Data Model Participation Constraints in SQL We can capture participation constraints involving one entity set in a binary relationship, but little else (without resorting to CHECK constraints). CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employee, ON DELETE NO ACTION)

34 3434 L3 Relational Data Model Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Owner entity set and weak entity set must participate in a one-to- many relationship set (1 owner, many weak entities). Weak entity set must have total participation in this identifying relationship set. lot name age pname Dependent Employee ssn Policy cost

35 3535 L3 Relational Data Model Translating Weak Entity Sets Weak entity set and identifying relationship set are translated into a single table. When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employee, ON DELETE CASCADE )

36 3636 L3 Relational Data Model Multi-valued attributes Resolution of a Multi-valued attribute Finite & non-critical: split the attribute into multiple attributes Video Store Phone# Customer Video Store Home Phone# Customer Office Phone# Infinite & critical: create a new entity Phone Company Phone# Customer Phone Company CustomerPhone Phone# Phone type Servicetype

37 3737 L3 Relational Data Model Mapping Finite Multi-valued Attributes LicenceNoNameContactAge PILOT Identifier Attribute Licence Number ENTITY Multi-valued Attribute PILOT Attribute NameAge Contact Aircraft Type Endorsement Endorsement 1Endorsement 2

38 3838 L3 Relational Data Model Mapping Infinite Multi-valued Attributes LicenceNoNameContactAge PILOT Licence Number ENTITYPILOT NameAge Contact Aircraft Type Endorsement LicenceNo ENDORSEMENT Has

39 3939 L3 Relational Data Model Review: ISA Hierarchies   As in C++, or other PLs, attributes are inherited.   If we declare A ISA B, every A entity is also considered to be a B entity. Overlap constraints: Can Joe be an Hourly_Emp as well as a Contract_Emp entity? (Allowed/disallowed) Covering constraints: Does every Employee entity also have to be an Hourly_Emp or a Contract_Emp entity? (Yes/no) Contract_Emp name ssn Employee lot hourly_wages ISA Hourly_Emp contractid hours_worked

40 4040 L3 Relational Data Model Translating ISA Hierarchies to Relations General approach: 3 relations: Employee, Hourly_Emp and Contract_Emp. Hourly_Emp: Every employee is recorded in Employee. For hourly emps, extra info recorded in Hourly_Emp (hourly_wages, hours_worked, ssn); must delete Hourly_Emp tuple if referenced Employee tuple is deleted). Queries involving all employees easy, those involving just Hourly_Emp require a join to get some attributes. Alternative: Just Hourly_Emp and Contract_Emp. Hourly_Emp: ssn, name, lot, hourly_wages, hours_worked. Each employee must be in one of these two subclasses.

41 4141 L3 Relational Data Model Review: Binary vs. Ternary Relationships What are the additional constraints in the 2nd diagram? age pname Dependent Covers name Employee ssn lot Policy policyid cost Beneficiary age pname Dependent policyid cost Policy Purchaser name Employee ssn lot Bad design Better design

42 4242 L3 Relational Data Model Binary vs. Ternary Relationships (Contd.) The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents. Participation constraints lead to NOT NULL constraints. What if Policies is a weak entity set? CREATE TABLE Policy ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employee, ON DELETE CASCADE) CREATE TABLE Dependent ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policy, ON DELETE CASCADE)

43 4343 L3 Relational Data Model Wrap-Up Relational data model SQL DML, especially for constraints Mathematics of Relations Logical Database Design Transformation from ER to Relational


Download ppt "COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model."

Similar presentations


Ads by Google