[ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the...

26
[ER MODELLING] CIS 404 SPRING 2015 ABSTRACT This document describes the process of drawing ER diagrams and implementing the corresponding databases in SQL Server. Soberekon Lolia

Transcript of [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the...

Page 1: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

[ER MODELLING]

CIS 404 SPRING 2015

ABSTRACTThis document describes the process of drawing ER diagrams and implementing the corresponding databases in SQL Server.

Soberekon Lolia

Page 2: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

1

Entity Relationship Diagram

1. Entity is an object in the users’ world. An entity can be physical (e.g. student) or conceptual (cost). Entities have attributes including a key attribute.

2. Entities are related to each other. There are three types of relationship: 1 to 1, 1 to N, N to M.

3. An ER diagram is a data model which is used to capture the users’ requirements.

Rules for converting ER Diagrams into Relational Diagrams

1. Every entity becomes a table. Every attribute becomes a field. They key attribute becomes the primary key.

2. For Every 1 to 1 and 1 to N relationships add the primary key of the parent entity to the child table as a foreign key.

3. For every N to M relationships create an Intersection table. The keys of the two participating entities combined becomes the key of this table. An attributes of the relationship, if any, becomes a field in this table.

[Date]

Page 3: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

2

This case has 3 entities:

1. Sales order (SO_Number)2. Item (Item_Number, Item_Name)3. Customer (Cust_Code, Cust_Name)

ER Diagrams for the sales order case

[Date]

Page 4: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

3

ER Diagram in SQL data modeler

[Date]

This relationship has an attribute: Qty_Ordered

Page 5: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

4

Relational Model drawn in SQL Data Modeler

[Date]

Page 6: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

5

DDL (Data Definition Language) Statements for creating the database in SQL Server

-- Generated by Oracle SQL Developer Data Modeler 4.0.2.840-- at: 2015-03-09 10:20:34 CDT-- site: SQL Server 2008-- type: SQL Server 2008

CREATE TABLE Customer ( Cust_Code VARCHAR (10) NOT NULL , Cust_Name VARCHAR (25) NOT NULL , CONSTRAINT Customer_PK PRIMARY KEY CLUSTERED (Cust_Code)WITH ( ALLOW_PAGE_LOCKS = ON , ALLOW_ROW_LOCKS = ON ) ON "default" ) ON "default"GO

CREATE TABLE Item ( Item_Number VARCHAR (10) NOT NULL , Item_Name VARCHAR (25) NOT NULL , CONSTRAINT Item_PK PRIMARY KEY CLUSTERED (Item_Number)WITH ( ALLOW_PAGE_LOCKS = ON , ALLOW_ROW_LOCKS = ON ) ON "default" ) ON "default"GO

CREATE TABLE Relation_4 ( Item_Item_Number VARCHAR (10) NOT NULL , SalesOrder_SO_Number NUMERIC (6) NOT NULL ,

[Date]

Page 7: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

6

Qty_Order NUMERIC (3) NOT NULL , CONSTRAINT Relation_4__IDX PRIMARY KEY CLUSTERED (Item_Item_Number, SalesOrder_SO_Number)WITH ( ALLOW_PAGE_LOCKS = ON , ALLOW_ROW_LOCKS = ON ) ON "default" ) ON "default"GO

CREATE TABLE SalesOrder ( SO_Number NUMERIC (6) NOT NULL , Customer_Cust_Code VARCHAR (10) NOT NULL , CONSTRAINT SalesOrder_PK PRIMARY KEY CLUSTERED (SO_Number)WITH ( ALLOW_PAGE_LOCKS = ON , ALLOW_ROW_LOCKS = ON ) ON "default" ) ON "default"GO

ALTER TABLE Relation_4ADD CONSTRAINT FK_ASS_2 FOREIGN KEY(Item_Item_Number)REFERENCES Item(Item_Number)ONDELETE NO ACTION ONUPDATE NO ACTIONGO

ALTER TABLE Relation_4ADD CONSTRAINT FK_ASS_3 FOREIGN KEY

[Date]

Page 8: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

7

(SalesOrder_SO_Number)REFERENCES SalesOrder(SO_Number)ONDELETE NO ACTION ONUPDATE NO ACTIONGO

ALTER TABLE SalesOrderADD CONSTRAINT SalesOrder_Customer_FK FOREIGN KEY(Customer_Cust_Code)REFERENCES Customer(Cust_Code)ONDELETE NO ACTION ONUPDATE NO ACTIONGO

-- Oracle SQL Developer Data Modeler Summary Report: -- -- CREATE TABLE 4-- CREATE INDEX 0-- ALTER TABLE 3-- CREATE VIEW 0-- CREATE PACKAGE 0-- CREATE PACKAGE BODY 0-- CREATE PROCEDURE 0-- CREATE FUNCTION 0-- CREATE TRIGGER 0-- ALTER TRIGGER 0-- CREATE DATABASE 0-- CREATE DEFAULT 0-- CREATE INDEX ON VIEW 0-- CREATE ROLLBACK SEGMENT 0-- CREATE ROLE 0

[Date]

Page 9: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

8

-- CREATE RULE 0-- CREATE PARTITION FUNCTION 0-- CREATE PARTITION SCHEME 0-- -- DROP DATABASE 0-- -- ERRORS 0-- WARNINGS 0

[Date]

Page 10: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

9

Relational Model in SQL Server

[Date]

Page 11: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

10

USERS’ REQUIREMENTS

THE MOVIE DATABASE

“I’m the owner of a small movie rental store. We have over 3,000 movies that we need to keep track of. Each of our movies has a DVD or VHS tape number. For each movie, we need to know its title and category (e.g., comedy, suspense, drama, action, war, or sci-fi).Yes, we do have multiple copies of many of our movies. We give each movie a specific ID, andthen track which DVD or VHS contains the movie. A movie can be either DVD or VHS format.We always have at least one DVD or VHS tape for each movie we track, and each DVD or VHStape is always a copy of a single, specific movie.Our DVDs and VHS tapes are very long. We don’t have any movies that require multiple DVDs or VHS tapes."

"We are frequently asked for movies starring specific actors. John Wayne and Julia Roberts are always popular. So we’d like to keep track of the star actors appearing in each movie. Not all of our movies have star actors. Customers like to know each actor’s “real” birth name and date of birth. We track only actors who appear in the movies in our inventory.

We have lots of customers. We rent videos only to people who have joined our 'video club.' To belong to our club, they must have good credit. For each club member, we’d like to keep their first and last name, current phone number, and current address. And, of course, each club member has a membership number.

Then we need to keep track of what video tapes each customer currently has checked out. A customer may check out multiple video tapes at any given time. We just track current rentals. We don’t keep track of any rental histories.”

We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep a due date. Keeping this rental history will allow us to analyze the pattern of our rentals. We will be able to determine how many tapes each customer rents and how many times a customer has returned a tape late. We will also know how many times a particular tape has been used and will then know when to retire each tape. We will also be able to analyze our customers’ movie preferences.”

[Date]

Page 12: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

11

Entities and their attributes1. Movie (Movie ID, Title, Category)2. Member (Member ID, First name, Last name, Address, Phone Number)3. Rentals (Rental ID, Date and Time Issued, Date and Time Returned) 4. Copies (Copy ID, Types)5. Actors (Actor ID, Birth Name, Name, Date of birth)

[Date]

Page 13: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

12

ER Diagram in Data Modeler

[Date]

Page 14: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

13

Relational Model

[Date]

Page 15: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

14

DDL Statement

-- Generated by Oracle SQL Developer Data Modeler 4.0.2.840

-- at: 2015-03-11 10:27:17 CDT

-- site: SQL Server 2008

-- type: SQL Server 2008

CREATE

TABLE Actors

(

Actors_ID NUMERIC (6) NOT NULL ,

Actor_Name VARCHAR (25) NOT NULL ,

Birthname VARCHAR (25) ,

Birthdate DATETIME ,

CONSTRAINT Actors_PK PRIMARY KEY CLUSTERED (Actors_ID)

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

ON "default"

)

ON "default"

GO

CREATE

TABLE Copies

[Date]

Page 16: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

15

(

Copy_ID NUMERIC (6) NOT NULL ,

Type CHAR (3) NOT NULL ,

Movie_Movie_ID NUMERIC (6) NOT NULL ,

CONSTRAINT Copies_PK PRIMARY KEY CLUSTERED (Copy_ID, Movie_Movie_ID)

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

ON "default"

)

ON "default"

GO

CREATE

TABLE Member

(

Member_ID NUMERIC (6) NOT NULL ,

F_Name VARCHAR (25) NOT NULL ,

L_Name VARCHAR (25) NOT NULL ,

Member_Address VARCHAR (30) NOT NULL ,

Phone CHAR (10) NOT NULL ,

CONSTRAINT Member_PK PRIMARY KEY CLUSTERED (Member_ID)

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

[Date]

Page 17: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

16

ON "default"

)

ON "default"

GO

CREATE

TABLE Movie

(

Movie_ID NUMERIC (6) NOT NULL ,

Movie_Title VARCHAR (50) NOT NULL ,

Movie_Category VARCHAR (15) NOT NULL ,

CONSTRAINT Movie_PK PRIMARY KEY CLUSTERED (Movie_ID)

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

ON "default"

)

ON "default"

GO

CREATE

TABLE Movie_Actors

(

Movie_Movie_ID NUMERIC (6) NOT NULL ,

Actors_Actors_ID NUMERIC (6) NOT NULL ,

CONSTRAINT Relation_5__IDX PRIMARY KEY CLUSTERED (Movie_Movie_ID,

Actors_Actors_ID)

[Date]

Page 18: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

17

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

ON "default"

)

ON "default"

GO

CREATE

TABLE Rental

(

Rental_ID NUMERIC (6) NOT NULL ,

DateIssued DATETIME NOT NULL ,

DateReturned DATETIME ,

Member_Member_ID NUMERIC (6) NOT NULL ,

CONSTRAINT Rental_PK PRIMARY KEY CLUSTERED (Rental_ID)

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

ON "default"

)

ON "default"

GO

CREATE

[Date]

Page 19: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

18

TABLE Rental_Copy

(

Rental_Rental_ID NUMERIC (6) NOT NULL ,

Copies_Copy_ID NUMERIC (6) NOT NULL ,

Copies_Movie_ID NUMERIC (6) NOT NULL ,

CONSTRAINT Relation_2__IDX PRIMARY KEY CLUSTERED (Rental_Rental_ID,

Copies_Copy_ID, Copies_Movie_ID)

WITH

(

ALLOW_PAGE_LOCKS = ON ,

ALLOW_ROW_LOCKS = ON

)

ON "default"

)

ON "default"

GO

ALTER TABLE Copies

ADD CONSTRAINT Copies_Movie_FK FOREIGN KEY

(

Movie_Movie_ID

)

REFERENCES Movie

(

Movie_ID

)

ON

DELETE

NO ACTION ON

[Date]

Page 20: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

19

UPDATE NO ACTION

GO

ALTER TABLE Rental_Copy

ADD CONSTRAINT FK_ASS_2 FOREIGN KEY

(

Rental_Rental_ID

)

REFERENCES Rental

(

Rental_ID

)

ON

DELETE

NO ACTION ON

UPDATE NO ACTION

GO

ALTER TABLE Rental_Copy

ADD CONSTRAINT FK_ASS_3 FOREIGN KEY

(

Copies_Copy_ID,

Copies_Movie_ID

)

REFERENCES Copies

(

Copy_ID ,

Movie_Movie_ID

)

[Date]

Page 21: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

20

ON

DELETE

NO ACTION ON

UPDATE NO ACTION

GO

ALTER TABLE Movie_Actors

ADD CONSTRAINT FK_ASS_5 FOREIGN KEY

(

Movie_Movie_ID

)

REFERENCES Movie

(

Movie_ID

)

ON

DELETE

NO ACTION ON

UPDATE NO ACTION

GO

ALTER TABLE Movie_Actors

ADD CONSTRAINT FK_ASS_6 FOREIGN KEY

(

Actors_Actors_ID

)

REFERENCES Actors

(

Actors_ID

[Date]

Page 22: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

21

)

ON

DELETE

NO ACTION ON

UPDATE NO ACTION

GO

ALTER TABLE Rental

ADD CONSTRAINT Rental_Member_FK FOREIGN KEY

(

Member_Member_ID

)

REFERENCES Member

(

Member_ID

)

ON

DELETE

NO ACTION ON

UPDATE NO ACTION

GO

-- Oracle SQL Developer Data Modeler Summary Report:

--

-- CREATE TABLE 7

-- CREATE INDEX 0

-- ALTER TABLE 6

-- CREATE VIEW 0

[Date]

Page 23: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

22

-- CREATE PACKAGE 0

-- CREATE PACKAGE BODY 0

-- CREATE PROCEDURE 0

-- CREATE FUNCTION 0

-- CREATE TRIGGER 0

-- ALTER TRIGGER 0

-- CREATE DATABASE 0

-- CREATE DEFAULT 0

-- CREATE INDEX ON VIEW 0

-- CREATE ROLLBACK SEGMENT 0

-- CREATE ROLE 0

-- CREATE RULE 0

-- CREATE PARTITION FUNCTION 0

-- CREATE PARTITION SCHEME 0

--

-- DROP DATABASE 0

--

-- ERRORS 0

-- WARNINGS 0

[Date]

Page 24: [ER MODELLING]  · Web view2015. 4. 15. · We would like to keep the rental date/time and the return date/time. All our tapes are due back the next day, so we don’t need to keep

23

Database in SQL Server

[Date]