Download presentation
Presentation is loading. Please wait.
Published byBernice Lynch Modified over 9 years ago
1
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
2
Oracle Editor Debugging individual commands One command at a time! No semicolon at the end 2
3
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
4
Using Oracle Editor Using Notepad Save file after editing SQL command SQL> run SQL> / Run the last command! 4
5
SQL Script File Using any text editor outside SQL*Plus File extension.SQL UserName_Lab7.Sql Multiple SQL commands Each command ends with ; 5
6
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
7
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
8
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
9
Referential Integrity insert 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 comment 9
10
Referential Integrity drop 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
11
Referential Integrity drop table test2; -- Table dropped drop table test1; -- Table dropped 11
12
Referential Integrity create 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
13
Referential Integrity Create 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
14
Referential Integrity Drop 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
15
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
16
Integrity Rules: Constraints Column Constraints Table Constraints 16
17
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
18
Table Constraints Constraints on one or more columns –Composite PK, AK, FK Cannot use Not Null in table constraints 18
19
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
20
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
21
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.