Instructions
Lab #2: Tables
Learn about records, tables, Data Definition Language (DDL) commands, and database constraints.
- Lab
- Description
- Help Section
- Instructions
Lab Instructions
This lab has ten unit testing SQL script files, and one integration SQL script file. Each unit SQL script file contains the steps to manage table creation in a re-runnable script file. The integration SQL script file calls the unit test programs in the proper sequence necessary due to dependencies between the scripts.
[40 points] Click the Lab Instructions link to open the instructions inside the current webpage.
Lab Instructions →
Please perform the following tasks:
You copy all the necessary files from that working directory’s lib1/create
subdirectory to that working directory’s lab2
subdirectory. After you copy the files you will rename them and edit them in the rest of Lab #2.
Setting Up Lab 2 Directory →
This section provides all the supporting instructions to copy and rename the base files from the Linux Command-Line Interface (CLI). Using the Linux CLI is a necessary skill in the IT industry, which you will practice in every lab.
- Open
Konsole
and change to thelab2
directory with the following absolute syntax:cd /home/student/Data/cit225/oracle/lab2
Verify your working directory with the following command:
pwd
The command should show you that you’re in the correct directory:
/home/student/Data/cit225/oracle/lab2
From this directory, you can copy the necessary files with the following command when you a:
cp /home/student/Data/cit225/oracle/lib1/create/* /home/student/Data/cit225/oracle/lab2
- After copying the files from the
lib1/create
subdirectory, you should be able to display them with thell
(long list) Linux command in thelab2
subdirectory, like:ll
The
ll
command should display the following contents of thelab2
subdirectory:-rw-r--r--. 1 student wheel 6012 Sep 16 01:38 address.sql -rw-r--r--. 1 student wheel 6876 Sep 16 01:38 common_lookup.sql -rw-r--r--. 1 student wheel 5968 Sep 16 01:38 contact.sql -rw-r--r--. 1 student wheel 3364 Sep 16 01:38 create_oracle_store2.sql -rw-r--r--. 1 student wheel 5174 Sep 16 01:38 item.sql -rw-r--r--. 1 student wheel 5822 Sep 16 01:38 member.sql -rw-r--r--. 1 student wheel 5034 Sep 16 01:38 rental_item.sql -rw-r--r--. 1 student wheel 4961 Sep 16 01:38 rental.sql -rw-r--r--. 1 student wheel 4987 Sep 16 01:38 street_address.sql -rw-r--r--. 1 student wheel 5325 Sep 16 01:38 system_user.sql -rw-r--r--. 1 student wheel 6155 Sep 16 01:38 telephone.sql
- After copying and verifying the file copy, change the name of the
create_oracle_store2.sql
script file toapply_oracle_lab2.sql
. You can make that change with the followingmv
(move) command:mv create_oracle_store2.sql apply_oracle_lab2.sql
- The other ten files require that you append a “
_lab
” substring inside each file name before the “.sql
” file suffix. This would require ten separate commands unless you script a solution.You may not know how to write the script solution, so here is a scripting solution that you can use by creating a rename.sh file in your
lab2
subdirectory. It is a simple Bash shell script.#!/bin/bash # Read all but the apply_oracle_lab2.sql file and rename them. for i in $(ls *[^2].sql); do mv $i ${i/./_lab.} done
Create a
rename.sh
script in yourlab2
subdirectory, which you can do with KDevelop or KWrite utilities in thelab2
subdirectory. Then, change therename.sh
privileges to make it an executable file with the chmod (Change Modification) utility, likechmod 755 rename.sh
You can run the file from the
lab2
subdirectory with this syntax:./rename.sh
After running the
rename.sh
command, you can verify the changes with thell
(long list) Linux command from inside the working directory:ll lab2/*[^2].sql
It should display the following ten files:
-rw-r--r--. 1 student wheel 6012 Sep 16 02:05 lab2/address_lab.sql -rw-r--r--. 1 student wheel 6876 Sep 16 02:05 lab2/common_lookup_lab.sql -rw-r--r--. 1 student wheel 5968 Sep 16 02:05 lab2/contact_lab.sql -rw-r--r--. 1 student wheel 5174 Sep 16 02:05 lab2/item_lab.sql -rw-r--r--. 1 student wheel 5822 Sep 16 02:05 lab2/member_lab.sql -rw-r--r--. 1 student wheel 5034 Sep 16 02:05 lab2/rental_item_lab.sql -rw-r--r--. 1 student wheel 4961 Sep 16 02:05 lab2/rental_lab.sql -rw-r--r--. 1 student wheel 4987 Sep 16 02:05 lab2/street_address_lab.sql -rw-r--r--. 1 student wheel 5325 Sep 16 02:05 lab2/system_user_lab.sql -rw-r--r--. 1 student wheel 6155 Sep 16 02:05 lab2/telephone_lab.sql
You begin these steps by running two scripts:
- The
/home/student/Data/cit225/oracle/lib1/utility/cleanup_oracle.sql
script. - The
/home/student/Data/cit225/oracle/lib1/create/create_oracle_store2.sql
script.
You put these two scripts into your apply_oracle_lab2.sql
script, which should be in the following directory:
/home/student/Data/cit225/lab2 |
The create_oracle_store2.sql
script creates the following ten tables by calling ten other script files:
SYSTEM_USER
COMMON_LOOKUP
MEMBER
CONTACT
ADDRESS
STREET_ADDRESS
TELEPHONE
ITEM
RENTAL
RENTAL_ITEM
You call the apply_oracle_lab1.sql
script file as the first step in your apply_oracle_lab2.sql
script file. You won’t write the apply_oracle_lab2.sql
script file or any of the other files from scratch.
At this point, you should consider your working home directory as:
/home/student/Data/cit225/oracle |
Tutorial for Editing a Table Creation Script →
This section provides supporting instructions to how you edit the ten table creation script files. You should review it carefully because it will save you time.
- Edit each unit test file to create mirrored tables (tables with mirrored structures, like columns and constraints) with an
_lab
suffix by doing the following:- Change the program name value in the comment section.
- Change the bind variable from
system_user
tosystem_user_lab
.-- ------------------------------------------------------------------ -- Create and assign bind variable for table name. -- ------------------------------------------------------------------ VARIABLE table_name VARCHAR2(30) BEGIN :table_name := UPPER('system_user'); END; /
It should look like this:
-- ------------------------------------------------------------------ -- Create and assign bind variable for table name. -- ------------------------------------------------------------------ VARIABLE table_name VARCHAR2(30) BEGIN :table_name := UPPER('system_user_lab'); END; /
- Change the
CREATE TABLE
statement fromsystem_user
tosystem_user_lab
. - Change the
system_user_id
column name tosystem_user_lab_id
. - Change the not null constraint names from
nn_system_user_x
tonn_system_user_lab_x
. - Change all foreign key lowercase references from
fk_system_user_x
tofk_system_user_lab_x
. - Change the
pk_system_user
constraint name topk_system_user_lab
; and thesystem_user_id
primary key column tosystem_user_lab_id
. - Change the
fk_system_user_1
andfk_system_user_2
tofk_system_user_lab_1
andfk_system_user_lab_2
respectively; and- Change the
system_user
table name tosystem_user_lab
table name in theREFERENCES
clause - Change the
system_user_id
column name tosystem_user_lab_id
column name in theREFERENCES
clause.
- Change the
- Change the
uq_system_user_1
index name touq_system_user_lab_1
. - Change the
system_user_s1
sequence name tosystem_user_lab_s1
.
It should look like this:
-- Create table. CREATE TABLE system_user_lab ( system_user_lab_id NUMBER , system_user_name VARCHAR2(20) CONSTRAINT nn_system_user_lab_1 NOT NULL , system_user_group_id NUMBER CONSTRAINT nn_system_user_lab_2 NOT NULL , system_user_type NUMBER CONSTRAINT nn_system_user_lab_3 NOT NULL , first_name VARCHAR2(20) , middle_name VARCHAR2(20) , last_name VARCHAR2(20) , created_by NUMBER CONSTRAINT nn_system_user_lab_4 NOT NULL , creation_date DATE CONSTRAINT nn_system_user_lab_5 NOT NULL , last_updated_by NUMBER CONSTRAINT nn_system_user_lab_6 NOT NULL , last_update_date DATE CONSTRAINT nn_system_user_lab_7 NOT NULL , CONSTRAINT pk_system_user_lab_1 PRIMARY KEY(system_user_lab_id) , CONSTRAINT fk_system_user_lab_1 FOREIGN KEY(created_by) REFERENCES system_user_lab(system_user_lab_id) , CONSTRAINT fk_system_user_lab_2 FOREIGN KEY(last_updated_by) REFERENCES system_user_lab(system_user_lab_id));
More or less the changes for the SYSTEM_USER_LAB script should look like the following:
- Edit the
apply_oracle_lab2.sql
file. Open theapply_oracle_lab2.sql
file and add the following command on line 16:12 13 14 15 16
-- ------------------------------------------------------------------ -- This creates tables, sequences, indexes, and constraints necessary -- to begin lesson #3. Demonstrates proper process and syntax. -- ------------------------------------------------------------------ @@/home/student/Data/cit225/oracle/lab1/apply_oracle_lab1.sql
- Edit the integration file by changing all the script references from file names like
system_user.sql
to the new files from yourlab2
folder such that they look likesystem_user_lab.sql
.It should look like this when you have finished the edit:
@@system_user_lab.sql @@common_lookup_lab.sql @@member_lab.sql @@contact_lab.sql @@address_lab.sql @@street_address_lab.sql @@telephone_lab.sql @@rental_lab.sql @@item_lab.sql @@rental_item_lab.sql
After you’ve made all those changes, you are ready to perform an integration test by running the
apply_oracle_lab2.sql
file.
- [2 points] Create the
SYSTEM_USER_LAB
table described by the following and theSYSTEM_USER_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, system_user.sql
, into a new unit test file, system_user_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: SYSTEM_USER_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
SYSTEM_USER_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
SYSTEM_USER_NAME | NOT NULL | String | 20 | ||
SYSTEM_USER_GROUP_ID | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_LAB_ID | Integer | Maximum |
NOT NULL | |||||
SYSTEM_USER_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_LAB_ID | Integer | Maximum |
NOT NULL | |||||
FIRST_NAME | String | 20 | |||
MIDDLE_NAME | String | 20 | |||
LAST_NAME | String | 20 | |||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A16 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
TABLE_NAME COLUMN_ID COLUMN_NAME NULLABLE DATA_TYPE ---------------- --------- ---------------------- -------- ------------ SYSTEM_USER_LAB 1 SYSTEM_USER_LAB_ID NOT NULL NUMBER(22) SYSTEM_USER_LAB 2 SYSTEM_USER_NAME NOT NULL VARCHAR2(20) SYSTEM_USER_LAB 3 SYSTEM_USER_GROUP_ID NOT NULL NUMBER(22) SYSTEM_USER_LAB 4 SYSTEM_USER_TYPE NOT NULL NUMBER(22) SYSTEM_USER_LAB 5 FIRST_NAME VARCHAR2(20) SYSTEM_USER_LAB 6 MIDDLE_NAME VARCHAR2(20) SYSTEM_USER_LAB 7 LAST_NAME VARCHAR2(20) SYSTEM_USER_LAB 8 CREATED_BY NOT NULL NUMBER(22) SYSTEM_USER_LAB 9 CREATION_DATE NOT NULL DATE SYSTEM_USER_LAB 10 LAST_UPDATED_BY NOT NULL NUMBER(22) SYSTEM_USER_LAB 11 LAST_UPDATE_DATE NOT NULL DATE 11 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type = UPPER('c') ORDER BY uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - NN_SYSTEM_USER_LAB_1 "SYSTEM_USER_NAME" IS NOT NULL C NN_SYSTEM_USER_LAB_2 "SYSTEM_USER_GROUP_ID" IS NOT NULL C NN_SYSTEM_USER_LAB_3 "SYSTEM_USER_TYPE" IS NOT NULL C NN_SYSTEM_USER_LAB_4 "CREATED_BY" IS NOT NULL C NN_SYSTEM_USER_LAB_5 "CREATION_DATE" IS NOT NULL C NN_SYSTEM_USER_LAB_6 "LAST_UPDATED_BY" IS NOT NULL C NN_SYSTEM_USER_LAB_7 "LAST_UPDATE_DATE" IS NOT NULL C 7 rows selected. |
You can validate that you created the two self-referencing foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ------------------------------------------ FK_SYSTEM_USER_LAB_1 REFERENCES (SYSTEM_USER_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_SYSTEM_USER_LAB_2 REFERENCES (SYSTEM_USER_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
Please note that the foreign keys on the SYSTEM_USER_GROUP_ID
and SYSTEM_USER_TYPE
columns haven’t been created at this point. That’s because they can’t exists until you create the COMMON_LOOKUP_LAB
table.
You should then create a UQ_SYSTEM_USER_LAB_1
unique index on the SYSTEM_USER_NAME
column of the SYSTEM_USER_LAB
table. You can then use this query to display the unique constants on the table:
COLUMN index_name FORMAT A20 HEADING "Index Name" SELECT index_name FROM user_indexes WHERE table_name = :table_name; |
It should display:
Index Name -------------------- PK_SYSTEM_USER_LAB_1 UQ_SYSTEM_USER_LAB_1 |
You can verify the existence of the SYSTEM_USER_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- SYSTEM_USER_LAB_S1 1 row selected. |
- [2 points] Create the
COMMON_LOOKUP_LAB
table described by the following and theCOMMON_LOOKUP_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, common_lookup.sql
, into a new unit test file, common_lookup_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: COMMON_LOOKUP_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
COMMON_LOOKUP_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
COMMON_LOOKUP_CONTEXT | NOT NULL | String | 30 | ||
COMMON_LOOKUP_TYPE | NOT NULL | String | 30 | ||
COMMON_LOOKUP_MEANING | NOT NULL | String | 30 | ||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
TABLE_NAME COLUMN_ID COLUMN_NAME NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ COMMON_LOOKUP_LAB 1 COMMON_LOOKUP_LAB_ID NOT NULL NUMBER(22) COMMON_LOOKUP_LAB 2 COMMON_LOOKUP_CONTEXT NOT NULL VARCHAR2(30) COMMON_LOOKUP_LAB 3 COMMON_LOOKUP_TYPE NOT NULL VARCHAR2(30) COMMON_LOOKUP_LAB 4 COMMON_LOOKUP_MEANING NOT NULL VARCHAR2(30) COMMON_LOOKUP_LAB 5 CREATED_BY NOT NULL NUMBER(22) COMMON_LOOKUP_LAB 6 CREATION_DATE NOT NULL DATE COMMON_LOOKUP_LAB 7 LAST_UPDATED_BY NOT NULL NUMBER(22) COMMON_LOOKUP_LAB 8 LAST_UPDATE_DATE NOT NULL DATE 8 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_CLOOKUP_LAB_1 P NN_CLOOKUP_LAB_1 "COMMON_LOOKUP_CONTEXT" IS NOT NULL C NN_CLOOKUP_LAB_2 "COMMON_LOOKUP_TYPE" IS NOT NULL C NN_CLOOKUP_LAB_3 "COMMON_LOOKUP_MEANING" IS NOT NULL C NN_CLOOKUP_LAB_4 "CREATED_BY" IS NOT NULL C NN_CLOOKUP_LAB_5 "CREATION_DATE" IS NOT NULL C NN_CLOOKUP_LAB_6 "LAST_UPDATED_BY" IS NOT NULL C NN_CLOOKUP_LAB_7 "LAST_UPDATE_DATE" IS NOT NULL C 8 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ----------------------------------------- FK_CLOOKUP_LAB_1 REFERENCES (COMMON_LOOKUP_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_CLOOKUP_LAB_2 REFERENCES (COMMON_LOOKUP_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You should then create a unique COMMON_LOOKUP_LAB_U1
unique index on the COMMON_LOOKUP_CONTEXT
and COMMON_LOOKUP_TYPE
columns of the COMMON_LOOKUP
table; and a non-unique COMMON_LOOKUP_LAB_N1
index on the COMMON_LOOKUP_CONTEXT
column of the COMMON_LOOKUP
table:
COLUMN sequence_name FORMAT A22 HEADING "Sequence Name" COLUMN column_position FORMAT 999 HEADING "Column|Position" COLUMN column_name FORMAT A22 HEADING "Column|Name" SELECT ui.index_name , uic.column_position , uic.column_name FROM user_indexes ui INNER JOIN user_ind_columns uic ON ui.index_name = uic.index_name AND ui.table_name = uic.table_name WHERE ui.table_name = :table_name ORDER BY ui.index_name , uic.column_position; |
It should display:
Column Column Index Name Position Name -------------------- -------- ---------------------- COMMON_LOOKUP_LAB_N1 1 COMMON_LOOKUP_CONTEXT COMMON_LOOKUP_LAB_U2 1 COMMON_LOOKUP_CONTEXT COMMON_LOOKUP_LAB_U2 2 COMMON_LOOKUP_TYPE PK_CLOOKUP_LAB_1 1 COMMON_LOOKUP_LAB_ID |
You add two new foreign keys to the SYSTEM_USER_LAB
table. They should:
- Add a
FK_SYSTEM_USER_3
foreign key to theSYSTEM_USER_LAB
table. It should be placed on on theSYSTEM_USER_GROUP_ID
column of theSYSTEM_USER_LAB
table; and it should reference theCOMMON_LOOKUP_LAB_ID
column of theCOMMON_LOOKUP_LAB
table. - Add a
FK_SYSTEM_USER_4
foreign key to theSYSTEM_USER_LAB
table. It should be placed on on theSYSTEM_USER_TYPE
column of theSYSTEM_USER_LAB
table; and it should reference theCOMMON_LOOKUP_LAB_ID
column of theCOMMON_LOOKUP_LAB
table.
Run the following query after you add the new foreign key constraints.
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.position = ucc2.position -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should show you the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ---------------------------------------- FK_SYSTEM_USER_1 REFERENCES (SYSTEM_USER.CREATED_BY) (SYSTEM_USER.SYSTEM_USER_ID) FK_SYSTEM_USER_2 REFERENCES (SYSTEM_USER.LAST_UPDATED_BY) (SYSTEM_USER.SYSTEM_USER_ID) FK_SYSTEM_USER_3 REFERENCES (SYSTEM_USER.SYSTEM_USER_GROUP_ID) (COMMON_LOOKUP.COMMON_LOOKUP_ID) FK_SYSTEM_USER_4 REFERENCES (SYSTEM_USER.SYSTEM_USER_TYPE) (COMMON_LOOKUP.COMMON_LOOKUP_ID) |
You can verify the existence of the COMMON_LOOKUP_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- COMMON_LOOKUP_LAB_S1 1 row selected. |
- [2 points] Create the
MEMBER_LAB
table described by the following and theMEMBER_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, member.sql
, into a new unit test file, member_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: MEMBER_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
MEMBER_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
MEMBER_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_LAB_ID | Integer | Maximum |
ACCOUNT_NUMBER | NOT NULL | String | 10 | ||
CREDIT_CARD_NUMBER | NOT NULL | String | 20 | ||
CREDIT_CARD_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_ID | Integer | Maximum |
NOT NULL | |||||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
TABLE_NAME COLUMN_ID COLUMN_NAME NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ MEMBER_LAB 1 MEMBER_LAB_ID NOT NULL NUMBER(22) MEMBER_LAB 2 MEMBER_TYPE NUMBER(22) MEMBER_LAB 3 ACCOUNT_NUMBER NOT NULL VARCHAR2(10) MEMBER_LAB 4 CREDIT_CARD_NUMBER NOT NULL VARCHAR2(19) MEMBER_LAB 5 CREDIT_CARD_TYPE NOT NULL NUMBER(22) MEMBER_LAB 6 CREATED_BY NOT NULL NUMBER(22) MEMBER_LAB 7 CREATION_DATE NOT NULL DATE MEMBER_LAB 8 LAST_UPDATED_BY NOT NULL NUMBER(22) MEMBER_LAB 9 LAST_UPDATE_DATE NOT NULL DATE 9 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_MEMBER_LAB_1 P NN_MEMBER_LAB_2 "ACCOUNT_NUMBER" IS NOT NULL C NN_MEMBER_LAB_3 "CREDIT_CARD_NUMBER" IS NOT NULL C NN_MEMBER_LAB_4 "CREDIT_CARD_TYPE" IS NOT NULL C NN_MEMBER_LAB_5 "CREATED_BY" IS NOT NULL C NN_MEMBER_LAB_6 "CREATION_DATE" IS NOT NULL C NN_MEMBER_LAB_7 "LAST_UPDATED_BY" IS NOT NULL C NN_MEMBER_LAB_8 "LAST_UPDATE_DATE" IS NOT NULL C 9 rows selected. |
You can validate that you created the four foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ----------------------------------------- FK_MEMBER_LAB_1 REFERENCES (MEMBER_LAB.MEMBER_TYPE) (COMMON_LOOKUP_LAB.COMMON_LOOKUP_LAB_ID) FK_MEMBER_LAB_2 REFERENCES (MEMBER_LAB.CREDIT_CARD_TYPE) (COMMON_LOOKUP_LAB.COMMON_LOOKUP_LAB_ID) FK_MEMBER_LAB_3 REFERENCES (MEMBER_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_MEMBER_LAB_4 REFERENCES (MEMBER_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You should then create a MEMBER_N1
non-unique index on the CREDIT_CARD_TYPE
column of the MEMBER
table:
COLUMN sequence_name FORMAT A22 HEADING "Sequence Name" COLUMN column_position FORMAT 999 HEADING "Column|Position" COLUMN column_name FORMAT A22 HEADING "Column|Name" SELECT ui.index_name , uic.column_position , uic.column_name FROM user_indexes ui INNER JOIN user_ind_columns uic ON ui.index_name = uic.index_name AND ui.table_name = uic.table_name WHERE ui.table_name = :table_name AND NOT ui.index_name IN (SELECT constraint_name FROM user_constraints) ORDER BY ui.index_name , uic.column_position; |
It should display:
COLUMN COLUMN INDEX Name POSITION Name -------------------- -------- ---------------------- MEMBER_LAB_N1 1 CREDIT_CARD_TYPE |
You can verify the existence of the MEMBER_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- MEMBER_LAB_S1 |
- [2 points] Create the
CONTACT_LAB
table described by the following and theCONTACT_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, contact.sql
, into a new unit test file, contact_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: CONTACT_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
CONTACT_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
MEMBER_LAB_ID | FOREIGN KEY | MEMBER_LAB | MEMBER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CONTACT_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_NAME | NOT NULL | String | 20 | ||
FIRST_NAME | NOT NULL | String | 20 | ||
MIDDLE_NAME | String | 20 | |||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ CONTACT_LAB 1 CONTACT_LAB_ID NOT NULL NUMBER(22) CONTACT_LAB 2 MEMBER_LAB_ID NOT NULL NUMBER(22) CONTACT_LAB 3 CONTACT_TYPE NOT NULL NUMBER(22) CONTACT_LAB 4 FIRST_NAME NOT NULL VARCHAR2(20) CONTACT_LAB 5 MIDDLE_NAME VARCHAR2(20) CONTACT_LAB 6 LAST_NAME NOT NULL VARCHAR2(20) CONTACT_LAB 7 CREATED_BY NOT NULL NUMBER(22) CONTACT_LAB 8 CREATION_DATE NOT NULL DATE CONTACT_LAB 9 LAST_UPDATED_BY NOT NULL NUMBER(22) CONTACT_LAB 10 LAST_UPDATE_DATE NOT NULL DATE 10 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_CONTACT_LAB_1 P NN_CONTACT_LAB_1 "MEMBER_LAB_ID" IS NOT NULL C NN_CONTACT_LAB_2 "CONTACT_TYPE" IS NOT NULL C NN_CONTACT_LAB_3 "FIRST_NAME" IS NOT NULL C NN_CONTACT_LAB_4 "LAST_NAME" IS NOT NULL C NN_CONTACT_LAB_5 "CREATED_BY" IS NOT NULL C NN_CONTACT_LAB_6 "CREATION_DATE" IS NOT NULL C NN_CONTACT_LAB_7 "LAST_UPDATED_BY" IS NOT NULL C NN_CONTACT_LAB_8 "LAST_UPDATE_DATE" IS NOT NULL C 9 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ----------------------------------------- FK_CONTACT_LAB_1 REFERENCES (CONTACT_LAB.MEMBER_ID) (MEMBER_LAB.MEMBER_LAB_ID) FK_CONTACT_LAB_2 REFERENCES (CONTACT_LAB.CONTACT_TYPE) (COMMON_LOOKUP_LAB.COMMON_LOOKUP_LAB_ID) FK_CONTACT_LAB_3 REFERENCES (CONTACT_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_CONTACT_LAB_4 REFERENCES (CONTACT_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You should then create a CONTACT_N1
non-unique index on the MEMBER_ID
column of the CONTACT
table, and a CONTACT_N2
non-unique index on the CONTACT_TYPE
column of the CONTACT
table:
COLUMN sequence_name FORMAT A22 HEADING "Sequence Name" COLUMN column_position FORMAT 999 HEADING "Column|Position" COLUMN column_name FORMAT A22 HEADING "Column|Name" SELECT ui.index_name , uic.column_position , uic.column_name FROM user_indexes ui INNER JOIN user_ind_columns uic ON ui.index_name = uic.index_name AND ui.table_name = uic.table_name WHERE ui.table_name = :table_name AND NOT ui.index_name IN (SELECT constraint_name FROM user_constraints) ORDER BY ui.index_name , uic.column_position; |
It should display:
Column Column Index Name Position Name -------------------- -------- ---------------------- CONTACT_LAB_N1 1 MEMBER_LAB_ID CONTACT_LAB_N2 1 CONTACT_TYPE |
You can verify the existence of the CONTACT_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- CONTACT_LAB_S1 |
- [2 points] Create the
ADDRESS_LAB
table described by the following and theADDRESS_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, address.sql
, into a new unit test file, address_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: ADDRESS_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
ADDRESS_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
CONTACT_LAB_ID | FOREIGN KEY | CONTACT_LAB | CONTACT_LAB_ID | Integer | Maximum |
NOT NULL | |||||
ADDRESS_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CITY | NOT NULL | String | 30 | ||
STATE_PROVINCE | NOT NULL | String | 30 | ||
POSTAL_CODE | NOT NULL | String | 20 | ||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
Column TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ ADDRESS_LAB 1 ADDRESS_LAB_ID NOT NULL NUMBER(22) ADDRESS_LAB 2 CONTACT_LAB_ID NOT NULL NUMBER(22) ADDRESS_LAB 3 ADDRESS_TYPE NOT NULL NUMBER(22) ADDRESS_LAB 4 CITY NOT NULL VARCHAR2(30) ADDRESS_LAB 5 STATE_PROVINCE NOT NULL VARCHAR2(30) ADDRESS_LAB 6 POSTAL_CODE NOT NULL VARCHAR2(20) ADDRESS_LAB 7 CREATED_BY NOT NULL NUMBER(22) ADDRESS_LAB 8 CREATION_DATE NOT NULL DATE ADDRESS_LAB 9 LAST_UPDATED_BY NOT NULL NUMBER(22) ADDRESS_LAB 10 LAST_UPDATE_DATE NOT NULL DATE 10 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_ADDRESS_LAB_1 P NN_ADDRESS_LAB_1 "CONTACT_LAB_ID" IS NOT NULL C NN_ADDRESS_LAB_2 "ADDRESS_TYPE" IS NOT NULL C NN_ADDRESS_LAB_3 "CITY" IS NOT NULL C NN_ADDRESS_LAB_4 "STATE_PROVINCE" IS NOT NULL C NN_ADDRESS_LAB_5 "POSTAL_CODE" IS NOT NULL C NN_ADDRESS_LAB_6 "CREATED_BY" IS NOT NULL C NN_ADDRESS_LAB_7 "CREATION_DATE" IS NOT NULL C NN_ADDRESS_LAB_8 "LAST_UPDATED_BY" IS NOT NULL C NN_ADDRESS_LAB_9 "LAST_UPDATE_DATE" IS NOT NULL C 10 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- -------------------------------------- FK_ADDRESS_LAB_1 REFERENCES (ADDRESS_LAB.CONTACT_ID) (CONTACT_LAB.CONTACT_LAB_ID) FK_ADDRESS_LAB_2 REFERENCES (ADDRESS_LAB.ADDRESS_TYPE) (COMMON_LOOKUP_LAB.COMMON_LOOKUP_LAB_ID) FK_ADDRESS_LAB_3 REFERENCES (ADDRESS_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_ADDRESS_LAB_4 REFERENCES (ADDRESS_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You should then create a ADDRESS_LAB_N1
non-unique index on the CONTACT_ID
column of the ADDRESS_LAB
table, and a CONTACT_N2
non-unique index on the ADDRESS_TYPE
column of the ADDRESS_LAB
table:
COLUMN sequence_name FORMAT A22 HEADING "Sequence Name" COLUMN column_position FORMAT 999 HEADING "Column|Position" COLUMN column_name FORMAT A22 HEADING "Column|Name" SELECT ui.index_name , uic.column_position , uic.column_name FROM user_indexes ui INNER JOIN user_ind_columns uic ON ui.index_name = uic.index_name AND ui.table_name = uic.table_name WHERE ui.table_name = :table_name AND NOT ui.index_name IN (SELECT constraint_name FROM user_constraints) ORDER BY ui.index_name , uic.column_position; |
It should display:
COLUMN COLUMN INDEX Name POSITION Name -------------------- -------- ---------------------- ADDRESS_LAB_N1 1 CONTACT_LAB_ID ADDRESS_LAB_N2 1 ADDRESS_TYPE |
You can verify the existence of the ADDRESS_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- ADDRESS_LAB_S1 |
- [2 points] Create the
STREET_ADDRESS_LAB
table described by the following and theSTREET_ADDRESS_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, street_address.sql
, into a new unit test file, street_address_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: STREET_ADDRESS_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
STREET_ADDRESS_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
ADDRESS_LAB_ID | FOREIGN KEY | ADDRESS_LAB | ADDRESS_LAB_ID | Integer | Maximum |
NOT NULL | |||||
STREET_ADDRESS | NOT NULL | String | 30 | ||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ STREET_ADDRESS_LAB 1 STREET_ADDRESS_LAB_ID NOT NULL NUMBER(22) STREET_ADDRESS_LAB 2 ADDRESS_LAB_ID NOT NULL NUMBER(22) STREET_ADDRESS_LAB 3 STREET_ADDRESS NOT NULL VARCHAR2(30) STREET_ADDRESS_LAB 4 CREATED_BY NOT NULL NUMBER(22) STREET_ADDRESS_LAB 5 CREATION_DATE NOT NULL DATE STREET_ADDRESS_LAB 6 LAST_UPDATED_BY NOT NULL NUMBER(22) STREET_ADDRESS_LAB 7 LAST_UPDATE_DATE NOT NULL DATE 7 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_SADDRESS_LAB_1 P NN_SADDRESS_LAB_1 "ADDRESS_LAB_ID" IS NOT NULL C NN_SADDRESS_LAB_2 "STREET_ADDRESS" IS NOT NULL C NN_SADDRESS_LAB_3 "CREATED_BY" IS NOT NULL C NN_SADDRESS_LAB_4 "CREATION_DATE" IS NOT NULL C NN_SADDRESS_LAB_5 "LAST_UPDATED_BY" IS NOT NULL C NN_SADDRESS_LAB_6 "LAST_UPDATE_DATE" IS NOT NULL C 7 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ---------------------------------------- FK_SADDRESS_LAB_1 REFERENCES (STREET_ADDRESS_LAB.ADDRESS_LAB_ID) (ADDRESS_LAB.ADDRESS_LAB_ID) FK_SADDRESS_LAB_2 REFERENCES (STREET_ADDRESS_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_SADDRESS_LAB_3 REFERENCES (STREET_ADDRESS_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You can verify the existence of the STREET_ADDRESS_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A22 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name ---------------------- STREET_ADDRESS_LAB_S1 |
- [2 points] Create the
TELEPHONE_LAB
table described by the following and theTELEPHONE_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, telephone.sql
, into a new unit test file, telephone_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: TELEPHONE_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
TELEPHONE_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
CONTACT_LAB_ID | FOREIGN KEY | CONTACT_LAB | CONTACT_LAB_ID | Integer | Maximum |
NOT NULL | |||||
ADDRESS_LAB_ID | FOREIGN KEY | ADDRESS_LAB | ADDRESS_LAB_ID | Integer | Maximum |
TELEPHONE_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_LAB_ID | Integer | Maximum |
NOT NULL | |||||
COUNTRY_CODE | NOT NULL | String | 3 | ||
AREA_CODE | NOT NULL | String | 6 | ||
TELEPHONE_NUMBER | NOT NULL | String | 10 | ||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
Column TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ TELEPHONE_LAB 1 TELEPHONE_LAB_ID NOT NULL NUMBER(22) TELEPHONE_LAB 2 CONTACT_LAB_ID NOT NULL NUMBER(22) TELEPHONE_LAB 3 ADDRESS_LAB_ID NUMBER(22) TELEPHONE_LAB 4 TELEPHONE_TYPE NOT NULL NUMBER(22) TELEPHONE_LAB 5 COUNTRY_CODE NOT NULL VARCHAR2(3) TELEPHONE_LAB 6 AREA_CODE NOT NULL VARCHAR2(6) TELEPHONE_LAB 7 TELEPHONE_NUMBER NOT NULL VARCHAR2(10) TELEPHONE_LAB 8 CREATED_BY NOT NULL NUMBER(22) TELEPHONE_LAB 9 CREATION_DATE NOT NULL DATE TELEPHONE_LAB 10 LAST_UPDATED_BY NOT NULL NUMBER(22) TELEPHONE_LAB 11 LAST_UPDATE_DATE NOT NULL DATE 11 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_TELEPHONE_LAB_1 P NN_TELEPHONE_LAB_1 "CONTACT_ID" IS NOT NULL C NN_TELEPHONE_LAB_2 "TELEPHONE_TYPE" IS NOT NULL C NN_TELEPHONE_LAB_3 "COUNTRY_CODE" IS NOT NULL C NN_TELEPHONE_LAB_4 "AREA_CODE" IS NOT NULL C NN_TELEPHONE_LAB_5 "TELEPHONE_NUMBER" IS NOT NULL C NN_TELEPHONE_LAB_6 "CREATED_BY" IS NOT NULL C NN_TELEPHONE_LAB_7 "CREATION_DATE" IS NOT NULL C NN_TELEPHONE_LAB_8 "LAST_UPDATED_BY" IS NOT NULL C NN_TELEPHONE_LAB_9 "LAST_UPDATE_DATE" IS NOT NULL C 10 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- -------------------------------------- FK_TELEPHONE_LAB_1 REFERENCES (TELEPHONE_LAB.CONTACT_LAB_ID) (CONTACT_LAB.CONTACT_LAB_ID) FK_TELEPHONE_LAB_2 REFERENCES (TELEPHONE_LAB.TELEPHONE_TYPE) (COMMON_LOOKUP_LAB.COMMON_LOOKUP_LAB_ID) FK_TELEPHONE_LAB_3 REFERENCES (TELEPHONE_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_TELEPHONE_LAB_4 REFERENCES (TELEPHONE_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You should then create a TELEPHONE_LAB_N1
non-unique index on the CONTACT_ID
and ADDRESS_ID
columns of the TELEPHONE_LAB
table, a TELEPHONE_N2
non-unique index on the ADDRESS_ID
column of the TELEPHONE_LAB
table, and a TELEPHONE_N3
non-unique index on the TELEPHONE_TYPE
column of the TELEPHONE_LAB
table:
COLUMN sequence_name FORMAT A22 HEADING "Sequence Name" COLUMN column_position FORMAT 999 HEADING "Column|Position" COLUMN column_name FORMAT A22 HEADING "Column|Name" SELECT ui.index_name , uic.column_position , uic.column_name FROM user_indexes ui INNER JOIN user_ind_columns uic ON ui.index_name = uic.index_name AND ui.table_name = uic.table_name WHERE ui.table_name = :table_name AND NOT ui.index_name IN (SELECT constraint_name FROM user_constraints) ORDER BY ui.index_name , uic.column_position; |
It should display:
Column Column Index Name Position Name -------------------- -------- ---------------------- TELEPHONE_LAB_N1 1 CONTACT_LAB_ID TELEPHONE_LAB_N1 2 ADDRESS_LAB_ID TELEPHONE_LAB_N2 1 ADDRESS_LAB_ID TELEPHONE_LAB_N3 1 TELEPHONE_TYPE |
You can verify the existence of the TELEPHONE_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- TELEPHONE_LAB_S1 |
- [2 points] Create the
RENTAL_LAB
table described by the following and theRENTAL_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, rental.sql
, into a new unit test file, rental_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: RENTAL_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
RENTAL_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
CUSTOMER_ID | FOREIGN KEY | CONTACT_LAB | CONTACT_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CHECK_OUT_DATE | NOT NULL | DATE | Date | ||
RETURN_DATE | NOT NULL | DATE | Date | ||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
Column TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ RENTAL_LAB 1 RENTAL_LAB_ID NOT NULL NUMBER(22) RENTAL_LAB 2 CUSTOMER_ID NOT NULL NUMBER(22) RENTAL_LAB 3 CHECK_OUT_DATE NOT NULL DATE RENTAL_LAB 4 RETURN_DATE NOT NULL DATE RENTAL_LAB 5 CREATED_BY NOT NULL NUMBER(22) RENTAL_LAB 6 CREATION_DATE NOT NULL DATE RENTAL_LAB 7 LAST_UPDATED_BY NOT NULL NUMBER(22) RENTAL_LAB 8 LAST_UPDATE_DATE NOT NULL DATE 8 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_RENTAL_LAB_1 P NN_RENTAL_LAB_1 "CUSTOMER_ID" IS NOT NULL C NN_RENTAL_LAB_2 "CHECK_OUT_DATE" IS NOT NULL C NN_RENTAL_LAB_3 "RETURN_DATE" IS NOT NULL C NN_RENTAL_LAB_4 "CREATED_BY" IS NOT NULL C NN_RENTAL_LAB_5 "CREATION_DATE" IS NOT NULL C NN_RENTAL_LAB_6 "LAST_UPDATED_BY" IS NOT NULL C NN_RENTAL_LAB_7 "LAST_UPDATE_DATE" IS NOT NULL C 8 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ---------------------------------------- FK_RENTAL_LAB_1 REFERENCES (RENTAL_LAB.CUSTOMER_ID) (CONTACT_LAB.CONTACT_LAB_ID) FK_RENTAL_LAB_2 REFERENCES (RENTAL_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_RENTAL_LAB_3 REFERENCES (RENTAL_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You can verify the existence of the RENTAL_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- RENTAL_LAB_S1 |
- [2 points] Create the
ITEM_LAB
table described by the following and theITEM_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, item.sql
, into a new unit test file, item_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: ITEM_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
ITEM_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
ITEM_BARCODE | NOT NULL | String | 14 | ||
ITEM_TYPE | FOREIGN KEY | COMMON_LOOKUP_LAB | COMMON_LOOKUP_ID | Integer | Maximum |
NOT NULL | |||||
ITEM_TITLE | NOT NULL | String | 60 | ||
ITEM_SUBTITLE | String | 60 | |||
ITEM_RATING | NOT NULL | String | 8 | ||
ITEM_RELEASE_DATE | NOT NULL | DATE | Date | ||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
Column TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ ITEM_LAB 1 ITEM_LAB_ID NOT NULL NUMBER(22) ITEM_LAB 2 ITEM_BARCODE NOT NULL VARCHAR2(14) ITEM_LAB 3 ITEM_TYPE NOT NULL NUMBER(22) ITEM_LAB 4 ITEM_TITLE NOT NULL VARCHAR2(60) ITEM_LAB 5 ITEM_SUBTITLE VARCHAR2(60) ITEM_LAB 6 ITEM_RATING NOT NULL VARCHAR2(8) ITEM_LAB 7 ITEM_RELEASE_DATE NOT NULL DATE ITEM_LAB 8 CREATED_BY NOT NULL NUMBER(22) ITEM_LAB 9 CREATION_DATE NOT NULL DATE ITEM_LAB 10 LAST_UPDATED_BY NOT NULL NUMBER(22) ITEM_LAB 11 LAST_UPDATE_DATE NOT NULL DATE 11 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_ITEM_LAB_1 P NN_ITEM_LAB_1 "ITEM_BARCODE" IS NOT NULL C NN_ITEM_LAB_2 "ITEM_TYPE" IS NOT NULL C NN_ITEM_LAB_3 "ITEM_TITLE" IS NOT NULL C NN_ITEM_LAB_4 "ITEM_RATING" IS NOT NULL C NN_ITEM_LAB_5 "ITEM_RELEASE_DATE" IS NOT NULL C NN_ITEM_LAB_6 "CREATED_BY" IS NOT NULL C NN_ITEM_LAB_7 "CREATION_DATE" IS NOT NULL C NN_ITEM_LAB_8 "LAST_UPDATED_BY" IS NOT NULL C NN_ITEM_LAB_9 "LAST_UPDATE_DATE" IS NOT NULL C 10 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A38 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ---------------------------------------- FK_ITEM_LAB_1 REFERENCES (ITEM_LAB.ITEM_TYPE) (COMMON_LOOKUP_LAB.COMMON_LOOKUP_LAB_ID) FK_ITEM_LAB_2 REFERENCES (ITEM_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_ITEM_LAB_3 REFERENCES (ITEM_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You can verify the existence of the ITEM_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- ITEM_LAB_S1 |
- [2 points] Create the
RENTAL_ITEM_LAB
table described by the following and theRENTAL_ITEM_LAB_S1
sequence starting with a value of1001
:
Instruction Details →
Use the pattern of change explained in the command-line or GUI menu tutorial above to change base unit test file, rental_item.sql
, into a new unit test file, rental_item_lab.sql
. After making the necessary changes in the file, run the script by itself (a unit test) and verify the contents of the text-based log file.
Table Name: RENTAL_ITEM_LAB | |||||
---|---|---|---|---|---|
Column Name | Constraint | Data Type |
Physical Size |
||
Type | Reference Table | Reference Column | |||
RENTAL_ITEM_LAB_ID | PRIMARY KEY | Integer | Maximum | ||
RENTAL_LAB_ID | FOREIGN KEY | RENTAL_LAB | RENTAL_LAB_ID | Integer | Maximum |
NOT NULL | |||||
ITEM_LAB_ID | FOREIGN KEY | ITEM_LAB | ITEM_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
CREATION_DATE | NOT NULL | DATE | Date | ||
LAST_UPDATED_BY | FOREIGN KEY | SYSTEM_USER_LAB | SYSTEM_USER_LAB_ID | Integer | Maximum |
NOT NULL | |||||
LAST_UPDATE_DATE | NOT NULL | DATE | Date |
You should use the following formatting and query to verify completion of this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SET NULL '' COLUMN table_name FORMAT A18 COLUMN column_id FORMAT 9999 COLUMN column_name FORMAT A22 COLUMN data_type FORMAT A12 SELECT table_name , column_id , column_name , CASE WHEN nullable = 'N' THEN 'NOT NULL' ELSE '' END AS nullable , CASE WHEN data_type IN ('CHAR','VARCHAR2','NUMBER') THEN data_type||'('||data_length||')' ELSE data_type END AS data_type FROM user_tab_columns WHERE table_name = :table_name ORDER BY 2; |
It should display the following results:
Column TABLE_NAME COLUMN_ID Name NULLABLE DATA_TYPE ------------------ --------- ---------------------- -------- ------------ RENTAL_ITEM_LAB 1 RENTAL_ITEM_LAB_ID NOT NULL NUMBER(22) RENTAL_ITEM_LAB 2 RENTAL_LAB_ID NOT NULL NUMBER(22) RENTAL_ITEM_LAB 3 ITEM_LAB_ID NOT NULL NUMBER(22) RENTAL_ITEM_LAB 4 CREATED_BY NOT NULL NUMBER(22) RENTAL_ITEM_LAB 5 CREATION_DATE NOT NULL DATE RENTAL_ITEM_LAB 6 LAST_UPDATED_BY NOT NULL NUMBER(22) RENTAL_ITEM_LAB 7 LAST_UPDATE_DATE NOT NULL DATE 7 rows selected. |
You should use the following formatting and query to verify completion of the constraints for this step:
1 2 3 4 5 6 7 8 9 10 11 12 13 | COLUMN constraint_name FORMAT A22 COLUMN search_condition FORMAT A36 COLUMN constraint_type FORMAT A1 SELECT uc.constraint_name , uc.search_condition , uc.constraint_type FROM user_constraints uc INNER JOIN user_cons_columns ucc ON uc.table_name = ucc.table_name AND uc.constraint_name = ucc.constraint_name WHERE uc.table_name = :table_name AND uc.constraint_type IN (UPPER('c'),UPPER('p')) ORDER BY uc.constraint_type DESC , uc.constraint_name; |
It should display the following results:
CONSTRAINT_NAME SEARCH_CONDITION C ---------------------- ------------------------------------ - PK_RENTAL_ITEM_LAB_1 P NN_RENTAL_ITEM_LAB_1 "RENTAL_LAB_ID" IS NOT NULL C NN_RENTAL_ITEM_LAB_2 "ITEM_LAB_ID" IS NOT NULL C NN_RENTAL_ITEM_LAB_3 "CREATED_BY" IS NOT NULL C NN_RENTAL_ITEM_LAB_4 "CREATION_DATE" IS NOT NULL C NN_RENTAL_ITEM_LAB_5 "LAST_UPDATED_BY" IS NOT NULL C NN_RENTAL_ITEM_LAB_6 "LAST_UPDATE_DATE" IS NOT NULL C 7 rows selected. |
You can validate that you created the two foreign key constraints with the following query:
COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column" COL references_column FORMAT A40 HEADING "References:| Table.Column" SELECT uc.constraint_name||CHR(10) || '('||ucc1.table_name||'.'||ucc1.column_name||')' constraint_source , 'REFERENCES'||CHR(10) || '('||ucc2.table_name||'.'||ucc2.column_name||')' references_column FROM user_constraints uc , user_cons_columns ucc1 , user_cons_columns ucc2 WHERE uc.constraint_name = ucc1.constraint_name AND uc.r_constraint_name = ucc2.constraint_name AND ucc1.POSITION = ucc2.POSITION -- Correction for multiple column primary keys. AND uc.constraint_type = 'R' AND ucc1.table_name = :table_name ORDER BY ucc1.table_name , uc.constraint_name; |
It should display only the following:
Constraint Name: References: Table.Column Table.Column -------------------------------------- ---------------------------------------- FK_RENTAL_ITEM_LAB_1 REFERENCES (RENTAL_ITEM_LAB.RENTAL_LAB_ID) (RENTAL_LAB.RENTAL_LAB_ID) FK_RENTAL_ITEM_LAB_2 REFERENCES (RENTAL_ITEM_LAB.ITEM_LAB_ID) (ITEM_LAB.ITEM_LAB_ID) FK_RENTAL_ITEM_LAB_3 REFERENCES (RENTAL_ITEM_LAB.CREATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) FK_RENTAL_ITEM_LAB_4 REFERENCES (RENTAL_ITEM_LAB.LAST_UPDATED_BY) (SYSTEM_USER_LAB.SYSTEM_USER_LAB_ID) |
You can verify the existence of the ITEM_LAB_S1
sequence with the following query:
COLUMN sequence_name FORMAT A20 HEADING "Sequence Name" SELECT sequence_name FROM user_sequences WHERE sequence_name = :table_name||'_S1'; |
It should display the following:
Sequence Name -------------------- RENTAL_ITEM_LAB_S1 |
- [0 points] You can confirm the creation of the ten tables with the following query, which you should add to your
apply_oracle_lab2.sql
file:
Confirmation Details for Tables →
COLUMN table_name_base FORMAT A30 HEADING "Base Tables" COLUMN table_name_lab FORMAT A30 HEADING "Lab Tables" SELECT a.table_name_base , b.table_name_lab FROM (SELECT table_name AS table_name_base FROM user_tables WHERE table_name IN ('SYSTEM_USER' ,'COMMON_LOOKUP' ,'MEMBER' ,'CONTACT' ,'ADDRESS' ,'STREET_ADDRESS' ,'TELEPHONE' ,'ITEM' ,'RENTAL' ,'RENTAL_ITEM')) a INNER JOIN (SELECT table_name AS table_name_lab FROM user_tables WHERE table_name IN ('SYSTEM_USER_LAB' ,'COMMON_LOOKUP_LAB' ,'MEMBER_LAB' ,'CONTACT_LAB' ,'ADDRESS_LAB' ,'STREET_ADDRESS_LAB' ,'TELEPHONE_LAB' ,'ITEM_LAB' ,'RENTAL_LAB' ,'RENTAL_ITEM_LAB')) b ON a.table_name_base = SUBSTR( b.table_name_lab, 1, REGEXP_INSTR(table_name_lab,'_LAB') - 1) ORDER BY CASE WHEN table_name_base LIKE 'SYSTEM_USER%' THEN 0 WHEN table_name_base LIKE 'COMMON_LOOKUP%' THEN 1 WHEN table_name_base LIKE 'MEMBER%' THEN 2 WHEN table_name_base LIKE 'CONTACT%' THEN 3 WHEN table_name_base LIKE 'ADDRESS%' THEN 4 WHEN table_name_base LIKE 'STREET_ADDRESS%' THEN 5 WHEN table_name_base LIKE 'TELEPHONE%' THEN 6 WHEN table_name_base LIKE 'ITEM%' THEN 7 WHEN table_name_base LIKE 'RENTAL%' AND NOT table_name_base LIKE 'RENTAL_ITEM%' THEN 8 WHEN table_name_base LIKE 'RENTAL_ITEM%' THEN 9 END; |
It returns:
Base Tables Lab Tables ------------------------------ ------------------------------ SYSTEM_USER SYSTEM_USER_LAB COMMON_LOOKUP COMMON_LOOKUP_LAB MEMBER MEMBER_LAB CONTACT CONTACT_LAB ADDRESS ADDRESS_LAB STREET_ADDRESS STREET_ADDRESS_LAB TELEPHONE TELEPHONE_LAB ITEM ITEM_LAB RENTAL RENTAL_LAB RENTAL_ITEM RENTAL_ITEM_LAB 10 rows selected. |
- [0 points] You can confirm the creation of the ten sequences with the following query, which you should add to your
apply_oracle_lab2.sql
file:
Confirmation Details for Sequences →
COLUMN sequence_name_base FORMAT A30 HEADING "Base Sequences" COLUMN sequence_name_lab FORMAT A30 HEADING "Lab Sequences" SELECT a.sequence_name_base , b.sequence_name_lab FROM (SELECT sequence_name AS sequence_name_base FROM user_sequences WHERE sequence_name IN ('SYSTEM_USER_S1' ,'COMMON_LOOKUP_S1' ,'MEMBER_S1' ,'CONTACT_S1' ,'ADDRESS_S1' ,'STREET_ADDRESS_S1' ,'TELEPHONE_S1' ,'ITEM_S1' ,'RENTAL_S1' ,'RENTAL_ITEM_S1')) a INNER JOIN (SELECT sequence_name AS sequence_name_lab FROM user_sequences WHERE sequence_name IN ('SYSTEM_USER_LAB_S1' ,'COMMON_LOOKUP_LAB_S1' ,'MEMBER_LAB_S1' ,'CONTACT_LAB_S1' ,'ADDRESS_LAB_S1' ,'STREET_ADDRESS_LAB_S1' ,'TELEPHONE_LAB_S1' ,'ITEM_LAB_S1' ,'RENTAL_LAB_S1' ,'RENTAL_ITEM_LAB_S1')) b ON SUBSTR(a.sequence_name_base, 1, REGEXP_INSTR(a.sequence_name_base,'_S1') - 1) = SUBSTR( b.sequence_name_lab, 1, REGEXP_INSTR(b.sequence_name_lab,'_LAB_S1') - 1) ORDER BY CASE WHEN sequence_name_base LIKE 'SYSTEM_USER%' THEN 0 WHEN sequence_name_base LIKE 'COMMON_LOOKUP%' THEN 1 WHEN sequence_name_base LIKE 'MEMBER%' THEN 2 WHEN sequence_name_base LIKE 'CONTACT%' THEN 3 WHEN sequence_name_base LIKE 'ADDRESS%' THEN 4 WHEN sequence_name_base LIKE 'STREET_ADDRESS%' THEN 5 WHEN sequence_name_base LIKE 'TELEPHONE%' THEN 6 WHEN sequence_name_base LIKE 'ITEM%' THEN 7 WHEN sequence_name_base LIKE 'RENTAL%' AND NOT sequence_name_base LIKE 'RENTAL_ITEM%' THEN 8 WHEN sequence_name_base LIKE 'RENTAL_ITEM%' THEN 9 END; |
It returns:
Base Sequences Lab Sequences ------------------------------ ------------------------------ SYSTEM_USER_S1 SYSTEM_USER_LAB_S1 COMMON_LOOKUP_S1 COMMON_LOOKUP_LAB_S1 MEMBER_S1 MEMBER_LAB_S1 CONTACT_S1 CONTACT_LAB_S1 ADDRESS_S1 ADDRESS_LAB_S1 STREET_ADDRESS_S1 STREET_ADDRESS_LAB_S1 TELEPHONE_S1 TELEPHONE_LAB_S1 ITEM_S1 ITEM_LAB_S1 RENTAL_S1 RENTAL_LAB_S1 RENTAL_ITEM_S1 RENTAL_ITEM_LAB_S1 10 rows selected. |
Test Case
Click the Test Case Instructions link to open the test case instructions inside the current webpage.
Test Case Instructions →
After you write the apply_oracle_lab2.sql
script, you should navigate to the /home/student/Data/cit225/oracle/lab2
directory. You call the sqlplus
utility from the same directory and then you run the apply_oracle_lab2.sql
script with the following syntax:
@apply_oracle_lab2.sql |
You should submit your apply_oracle_lab2.sql
script file and apply_oracle_lab2.txt
log file for a grade.