Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS4222 Principles of Database System

Similar presentations


Presentation on theme: "CS4222 Principles of Database System"— Presentation transcript:

1 CS4222 Principles of Database System
11/22/2018 CS4222 Principles of Database System 8. ER/EER –to-Relational Mapping Huiping Guo Department of Computer Science California State University, Los Angeles

2 Outline SQL: Database Definition SQL: Constraints
11/22/2018 Outline SQL: Database Definition SQL: Constraints ER-to-Relational Mapping Step 1: Mapping of Regular Entity Types Step 2: Mapping of Binary M:N Relationship Types Step 3: Mapping of Binary 1:N Relationship Types Step 4: Mapping of Binary 1:1 Relationship Types 8. Mapping CS4222_Su17

3 Database Definition Language (DDL)
Subset of SQL commands to create and modify database objects relations, attributes, constraints,… Can create an entire database using SQL rather than a graphical user interface With DDL you gain more control over exactly what database objects are built and what they are named 8. Mapping CS4222_Su17

4 Databases PostgreSQL CREATE DATABASE dbname; USE dbname; You don’t have the privilege to create a database! 8. Mapping CS4222_Su17

5 Creating a Table Syntax CREATE TABLE tablename (
Fieldname1 DataType NULL | NOT NULL, Fieldname2 DataType NULL | NOT NULL ) Null means that the field may contain null (empty) values. Not Null means that the field cannot contain null values. 8. Mapping CS4222_Su17

6 Datatype Numeric types Character types Date/time types More dataypes
Integer, smallint, bigint, decimal, real,… Character types Varchar(n): variable-length with limit Char(n): fixed-length Text: variable unlimited length Date/time types Date, time,... More dataypes 8. Mapping CS4222_Su17

7 Creating a Table: Example
CREATE TABLE DEPARTMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9) ); 8. Mapping CS4222_Su17

8 Dropping a Table Syntax DROP Table tablename
Used to remove a relation (base table) and its definition The relation can no longer be used in queries, updates, or any other commands since its description no longer exists Example: Remove the DEPARTMENT table from the database DROP Table DEPARTMENT 8. Mapping CS4222_Su17

9 Alter table Used to add an attribute to an existing relation
Used to altering an existing attribute in a relation Remove an attribute from a relation 8. Mapping CS4222_Su17

10 Adding attributes to an Existing Table
Syntax ALTER TABLE tablename ADD fieldname DataType NULL | NOT NULL Example: ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12); 8. Mapping CS4222_Su17

11 Altering existing attributes in a Table
Syntax ALTER TABLE tablename MODIFY fieldname DataType NULL | NOT NULL Change the length on the job field to 15 in the Employee table ALTER TABLE Employee MODIFY job VARCHAR(15) NULL 8. Mapping CS4222_Su17

12 Dropping an attribute Syntax
ALTER TABLE tablename DROP COLUMN attributeName Example: Remove the job attribute from the Employee table ALTER TABLE Employee DROP COLUMN job 8. Mapping CS4222_Su17

13 Contraints Create primary key constraints Create unique constraints
Create foreign key constraints 8. Mapping CS4222_Su17

14 Primary Key Constraint With Create Table
Two ways with Create Table First form cannot create multi-field PK First way Create Table tablename ( fieldname1 datatype null | not null Primary Key, fieldname2 datatype null | not null ) 8. Mapping CS4222_Su17

15 Primary Key Constraint With Create Table (Cont.)
Second way Create Table tablename ( Fieldname1 datatype null | not null, Fieldname2 datatype null | not null, Primary Key (fieldname1,fieldname2) ) 8. Mapping CS4222_Su17

16 Example Create a table called Department. Set the primary key to Dnumber. CREATE TABLE DEPARTMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER primary key, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9) ); 8. Mapping CS4222_Su17

17 Unique Constraint With Create Table
Sometimes want to make sure a non-primary key column have unique values Two ways with Create Table First form cannot create multi-field constraint First form does not allow for naming constraint Syntax Create Table tablename ( Fieldname1 datatype null | not null Primary key, Fieldname2 datatype null | not null Unique ) 8. Mapping CS4222_Su17

18 Unique Constraint With Create Table (Cont.)
Second way: syntax Create Table tablename ( Fieldname1 datatype null | not null, Fieldname2 datatype null | not null, Unique (fieldname1, fieldname2) ) 8. Mapping CS4222_Su17

19 11/22/2018 Example Create a table called tblX with an integer ID field and an integer anotherfield. Both fields are not null. Make anotherfield unique. Create Table tblX ( ID int Not Null, anotherfield int Not Null Unique ) anotherfield int Not Null, Unique(anotherfield) 8. Mapping CS4222_Su17

20 Primary key and Foreign Key
Parent Table “One” Primary Key Foreign Key Child Table “Many” 8. Mapping CS4222_Su17

21 Foreign key constraints
Parent table and child table Parent table: the table whose primary key is involved in the relationship Child table: the table whose foreign key is involved in the table The purpose of foreign key constraints Make sure that all foreign keys in the child table match the primary keys in the parent table Which table to create the foreign key constraint? The child table 8. Mapping CS4222_Su17

22 Foreign Key Constraints With Create Table
Syntax 1: Create Table tablename ( child_field1 datatype null | not null, child_field2 datatype null | not null, Foreign Key (child_field1, child_field2) References parent_tablename (parent_field1, parent_field2) ) 8. Mapping CS4222_Su17

23 Foreign Key Constraints With Create Table
Syntax 2: Create Table tablename ( Fieldname1 datatype References parent_tablename, Fieldname2 datatype null | not null, ) 8. Mapping CS4222_Su17

24 Example Create a table called Dependent with 3 attributes: essn, name and sex. Create a foreign key constraint between essn and the ssn in the Employee table. Create Table Dependent ( essn Integer Not Null, name varchar(20) Not Null, sex char, Foreign Key (essn) References Employee (ssn) ) 8. Mapping CS4222_Su17

25 Foreign Key Implications
Foreign Key constraint requires that every FK field in child table matches existing PK in Parent table Constraint will not allow "orphan" records if updating or deleting PK value in parent table use Cascade Update or delete CASCADE specifies that when a referenced row is deleted/modified, row(s) referencing it should be automatically deleted as well. use Restrict Update or delete RESTRICT prevents deletion/modification of a referenced row 8. Mapping CS4222_Su17

26 Example CREATE TABLE products ( product_no integer PRIMARY KEY,
name text, price numeric ); CREATE TABLE orders ( order_id integer PRIMARY KEY, shipping_address text, ... ); From: 8. Mapping CS4222_Su17

27 Example (cont.) CREATE TABLE order_items (
product_no integer REFERENCES products ON DELETE RESTRICT ON MODIFY restrict, order_id integer REFERENCES orders ON DELETE CASCADE ON MODIFY CASCADE, quantity integer, PRIMARY KEY (product_no, order_id) ); 8. Mapping CS4222_Su17

28 ER-to-Relational Mapping
Problem ER Diagram Tables Modeling (Conceptual design) Mapping (Logical design) Mapping Get tables from the ER/EER diagram 8. Mapping CS4222_Su17

29 GOALS during Mapping Preserve all information (that includes all attributes) Maintain the constraints to the extent possible Relational Model cannot preserve all contstraints- e.g., max cardinality ratio such as 1:10 in ER; exhaustive classification into subtypes, e.g., STUDENTS are specialized into Domestic and Foreign Minimize null values The mapping procedure described has been implemented in many commercial tools. 8. Mapping CS4222_Su17

30 ER-to-Relational Mapping
Step 1: Mapping of Regular Entity Types Step 2: Mapping of Binary M:N Relation Types Step 3: Mapping of Binary 1:N Relation Types Step 4: Mapping of Binary 1:1 Relation Types 8. Mapping CS4222_Su17

31 Step 1: Mapping of Regular Entity Types
11/22/2018 Step 1: Mapping of Regular Entity Types For each regular entity type E in the ER schema, create a relation R that includes all the simple attributes of E If an attribute is composite, includes only the simple attributes of the composite attribute Choose one of the key attributes of E as the primary key for R If the chosen key of E is composite, the set of simple attributes that form it will together form the primary key of R. 8. Mapping CS4222_Su17

32 Step 1: Mapping of Regular Entity Types
Employees ssn name sex f_name l_name ssn f_name l_name sex Employees 8. Mapping CS4222_Su17

33 Step 1: Mapping of Regular Entity Types
CREATE TABLE Employees( ssn CHAR(11), f_name CHAR(20), l_name CHAR(20), sex CHAR(5), PRIMARY KEY (ssn) ) 8. Mapping CS4222_Su17

34 Step 2: Mapping of Binary M:N Relationship Types
11/22/2018 Step 2: Mapping of Binary M:N Relationship Types For each regular binary M:N relationship type R, create a new relation S to represent R. Attributes of the relation S much include Primary Keys of each participating entity set, which form the primary key of the relationship set S The primary keys are also the foreign keys of S All descriptive attributes of the relationship set 8. Mapping CS4222_Su17

35 Step 2: Mapping of Binary M:N Relationship Types
sex pname hours pno since name Works_on Projects Employees ssn Employees ssn name sex ssn pno since Works_On pno Projects pname hours 8. Mapping CS4222_Su17

36 Step 2: Mapping of Binary M:N Relationship Types
CREATE TABLE Works_On( ssn CHAR(11), pno INTEGER, since DATE, PRIMARY KEY (ssn, pno), FOREIGN KEY (ssn) REFERENCES Employees (ssn), FOREIGN KEY (pno) REFERENCES Projects (pno) ) 8. Mapping CS4222_Su17

37 Step 3: Mapping of Binary 1:N Relationship Types
11/22/2018 Step 3: Mapping of Binary 1:N Relationship Types Two approaches Approach 1 For each regular binary 1:N relationship type R, create a new relation S to represent R. Attributes of the relation S must include Primary Keys of each participating entity set The primary keys are also the foreign keys of S All descriptive attributes of the relationship set The primary key of S is the primary key of the participating entity type on the N side Map the two participating entity types to two relations 8. Mapping CS4222_Su17

38 Step 3: Mapping of Binary 1:N Relationship Types
sex dname budget dno since name Manage Departments Employees ssn 1 N Employees ssn name sex ssn dno since Manage dno Departments dname budget 8. Mapping CS4222_Su17

39 Step 3: Mapping of Binary 1:N Relationship Types
CREATE TABLE Manage( ssn CHAR(11), dno INTEGER, since DATE, PRIMARY KEY (dno), FOREIGN KEY (ssn) REFERENCES Employees (ssn), FOREIGN KEY (dno) REFERENCES Departments (dno) ) 8. Mapping CS4222_Su17

40 Step 3: Mapping of Binary 1:N Relationship Types
Approach 2 Since each department has at most one manager, we could instead combine Manages and Departments. Employees ssn name sex Departments dno dname budget Mg_Ssn Since 8. Mapping CS4222_Su17

41 Step 3: Mapping of Binary 1:N Relationship Types
CREATE TABLE Departments( dno INTEGER, dname CHAR(20), budget REAL, mg_ssn CHAR(11), since DATE, PRIMARY KEY (dno), FOREIGN KEY (mg_ssn) REFERENCES Employees (ssn) ) 8. Mapping CS4222_Su17

42 Step 4: Mapping of Binary 1:1 Relationship Types
11/22/2018 Step 4: Mapping of Binary 1:1 Relationship Types For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that correspond to the entity types participating in R. There are three possible approaches: Approach 1: Create a new relation for the relationship type Approach 2: Merged relationship type with one of the entity type Approach 3: 8. Mapping CS4222_Su17

43 Example One department has at most one manager
sex dname budget dno since name Manage Departments Employees ssn 1 1 One department has at most one manager One manager manages at most one department 8. Mapping CS4222_Su17

44 Approach 1: Map Manage to a table
CREATE TABLE Manage( ssn CHAR(11), dno INTEGER UNIQUE, since DATE, PRIMARY KEY (ssn), FOREIGN KEY (ssn) REFERENCES Employees (ssn), FOREIGN KEY (dno) REFERENCES Departments(dno)) OR CREATE TABLE Manage( ssn CHAR(11) UNIQUE, dno INTEGER, since DATE, PRIMARY KEY (dno), FOREIGN KEY (ssn) REFERENCES Employees (ssn), FOREIGN KEY (dno) REFERENCES Departments) (dno) ) 8. Mapping CS4222_Su17

45 Approach 2: Combine Manage and Employees
CREATE TABLE Emp_Mgr( ssn CHAR(11), name CHAR(20), sex CHAE, dno INTEGER UNIQUE, since DATE, PRIMARY KEY (ssn), FOREIGN KEY (dno) REFERENCES Departments (dno) ) 8. Mapping CS4222_Su17

46 Approach 3: Combine Manage and Departments
CREATE TABLE Dept_Mgr( dno INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) UNIQUE, since DATE, PRIMARY KEY (dno), FOREIGN KEY (ssn) REFERENCES Employees (ssn) ) 8. Mapping CS4222_Su17

47 Summary of Mapping constructs and constraints
8. Mapping CS4222_Su17


Download ppt "CS4222 Principles of Database System"

Similar presentations


Ads by Google