68
Database Programming - Section 10 Instructor Guide

Database Programming - Section 10 - Oracle Academyacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · 2013-09-26 · Tell Me / Show Me Tell Me / Show Me Tell Me / Show Me Review

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Database Programming - Section 10 Instructor Guide

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page i

Table of Contents

Database Programming - Section 10..............................................................................................................1 Lesson 1 - Defining NOT NULL and UNIQUE Constraints.........................................................................1 What Will I Learn? ........................................................................................................................................2 Why Learn It?................................................................................................................................................3 Tell Me / Show Me........................................................................................................................................4 Try It / Solve It ..............................................................................................................................................13 Lesson 2 - PRIMARY KEY, FOREIGN KEY, and CHECK Constraints.....................................................16 What Will I Learn? ........................................................................................................................................17 Why Learn It?................................................................................................................................................18 Tell Me / Show Me........................................................................................................................................19 Try It / Solve It ..............................................................................................................................................28 Lesson 3 - Managing Constraints ..................................................................................................................32 What Will I Learn? ........................................................................................................................................33 Why Learn It?................................................................................................................................................34 Tell Me / Show Me........................................................................................................................................35 Try It / Solve It ..............................................................................................................................................48 Lesson 4 - Practice Exercises and Review.....................................................................................................54 What Will I Learn? ........................................................................................................................................55 Why Learn It?................................................................................................................................................56 Tell Me / Show Me........................................................................................................................................57 Try It / Solve It ..............................................................................................................................................58 Lesson 5 - Practice Exercises and Quiz .........................................................................................................60 What Will I Learn? ........................................................................................................................................61 Why Learn It?................................................................................................................................................62 Tell Me / Show Me........................................................................................................................................63 Try It / Solve It ..............................................................................................................................................64

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 1

Lesson 1 - Defining NOT NULL and UNIQUE Constraints

Lesson 1 - Defining NOT NULL and UNIQUE Constraints

Lesson Preparation

Giving constraints meaningful names is important for later documentation. Enforce and practice the naming conventions that apply to all types of constraints: table name_column name_constraint type. For example, a primary-key constraint on the employee_id column of the employees table would be named: emp_emp_id_pk.

What to Watch For Students may have trouble distinguishing between the constraint definitions at the column or table level. Defining constraints will create many syntax errors if not coded carefully. Show students the details of the CREATE TABLE statement: commas end each column definition; parentheses enclose the column definitions; constraint names incorporate underscores if they are made up of more than one word. For example: g_loc_address_pk.

Connections Practice the CREATE TABLE statement with entity diagrams from data modeling such as the ERD from the Animal Shelter activity.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 2

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 3

Why Learn It?

Why Learn It?

Why Learn It?

Share with students other constraints we live with in everyday life. Ask students for their own constraints! - You can't be absent more than three days from school. - You can't access the computer network without entering a username and/or password. - You can't be put on the graduation list with null credits. - You can't have the same student identification number as someone else.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 4

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question: How would you add a new column called birth date to 'MY_TABLE'? ALTER TABLE my_table ADD (birth_date DATE); Read the introduction to introduce constraints.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 5

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review the CREATE TABLE syntax with students. Point out that we cannot tell which columns are the primary or foreign-key columns.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 6

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain that "column level" simply refers to where the constraint is named. Reinforce the naming conventions for constraints and insist that students use them. There are other variations of naming; show students what you expect. Give students the following examples and ask them to create constraint names. Answers will vary. The salary column in the employees table: emp_sal The last_name column in the f_staffs table: f_staffs_last_name The song_id column in the d_track_listings table: d_song_d_track_list Primary-key constraint: tablename_column_name_pk Foreign-key constraint: tablename_column_name_fk Unique-key constraint: tablename_column_name_uk Composite unique-key constraint: tablename_tablename_uk UNIQUE(tablename,tablename) NOT NULL tablename_column_name_nn

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 7

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain that "column level" simply refers to where the constraint is named. Reinforce the naming conventions for constraints and insist that students use them. There are other variations of naming; show students what you expect. Give students the following examples and ask them to create constraint names. Answers will vary. The salary column in the employees table: emp_sal The last_name column in the f_staffs table: f_staffs_last_name The song_id column in the d_track_listings table: d_song_d_track_list Primary-key constraint: tablename_column_name_pk Foreign-key constraint: tablename_column_name_fk Unique-key constraint: tablename_column_name_uk Composite unique-key constraint: tablename_tablename_uk UNIQUE(tablename,tablename) NOT NULL tablename_column_name_nn

.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 8

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review the table-level constraint syntax with students. Point out the comma that ends the column-level constraints. The table-level constraints are then listed below ending with )); Review the graphic. Ask students to write a CREATE TABLE statement that includes the NOT NULL constraint. Use the clients example shown below. In the next section, add the UNIQUE constraint on any column to demonstrate the syntax. CREATE TABLE clients (client_number NUMBER(4) NOT NULL, first_name VARCHAR2(14), last_name VARCHAR2(13)); Oracle limits the length of constraint names to 30 characters.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 9

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review the Violators Beware graphic. Ask students to correct the example. Answers will vary: CREATE TABLE clients ( client_number NUMBER(6) NOT NULL, first_name VARCHAR2(20), last_name VARCHAR2(20), phone VARCHAR2(20), email VARCHAR2(10) NOT NULL, CONSTRAINT clients_phone_email_uk UNIQUE(email,phone)); Alternatively, the constraint could be the following: CONSTRAINT clients_client_num_pk PRIMARY KEY (client_number));

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 10

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Remind students that NOT NULL means that a value must be assigned. Any value that works with the data type defined for the column can be entered. A zero or a letter or a number or even a space are considered "values." A NULL means “no value.”

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 11

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Use the table graphic to show the email columns. If two people lived in the same household and shared an email address, an email address could not be shared in this table. Each email address must be unique.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 12

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask students what they think a unique constraint is and what it means. Then go through the detailed information on the slide. Ask students to give examples of composite unique-key columns. Answers will vary: city and state, last_name and email, student_id and last_name

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 13

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. See table.

2. See table.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 14

3. CREATE TABLE global_locations ( Id NUMBER(4) CONSTRAINT g_loc_id_nn NOT NULL, loc_name VARCHAR2(20), date_opened DATE CONSTRAINT g_loc_date_nn NOT NULL, address VARCHAR2(30) CONSTRAINT g_loc_address_nn NOT NULL, city VARCHAR2(20) CONSTRAINT g_loc_city_nn NOT NULL, zip_postal_code VARCHAR2(20), phone VARCHAR2(15), email VARCHAR2(15) CONSTRAINT g_loc_email_uk UNIQUE, manager_id NUMBER(4), emergency_contact VARCHAR2(20));

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 15

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. Execute the CREATE TABLE statement in HTML DB. 5. DESCRIBE global_locations; 6. CREATE TABLE global_locations ( Id NUMBER(4) CONSTRAINT g_loc_id_nn NOT NULL, loc_name VARCHAR2(20), date_opened DATE CONSTRAINT g_loc_date_nn NOT NULL, address VARCHAR2(30) CONSTRAINT g_loc_address_nn NOT NULL, city VARCHAR2(20) CONSTRAINT g_loc_city_nn NOT NULL, zip_postal_code VARCHAR2(20), phone VARCHAR2(15), email VARCHAR2(15), manager_id NUMBER(4), emergency_contact VARCHAR2(20), CONSTRAINT g_loc_email_uk UNIQUE(email));

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 16

Lesson 2 - PRIMARY KEY, FOREIGN KEY, and CHECK Constraints

Lesson 2 - PRIMARY KEY, FOREIGN KEY, and CHECK Constraints

Lesson Preparation

None. What to Watch For

Constraint syntax may be difficult for students. Practice reading the syntax aloud in sentence form. For example: "The child table column named _______with a data type of _______has a CONSTRAINT named ____________which references its parent table called ______ which has a column called __________." Have students practice writing constraint syntax in an acceptable form. This will discourage easy shortcuts such as naming a constraint "whatever." The constraint will still work, but it’s not a good practice.

Connections None.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 17

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 18

Why Learn It?

Why Learn It?

Why Learn It?

This is a good introduction for why you have constraints. Ask students to respond in writing to the following question: As a database developer, you are caught between what your client envisions you can do and what you know is possible to do. What kinds of constraints will you have to deal with to satisfy your customer and at the same time be able to get the job done? Possible responses: money, time, manpower, space, resistance to change, power struggles among decision makers, customer "wants" versus customer "needs," natural disasters

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 19

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question. When creating a table, how do you specify a column to have a NOT NULL and UNIQUE KEY constraint? client_number NUMBER(7) NOT NULL client_number NUMBER(7) CONSTRAINT clients_client_num_d_clients_nn NOT NULL

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 20

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Make sure students know that the column level simply refers to the area in the CREATE TABLE statement where the columns are defined. The table level refers to the last lines in the statement below where the individual column are defined.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 21

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

To help students remember the syntax for a FOREIGN KEY, restate the column-level syntax as "The child table column named _______with a data type of _______has a CONSTRAINT named ____________which references its parent table called ______ which has a column called __________." For the table-level foreign-key constraint, restate the syntax as "There is a table-level CONSTRAINT named ______which is a FOREIGN KEY (in the ______table); it REFERENCES the parent _________table (which has a column named ______)."

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 22

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Use the tables to explain referential integrity. Ask students for other examples of primary key - foreign key relationships in the DJ on Demand and Global Fast Foods databases.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 23

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Use the examples shown to review the syntax for defining foreign-key constraints at the column and table level.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 24

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 25

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 26

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Use the example shown. Ask students to determine what each constraint is limiting. Answer: cd_numbers must be between 10 and 999; year must be greater than 1996; the producer must be in the list shown.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 27

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Create challenges that require students to first find out what columns in the database tables are designated as primary, foreign, or check. Use the table's primary-foreign key relationships to write out the constraint syntax. Students should be able to create names for their constraints. The student-named constraints may not be exactly like the HTML DB listings, but that's to be expected. Ask students what they think a check constraint is, based on the name.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 28

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. To prevent mistakes, students should write this out on paper or in a word-processing program before creating the tables. animal_id NUMBER(6) name VARCHAR2(25) license_tag_number NUMBER(10) admit_date DATE adoption_id NUMBER(5), vaccination_date DATE

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 29

2. CREATE TABLE animals ( animal_id NUMBER(6), name VARCHAR2(25), license_tag_number NUMBER(10), admit_date DATE CONSTRAINT animals_admit_nn NOT NULL, adoption_id NUMBER(5), vaccination_date DATE CONSTRAINT animals_vacc_nn NOT NULL, CONSTRAINT animal_id_pk PRIMARY KEY(animal_id), CONSTRAINT lic_tag_num_uk UNIQUE (license_tag_number));

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 30

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 3. INSERT INTO animals (animal_id, name, license_tag_number, admit_date, adoption_id, vaccination_date) VALUES (101, 'Spunky', 35540, '10-OCT-04', 205, '12-OCT-04'); 4. Column level: adoption_id NUMBER(5) CONSTRAINT adopt_date_fk REFERENCES adoptions(adoption_id); Table level: CONSTRAINT adopt_date_fk FOREIGN KEY(adoption_id) REFERENCES adoptions (adoption_id);

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 31

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. See student content. Explain that a pseudocolumn behaves like a table column but is not actually stored in the table. You can select from pseudocolumns, but you cannot insert, update, or delete their values. Pseudocolumns (CURRVAL and NEXTVAL, ROWID, ROWNUM) will be used in the Section 15, Lesson 3 sequences. In constraint types listed in the data dictionary, C stands for CHECK, P for PRIMARY KEY, R for REFERENTIAL INTEGRITY, and U for UNIQUE.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 32

Lesson 3 - Managing Constraints

Lesson 3 - Managing Constraints

Lesson Preparation

None. What to Watch For

Help students understand referential integrity and the process of altering constraints. Students will get involved in the syntax and not understand the bigger picture.

Connections Relate the managing of constraints to the work students completed in making ERDs in data modeling. Making sure the correct relationships are identified for all tables is important. In the design of the physical database, the relationships map directly to the constraints for columns in tables. If the correct relationships are not established in the design stage, it may be difficult or impossible to add constraints after data has been entered in the tables. Deleting data to add constraints would not be desirable.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 33

What Will I Learn?

What Will I Learn?

What Will I Learn?

This section's objectives deal with constraint management -- the ability of a DBA to add, modify, enable, disable, or drop constraints. Constraints do the following: - Enforce rules on the data in a table whenever a table row is inserted, updated, or deleted. - Prevent the deletion of a table if there are dependencies from other tables. - Provide rules for Oracle tools, such as Oracle Forms Developer.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 34

Why Learn It?

Why Learn It?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 35

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Show students each required element in the syntax.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 36

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 37

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain the syntax in the example shown. Ask students why ON DELETE CASCADE was added to the foreign-key constraint. Answer: to enable the parent key to be deleted if there is data in the child foreign-key column. Use other tables from the DJ on Demand database such as the foreign-key column song_id in D_PLAY_LIST_ITEMS to practice the foreign-key syntax.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 38

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 39

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

To drop the primary-key constraint on the D_CLIENTS table, the word CASCADE is used to disable the foreign- key constraint in the child tabl ALTER TABLE c_clients DROP PRIMARY KEY CASCADE; Note: Since there is only one primary key for a table, we did not have to specify the column name.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 40

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain the syntax in the examples shown. Ask students to give an example where it might be necessary to disable a constraint in the D_CLIENTS table. Event data needs to be entered in the child D_EVENTS table, but the client_number is not known as yet. Use the SELECT statement syntax to view USER_CONSTRAINTS and USER_CONS_COLUMNS. Although creating indices is not included in this course, students may ask what they are. Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes; they are just used to speed up queries. The following syntax is used to create a unique index on a table. A unique index means that two rows cannot have the same index value. When a primary key is created, a unique index is automatically created. In Oracle9i, the index used to support primary and unique keys can be defined independently of the constraint itself by using the CREATE INDEX syntax within the USING INDEX clause of the CREATE TABLE statement:

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 41

CREATE TABLE student ( student_id NUMBER(6), student_name VARCHAR2(30), course_no NUMBER(2), CONSTRAINT student_student_id_pk PRIMARY KEY(student_id) USING INDEX (CREATE INDEX student_student_pk_idx ON employee(student_id)) ); Once defined, the unique constraint can be dropped without dropping the index. ALTER TABLE student DROP PRIMARY KEY KEEP INDEX; ALTER TABLE student DROP CONSTRAINT student_student_id_pk; CREATE UNIQUE INDEX index_name ON table_name (column_name to be indexed)

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 42

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain the syntax in the examples shown. Ask students to give an example where it might be necessary to disable a constraint in the D_CLIENTS table. Event data needs to be entered in the child D_EVENTS table, but the client_number is not known as yet. Use the SELECT statement syntax to view USER_CONSTRAINTS and USER_CONS_COLUMNS. Although creating indices is not included in this course, students may ask what they are. Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes; they are just used to speed up queries. The following syntax is used to create a unique index on a table. A unique index means that two rows cannot have the same index value. When a primary key is created, a unique index is automatically created. In Oracle9i, the index used to support primary and unique keys can be defined independently of the constraint itself by using the CREATE INDEX syntax within the USING INDEX clause of the CREATE TABLE statement:

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 43

CREATE TABLE student ( student_id NUMBER(6), student_name VARCHAR2(30), course_no NUMBER(2), CONSTRAINT student_student_id_pk PRIMARY KEY(student_id) USING INDEX (CREATE INDEX student_student_pk_idx ON employee(student_id)) ); Once defined, the unique constraint can be dropped without dropping the index. ALTER TABLE student DROP PRIMARY KEY KEEP INDEX; ALTER TABLE student DROP CONSTRAINT student_student_id_pk; CREATE UNIQUE INDEX index_name ON table_name (column_name to be indexed)

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 44

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain that in our previous example, the primary key was DISABLED with the CASCADE option. In this ENABLE statement, the foreign key in the D_EVENTS client_number column will not be enabled. Ask students how they could reestablish the D_EVENTS client_number foreign-key constraint. Answer: Use the ALTER TABLE – ADD CONSTRAINT syntax.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 45

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 46

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Students need to know how to use the SELECT statement to view the data dictionary constraints. Make sure they write this code so that they will be familiar with it. SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'EMPLOYEES';

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 47

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Students need to know how to use the SELECT statement to view the data dictionary constraints. Make sure they write this code so that they will be familiar with it. SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'EMPLOYEES';

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 48

Try It / Solve It

Try It / Solve It

Try It / Solve It

The practice exercises use the d_clients and d_events tables in the DJ on Demand database. Students will work with copies of these two tables named copy_d_clients and copy_d_events. Make sure they have new copies of the tables (without changes made from previous exercises). Remember, tables copied using a subquery do not have the integrity constraints as established in the original tables. When using the SELECT statement to view the constraint name, the tablenames must be all capital letters. Do the practice exercises one at a time. Discuss what is happening and why the error messages are generated. Help students see the bigger picture of referential integrity. Answers: 1. ALTER TABLE copy_d_clients ADD CONSTRAINT copy_d_clients_pk PRIMARY KEY(client_number); 2. ALTER TABLE copy_d_events ADD CONSTRAINT copy_d_events_fk FOREIGN KEY(client_number) REFERENCES copy_d_clients(client_number);

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 49

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 3. SELECT constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE table_name = 'COPY_D_CLIENTS';

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 50

SELECT constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE table_name = 'COPY_D_EVENTS';

4. ALTER TABLE copy_d_clients DROP CONSTRAINT copy_d_clients_pk; ORA-02273: this unique/primary key is referenced by some foreign keys **A primary key cannot be dropped if there are foreign keys referencing it.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 51

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. INSERT INTO copy_d_events (id, name,event_date,description,cost,venue_id,package_code,theme_code,client_number) VALUES(140,'Cline Bas Mitzvah','15-JUL-04','Church and Private Home, formal',4500,105,87,77,7125); ORA-02291: integrity constraint (USTA_SDHS_SQL01_S01.COPY_D_EVENTS_FK) violated - parent key not found **The attempt to drop the primary-key column in the copy_d_clients table was not successful in step 4. Therefore, the attempt to insert a row that has a value in a foreign-key child column that does not have a corresponding value in a parent column will also fail.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 52

6. ALTER TABLE copy_d_clients DISABLE CONSTRAINT copy_d_clients_pk; ORA-02297: cannot disable constraint (USTA_SDHS_SQL01_S01.COPY_D_CLIENTS_PK) - dependencies exist ALTER TABLE copy_d_clients DISABLE CONSTRAINT copy_d_clients_pk CASCADE; If the ALTER TABLE query did not have the CASCADE option, the FOREIGN KEY constraint was not disabled, so the ALTER TABLE DISABLE failed. If the CASCADE option was added to the ALTER TABLE query, the INSERT will be successful.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 53

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 7. INSERT INTO copy_d_events (id, name,event_date,description,cost,venue_id,package_code,theme_code,client_number) VALUES(140,'Cline Bas Mitzvah','15-JUL-04','Church and Private Home, formal',4500,105,87,77,7125) The primary key has been disabled, allowing values to be inserted into the copy_d_events table even though there is no corresponding client_number in the parent table copy_d_clients. 8. ALTER TABLE copy_d_clients ENABLE CONSTRAINT copy_d_clients_pk The ENABLE was successful even though the new addition in the copy_d_events table has a client number not found in the parent table. When enabling a primary key that was disabled with the CASCADE option, the foreign keys are not enabled. 9. The copy_d_clients table would need to have the client number and information added to it before the copy_d_events table could reenable the foreign-key constraint on the client_number column.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 54

Lesson 4 - Practice Exercises and Review

Lesson 4 - Practice Exercises and Review

Lesson Preparation

None. What to Watch For

Constraints are challenging for students. Review each practice exercise and check for understanding.

Connections None.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 55

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 56

Why Learn It?

Why Learn It?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 57

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 58

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: Review the Study Guide and Vocabulary for this section. Students should access self-test software question(s) for review. 1. Constraints enforce rules and prevent deletion of tables when there are dependencies. 2. CHECK constraints specify a condition that must be True. Example: A negative salary amount cannot be entered in a table. 3. Query the data dictionary USER_CONSTRAINTS. 4. When a table is created or added after the table is created (depending on data already in the table). 5. The SYSn name does not explicitly name which column the constraint refers to. 6. Referential integrity prevents additions, deletions, or modifications of information that violates business rules. 7. It is really a personal preference, but NON NULL constraints must be created at the column level and composite keys at the table level.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 59

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 8. To prevent duplicate information in a column that is not a primary-key column 9. Yes, because nulls are not equal to anything -- just can't have identical values in nonnull columns 10. last name and email address 11. one 12. nonduplicate values in a column 13. referential integrity constraint 14. ODC deletes the corresponding foreign key referenced row when parent rows are deleted 15. Release the foreign key constraint on the parent table.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 60

Lesson 5 - Practice Exercises and Quiz

Lesson 5 - Practice Exercises and Quiz

Lesson Preparation

None. What to Watch For

None. Connections

None.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 61

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 62

Why Learn It?

Why Learn It?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 63

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 10 Page 64

Try It / Solve It

Try It / Solve It

Try It / Solve It

Complete and review the practice exercises. Assign SQL Quiz. A passing score is 7 out of 10. Assessment: You may want to encourage students to retake the quiz until they achieve a passing score. Or you may prefer to allow only one attempt at the quiz. Allow 20 minutes for the quiz and 10 minutes for assessment/discussion afterward. Have students work in in pairs to review what they missed on the quiz. Based on the types of questions they missed, have each pair of students choose one question they missed or did not understand to present to the group. Use this discussion to assess student understanding.