Oracle_ch4.pptx

download Oracle_ch4.pptx

of 54

Transcript of Oracle_ch4.pptx

  • 7/26/2019 Oracle_ch4.pptx

    1/54

    Dr. Chen, Oracle Database System (Oracle) 1

    Chapter 4Constraints

    Jason C. H. Chen,Ph.D.

    Professor of MIS

    School of BusinessGonzaga University

    Spokane, WA 99258 USA

    [email protected]

  • 7/26/2019 Oracle_ch4.pptx

    2/54

    Dr. Chen, Oracle Database System (Oracle) 2

    What will we study today?

    IntegrityData

    Referential

    Integrity

    How to

    achieve it?

  • 7/26/2019 Oracle_ch4.pptx

    3/54

    Dr. Chen, Oracle Database System (Oracle) 3

    Objectives

    Explain the purpose of constraints in a table

    Distinguish among PRIMARY KEY, FOREIGNKEY, UNIQUE, CHECK, and NOT NULLconstraints and the appropriate use for eachconstraint

    Understand how constraints can be created whencreating a table or modifying an existing table

    Distinguish between creating constraints at thecolumn level and table level

  • 7/26/2019 Oracle_ch4.pptx

    4/54

    Dr. Chen, Oracle Database System (Oracle) 4

    Objectives (continued)

    Create PRIMARY KEY constraints for a singlecolumn and a composite primary key (cpk)

    Create a FOREIGN KEY constraint

    Create a UNIQUE constraint Create a CHECK constraint

    Create a NOT NULL constraint using the ALTER

    TABLEMODIFY command

    Include constraints during table creation

    Use DISABLE and ENABLE commands

    Use the DROP command

  • 7/26/2019 Oracle_ch4.pptx

    5/54

    Dr. Chen, Oracle Database System (Oracle) 5

    customers

    orders

    pk

    fkpk

    customer# LastName Referred Region

    1001 MORALES NULL SE

    1005 GIRARD NULL NW

    1020 FALAH NULL NE

    Order# customer# ShipZip ShipCost

    1000 1005 98114 2.00

    1003 1001 32328 4.00

    1012 1007 49002 6.00

    Can we create orders#1 if customers#5 is not

    created? Why?

    Can we delete customers#5 if orders#1 is still in thedatabase? Why?

    Customers#5

    orders#1

    Referential Integrity

  • 7/26/2019 Oracle_ch4.pptx

    6/54

    Dr. Chen, Oracle Database System (Oracle) 6

    Refresh the Database

    1. Create a new folder on c:\ as follows:

    c:\oradata\chapter4

    2. Go to Blackboard and download datafiles from Oracle chapter4 and save under

    c:\oradata\chapter4\

    3. Run the following script file

    Start c:\oradata\chapter4\JLDB_Build_4.sql

  • 7/26/2019 Oracle_ch4.pptx

    7/54

    Dr. Chen, Oracle Database System (Oracle) 7

    Type the following SQL commands

    -- chapter 4, Figure 4-5; p. 104INSERT INTO customers (customer#, lastname,

    firstname, region)

    VALUES (1020, 'PADDY', 'JACK', 'NE');

    --extra INSERT command

    INSERT INTO orders (order#, customer#, orderdate)

    VALUES (1021, 1021, '06-APR-09');

    -- DELETE command

    DELETE FROM customers

    WHERE customer# = 1005;

  • 7/26/2019 Oracle_ch4.pptx

    8/54

    Dr. Chen, Oracle Database System (Oracle) 8

    pk

    pk fk

    Customers#5

    orders#1

    Can we create

    orders#1 if

    customers#5 isnot created?

    Why?

    Can we delete

    customers#5 if

    orders#1 is still in

    the database?

    Why?

    Can we insert a

    different customerwith Duplicate pk?

  • 7/26/2019 Oracle_ch4.pptx

    9/54

    Dr. Chen, Oracle Database System (Oracle) 9

    customers

    RULES:

    1.

    You cant adda record to TABLE- (or the table with fk,e.g., orders)

    unless there is a corresponding record in TABLE-1 (or the table with pk).

    2.You cant deletea record in TABLE-1 (or the table with pk, e.g.,

    customers) if there is a record in TABLE-

    (or the table with fk).

    Order of enteringdata into the database: customersorders

    Order of deletingdata from the database: orders customers

    orders

    pk

    fkpk

    customer# LastName Referred Region

    1001 MORALES NULL SE

    1005 GIRARD NULL NW

    1020 FALAH NULL NE

    Order# customer# ShipZip ShipCost

    1000 1005 98114 2.00

    1003 1001 32328 4.00

    1012 1007 49002 6.00

  • 7/26/2019 Oracle_ch4.pptx

    10/54

    Dr. Chen, Oracle Database System (Oracle) 10

    Review from Last Class

    What are the three rules of naming table and field?

    What are the three (total of four) required information

    that should be described for each field?1. Name

    2. Type

    3. Size

    4. Constraint

    L

  • 7/26/2019 Oracle_ch4.pptx

    11/54

    Dr. Chen, Oracle Database System (Oracle) 11

    What is a Constraint?

    A rule used to enforce business rules, practices,and policies

    A rule used to ensure accuracy and integrity of

    data A mechanism used to protect

    the relationship between data within an Oracletable, or

    the correspondence between data in two differenttables.

    For example, the state entered must be one of the 50

    states in the U.S.

  • 7/26/2019 Oracle_ch4.pptx

    12/54

    Dr. Chen, Oracle Database System (Oracle) 12

    Types of Constraints

    I ntegr i ty constraints: define primary and foreignkeys

    Value constraints:define specific data values ordata ranges that must be inserted into columns and

    whether values must be unique or not NULL

    Table constraint: restricts the data value withrespect to all other values in the table

    Field (column) constraint: limits the value thatcan be placed in a specific field, irrespective ofvalues that exist in other table records

  • 7/26/2019 Oracle_ch4.pptx

    13/54

    Dr. Chen, Oracle Database System (Oracle) 13

    I. Naming conventions for constraints

    __

    Where is:

    pk PRIMARY KEY fk REFERENCES (pk)

    ck CHECK (note that cc stands for CHECK CONDITION)

    nn NOT NULL

    uk UNIQUE

    e.g.,

    s_id NUMBER (6) CONSTRAINT student_s_id_pkPRIMARY KEY;

  • 7/26/2019 Oracle_ch4.pptx

    14/54

    Dr. Chen, Oracle Database System (Oracle) 14

    Integrity Constraints

    Define primary key fields

    Specify foreign keys and theircorresponding table and columnreferences

    Specify composite keys

  • 7/26/2019 Oracle_ch4.pptx

    15/54

    Dr. Chen, Oracle Database System (Oracle) 15

    Creating a Table

    CREATE TABLE tablename

    (fieldname1data_type (size)

    [CONSTRAINT constraint_name constraint_type],fieldname2 data_type (size),

    [CONSTRAINT constraint_name constraint_type,]

    );

  • 7/26/2019 Oracle_ch4.pptx

    16/54

    Dr. Chen, Oracle Database System (Oracle) 16

    Creating Constraints

    When

    During table creation

    After table creation, by modifying the existing

    table

    How

    Column level approach

    Table level approach

  • 7/26/2019 Oracle_ch4.pptx

    17/54

    Dr. Chen, Oracle Database System (Oracle) 17

    Creating Constraints at the Column Level

    If a constraint is being created at the column level, the

    constraint applies to the column specified

    Creating Constraints at the Table Level

    Approach can be used to create any constraint type after alltable field definitions are completed except NOT NULL

    Required if constraint is based on multiple columns

    Figure 4-1 Syntax for creating a column-level constraint

    Figure 4-2 Syntax for creating a table-level constraint

  • 7/26/2019 Oracle_ch4.pptx

    18/54

    Dr. Chen, Oracle Database System (Oracle) 18

    Customer# LastName FirstName Address City State Zip Referred Region EmailNUMBER(4) VARCHAR2(10) VARCHAR2

    (10)

    VARCHAR2

    (20)

    VARCHAR2(12) VARCHAR

    2(2)

    VARCHAR2

    (5)

    NUMBER(

    4)

    CHAR(2) VARCHAR2(

    30)

    Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)

    CREATE TABLE Customers

    (Customer# NUMBER(4),

    LastName VARCHAR2(10) NOT NULL,

    FirstName VARCHAR2(10) NOT NULL,

    Address VARCHAR2(20),

    City VARCHAR2(12),

    State VARCHAR2(2),

    Zip VARCHAR2(5),

    Referred NUMBER(4),Region CHAR(2),

    Email VARCHAR2(30),

    CONSTRAINT customers_customer#_pk PRIMARY KEY(customer#),

    CONSTRAINT customers_region_ck

    CHECK (region IN ('N', 'NW', 'NE', 'S', 'SE', 'SW', 'W', 'E')) );

    CUSTOMERSpk

    ORDERSfkpk

    Optional(variable name)

  • 7/26/2019 Oracle_ch4.pptx

    19/54

    Dr. Chen, Oracle Database System (Oracle) 19

    Customer# LastName FirstName Address City State Zip Referred Region EmailNUMBER(4) VARCHAR2(10) VARCHAR2

    (10)

    VARCHAR2

    (20)

    VARCHAR2(12) VARCHAR

    2(2)

    VARCHAR2

    (5)

    NUMBER(

    4)

    CHAR(2) VARCHAR2(

    30)

    Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)

    CREATE TABLE Orders

    (Order# NUMBER(4),

    Customer# NUMBER(4),OrderDate DATE NOT NULL,

    ShipDate DATE,

    ShipStreet VARCHAR2(18),

    ShipCity VARCHAR2(15),

    ShipState VARCHAR2(2),ShipZip VARCHAR2(5),

    ShipCost NUMBER(4,2),

    CONSTRAINT orders_order#_pkPRIMARY KEY(order#),

    CONSTRAINT orders_customer#_fkFOREIGN KEY (customer#)

    REFERENCEScustomers(customer#));

    CUSTOMERSpk

    ORDERS

    fk

    pk

    Optional

    (variable name)

  • 7/26/2019 Oracle_ch4.pptx

    20/54

    Dr. Chen, Oracle Database System (Oracle) 20

    Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)

    ORDERS

    Order# Item# ISBN Quantity PaidEach

    NUMBER(4) NUMBER(2) VARCHAR2(10) NUMBER(3) NUMBER(5,2)

    ORDERITEMS

    CREATE TABLE ORDERITEMS

    ( Order# NUMBER(4),

    Item# NUMBER(2),

    ISBN VARCHAR2(10),

    Quantity NUMBER(3) NOT NULL,

    PaidEach NUMBER(5,2) NOT NULL,

    CONSTRAINT orderitems_order#item#_pkPRIMARY KEY (order#,

    item#),CONSTRAINT orderitems_order#_fkFOREIGN KEY (order#)

    REFERENCESorders (order#) ,

    CONSTRAINT orderitems_isbn_fkFOREIGN KEY (isbn)

    REFERENCESbooks (isbn) ,

    CONSTRAINT oderitems_quantity_ckCHECK(quantity > 0) );

    ISBN Title PubDate PubID Cost Retail Discount CategoryVARCHAR2(10) VARCHAR2(30) DATE NUMBER(2) NUMBER(5,2) NUMBER(5,2) NUMBER(4,2) VARCHAR2(12)

    BOOKSpk

    fkfk

    pk

    Optional

    (variable name)

    cpk

    How to define composite key?

  • 7/26/2019 Oracle_ch4.pptx

    21/54

    Dr. Chen, Oracle Database System (Oracle) 21

    Your Job

    You need to study and understand all

    CREATE TABLE SQL commands in

    JLDB_Build_4.sql

  • 7/26/2019 Oracle_ch4.pptx

    22/54

    Dr. Chen, Oracle Database System (Oracle) 22

    Enforcement of Constraints

    All constraints are enforced at the table

    level

    If a data value violates a constraint, the

    entire row is rejected

  • 7/26/2019 Oracle_ch4.pptx

    23/54

    Dr. Chen, Oracle Database System (Oracle) 23

    Constraint Types

    Table 4-1 Constraint types

  • 7/26/2019 Oracle_ch4.pptx

    24/54

    Dr. Chen, Oracle Database System (Oracle) 24

    Your Turn

  • 7/26/2019 Oracle_ch4.pptx

    25/54

    Dr. Chen, Oracle Database System (Oracle) 25

    Syntax:CONSTRAINT constraint_name PRIMARY KEY

    Primary Key Constraints

    SQL> CREATE TABLE students

    2 (s_id NUMBER(6) CONSTRAINT students_s_id_pk PRIMARYKEY,

    3 s_name VARCHAR2(30),

    4 s_class CHAR(2),

    5 s_dob DATE);

    Create a table with the following information:

    1. Name of the table: students

    2. Fields: s_id number with 6 digits and is a primary key, s_namecharacter with 30 chars, s_class with 2 chars, s_dob with DATE

  • 7/26/2019 Oracle_ch4.pptx

    26/54

    Dr. Chen, Oracle Database System (Oracle) 26

    at the Column-Level

    Primary Key Constraints (cont.)

    SQL> CREATE TABLE students

    2 (s_id NUMBER(6),3 s_name VARCHAR2(30),

    4 s_class CHAR(2),

    5 s_dob DATE,

    6 CONSTRAINT students_s_id_pkPRIMARY KEY (s_id));

    SQL> CREATE TABLE students

    2 (s_id NUMBER(6) CONSTRAINT students_s_id_pkPRIMARY

    KEY,

    3 s_name VARCHAR2(30),

    4 s_class CHAR(2),

    5 s_dob DATE);

    Practice:

    Type in one of the command. at the Table-Level

  • 7/26/2019 Oracle_ch4.pptx

    27/54

    Dr. Chen, Oracle Database System (Oracle) 27

    Adding Constraints to Existing Tables

    Constraints are added to an existing table

    with the ALTER TABLE command

    Add a NOT NULL constraint using

    MODIFY clause

    All other constraints are added using ADD

    clause

  • 7/26/2019 Oracle_ch4.pptx

    28/54

    Dr. Chen, Oracle Database System (Oracle) 28

    Using the PRIMARY KEY Constraint

    Ensures that columns do not contain duplicate

    or NULL values

    Only one per table is allowed

    Figure 4-3 Syntax of the ALTER TABLE command to add a PRIMARY KEY constraint

  • 7/26/2019 Oracle_ch4.pptx

    29/54

    Dr. Chen, Oracle Database System (Oracle) 29

    Constraint Checked with Data Input

    Figure 4-5 Insert a row to test the constraint

  • 7/26/2019 Oracle_ch4.pptx

    30/54

    Dr. Chen, Oracle Database System (Oracle) 30

    PRIMARY KEY Constraint for Composite Key

    List column names within parentheses

    separated by commas

    Figure 4-7 Adding a composite PRIMARY KEY constraint

  • 7/26/2019 Oracle_ch4.pptx

    31/54

    Dr. Chen, Oracle Database System (Oracle) 31

    Drop Contraint

    ALTER TABLE orderitems

    DROP CONSTRAINT

    orderitems_order#item#_pk PRIMARY

    KEY (order#, item#);

  • 7/26/2019 Oracle_ch4.pptx

    32/54

    Dr. Chen, Oracle Database System (Oracle) 32

    Multiple Constraints on a Single Column

    A column may be included in multiple constraints

    The order# column is included in a primary key and a

    foreign key constraint

    Figure 4-32 Assigning multiple constraints to a column

    Optional

    (variable name)

  • 7/26/2019 Oracle_ch4.pptx

    33/54

    Dr. Chen, Oracle Database System (Oracle) 33

    Using the FOREIGN KEY Constraint

    Requires a value to exist in the referenced column

    of another table

    NULL values are allowed

    Enforces referential integrity Maps to the PRIMARY KEY in parent table

    Customer# LastName FirstName Address Region

    Order# Customer# OrderDate ShipZip ShipCost

    customers

    orders

    pk

    fk

  • 7/26/2019 Oracle_ch4.pptx

    34/54

    Dr. Chen, Oracle Database System (Oracle) 34

    Using the FOREIGN KEY Constraint

    You cannot delete a value in a parent table (pk)

    referenced by a row in a child table (fk)

    Customer# LastName FirstName Address Region

    Order# Customer# OrderDate ShipZip ShipCost

    customers

    orders

    pk

    fk

  • 7/26/2019 Oracle_ch4.pptx

    35/54

    Dr. Chen, Oracle Database System (Oracle) 35

    Customer# LastName FirstName Address Region

    Order# Customer# OrderDate ShipZip ShipCost

    customers

    orders

    pk

    fk

    RULES:

    1.

    You cant adda record to TABLE- (or the table with fk,

    e.g., orders) unless there is a corresponding record in

    TABLE-1 (or the table with pk).

    2.You cant deletea record in TABLE-1 (or the table with pk, e.g.,

    customers) if there is a record in TABLE- (or the table with fk).

    Order of enteringdata into the database: customersorders

    Order of deletingdata from the database: orders customers

  • 7/26/2019 Oracle_ch4.pptx

    36/54

    Dr. Chen, Oracle Database System (Oracle) 36

    FOREIGN KEY Constraint Example

    Figure 4-9 Adding a FOREIGN KEY constraint

  • 7/26/2019 Oracle_ch4.pptx

    37/54

    Dr. Chen, Oracle Database System (Oracle) 37

    Deletion of Foreign Key Values

    You cannot delete a value in a parent table

    referenced by a row in a child table

    Use ON DELETE CASCADE keywords

    when creating FOREIGN KEY constraintit automatically deletes a parent row when

    the row in a child table is deleted

  • 7/26/2019 Oracle_ch4.pptx

    38/54

    Dr. Chen, Oracle Database System (Oracle) 38

    Using the UNIQUE Constraint

    No duplicates are allowed in the referencedcolumn

    NULL values are permitted

    Figure 4-16 Adding a UNIQUE constraint

  • 7/26/2019 Oracle_ch4.pptx

    39/54

    Dr. Chen, Oracle Database System (Oracle) 39

    Using the CHECK Constraint

    Updates and additions must meet specifiedcondition

    Figure 4-19 Adding a CHECK constraint to the ORDERS table

  • 7/26/2019 Oracle_ch4.pptx

    40/54

    Dr. Chen, Oracle Database System (Oracle) 40

    Using the NOT NULL Constraint

    The NOT NULL constraint is a special

    CHECK constraint with IS NOT NULL

    condition

    Can only be created at column level

    Included in output of DESCRIBE command

    Can only be added to an existing table using

    ALTER TABLEMODIFY command

  • 7/26/2019 Oracle_ch4.pptx

    41/54

    Dr. Chen, Oracle Database System (Oracle) 41

    NOT NULL Constraint Example

    Figure 4-23 Adding a NOT NULL constraint

  • 7/26/2019 Oracle_ch4.pptx

    42/54

    Dr. Chen, Oracle Database System (Oracle) 42

    Practice

    Lets try to create additional tablesJustLee Books would like to create some new

    tables to store office equipment inventory data.

    DeptID

    Dname

    Fax

    DEPTEquipID

    Edesc

    Purchdate

    Rating

    DeptID

    EtypeID

    EQUIP

    EtypeID

    Etypename

    ETYPES

    Figure 4-26 E-R model for equipment tables

  • 7/26/2019 Oracle_ch4.pptx

    43/54

    Dr. Chen, Oracle Database System (Oracle) 43

    Including Constraints during Table

    DeptID

    Dname

    Fax

    DEPT EquipIDEdesc

    Purchdate

    Rating

    DeptID

    EtypeID

    EQUIP

    EtypeID

    Etypename

    ETYPES

    Each department name must be unique.

    Each department must be assigned a name .

    Each equipment type name must be unique

    Each equipment type must be assigned a name.

    Each equipment item must be assigned a valid department.

    If an equipment item is assigned a type, it must be a valid type.

    Valid rating values for equipment are A, B, and C.

    unique

    NOT NULL

    unique

    NOT NULL

    ck

    fkfk

    pkpk pk

  • 7/26/2019 Oracle_ch4.pptx

    44/54

    Dr. Chen, Oracle Database System (Oracle) 44

    -- chapter 4, Figure 4-27; p. 117CREATE TABLE dept

    (deptid NUMBER(2),

    dname VARCHAR2(20) NOT NULL,

    fax VARCHAR2(12),CONSTRAINT dept_deptid_pk PRIMARY KEY(deptid),

    CONSTRAINT dept_dname_uk UNIQUE(dname) );

    DEPT table creation

    Each department name must be unique.

    Each department must be assigned a name .

    unique

    NOT NULL

    i i iff f i

  • 7/26/2019 Oracle_ch4.pptx

    45/54

    Dr. Chen, Oracle Database System (Oracle) 45

    -- chapter 4, Figure 4-27; p. 117CREATE TABLE dept

    (deptid NUMBER(2),

    dname VARCHAR2(20) NOT NULL,

    fax VARCHAR2(12),

    CONSTRAINT dept_deptid_pk PRIMARY KEY(deptid),CONSTRAINT dept_dname_uk UNIQUE(dname) );

    -- chapter 4, Figure 4-30; p. 119

    CREATE TABLE dept(deptid NUMBER(2) CONSTRAINT dept_deptid_pk PRIMARY KEY,

    dname VARCHAR2(20) NOT NULL CONSTRAINT dept_dname_uk

    UNIQUE,

    fax VARCHAR2(12));

    Constraints are defined at the column-level

    What is the main difference on the following

    CREATE TABLE statements?

    Constraints are defined at the table-level

  • 7/26/2019 Oracle_ch4.pptx

    46/54

    Dr. Chen, Oracle Database System (Oracle) 46

    -- chapter 4, Figure 4-28; p. 118

    CREATE TABLE etypes

    (etypeid NUMBER(2),

    etypename VARCHAR2(20) NOT NULL,

    CONSTRAINT etypes_etypeid_pk PRIMARY KEY (etypeid),

    CONSTRAINT etypes_etypename_uk UNIQUE(etypename) );

    -- chapter 4, Figure 4-29; p. 119

    CREATE TABLE equip

    (equipid NUMBER(3),edesc VARCHAR2(30),

    purchdate DATE,

    rating CHAR(1),

    deptid NUMBER(2) NOT NULL,

    etypeid NUMBER(2),

    CONSTRAINT equip_equipid_pk PRIMARY KEY(equipid),CONSTRAINT equip_deptid_fk FOREIGN KEY(deptid)

    REFERENCES dept (dept_id),

    CONSTRAINT equip_etypeid_fk FOREIGN KEY(etypeid)

    REFERENCES etypes (etypeid),

    CONSTRAINT equip_rating_ck CHECK(rating IN('A', 'B', 'C', 'D')) );

    Each equipment item must be assigned a valid department.

    If an equipment item is assigned a type, it must be a validtype.

    Valid rating values for equipment are A, B, and C.

    Each equipment type name must be unique

    Each equipment type must be assigned a name.

  • 7/26/2019 Oracle_ch4.pptx

    47/54

    Dr. Chen, Oracle Database System (Oracle) 47

    Including Constraints during Table CreationTable Level

    Include at end of column list

    Figure 4-29 EQUIP table creation

  • 7/26/2019 Oracle_ch4.pptx

    48/54

    Dr. Chen, Oracle Database System (Oracle) 48

    Viewing ConstraintsUSER_CONSTRAINTS

    Display constraint listing for a specific table

    Figure 4-33 SELECT statement to view data about existing constraints

  • 7/26/2019 Oracle_ch4.pptx

    49/54

    Dr. Chen, Oracle Database System (Oracle) 49

    Disabling Constraints

    -- chapter 4, Figure 4-36; p. 123ALTER TABLE equip

    DISABLE CONSTRAINT equip_rating_ck;

    ALTER TABLE equip

    ENABLE CONSTRAINT equip_rating_ck;

  • 7/26/2019 Oracle_ch4.pptx

    50/54

    Dr. Chen, Oracle Database System (Oracle) 50

    Dropping Constraints

    -- chapter 4, Figure 4-38; p. 124

    ALTER TABLE equip

    DROP CONSTRAINT equip_rating_ck;

    -- chapter 4, Figure 4-39; p. 125ALTER TABLE customers

    DROP PRIMARY KEY;

    -- chapter 4, Figure 4-40; p. 125ALTER TABLE customers

    DROP PRIMARY KEY CASCADE;

    HW!!

  • 7/26/2019 Oracle_ch4.pptx

    51/54

    Dr. Chen, Oracle Database System (Oracle) 51

    HW!!

    Practice all the examples in the text.

    A Script file is available on the Bb (file name:

    Ch4Queries.sql)

    After completing all examples, do the HW (hint: see

    the tables below for the final schema and the sample

    output on the Bb under Assignmentsrep_id last first comm base_salary

    NUMBER(5) VARCHAR2(15) VARCHAR2(10) CHAR(1) NUMBER(7,2)

    store_id name contact rep_id

    NUMBER(8) VARCHAR2(30) VARCHAR2(30) NUMBER(5)

    store_id name quarter rep_id

    NUMBER(8) VARCHAR2(5) CHAR(3) NUMBER(5)

    pk store_reps

    pk fkbook_stores

    cpk, fkcpk

    cpk, fkrep_contracts

    Homework Hands On Assignments

  • 7/26/2019 Oracle_ch4.pptx

    52/54

    Dr. Chen, Oracle Database System (Oracle) 52

    Homework - Hands-On Assignments

    Read and Practice all examples on Chapters 4

    1. Run the script files (in the folder \oradata\chapter4\):

    JLDB_Build_4.sql 2. Read Oracle assignment and create a script fileOracle_ch4_Lname_Fname.sql for questions (#1 to#8; p.133) on Hands-on Assignments. .

    3. Execute and test one problem at a time and make surethey are all running successfully.

    4. When you done, spool the script files (see next slidefor spooling instructions) and email the file(Oracle_ch4_Spool_Lname_Fname.txt) to me by themidnight before the next class.

    Email me with one attachment

    (Oracle_ch4_Spool_Lname_Fname.) to:

    [email protected] subject title of

    bmis441_Oracle_ch4

    H S l S i d O Fil

  • 7/26/2019 Oracle_ch4.pptx

    53/54

    Dr. Chen, Oracle Database System (Oracle) 53

    How to Spool your Script and Output Files

    After you tested the script file of Oracle_ch4_Lname_Fname.sql successfully,follow the instructions below to spool both script and output files:

    Step 0. Run the following script file from SQL*Plus (since you have createdJLDB tables)

    Start c:\oradata\chapter4\JLDB_Build_4.sql

    1. type the following on SQL> Spool c:\oradata\Oracle_ch4_Spool_Lname_Fname.txt(make sure your name is entered)

    2. open Oracle_ch4_Lname_Fname.sql that you already tested

    3. copy and paste all the SQL commands (including all comments) to theSQL*PLUS

    4. type Spool Off on the SQL>

    The output should contain your personal information, all SQL commands andtheir solution on the .txt file and saved in C: drive (oradata\ folder)

    Email me with the spooled file (.txt) with attachment to:

    [email protected]

    with subject title of

    bmis441_Oracle_ch4

  • 7/26/2019 Oracle_ch4.pptx

    54/54

    End of chapter 4