Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1...

21
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

Transcript of Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1...

Page 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.

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

Page 2: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

Oracle Editor

• Debugging individual commands

• One command at a time!

• No semicolon at the end

2

Page 3: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 4: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

Using Oracle Editor

• Using Notepad

• Save file after editing SQL command

• SQL> run

• SQL> /

• Run the last command!

4

Page 5: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

SQL Script File

• Using any text editor outside SQL*Plus

• File extension .SQL

UserName_Lab7.Sql• Multiple SQL commands

• Each command ends with ;

5

Page 6: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 7: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 8: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 9: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 10: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 11: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

Referential Integritydrop table test2;

-- Table dropped

drop table test1;

-- Table dropped

11

Page 12: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 13: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 14: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 15: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 16: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

Integrity Rules: Constraints

• Column Constraints

• Table Constraints

16

Page 17: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 18: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

Table Constraints

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

• Cannot use Not Null in table constraints

18

Page 19: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 20: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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

Page 21: Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

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