creating tables and inserting records -- not easy to edit! -- check constraints! create table test1...
TRANSCRIPT
Creating Tables and Inserting Records-- Not easy to edit!
-- check constraints!
Create table test1 (
C1 char(5) primary key,
C2 Varchar2(15) not null
Check (c2 in ('database', 'Windows', 'Web Protocols')),
C3 integer Not NULL Check (C3 in (10, 20, 30)),
C4 Date);
Insert into test1
Values('cs363', 'database', 10, null);
Insert into test1
Values('cs387', 'Web Protocols', 15, null);
Insert into test1
Values('cs334', ‘windows', 20, ‘3-14-13’);
Insert into test1
Values('cs334', ‘Windows', null, ‘3-14-13’);
1
Oracle Editor
• Debugging individual commands
• One command at a time!
• No semicolon at the end
2
Setting editfile to Use Oracle Editor
• Show editfile
• SQL> edit (return)
Not working!
No rights in the working folder
• Set editfile J:\CS363\DebugLab7.sql
• SQL> edit (return)
It’s working!
3
Using Oracle Editor
• Using Notepad
• Save file after editing SQL command
• SQL> run
• SQL> /
• Run the last command!
4
SQL Script File
• Using any text editor outside SQL*Plus
• File extension .SQL
UserName_Lab7.Sql• Multiple SQL commands
• Each command ends with ;
5
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!
6
Oracle Editor and Script File
• Use Oracle editor to debug individual commands
• Copy and paste to your script file
• Add semicolon at the end of each command
• Run the script file
• Drop your script file for Assignment7
7
Table with Foreign Keys
drop table test2;
create table test2 (
A1 char(5) references test1(C1),
A2 integer Primary key);
insert into test2
values ('cs363', 18);
insert into test2
values ('cs363', 15);
insert into test2
values (null, 19);
8
Referential Integrityinsert into test2
values ('cs363', 18);
insert into test2
values ('cs363', 15);
-- Multiple records reference one value in test1
insert into test2
values (null, 19);
-- can be null
insert into test2
values ('cs234', 20);
-- Parent key not found
-- This begins comment9
Referential Integritydrop table test1;
-- Unique/primary keys in table referenced
-- by foreign keys
Delete from test2;
drop table test1;
-- Unique/primary keys in table referenced
-- by foreign keys
10
Referential Integritydrop table test2;
-- Table dropped
drop table test1;
-- Table dropped
11
Referential Integritycreate table test2 (
A1 char(5) references test1(C1),
A2 integer Primary key);
-- table or view does not exist
Create table test1 (
C1 char(5) primary key,
C2 Varchar2(50),
C3 integer,
C4 Date);
12
Referential IntegrityCreate table test1 (
C1 char(5) primary key,
C2 Varchar2(50),
C3 integer,
C4 Date);
-- Table created
create table test2 (
A1 char(5) references test1(C1),
A2 integer Primary key);
-- Table created
13
Referential IntegrityDrop table test2;
Drop table test1;
Create table test1 (
C1 char(5) primary key,
C2 Varchar2(50),
C3 integer,
C4 Date);
create table test2 (
A1 char(5) references test1(C1),
A2 integer Primary key);
14
UserName_Lab7.sql------------------------------------------------
-- Name : Qi Yang
-- UserName : YangQ
-- Date : 03-14-13
-- Course : CS 3630
-- Description: Sql script example
------------------------------------------------
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;
. . .
15
Integrity Rules: Constraints
• Column Constraints
• Table Constraints
16
Column Constraints
Create table Staff (
SNo char(4) Primary Key,
Bno Char(4) Default 'B363' References Branch,
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),
-- between is inclusive
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
17
Table Constraints
• Constraints on one or more columns– Composite PK, AK, FK
• Cannot use Not Null in table constraints
18
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));19
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!
20
Schedule• Next Monday
Go over Quiz2 and Assignment6-2
Review for Test 1• Next Wednesday
Test 1• Next Friday
Lab 206 to do Assignment 7
Assignment 7 is due by 5 pm
21