CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose...

76
CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Transcript of CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose...

Page 1: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

CMPE 226

Database SystemsSeptember 9 Class Meeting

Department of Computer EngineeringSan Jose State University

Fall 2015Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

2

Teams?

Page 3: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

3

Assignment #1

Construct a single MySQL database table that contains some data. Your choice of fields and data values.

Create an HTML page containing various types of input (text, checkboxes, radio buttons, etc.).

Create a PHP page that makes different queries of the database table based on the form data.

Dynamically generate a web page that displays the query results.

Page 4: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

4

Assignment #1, cont’d

Make screen shots of your web pages.

Create a “dump” of your database with the mysqldump command located in the XAMPP bin directory:

Note: No space between –p and the password. Example:

The output file create.sql will allow the graders to recreate your database table.

mysqldump –u username –ppassword dbname > create.sql

mysqldump -u supercoders -psesame supercoders > create.sql

Page 5: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

5

Assignment #1, cont’d

Create a zip file named after your team (e.g., supercoders.zip) containing:

Your .html and .php files. Output from the mysqldump command. Screen shots of your web pages.

Email to [email protected]

Subject line: CMPE 226 Assignment #1 team name

CC all team members.

Page 6: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

6

Assignment #1, cont’d

This is a team assignment.

One submission per team. Each team member receives the same score.

Due Friday, Sept. 11 at 11:59 PM.

Page 7: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

7

PHP Syntax

Very similar to C. End each statement with a semicolon.

Case sensitive: variables, constants, array keys class properties and constraints

Case insensitive: functions (pre-defined and user-defined) class constructors and methods reserved words

Page 8: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

8

PHP Variables

All variable names start with $.

PHP is a dynamically typed language. You don’t declare a variable’s type. A variable can be assigned a value of any type.

PHP data types scalar: integer, float, boolean, string array object resource NULL

Page 9: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

9

PHP Strings

Enclose a string with single or double quotes. Examples:

Variables embedded in a double-quoted string are evaluated:

But not:

"Hello, world!"'Hello, world!'"It's a nice day."'Define "string" for me.'"Define \"string\" please."

"The first name is $first."

'The first name is $first.'

Page 10: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

10

PHP String Operations

The string concatenation operator is .

Some string functions: strlen() strtoupper() strtolower() ucwords() capitalize the first letter of every word

Demo

$name = $last . ", " . $first;$name .= ", Esq.";

Page 11: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

11

Heredocs

Use a heredoc to avoid string quoting issues. Example: $first = "John";

$last = "Smith";

print <<<HERE<table border="1"> <tr> <td>First name:</td> <td>$first</td> </tr> <tr> <td> Last name:</td> <td>$last</td> </tr></table>HERE;

Must be on a line by itselfwith no indentation.

Demo

Page 12: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

12

PHP Constants

Name constants with all uppercase letters, by convention. Constants are not variables, so do not use $.

Examples

But not:

define (PI, 3.1415926);define (HOST_NAME, "localhost");print "Host name is " . HOST_NAME;

print "Host name is HOST_NAME";

Page 13: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

13

Two Kinds of PHP Arrays

Indexed array Indexes are integers.

Associative array Indexes are strings. key-value pairs, like a hash table.

Page 14: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

14

Creating PHP Indexed Arrays

Use the array() function:

Specify the first index value. Subsequent elements are indexed incrementally.

An array of sequential numbers:

$bands[] = "Beatles";$bands[] = "Rolling Stones";$bands[] = "Queen";

$bands = array("Beatles", "Rolling Stones", "Queen");

$bands = array(2=>"Beatles", "Rolling Stones", "Queen");

$values = range(5, 10);

Page 15: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

15

Creating PHP Associative Arrays

Use the array() function:

$states["CA"] = "California";$states["NY"] = "New York";$states["TX"] = "Texas";

$states = array( "CA" => "California", "NY" => "New York", "TX" => "Texas");

An associative array is like a hash table.

Page 16: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

16

Looping over Array Elements

Use the foreach statement:

Examples:

foreach ($arrayname as $variable) { … }

foreach ($arrayname as $key => $value) { … }

foreach ($bands as $bandName) { print $bandName;}

foreach ($states as $abbrev => $fullName) { print "State $fullName is abbreviated $abbrev";}

Demo

Page 17: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

17

Multidimensional Arrays

$north = array("ND" => "North Dakota", "MN" => "Minnesota");$south = array("TX" => "Texas", "FL" => "Florida");$east = array("NY" => "New York", "ME" => "Maine");$west = array("CA" => "California", "OR" => "Oregon");

$us = array( "N" => $north, "S" => $south, "E" => $east, "W" => $west);

Demo

Page 18: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

18

PHP Functions

Syntax for programmer-defined functions:

Examples:

A function can optionally return a value.

function name (optional arguments){ // statements in the body}

function doSomething() { … }function sayHello($first, $last) { … }function greet($name, $language = "English") { … }function calculate($input, &$output) { … }

return value;

Default value

Passed by reference

Page 19: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

19

Scope of PHP Variables

Variables have the scope of the PHP filein which they reside.

A programmer-defined function creates a scope for its variables. Variables defined in a function

cannot be accessed outside the function. Variables defined outside the function

are not accessible inside the function. Use the global statement inside a function

to access outside variables. Example: global $outsideVar;

Page 20: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

20

PHP Data Objects (PDO)

Create a database abstraction layer:

Postgres MySQL Oracle

PHP Data Objects (PDO)

PHP

query()

PDO documentation:http://php.net/manual/en/book.pdo.php

Page 21: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

21

PDO Examples

Create a new PDO object to represent the database connection.

Set the error mode attribute to throw an exception if there is an error.

// Connect to the database.$con = new PDO("mysql:host=localhost;dbname=supercoders", "supercoders", "sesame");$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Page 22: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

22

PDO Examples, cont’d

PDO::query() executes an SQL statement and returns a result set as a PDOStatement object.

PDOStatement::fetch() fetches the next row of the result set. PDO::FETCH_ASSOC returns the row as an

associative array indexed by column names.

// Fetch the database field names.$result = $con->query($query);$row = $result->fetch(PDO::FETCH_ASSOC);

Page 23: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

23

PDO Examples, cont’d

Extract the column (field) names of the fetched row to construct the header rowof the HTML table.

// Construct the header row of the HTML table.print " <tr>\n";foreach ($row as $field => $value) { print " <th>$field</th>\n";}print " </tr>\n";

Page 24: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

24

PDO Examples, cont’d

PDOStatement::setFetchMode sets the default fetch mode for this statement.

// Fetch the matching database table rows.$data = $con->query($query);$data->setFetchMode(PDO::FETCH_ASSOC); // Construct the HTML table row by row.foreach ($data as $row) { print " <tr>\n"; foreach ($row as $name => $value) { print " <td>$value</td>\n"; } print " </tr>\n";}

Page 25: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

25

Database System Architecture Database system: A computer-based system

that enables efficient interaction between users and information stored in a database.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 26: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

26

Steps to Develop a Database

It’s an iterative process!

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 27: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

27

Database Requirements

First and most critical step:Collect, define, and visualize requirements.

What data will the database hold and how? What will be the capabilities and functionalities

of the database?

Use the requirements to model and implement the database and to create the front-end applications.

Page 28: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

28

Conceptual Database Model

Visualize the requirements.

Use a conceptual data modeling technique. Example: Entity-relationship (ER) modeling

Implementation independent: No dependencies on the logic of a particular database management system (DBMS). Example DBMS: Oracle, MySQL, etc.

Blueprint for the logical model.

Page 29: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

29

Logical Database Model

Create the relational database model. Later: Non-relational NoSQL models.

It’s usually straightforward to map an ER model to a relational model.

Page 30: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

30

Physical Database Model

The physical model is the actual database implementation.

Use relational DBMS (RDBMS) software. Later: NoSQL systems

Structured Query Language (SQL) commands to create, delete, modify, and query database structures.

Page 31: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

31

Front-End Application Development

End users generally do not access the database directly.

Front-end applications

Access the database directly. Provide end users with safe application-oriented

interfaces to query and manipulate the data. Fulfill the end user’s requirements.

Page 32: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

32

Operational vs. Analytical Databases

Operational database

Supports day-to-day operational business needs. Contains operational (transactional) information.

Analytical database

Supports analytical business tasks. Contains analytical information.

Examples: usage patterns, sales trends, etc. Derived from operational information.

Often associated with data warehousing.

Page 33: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

33

Break

Page 34: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

34

Entities and Attributes

Entity

Represents a real-world concept. Examples: customer, product, store, event, etc.

Data that the database stores.

Attribute

Characteristic of an entity that the database stores. Examples (for a customer): name, address, id, etc.

A unique attribute of an entity has a value that is different for each entity instance.

Page 35: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

35

Entities and Attributes, cont’d

In an ER diagram (ERD), show an entity with a rectangle and its attributes with ovals. Underline the unique attribute.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 36: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

36

Entities and Attributes, cont’d

An entity can have multiple unique attributes. Each one is called a candidate key.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 37: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

37

Composite Attributes

A composite attribute is composed of several attributes. Parenthesize the name of the composite attribute.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 38: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

38

Composite Attributes, cont’d

An entity’s unique attribute can be composite.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 39: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

39

Multivalued Attributes

An entity instance can have multiple values for an attribute.

If the number of values is fixed, we can use a compositeattribute instead.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 40: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

40

Derived Attributes

The value of a derived attribute is not stored. It’s calculated from the values of the other attributes

and additional data such as the current date. Show with a dashed oval.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 41: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

41

Optional Attributes

An optional attribute does not always have to have a value. Indicate with (O).

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 42: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

42

Relationships

Each entity in an ER diagram must be related to at least one other entity.

Show a relationship with a diamond and connect the diamond to the entities that are part of the relationship.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 43: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

43

Relationship Cardinality

Show cardinality (how many instances of an entity) with symbols at the end of the relationship lines.

Maximum symbol closest to the entity.

Minimum symbol further away.

Zero, one, many

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 44: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

44

Relationship Cardinality, cont’d

Read each relationship in both directions in this order: rectangle diamond cardinality rectangle

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 45: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

45

Types of Relationships

One-to-one (1:1)

One-to-many (1:M)

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 46: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

46

Types of Relationships, cont’d

Many-to-many (M:N)

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 47: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

47

Exact Cardinalities

Indicate exact cardinalities with parenthesized minimum and maximum values. Example: (2, 6) Use M for a non-specific minimum or maximum.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 48: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

48

Relationship Attributes

An relationship can also have attributes.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 49: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

49

Unary Relationships

In a unary relationship, an entity is involved in a relationship with itself. One instance has a relationship with

another instance of the same entity. You can indicate the relationship role.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 50: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

50

Multiple Relationships

Two entities can have multiple relationships with each other.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 51: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

51

Weak Entities

A weak entity does not have its own unique attribute. It only has a partial key. Underline the partial key with dashes.

Therefore, it must be associated with an owner entity via an identifying relationship. Indicate the weak entity and the identifying

relationship with double borders.

The partial key and the owner attribute’s unique attribute uniquely identifies the weak entity.

Page 52: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

52

Weak Entities, cont’d

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 53: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

53

Associative Entities

An associative entity is an alternate way to depict a many-to-many (M:N) relationship.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 54: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

54

Associative Entities, cont’d

Associative entity for a unary M:N relationship.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 55: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

55

ER Diagram Example

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 56: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

56

Assignment #2

Create an ER diagram (ERD) for a database to enable a university to schedule classes during the school year.

The university consists of multiple schools (science, engineering, humanities, etc.).

Each school has a unique id and multiple departments.

Page 57: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

57

Assignment #2, cont’d

Each department has a unique id, a full name and an abbreviation (e.g., Computer Engineering and CMPE).

Each department schedules multiple classes.

A school year consists of multiple semesters(fall, spring, summer).

Each semester has 10-16 weeks.

Each class has one or more sections.

Page 58: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

58

Assignment #2, cont’d

Each class section has a label and a name (e.g., CMPE 226-04 Database Design).

The class label consists of the department abbreviation, a class number, and a section.

The class number is unique only within its department.

A class section is scheduled during a certain time period (e.g., 1800-2045) on certain days (MTWRF) during certain semesters.

Page 59: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

59

Assignment #2, cont’d

Each class section has a location (e.g, Clark 200), maximum capacity, and a calculated current enrollment.

Each location consists of a building name and room number.

The room number is unique only within its building.

Page 60: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

60

Assignment #2, cont’d

A class section has one instructor and at least 15 students.

A student can take any number of different classes, including none.

A student has a grade level (freshman, sophomore, junior, senior, or graduate).

A class section optionally has a grader.

A grader grades one or more class sections.

Page 61: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

61

Assignment #2, cont’d

Each person (instructor, student, grader) has an id, a name, a gender (M or F), an address, and zero or more phone numbers.

An address consists of a house number, street, optional apartment number, city, state, and zip code.

Page 62: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

62

Assignment #2, cont’d

Or, you can create an ER diagram for your team database project instead (the preferred option). Minimum requirements:

At least 8 entities including at least one weak entity Different types of relationships and cardinalities Optional and derived attributes Multivalued attributes (e.g., phone numbers) Composite attributes (e.g., address) Some hierarchical data

(e.g., university school department)

Page 63: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

63

Assignment #2, cont’d

If your ER diagram is for your team database project, you must also list your database’s requirements.

This ER diagram will be your preliminary design. You can make changes during the semester.

Use standard Chen ER notation from the textbook and these slides.

Free ERDPlus drawing tool: https://erdplus.com/#/

Page 64: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

64

Assignment #2, cont’d

Turn in a zip file containing:

A screen shot, PDF, or JPEG, etc. of your ERD. Your database requirements

(if the ERD is for your team database project) Name the zip file after your team, e.g. Supers.zip

Page 65: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

65

Assignment #2, cont’d

Email as an attachment to [email protected] Subject: CMPE 226 Assignment #2 Team Name

One email per team. CC all team members

Due Tuesday, September 15 at 11:59 PM

Page 66: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

66

Logical Database Model

Map the ER diagram to a logical model represented as a relational schema.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 67: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

67

Conditions for a Table to be a Relation

Each column must have a name. Within a table, each column name must be unique.

All values in each column must be from the same (predefined) domain.

Within a table, each row must be unique.

Within each row, each value in each column must be single-valued. Multiple values of the content represented by the

column are not allowed in any rows of the table.

Page 68: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

68

Relational vs. Non-Relational Tables

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 69: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

69

Additional Properties for a Relational Table

The order of columns is irrelevant.

The order of rows is irrelevant.

Page 70: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

70

Primary Key

Each relation must have a primary key. A column or set of columns whose value

uniquely identifies each row. Underline the primary key of the relational table.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 71: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

71

Mapping Entities

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 72: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

72

Mapping Entities, cont’d

Entity with a composite attribute.

As mapped.

As seen by a front-end application.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 73: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

73

Mapping Entities, cont’d

Attribute with a composite primary key.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 74: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

74

Mapping Entities, cont’d

Entity with an optional attribute.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 75: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

75

Entity Integrity Constraint

No primary key column of a relational table can have null (empty) values.

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6

Page 76: CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Engineering Dept.Fall 2015: September 9

CMPE 226: Database Systems© R. Mak

76

Entity Integrity Constraint, cont’d

Database Systemsby Jukić, Vrbsky, & NestorovPearson 2014ISBN 978-0-13-257567-6