Database Programming - Section 10. Instructor Guide

Similar documents
CIS 207 Oracle - Database Programming and SQL HOMEWORK: # 13

Database Programming with SQL

Database Programming - Section 7. Instructor Guide

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Database Programming - Section 11. Instructor Guide

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

CHAPTER4 CONSTRAINTS

Database Programming - Section 18. Instructor Guide

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

A <column constraint> is a constraint that applies to a single column.

Database Programming - Section 1. Instructor Guide

DUE: CD_NUMBER TITLE PRODUCER YEAR 97 Celebrate the Day R & B Inc Holiday Tunes for All Tunes are US 2004

Lecture 07. Spring 2018 Borough of Manhattan Community College

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Consistency The DBMS must ensure the database will always be in a consistent state. Whenever data is modified, the database will change from one

Database Programming with SQL

Database Programming - Section 3. Instructor Guide

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Creating and Managing Tables Schedule: Timing Topic

Indexes (continued) Customer table with record numbers. Source: Concepts of Database Management

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Course Outline and Objectives: Database Programming with SQL

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Programming with SQL

Database Programming - Section 8. Instructor Guide

Oracle Database 10g: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Student Guide Volume 2. D17108GC30 Edition 3.0 January 2009 D57871

3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

Introduction to Computer Science and Business

Oracle Database 10g Express

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

chapter 2 G ETTING I NFORMATION FROM A TABLE

Database Design - Section 11. Instructor Guide

Vendor: Oracle. Exam Code: 1Z Exam Name: Oracle Database: SQL Fundamentals I. Q&As: 292

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Oracle MOOC: SQL Fundamentals

QUETZALANDIA.COM. 5. Data Manipulation Language

Data Definition Language (DDL)

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Creating Other Schema Objects

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Cartesian Product and the Join Operations. Copyright 2008, Oracle. All rights reserved.

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Getting Information from a Table

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Updating Column Values and Deleting Rows. Copyright 2008, Oracle. All rights reserved.

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

ÇALIŞMA TEST SORULARI

BraindumpsVCE. Best vce braindumps-exam vce pdf free download

The Relational Model

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

SQL functions fit into two broad categories: Data definition language Data manipulation language

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Database Design. 9-2 Basic Mapping: The Transformation Process. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

INFSCI 2710 Database Management Solution to Final Exam

Item: 1 (Ref:Cert-1Z )

Tables From Existing Tables

Physical Design of Relational Databases

School of Computing and Information Technology. Examination Paper Autumn Session 2017

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Basic Mapping: The Transformation Process. Copyright 2011, Oracle. All rights reserved.

Oracle Syllabus Course code-r10605 SQL

Triggers- View-Sequence

CS 275 Winter 2011 Problem Set 3

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Exam code: Exam name: Database Fundamentals. Version 16.0

Intermediate SQL ( )

CIS Slightly-different version of Week 10 Lab, also intro to SQL UPDATE and DELETE, and more

From theory to practice. Designing Tables for a postgresql Database System. psql. Reminder. A few useful commands

Oracle Exam 1z0-882 Oracle Certified Professional, MySQL 5.6 Developer Version: 7.0 [ Total Questions: 100 ]

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Appendix A Practices and Solutions

CSC 453 Database Technologies. Tanu Malik DePaul University

Assignment Grading Rubric

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Database Programming - Section 16. Instructor Guide

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

1) Introduction to SQL

5 Integrity Constraints and Triggers

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

EE221 Databases Practicals Manual

Objectives. After completing this lesson, you should be able to do the following:

Oracle Create Table Foreign Key On Delete No

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Database Design - Section 18. Instructor Guide

Downloaded from

Teach Yourself InterBase

Database Programming with SQL

ORACLE 12C NEW FEATURE. A Resource Guide NOV 1, 2016 TECHGOEASY.COM

Creating SQL Tables and using Data Types

Transcription:

Database Programming - Section 10 Instructor Guide

Table of Contents...1 Lesson 1 - Defining NOT NULL and UNIQUE Constraints...1 What Will I Learn?...2 Why Learn It?...3...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...19 Try It / Solve It...28 Lesson 3 - Managing Constraints...32 What Will I Learn?...33 Why Learn It?...34...35 Try It / Solve It...48 Lesson 4 - Practice Exercises and Review...54 What Will I Learn?...55 Why Learn It?...56...57 Try It / Solve It...58 Lesson 5 - Practice Exercises and Quiz...60 What Will I Learn?...61 Why Learn It?...62...63 Try It / Solve It...64 Page i

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

What Will I Learn? What Will I Learn? Page 2

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

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

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

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

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

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

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)); Page 9

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

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

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

Try It / Solve It Try It / Solve It Try It / Solve It Answers: 1. See table. 2. See table. Page 13

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)); Page 14

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)); Page 15

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

What Will I Learn? What Will I Learn? Page 17

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

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

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

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 )." Page 21

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

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

Page 24

Page 25

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

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

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

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)); Page 29

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); Page 30

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

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

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

Why Learn It? Why Learn It? Page 34

Show students each required element in the syntax. Page 35

Page 36

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

Page 38

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

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: Page 40

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) Page 41

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: Page 42

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) Page 43

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

Page 45

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'; Page 46

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'; Page 47

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); Page 48

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'; Page 49

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

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

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

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

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

What Will I Learn? What Will I Learn? Page 55

Why Learn It? Why Learn It? Page 56

Page 57

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

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

Lesson 5 - Practice Exercises and Quiz Lesson 5 - Practice Exercises and Quiz Lesson Preparation None. What to Watch For None. Connections None. Page 60

What Will I Learn? What Will I Learn? Page 61

Why Learn It? Why Learn It? Page 62

Page 63

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