Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The...

104
Chapter 7 The Entity-Relationship Model Informática y Comunicaciones The Entity-Relationship Model KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall The Entity-Relationship Model 7-1

Transcript of Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The...

Page 1: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Chapter 7The Entity-Relationship Model

Informática y Comunicaciones

The Entity-Relationship Model

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall The Entity-Relationship Model 7-1

Page 2: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Chapter 7: outline

7.1 SQL

7.2 The Entity-Relationship Model

The Entity-Relationship Model 7-2

Page 3: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Structured Query Language

� Structured Query Language� Acronym: SQL

� Pronounced as “S-Q-L” [“Ess-Que-El”]

� Originally developed by IBM as the SEQUEL language in the 1970sin the 1970s

� SQL-92 is an ANSI national standard adopted in 1992.

� SQL:2008 is current standard.

The Entity-Relationship Model 7-3

Page 4: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL Defined

� SQL is not a programming language, but rather a data sublanguage.

� SQL is comprised of� A data definition language (DDL)

• Used to define database structures• Used to define database structures

� A data manipulation language (DML)• Data definition and updating• Data retrieval (Queries)

The Entity-Relationship Model 7-4

Page 5: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Definition

� The SQL data definition statements include:� CREATE

• To create database objects

� ALTER• To modify the structure and/or characteristics • To modify the structure and/or characteristics of database objects

� DROP• To delete database objects

� TRUNCATE• To delete table data while keeping structure

The Entity-Relationship Model 7-5

Page 6: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Definition: CREATE

� Creating database tables� The SQL CREATE TABLE statement

CREATE TABLE EMPLOYEE(EmpID Integer PRIMARY KEY,EmpID Integer PRIMARY KEY,EmpName Char(25) NOT NULL);

The Entity-Relationship Model 7-6

Page 7: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Definition:

CREATE with CONSTRAINT I

� Creating database tables with PRIMARY KEY constraints� The SQL CREATE TABLE statement

� The SQL CONSTRAINT keyword

CREATE TABLE EMPLOYEE(EmpID Integer NOT NULL,EmpName Char(25) NOT NULLCONSTRAINT Emp_PK PRIMARY KEY(EmpID));

The Entity-Relationship Model 7-7

Page 8: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Definition:

CREATE with CONSTRAINT II

� Creating database tables with composite primary keys using PRIMARY KEY constraints� The SQL CREATE TABLE statement

� The SQL CONSTRAINT keyword

CREATE TABLE EMP_SKILL(

EmpID Integer NOT NULL,

SkillID Integer NOT NULL,

SkillLevel Integer NULL,

CONSTRAINT EmpSkill_PK PRIMARY KEY

(EmpID, SkillID)

);

The Entity-Relationship Model 7-8

Page 9: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Definition:

CREATE with CONSTRAINT III

� Creating database tables using PRIMARY KEY and FOREIGN KEY constraints� The SQL CREATE TABLE statement� The SQL CONSTRAINT keyword

CREATE TABLE EMP_SKILL(EmpID Integer NOT NULL,SkillID Integer NOT NULL,SkillLevel Integer NULL,CONSTRAINT EmpSkill_PK PRIMARY KEY

(EmpID, SkillID),CONSTRAINT Emp_FK FOREIGN KEY(EmpID)

REFERENCES EMPLOYEE(EmpID),CONSTRAINT Skill_FK FOREIGN KEY(SkillID)

REFERENCES SKILL(SkillID));

The Entity-Relationship Model 7-9

Page 10: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Definition:

CREATE with CONSTRAINT IV

� Creating database tables using PRIMARY KEY and FOREIGN KEY constraints� The SQL CREATE TABLE statement� The SQL CONSTRAINT keyword� ON UPDATE CASCADE and ON DELETE CASCADE

CREATE TABLE EMP_SKILL(EmpID Integer NOT NULL,EmpID Integer NOT NULL,SkillID Integer NOT NULL,SkillLevel Integer NULL,CONSTRAINT EmpSkill_PK PRIMARY KEY(EmpID, SkillI D),CONSTRAINT Emp_FK FOREIGN KEY(EmpID)

REFERENCES EEMPLOYEE(EmpID)ON DELETE CASCADE,

CONSTRAINT Skill_FK FOREIGN KEY(SkillID)REFERENCES SKILL(SkillID)

ON UPDATE CASCADE);

The Entity-Relationship Model 7-10

Page 11: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Process SQL CREATE TABLE Statements:

Microsoft SQL Server 2012

The Entity-Relationship Model 7-11

Page 12: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Process SQL CREATE TABLE Statements:

Oracle Database 11g Release 2

The Entity-Relationship Model 7-12

Page 13: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Process SQL CREATE TABLE Statements:

Oracle MySQL 5.5

The Entity-Relationship Model 7-13

Page 14: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Database Diagram in theMicrosoft SQL Server Management Studio

The Entity-Relationship Model 7-14

Page 15: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Primary Key Constraint:

ALTER I

� Adding primary key constraints to an existing table� The SQL ALTER statement

ALTER TABLE EMPLOYEEADD CONSTRAINT Emp_PK PRIMARY KEY(EmpID);

The Entity-Relationship Model 7-15

Page 16: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Composite Primary Key Constraints: ALTER II

� Adding a composite primary key constraint to an existing table� The SQL ALTER statement

ALTER TABLE EMP_SKILLALTER TABLE EMP_SKILLADD CONSTRAINT EmpSkill_PK

PRIMARY KEY(EmpID, SkillID);

The Entity-Relationship Model 7-16

Page 17: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Foreign Key Constraint:

ALTER III

� Adding foreign key constraints to an existing table� The SQL ALTER statement

ALTER TABLE EMPLOYEE ADDCONSTRAINT Emp_FK FOREIGN KEY(DeptID)CONSTRAINT Emp_FK FOREIGN KEY(DeptID)

REFERENCES DEPARTMENT(DeptID);

The Entity-Relationship Model 7-17

Page 18: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Adding Data:

INSERT

� To add a row to an existing table, use the INSERT statement.

� Non-numeric data must be enclosed in straight ( ' ) single quotes.

INSERT INTO EMPLOYEE VALUES(91, 'Smither', 12);

INSERT INTO EMPLOYEE (EmpID, SalaryCode)

VALUES (62, 11);

The Entity-Relationship Model 7-18

Page 19: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Queries

� SELECT is the best known SQL statement.

� SELECT will retrieve information from the database that matches the specified criteria using the SELECT/FROM/WHERE framework.the SELECT/FROM/WHERE framework.

SELECT EmpName

FROM EMPLOYEEWHERE EmpID = 2010001;

The Entity-Relationship Model 7-19

Page 20: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:The Results of a Query Is a Relation

� A query pulls information from one or more relations and creates (temporarily) a new relation.

This allows a query to: � This allows a query to: � Create a new relation

� Feed information to another query (as a “sub-query”)

The Entity-Relationship Model 7-20

Page 21: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:Displaying All Columns

� To show all of the column values for the rows that match the specified criteria, use an asterisk ( * ).

SELECT *

FROM EMPLOYEE;

The Entity-Relationship Model 7-21

Page 22: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Showing Each Row Only Once

� The DISTINCT keyword may be added to the SELECT statement to inhibit duplicate rows from displaying.

SELECT DISTINCT DeptIDFROM EMPLOYEE;

The Entity-Relationship Model 7-22

Page 23: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Specifying Search Criteria

� The WHERE clause stipulates the matching criteria for the record that is to be displayed.

SELECT EmpNameFROM EMPLOYEEWHERE DeptID = 15;

The Entity-Relationship Model 7-23

Page 24: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Processing SQL Query Statements:

Microsoft SQL Server 2012

The Entity-Relationship Model 7-24

Page 25: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Processing SQL Query Statements:

Oracle Database 11g Release 2

The Entity-Relationship Model 7-25

Page 26: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Processing SQL Query Statements:

Oracle MySQL 5.5

The Entity-Relationship Model 7-26

Page 27: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:Match Criteria

� The WHERE clause match criteria may include� Equals “=”

� Not Equals “<>”

� Greater than “>”� Greater than “>”

� Less than “<”

� Greater than or Equal to “>=”

� Less than or Equal to “<=”

The Entity-Relationship Model 7-27

Page 28: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Match Operators

� Multiple matching criteria may be specified using� AND

• Representing an intersection of the data sets

� OR� OR

• Representing a union of the data sets

The Entity-Relationship Model 7-28

Page 29: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Operator Examples

SELECT EmpNameFROM EMPLOYEEWHERE DeptID < 7

OR DeptID > 12;

SELECT EmpNameFROM EMPLOYEEWHERE DeptID = 9

AND SalaryCode <= 23;

The Entity-Relationship Model 7-29

Page 30: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

A List of Values

� The WHERE clause may include the IN keyword to specify that a particular column value must be included in a list of values.

SELECT EmpName

FROM EMPLOYEE

WHERE DeptID IN (4, 8, 9);

The Entity-Relationship Model 7-30

Page 31: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:The Logical NOT Operator

� Any criteria statement may be preceded by a NOT operator, which is to say that all information will be shown except that information matching the specified criteria

SELECT EmpName

FROM EMPLOYEE

WHERE DeptID NOT IN (4, 8, 9);

The Entity-Relationship Model 7-31

Page 32: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:Finding Data in a Range of Values

� SQL provides a BETWEEN keyword that allows a user to specify a minimum and maximum value on one line.

SELECT EmpName

FROM EMPLOYEE

WHERE SalaryCode BETWEEN 10 AND 45;

The Entity-Relationship Model 7-32

Page 33: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:Allowing for Wildcard Searches

� The SQL LIKE keyword allows searches on partial data values.

� LIKE can be paired with wildcards to find rows matching a string value.matching a string value.� Multiple character wildcard character is a percent sign (%).

� Single character wildcard character is an underscore (_).

The Entity-Relationship Model 7-33

Page 34: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Wildcard Search Examples

SELECT EmpID

FROM EMPLOYEE

WHERE EmpName LIKE 'Kr%';

SELECT EmpID

FROM EMPLOYEE

WHERE Phone LIKE '616-___-____';

The Entity-Relationship Model 7-34

Page 35: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Sorting the Results

� Query results may be sorted using the ORDER BY clause.

SELECT *SELECT *

FROM EMPLOYEE

ORDER BY EmpName;

The Entity-Relationship Model 7-35

Page 36: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Built-in SQL Functions

� SQL provides several built-in functions: � COUNT

• Counts the number of rows that match the specified criteria

� MIN� MIN• Finds the minimum value for a specific column for those rows matching the criteria

� MAX• Finds the maximum value for a specific column for those rows matching the criteria

The Entity-Relationship Model 7-36

Page 37: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:Built-in SQL Functions (Cont’d)

� SUM� Calculates the sum for a specific column for those rows matching the criteria

� AVG� AVG� Calculates the numerical average of a specific column for those rows matching the criteria

The Entity-Relationship Model 7-37

Page 38: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Built-in Function Examples

SELECT COUNT(DeptID)

FROM EMPLOYEE;

SELECT MIN(Hours) AS MinimumHours,

MAX(Hours) AS MaximumHours,

AVG(Hours) AS AverageHours

FROM PROJECT

WHERE ProjID > 7;

The Entity-Relationship Model 7-38

Page 39: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Providing Subtotals: GROUP BY

� Subtotals may be calculated by using the GROUP BY clause.

� The HAVING clause may be used to restrict which data is displayed.which data is displayed.

SELECT DeptID,COUNT(*) AS NumOfEmployees

FROM EMPLOYEE

GROUP BY DeptID

HAVING COUNT(*) > 3;

The Entity-Relationship Model 7-39

Page 40: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:Retrieving Information from Multiple Tables

� Subqueries� As stated earlier, the result of a query is a relation. As a result, a query may feed another query. This is called a subquery.

� Joins� Joins� Another way of combining data is by using a join .

• Join [also called an Inner Join]

• Left Outer Join

• Right Outer Join

The Entity-Relationship Model 7-40

Page 41: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Subquery Example

SELECT EmpName

FROM EMPLOYEE

WHERE DeptID in WHERE DeptID in

(SELECT DeptID

FROM DEPARTMENT

WHERE DeptName LIKE 'Account%');

The Entity-Relationship Model 7-41

Page 42: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

Join Example

SELECT EmpName

FROM EMPLOYEE AS E, DEPARTMENT AS D

WHERE E.DeptID = D.DeptID

AND D.DeptName LIKE ' Account% ' ;AND D.DeptName LIKE ' Account% ' ;

The Entity-Relationship Model 7-42

Page 43: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

JOIN…ON Example

� The JOIN…ON syntax can be used in joins.

� It has the advantage of moving the JOIN syntax into the FROM clause.

SELECT EmpName

FROM EMPLOYEE AS E JOIN DEPARTMENT AS D

ON E.DeptID = D.DeptID

WHERE D.DeptName LIKE 'Account%';

The Entity-Relationship Model 7-43

Page 44: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

LEFT OUTER JOIN Example

� The OUTER JOIN syntax can be used to obtain data that exists in one table without matching data in the other table.

SELECT EmpName

FROM EMPLOYEE AS E

LEFT JOIN DEPARTMENT AS D

ON E.DeptID = D.DeptID

WHERE D.DeptName LIKE 'Account%';

The Entity-Relationship Model 7-44

Page 45: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL for Data Retrieval:

RIGHT OUTER JOIN Example

� The unmatched data displayed can be from either table, depending on whether RIGHT JOIN or LEFT JOIN is used.

SELECT EmpName

FROM EMPLOYEE AS E

RIGHT JOIN DEPARTMENT AS D

ON E.DeptID = D.DeptID

WHERE D.DeptName LIKE 'Account%';

The Entity-Relationship Model 7-45

Page 46: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data using SQL

� Insert� Will add a new row in a table (already discussed above)

� Update� Will update the data in a table that matches the � Will update the data in a table that matches the specified criteria

� Delete� Will delete the data in a table that matches the specified criteria

The Entity-Relationship Model 7-46

Page 47: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data using SQL:

Changing Data Values: UPDATE

� To change the data values in an existing row (or set of rows) use the Update statement.

UPDATE EMPLOYEE

SET Phone ' 791 - 555 - 1234 'SET Phone ' 791 - 555 - 1234 '

WHERE EmpID = 29;

UPDATE EMPLOYEE

SET DeptID = 44

WHERE EmpName LIKE 'Kr%';

The Entity-Relationship Model 7-47

Page 48: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data using SQL:

MERGE

� SQL:2003 introduced the MERGE statement.� Combines INSERT and UPDATE into one statement

� Uses the equivalent of IF-THEN-ELSE logic to decide whether to use INSERT or UPDATE

� An advanced feature—learn to use INSERT and � An advanced feature—learn to use INSERT and UPDATE separately first, then consult DBMS documentation

The Entity-Relationship Model 7-48

Page 49: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data using SQL:

Deleting Data: DELETE

� To delete a row or set of rows from a table use the DELETE statement.

DELETE FROM EMPLOYEE

WHERE EmpID = 29;WHERE EmpID = 29;

DELETE FROM EMPLOYEE

WHERE EmpName LIKE ' Kr%' ;

The Entity-Relationship Model 7-49

Page 50: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data using SQL:

Deleting Database Objects: DROP

� To remove unwanted database objects from the database, use the SQL DROP statement.

� Warning… The DROP statement will permanently remove the object and all data.

DROP TABLE EMPLOYEE;

The Entity-Relationship Model 7-50

Page 51: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data using SQL:Removing a Constraint: ALTER & DROP

� To change the constraints on existing tables, you may need to remove the existing constraints before adding new constraints.

ALTER TABLE EMPLOYEE DROP CONSTRAINT EmpFK;

The Entity-Relationship Model 7-51

Page 52: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Modifying Data Using SQL:

The CHECK Constraint

� The CHECK constraint can be used to create sets of values to restrict the values that can be used in a column.

ALTER TABLE PROJECTADD CONSTRAINT PROJECT_Check_Dates

CHECK (StartDate < EndDate);

The Entity-Relationship Model 7-52

Page 53: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

SQL Views

� A SQL View is a virtual table created by a DBMS-stored SELECT statement that can combine access to data in multiple tables and even in other views.

� SQL views are discussed online in Appendix E.� SQL views are discussed online in Appendix E.

The Entity-Relationship Model 7-53

Page 54: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Chapter 7: outline

7.1 SQL

7.2 The Entity-Relationship Model

The Entity-Relationship Model 7-54

Page 55: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Three Stages ofDatabase Development

� The three stages of database development are:� Requirements Analysis Stage

� Component Design Stage� Component Design Stage

� Implementation Stage

The Entity-Relationship Model 7-55

Page 56: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

The Requirements Analysis Stage

� Sources of requirements� User Interviews

� Forms

� Reports

� Queries� Queries

� Use Cases

� Business Rules

The Entity-Relationship Model 7-56

Page 57: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Requirements Become the E-R Data Model

� After the requirements have been gathered, they are transformed into an Entity Relationship (E-R) Data Model.

� The most important elements of E-R Models are:� The most important elements of E-R Models are:� Entities

� Attributes

� Identifiers

� Relationships

The Entity-Relationship Model 7-57

Page 58: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Entity Class versus Entity Instance

� An entity class is a description of the structure and format of the occurrences of the entity.

� An entity instance is a specific occurrence of an entity within an entity class.entity within an entity class.

The Entity-Relationship Model 7-58

Page 59: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Entity Class and Entity Instance

The Entity-Relationship Model 7-59

Page 60: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Attributes

� Entities have attributes that describe the entity’s characteristics:� ProjectName

� StartDate

� ProjectType� ProjectType

� ProjectDescription

� Attributes have a data type and properties.

The Entity-Relationship Model 7-60

Page 61: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Identifiers

� Entity instances have identifiers.

� An identifier will identify a particular instance in the entity class:� SocialSecurityNumber

� StudentID� StudentID

� EmployeeID

The Entity-Relationship Model 7-61

Page 62: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Identifier Types

� Uniqueness� Identifiers may be unique or nonunique.

� If the identifier is unique, the data value for the identifier must be unique for all instances.

� Composite� Composite� A composite identifier consists of two or more attributes.

• E.g., OrderNumber & LineItemNumber are both required.

The Entity-Relationship Model 7-62

Page 63: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Levels of Entity Attribute Display

The Entity-Relationship Model 7-63

Page 64: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Relationships

� Entities can be associated with one another in relationships.

� Relationship degree defines the number of entity classes participating in the relationship:� Degree 2 is a binary relationship.� Degree 2 is a binary relationship.

� Degree 3 is a ternary relationship.

The Entity-Relationship Model 7-64

Page 65: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Degree 2 Relationship: Binary

The Entity-Relationship Model 7-65

Page 66: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Degree 3 Relationship: Ternary

The Entity-Relationship Model 7-66

Page 67: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

One-to-One Binary Relationship

� 1:1 (one-to-one)� A single entity instance in one entity class is related to a single entity instance in another entity class.

• An employee may have no more than one locker; and and

• A locker may only be accessible by one employee

The Entity-Relationship Model 7-67

Page 68: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

One-to-Many Binary Relationship

� 1:N (one-to-many)� A single entity instance in one entity class is related to many entity instances in another entity class.

• A quotation is associated with only one item; and

• An item may have several quotations• An item may have several quotations

The Entity-Relationship Model 7-68

Page 69: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Many-to-Many Binary Relationship

� N:M (many-to-many)� Many entity instances in one entity class is related to many entity instances in another entity class:

• a supplier may supply several items; and

• a particular item may be supplied by several suppliers.

• a particular item may be supplied by several suppliers.

The Entity-Relationship Model 7-69

Page 70: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Maximum Cardinality

� Relationships are named and classified by their cardinality, which is a word that means count.

� Each of the three types of binary relationships shown above have different maximum cardinalities.

� Maximum cardinality is the maximum number of � Maximum cardinality is the maximum number of entity instances that may participate in a relationship instance—one, many, or some other fixed number.

The Entity-Relationship Model 7-70

Page 71: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Minimum Cardinality

� Minimum cardinality is the minimum number of entity instances that must participate in a relationship instance.

� These values typically assume a value of zero (optional) or one (mandatory).(optional) or one (mandatory).

The Entity-Relationship Model 7-71

Page 72: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Cardinality Example

� Maximum cardinality is many for both ITEM and SUPPLIER.

� Minimum cardinality is zero (optional) for ITEM and one (mandatory) SUPPLIER.� A SUPPLIER does not have to supply an ITEM.

� An ITEM must have a SUPPLIER.� An ITEM must have a SUPPLIER.

The Entity-Relationship Model 7-72

Page 73: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Entity-Relationship Diagrams

� The diagrams in previous slides are called entity-relationship diagrams.� Entity classes are shown by rectangles.

� Relationships are shown by diamonds.

� The maximum cardinality of the relationship is shown � The maximum cardinality of the relationship is shown inside the diamond.

� The minimum cardinality is shown by the oval or hash mark next to the entity.

� The name of the entity is shown inside the rectangle.

� The name of the relationship is shown near the diamond.

The Entity-Relationship Model 7-73

Page 74: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

HAS-A Relationships

� The relationships in the previous slides are called HAS-A relationships.

� The term is used because each entity instance has a relationship to a second entity instance:� An employee has a badge.� An employee has a badge.

� A badge has an employee.

The Entity-Relationship Model 7-74

Page 75: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Types ofEntity-Relationship Diagrams

� Information Engineering (IE) [James Martin 1990]—Uses “crow’s feet” to show the many sides of a relationship, and it is sometimes called the crow’s foot model.

� Integrated Definition 1, Extended 3 (IDEF1X) � Integrated Definition 1, Extended 3 (IDEF1X)

� is a version of the E-R model that is a national standard.

� Unified Modeling Language (UML) is a set of structures and techniques for modeling and designing object-oriented programs (OOP) and applications

The Entity-Relationship Model 7-75

Page 76: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Crow’s Foot Example:One-to-Many Relationship

The Entity-Relationship Model 7-76

Page 77: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Crow’s Foot Symbols

The Entity-Relationship Model 7-77

Page 78: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Crow’s Foot Example:Many-to-Many Relationship

The Entity-Relationship Model 7-78

Page 79: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Weak Entity

� A weak entity is an entity that cannot exist in the database without the existence of another entity.

� Any entity that is not a weak entity is called a strong entity.

The Entity-Relationship Model 7-79

Page 80: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

ID-Dependent Weak Entities

� An ID-Dependent weak entity is a weak entity that cannot exist without its parent entity.

� An ID-dependent weak entity has a composite identifier.� The first part of the identifier is the identifier for the � The first part of the identifier is the identifier for the strong entity.

� The second part of the identifier is the identifier for the weak entity itself.

The Entity-Relationship Model 7-80

Page 81: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

ID-Dependent Weak Entity Examples

The Entity-Relationship Model 7-81

Page 82: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Weak Entity Relationships

� The relationship between a strong and weak entity is termed an identifying relationship if the weak entity is ID-dependent.� Represented by a solid line

� The relationship between a strong and weak � The relationship between a strong and weak entity is termed a nonidentifying relationship if the weak entity is non-ID-dependent.� Represented by a dashed line

� Also used between strong entities

The Entity-Relationship Model 7-82

Page 83: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Weak Entity Identifier:Non-ID-dependent

� All ID-dependent entities are weak entities, but there are other entities that are weak but not ID-dependent.

� A non-ID-dependent weak entity may have a � A non-ID-dependent weak entity may have a single or composite identifier, but the identifier of the parent entity will be a foreign key.

The Entity-Relationship Model 7-83

Page 84: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Non-ID-Dependent Weak Entity Examples

The Entity-Relationship Model 7-84

Page 85: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Strong and Weak Entity Examples

The Entity-Relationship Model 7-85

Page 86: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Subtype Entities

� A subtype entity is a special case of another entity called supertype.

� An attribute of the supertype may be included that indicates which of the subtypes is appropriate for a given instance; this attribute is called a discriminator.

� Subtypes can be exclusive or inclusive.� Subtypes can be exclusive or inclusive.� If exclusive, the supertype relates to at most one subtype.

� If inclusive, the supertype can relate to one or more subtypes.

The Entity-Relationship Model 7-86

Page 87: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Subtype Entity Identifiers

� The relationships that connect supertypes and subtypes are called IS-A relationships because a subtype is the same entity as the supertype.

� The identifier of a supertype and all of its subtypes is the same attribute.subtypes is the same attribute.

The Entity-Relationship Model 7-87

Page 88: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Subtype Entity Examples

The Entity-Relationship Model 7-88

Page 89: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Recursive Relationships

� It is possible for an entity to have a relationship to itself—this is called a recursive relationship.

The Entity-Relationship Model 7-89

Page 90: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Developing an E-R Diagram

� Heather Sweeney Designs will be used as an ongoing example throughout Chapters 4, 5, 6, and 7.� Heather Sweeney is an interior designer who specializes in home kitchen design.specializes in home kitchen design.

� She offers a variety of free seminars at home shows, kitchen and appliance stores, and other public locations.

� She earns revenue by selling books and videos that instruct people on kitchen design.

� She also offers custom-design consulting services.

The Entity-Relationship Model 7-90

Page 91: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

The Seminar Customer List

The Entity-Relationship Model 7-91

Page 92: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Initial E-R Diagram I

The Entity-Relationship Model 7-92

Page 93: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Initial E-R Diagram II

The Entity-Relationship Model 7-93

Page 94: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Initial E-R Diagram III

The Entity-Relationship Model 7-94

Page 95: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

The Customer Form Letter

The Entity-Relationship Model 7-95

Page 96: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Data Model with CONTACT

The Entity-Relationship Model 7-96

Page 97: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:Data Model with CONTACT as Weak Entity

The Entity-Relationship Model 7-97

Page 98: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:Data Model with Modified CUSTOMER

The Entity-Relationship Model 7-98

Page 99: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:Sales Invoice

The Entity-Relationship Model 7-99

Page 100: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Data Model with INVOICE

The Entity-Relationship Model 7-100

Page 101: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Data Model with LINE_ITEM

The Entity-Relationship Model 7-101

Page 102: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:

Final Data Model

The Entity-Relationship Model 7-102

Page 103: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

Heather Sweeney Designs:Business Rules and Model Validation

� Business rules may constrain the model and need to be recorded.� Heather Sweeney Designs has a business rule that no more than one form letter or email per day is to be sent to a customer.sent to a customer.

� After the data model has been completed, it needs to be validated.� Prototyping is commonly used to validate forms and reports.

The Entity-Relationship Model 7-103

Page 104: Informática y Comunicaciones Chapter 7 The Entity ... · Chapter 7: outline 7.1 SQL 7.2 The Entity-Relationship Model The Entity-Relationship Model7-2

� Structured Query Language� data definition

� keys

� data retrieval

� modifying data

� The Entity-Relationship Model

Chapter 7: summary

� The Entity-Relationship Model� analysis of requirements

� component design

� implementation

The Entity-Relationship Model 7-104