+ All Categories
Home > Documents > CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner...

CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner...

Date post: 05-Jan-2016
Category:
Upload: dominic-campbell
View: 216 times
Download: 1 times
Share this document with a friend
26
CS 3630 Database Design and Implementation
Transcript
Page 1: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

CS 3630 Database Design and Implementation

Page 2: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Database Schema

Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…)

2

Page 3: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

In what order to create the tables?

Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…)

Will it work?YES!

Branch (Bno…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Viewing (Rno, Pno, ViewDate…) Renter (Rno…)

Will it work?NO!

3

Referential Integrity!

Page 4: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

In what order to drop the tables?

Viewing (Rno, Pno, ViewDate…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Renter (Rno…) Branch (Bno…)

Will it work?YES!

Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…)

Will it work?NO!

4

Referential Integrity!

Page 5: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Insert Rows

The PK must be unique

The foreign key value must exist in the parent table if FK is provided

FK can be null, meaning not known at the time

5

Page 6: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Insert Rows

Insert into Branch: no problem

Insert into Staff: Cannot insert a staff with Bno being ‘B123’ if Branch table has no ‘B123’ in Bno column

FK can be null, meaning not known at the time

Branch

Bno Phone …

B101

B205

Staff

Sno Phone Bno

SG100 B101

SG363 Null

SA200 B123B123

6

Page 7: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Delete Rows

No records in other tables referencing the record to be deleted.

7

Page 8: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Delete Rows

Delete from Staff: no problem

Delete from Branch: Delete branch 'B101' What about staff in 'B101‘?

Branch

Bno Phone …

B101B101

B205

Staff

Sno Phone Bno

SG100 B101

SG363 Null

SA200 B101

8

Page 9: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

ANSI SQL Solutions In ANSI SQL, there are five choices

• No Action Cannot delete • Set to Null • Set to Default • No Check No good• Cascade Delete all staff in 'B101' from Staff table when deleting

branch ‘B101’ Dangerous!

9

Page 10: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Oracle Solutions In Oracle, only two choices are implemented

• No Action

Cannot delete

• Cascade

Delete all staff in 'B101' from Staff table when deleting branch ‘B101’

• Set to Null: not implemented Set to Null: not implemented

• Set to Default : not implemented Set to Default : not implemented

• No Check : not implementedNo Check : not implemented

10

Page 11: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Update Record

Update table Staff ‘B101’ of SG100 to ‘B205’ New value must exist in Branch

Update table Branch ‘B101’ to ‘B303’ Five choices in ANSI SQL, only No Action is implemented in Oracle

Branch

Bno Phone …

B101

B205

Staff

Sno Phone Bno

SG100 B101

SG363 Null

SA200 B205

11

Page 12: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Integrity Rules: Constraints

• Column Constraints

• Table Constraints

12

Page 13: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Column Constraints

• Primary key Sno Char(4) Primary Key,• Alternate Key SSN Char(9) Unique,• Foreign Key BNo char(4) References Branch, -- when attribute has the same name BNo char(4) References Branch, -- even when FK and PK have different names BNo char(4) References Branch on Delete Cascade, -- Do NOT use “Foreign Key” in column constaints!

13

Page 14: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

References

• Only for Foreign Key• Cannot reference other attributes

• Even not AKs

14

Page 15: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Column Constraints

• Domain constraint

Salary Number Check (Salary > 10000 and Salary < 200000),

PType varchar2(6) Check (PType in ('House', 'Flat', 'Appt')),

-- Strings are in single quotes, NOT double quotes

Bno Char(4) Default 'B363' References Branch,

Rent Float Check (Rent Between 200 and 400),

-- between is Inclusive

• Required data

LName Varchar2(20) Not Null,

-- Can be specified only by column constraint

15

Page 16: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Column Constraints

Create table Staff (

SNo char(4) Primary Key,

Bno Char(4) Default 'B363' References Branch on Delete Cascade,

FName Varchar2(20) Not Null,

LName Varchar2(20) Not Null,

-- assuming functions DateDiff and Now

DOB Date Not Null Check (DateDiff(Year, Now, DOB) >= 16),

Salary Number Check (Salary Between 30000 and 100000),

SSN Char(9) Unique,

Tel_No Char(12));

-- Primary Key, Unique, References should be the last constraint for a column

-- Do not use Foreign key for column constraints, only use References

16

Page 17: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Table Constraints

• Constraints on one or more columns– Composite PK, AK, FK

• Cannot use Not Null in table constraints

17

Page 18: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Table Constraints

Create table Staff (

SNo char(4),

FName Varchar2(20) Not Null,

LName Varchar2(20) Not Null,

DOB Date,

Salary Number default,

BNo Char(4),

Tel_No Char(12),

SSN Char(11),

Constraint PK_Staff

Primary Key (SNo),

Constraint Range_of_Salary

Check (Salary between 30000 and 200000),

Unique (SSN),

Foreign Key (BNo) References Branch (BNo));18

Page 19: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Attribute Order in Foreign Key

-- Primary key (c1, c2) for TableA

Foreign Key (c1, c2) References TableA, -- Same order as PK

Foreign Key (c2, c1) References TableA(c2, c1), -- Different order from PK

Foreign Key (c2, c1) References TableA, -- Incorrect!

19

Page 20: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

-- Composite PK

Create Table Test3 (

C1 Char(5),

C2 Varchar2(50) ,

C3 Integer,

C4 Date,

Constraint Test3_PK

Primary Key (C1, C3));

Desc Test3

pause;

Create Table Test4 (

D1 VARCHAR2(25) Primary Key,

D2 Char(5),

D3 Integer,

Constraint Test4_FK

Foreign Key (D2, D3) references Test3);

Desc Test4;

Pause

Works.Works.20

Page 21: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

-- Composite PK

Create Table Test3 (

C1 Char(5),

C2 Varchar2(50) ,

C3 Integer,

C4 Date,

Constraint Test3_PK

Primary Key (C1, C3));

Desc Test3

pause;

Create Table Test4 (

D1 VARCHAR2(25) Primary Key,

D2 Char(5),

D3 Integer,

Constraint Test4_FK

Foreign Key (D3, D2) references Test3);

Desc Test4;

Pause

Does Not Work.Does Not Work.21

Page 22: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

-- Composite PK

Create Table Test3 (

C1 Char(5),

C2 Varchar2(50) ,

C3 Integer,

C4 Date,

Constraint Test3_PK

Primary Key (C1, C3));

Desc Test3

pause;

Create Table Test4 (

D1 VARCHAR2(25) Primary Key,

D2 Char(5),

D3 Integer,

Constraint Test4_FK

Foreign Key (D3, D2) references Test3(C3, C1));

Desc Test4;

Pause

Works.Works.22

Page 23: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

UserName_Lab7.sql------------------------------------------------

-- Name : Qi Yang

-- UserName : YangQ

-- Date : 03-26-14

-- Course : CS 3630

-- Description: Creating tables

-- Inserting records

------------------------------------------------

Drop Table test2;

Drop Table test1;

Create table test1 . . .

Desc Test1

Pause

Create Table Test2 . . .

Desc test2

Pause

Insert into test1 . . .

Commit;

Select *

From Test1;

. . . 23

Page 24: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Assignment7

Table names and column names must be exactly the same as specified.

24

Page 25: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

SQL Script File

• Using any text editor outside SQL*Plus

• File extension .SQL

UserName_Lab7.Sql• Multiple SQL commands

• Each command ends with ;

25

Page 26: CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Running Script File

• Using either Start or @

SQL> Start file_name_with_full_path

SQL> @file_name_with_full_path

• Use Arrow Keys to get previous commands!

26


Recommended