Structured Query Language (SQL)

CS 3630
Database Design and Implementation
Assignment7
Table names and column names must be
exactly the same as specified.
Due Monday, March 27
2
SQL Script File
• Using any text editor outside SQL*Plus
• File extension .SQL
UserName_Lab7.Sql
• Multiple SQL commands
• Each command ends with ;
3
UserName_Lab7.sql
------------------------------------------------- Name
: Qi Yang
-- UserName
: YangQ
-- Date
: 03-21-2017
-- 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;
. . .
4
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!
5
Create a Table
Create Table Test1 (
C1
Char(5) Primary Key,
C2
Varchar2(50),
C3
Integer,
C4
Date);
6
Create another Table
Create Table Test1 (
C1
Char(5) Primary Key,
C2
Varchar2(50),
C3
Integer,
C4
Date);
Create Table
D1
D2
D3
Test2 (
VARCHAR2(15) Primary Key,
Char(5) references Test1,
Integer);
7
Drop Tables
Drop table test1;
Drop table test2;
Create Table
C1
C2
C3
C4
Test1 (
Char(5) Primary Key,
Varchar2(50),
Integer,
Date);
Create Table
D1
D2
D3
Test2 (
VARCHAR2(15) Primary Key,
Char(5) references Test1,
Integer);
8
Drop Tables
Drop table test2;
Drop table test1;
Create Table
C1
C2
C3
C4
Test1 (
Char(5) Primary Key,
Varchar2(50),
Integer,
Date);
Create Table
D1
D2
D3
Test2 (
VARCHAR2(15) Primary Key,
Char(5) references Test1,
Integer);
9
Using Pause
Drop table test2;
pause
Drop table test1;
Pause
Create Table
C1
C2
C3
C4
Pause
Test1 (
Char(5) Primary Key,
Varchar2(50),
Integer,
Date);
Create Table
D1
D2
D3
Test2 (
VARCHAR2(15) Primary Key,
Char(5) references Test1,
Integer);
10
Insert Records
Insert into Test1
Values ('cs363', 's1', 44, '28-feb-12');
Insert into Test1
Values ('cs334', 's2', 45, '29-feb-2012');
One record at a time!
Single quotes for string
Date is entered as string
in the default format
11
Entity Integrity
Insert into Test1
Values ('cs363', 'q2', 17, '2-Mar-12');
PK must be Unique!
12
Null Values
Insert into Test1
Values ('cs387', 'q2', 17, null);
Null is a key word, not a string!
NULL, null and Null are the same
SQL is not case sensitive
13
Entity Integrity
Insert into Test1
Values (NULL, 'q2', 17, '2-Mar-12');
PK cannot be null!
14
Column Constraints
15
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!
16
References
• For Foreign Key
17
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
18
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
19
Table Constraints
• Constraints on one or more columns
– Composite PK, AK, FK
• Cannot use Not Null in table constraints
• Naming the constraints
20
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));
21
-- Composite PK
Create Table Test1 (
C1
Char(5),
C2
Varchar2(50) ,
C3
Integer,
C4
Date,
Constraint Test1_PK
Primary Key (C1, C3));
Desc Test3
pause;
Create Table Test2 (
D1
VARCHAR2(25) Primary Key,
D2
Char(5),
D3
Integer,
Constraint Test2_FK
Foreign Key (D2, D3) references Test1);
Desc Test4;
Pause
Works.
22
-- Composite PK
Create Table Test1 (
C1
Char(5),
C2
Varchar2(50) ,
C3
Integer,
C4
Date,
Constraint Test1_PK
Primary Key (C1, C3));
Desc Test3
pause;
Create Table Test2 (
D1
VARCHAR2(25) Primary Key,
D2
Char(5),
D3
Integer,
Constraint Test2_FK
Foreign Key (D3, D2) references Test1);
Desc Test4;
Pause
Does Not Work.
23
-- Composite PK
Create Table Test1 (
C1
Char(5),
C2
Varchar2(50) ,
C3
Integer,
C4
Date,
Constraint Test1_PK
Primary Key (C1, C3));
Desc Test3
pause;
Create Table Test2 (
D1
VARCHAR2(25) Primary Key,
D2
Char(5),
D3
Integer,
Constraint Test2_FK
Foreign Key (D3, D2) references Test1(C3, C1));
Desc Test4;
Pause
This Works.
24
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!
25
Referential Integrity
Insert into Test2
Values ('golf', 'cs363', 44);
Insert into Test2
Values ('bridge', 'cs334', 44);
Insert into Test2
Values ('bridge', 'cs334', Null);
Insert into Test2
Values ('bridge1', null, null);
26
Database Schema
Branch (Bno…)
Staff (Sno…Bno)
Owner (Ono…)
PropertyForRent (Pno…Ono)
Renter (Rno…)
Viewing (Rno, Pno, ViewDate…)
27
In what order to create the tables?
Branch (Bno…)
Staff (Sno…Bno)
PropertyForRent (Pno…Ono)
Owner (Ono…)
Viewing (Rno, Pno, ViewDate…)
Renter (Rno…)
Branch (Bno…)
Staff (Sno…Bno)
Owner (Ono…)
PropertyForRent (Pno…Ono)
Renter (Rno…)
Viewing (Rno, Pno, ViewDate…)
Will it work?
NO!
Will it work?
YES!
Referential Integrity!
28
In what order to drop the tables?
Branch (Bno…)
Staff (Sno…Bno)
Owner (Ono…)
PropertyForRent (Pno…Ono)
Renter (Rno…)
Viewing (Rno, Pno, ViewDate…)
Viewing (Rno, Pno, ViewDate…)
Staff (Sno…Bno)
PropertyForRent (Pno…Ono)
Owner (Ono…)
Renter (Rno…)
Branch (Bno…)
Will it work?
NO!
Will it work?
YES!
Referential Integrity!
29
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
30
Insert Rows
Branch
Bno
Phone
Staff
…
Sno
Phone
Bno
B101
SG100
B101
B205
SG363
Null
SA200
B123
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
31
Delete Rows
No records in other tables referencing the record to
be deleted.
32
Delete Rows
Branch
Bno
Phone
Staff
…
Sno
Phone
Bno
B101
SG100
B101
B205
SG363
Null
SA200
B101
Delete from Staff: no problem
Delete from Branch:
Delete branch 'B101'
What about staff in 'B101‘?
33
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!
34
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 Default : not implemented
• No Check : not implemented
35
Update Record
Branch
Bno
Phone
Staff
…
Sno
Phone
Bno
B101
SG100
B101
B205
SG363
Null
SA200
B205
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
36
Integrity Rules: Constraints
• Column Constraints
• Table Constraints
37
Project
Phase I
Due Today by 5 PM
38