CS4222 Principles of Database System

Slides:



Advertisements
Similar presentations
Week 5 Relational Database Design by ER- -to-Relational Mapping.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Relational Database Design by ER- and EER-to-Relational Mapping.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Review: Application of Database Systems
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
SQL Structured Query Language Programming Course.
The Relational Model1 ER-to-Relational Mapping and Views.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Slide Chapter 7 Relational Database Design by ER- to-Relational Mapping.
Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 9 Relational Database Design by ER- and EERR-to-Relational Mapping Slide 9- 1.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Relational Database Design by ER- and EER-to-Relational Mapping
Relational Database Design by ER- and ERR-to-Relational Mapping
Chapter 10 SQL DDL.
Logical DB Design: ER to Relational
COP Introduction to Database Structures
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 480: Database Systems Lecture 13 February 13,2013.
Database Design The Relational Model Text Ch5
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Relational Database Design by ER- and EER-to-Relational Mapping
Relational Database Design by ER- and EERR-to-Relational Mapping
Relational Database Design by ER-to-Relational Mapping
Chapter (9) ER and EER-to-Relational Mapping, and other Relational Languages Objectives How a relational database schema can be created from a conceptual.
Designing Tables for a Database System
Chapter (9) ER and EER-to-Relational Mapping, and other Relational Languages Objectives How a relational database schema can be created from a conceptual.
Translation of ER-diagram into Relational Schema
Mapping ER Diagrams to Tables
ORACLE SQL Developer & SQLPLUS Statements
From ER to Relational Model
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
Translation of ER-diagram into Relational Schema
Relational Database Design by ER- and EER-to-Relational Mapping
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
SQL-1 Week 8-9.
Relational Database Design by ER- and EER-to-Relational Mapping
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Relational Database Design by ER- and EER-to- Relational Mapping
CS1222 Using Relational Databases and SQL
Data Definition Language
Data Definition Language
Database Design: Relational Model
Relational Database Design by ER- and EERR-to-Relational Mapping
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
CS122 Using Relational Databases and SQL
Mapping an ERD to a Relational Database
CS4433 Database Systems Relational Model.
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Relational Database Design by ER- and EER-to-Relational Mapping
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter (7) ER-to-Relational Mapping, and other Relational Languages
CS122 Using Relational Databases and SQL
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
CS4222 Principles of Database System
Relational Database Design by ER-to-Relational Mapping
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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: http://www.postgresql.org/docs/8.1/interactive/ddl-constraints.html#DDL-CONSTRAINTS-FK 8. Mapping CS4222_Su17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Summary of Mapping constructs and constraints 8. Mapping CS4222_Su17