Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logical DB Design: ER to Relational

Similar presentations


Presentation on theme: "Logical DB Design: ER to Relational"— Presentation transcript:

1 Logical DB Design: ER to Relational
Entity sets to tables. Employees ssn name lot The slides for this text are organized into several modules. Each lecture contains about enough material for a 1.25 hour class period. (The time estimate is very approximate--it will vary with the instructor, and lectures also differ in length; so use this as a rough guideline.) This covers Lectures 1 and 2 (of 6) in Module (5). Module (1): Introduction (DBMS, Relational Model) Module (2): Storage and File Organizations (Disks, Buffering, Indexes) Module (3): Database Concepts (Relational Queries, DDL/ICs, Views and Security) Module (4): Relational Implementation (Query Evaluation, Optimization) Module (5): Database Design (ER Model, Normalization, Physical Design, Tuning) Module (6): Transaction Processing (Concurrency Control, Recovery) Module (7): Advanced Topics CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) 3

2 Constraints in Create Table
Adding constraints to a table enables the database system to enforce data integrity. Different types of constraints: * Not Null * Default Values * Unique * Primary Key * Foreign Key * Check Condition

3 Null and Not Null CS174A Relational Data Model

4 Primary Key Constraint
CREATE TABLE Student ( ID NUMBER PRIMARY KEY, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, ); Primary Key implies: * NOT NULL * UNIQUE. There can only be one primary key.

5 Primary Key Constraint (Syntax 2)
CREATE TABLE Students ( ID NUMBER, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, PRIMARY KEY(ID) ); Needed when the primary key is made up of two or more fields

6 Relationship Sets to Tables
ssn name lot did dname budget Employees Departments Works_in since CREATE TABLE Works_In ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) We implement a Many-to-Many relationship by adding 2 Tables’ Primary Keys to a third Table CS174A Relational Data Model

7 Foreign Key Constraint
CREATE TABLE Studies( Course NUMBER, Student NUMBER, FOREIGN KEY (Student) REFERENCES Students(ID) ); NOTE: ID must be unique (or primary key) in Student

8 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 Students ) Enrolled sid cid grade 53666 Carnatic101 C Reggae203 B 53650 Topology112 A History105 Students sid name login age gpa 53666 Jones 18 3.4 53688 Smith 3.2 53650 19 3.8 CS174A Relational Data Model

9 Key Constraints Translation to relational model?
since lot name ssn dname Each dept has at most one manager, according to the key constraint on Manages. did budget Manages Employees Departments Translation to relational model? 1-to-1 1-to Many Many-to-1 Many-to-Many 6

10 ER Relationship Sets  Relations
since ssn name lot did dname budget Manages Employees Departments CREATE TABLE Employees ( ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Departments ( did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)) With the key constraint? CS174A Relational Data Model

11 … with Key Constraints Map relationship Manages to one table:
Note that did is the key Separate tables for Employees and Departments CREATE TABLE Departments ( did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)) CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) Relational Data Model CS174A

12 Improvements Using Key Constraints
CREATE TABLE Departments ( did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)) CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Dept_Mgr ( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) Since each department has a unique manager, we could instead combine Manages and Departments Relational Data Model CS174A

13 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 must appear in a row of Manages (with a non-null ssn value!) since ssn name lot did dname budget Manages Employees Departments Works_In since CS174A Relational Data Model

14 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 Employees ON DELETE NO ACTION) CS174A Relational Data Model

15 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 under pname denotes a partial key. both relationship set and entity set are thick lines. cost ssn name lot pname age Policy Employees Dependents CS174A Relational Data Model

16 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 Employees ON DELETE CASCADE) CS174A Relational Data Model

17 Translating ISA: Option 1
address Translating ISA: Option 1 id Movie Person name ISA picture Actor Director create table MoviePerson( ... ) create table Actor(id varchar2(20), picture bfile, primary key(id), foreign key (id) references MoviePerson)) create table Director(...)

18 Translating ISA: Option 2
address Translating ISA: Option 2 id Movie Person name ISA picture Actor Director No table for MoviePerson! create table Actor(id varchar2(20), address varchar2(100), name varchar2(20), picture blob, primary key(id)); create table Director(...)

19 Review: ISA Hierarchies
Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed) Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no) Reasons for using ISA: To add descriptive attributes specific to a subclass To identify entities that participate in a relationship ssn name lot Employees ISA Hourly_Emps Contract_Emps Hourly_wages contractid Hours_worked Relational Data Model CS174A

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

21 Views CS174A Relational Data Model

22 What is a View? In SQL, a VIEW is a virtual relation based on the result-set of a SELECT statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. In some cases, we can modify a view and present the data as if the data were coming from a single table. Syntax: CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

23 SQL – Relations, Tables & Views
When we say Relation, it could be a Table or a View. There are three kind of relations: Stored relations tables We sometimes use the term “base relation” or “base table” Virtual relations views Temporary results

24 SQL – Create View Example: Create a view with title and year and made by Paramount studio. Movie (title, year, length, inColor, studioName, producerC#) CREATE VIEW ParamountMovie AS SELECT title,year FROM Movie WHERE studioName = ‘Paramount’;

25 The SQL Query Language The most widely used relational query language. Current standard is SQL-99 To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 sid name login age gpa 53666 Jones 18 3.4 53688 Smith 3.2 To find just names and logins, replace the first line: SELECT S.name, S.login CS174A Relational Data Model

26 SQL - Basic Queries Example: Pose a query to get the names of all students: SELECT LastName, FirstName FROM Students

27 SQL - Basic Queries (cont.)
Names of freshmen students

28 SQL - Basic Queries –Joining tables based on foreign key
Connecting majors with their department chairs: we need to have ChairID = FacultyID Majors(MajorID, MajorName, Department, ChairID) Faculty (FacultyID, FirstName, LastName, Phone, )

29 SQL - Basic Queries – Joining tables
Student last name, advisor last name OR (aliasing)

30 SQL - Basic Queries – Joining tables

31 ** END **


Download ppt "Logical DB Design: ER to Relational"

Similar presentations


Ads by Google