cit 381 er basics - relationship types - foreign keys - er studio

17
CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Post on 22-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

CIT 381

ER Basics

-relationship types-foreign keys-ER Studio

Page 2: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Entities A concrete item we wish to represent in our database.

Examples: books, cars, employees, wombats, accounts, offices.

Completely described by its attributes.

Page 3: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Relationship An association between two or more entities.

Orders is a relationship between entities Customers and Books.

Assign is a relationship between Cars and Employees.

Page 4: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

First Step: Design Entities What are the important objects that we want to keep information for?

Example: cars, employees, customers,…

For each of these objects, write down all of the attributes we will need to keep.

Example: cars - licenseNum, vin, make, model, year, odometerRead

Page 5: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Example Entities

car_license_num

car_vincar_makecar_modelcar_yearcar_color

emp_ssn

emp_fnameemp_lnameemp_addressemp_cityemp_stateemp_zipemp_home_phone

primary keyscars employees

The primary key is the chosen identifier. At this stage, all other attributes belong solely to that entity. (Foreign keys come later.)

Page 6: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Next Step: Relationships Cars are assigned to employees (or vice versa)

assign is a relationship between cars and employees

car_license_num

car_vincar_makecar_modelcar_yearcar_color

emp_ssn

emp_fnameemp_lnameemp_addressemp_cityemp_stateemp_zipemp_home_phone

cars employees

assign

Page 7: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Relationship Cardinality Relationships can be

one-to-many (1:N) many-to-one (N:1) many-to-many (N:M) one-to-one (1:1)

Important to know the cardinality of each relationship as it affects the table design.

Page 8: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

One-to-many Suppose each car can be assigned to more than one employee, but each employee has exactly one car.

cars

car_license-num

car_VIN

car_make

car_model

car_year

car_color

employees

emp_ssn

car_license-num (FK)

emp_fname

emp_lname

emp_address

emp_city

emp_state

emp_zip

emp_home_phone

one car to … many employees(dot = “many”)

Page 9: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

One-to-many (optional) Same as previous, but an employee does not have to have a car – each employee has zero or one cars.

cars

car_license-num

car_VIN

car_make

car_model

car_year

car_color

employees

emp_ssn

car_license-num (FK)

emp_fname

emp_lname

emp_address

emp_city

emp_state

emp_zip

emp_home_phone

The open diamond indicates an optional relationship: an employee does not have to have a car.This means that the car_license_num field in employees can be NULL (that is, blank).

Page 10: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Many-to-one All cars are assigned to one employee.

An employee can have more than one car.cars

car_license-num

emp_ssn (FK)

car_VIN

car_make

car_model

car_year

car_color

employees

emp_ssn

emp_fname

emp_lname

emp_address

emp_city

emp_state

emp_zip

emp_home_phone

many cars to … one employee

Page 11: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Many-to-Many A car can be assigned to many employees.

An employee can be assigned to many cars.

cars

car_license-num

car_VIN

car_make

car_model

car_year

car_color

employees

emp_ssn

emp_fname

emp_lname

emp_address

emp_city

emp_state

emp_zip

emp_home_phone

ER Studio calls this a non-specific mandatory relationship.

Page 12: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Resolving many-to-many A M:N relationship needs to be represented by its own table, sometimes called a bridging table.cars

car_license-num

car_VIN

car_make

car_model

car_year

car_color

employees

emp_ssn

emp_fname

emp_lname

emp_address

emp_city

emp_state

emp_zip

emp_home_phone

assign

car_license-num (FK)

emp_ssn (FK)

assign_date

Notice the solid lines and curved corners. It is a weak entity.

Page 13: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Weak entities A weak entity is one that does not have enough information in its attributes to form a key (unique identifier).

Consider a bank check: check number 3512 does not identify it (unless we know the account number).

Note that account number is an attribute of account, not of check.

Page 14: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Weak entity example (1)

custID identifies customer,accountNum identifies account,checkNum only partially identifies check

Page 15: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Weak entity example (2)

We added a mandatory one-to-many relationship, as before.

Page 16: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Weak entity example (3)

We insert an identifying relationship:solid linemakes foreign key (accountNum) part of the primary key of checkcurved corners on check indicate it is a weak entity

Page 17: CIT 381 ER Basics - relationship types - foreign keys - ER Studio

Another weak entity

From the company database, handed out in class.