Академический Документы
Профессиональный Документы
Культура Документы
Page 1 of 12
After completing this topic, you should be able to create a table, define constraints on a table, and view constraint information.
Exercise overview
In this exercise, you are required to manage a table and its constraints. This involves the following tasks:
l l l l l
creating a table with column-level and table-level constraints dropping a column from a table adding a new column to a table adding a new constraint to a table viewing constraint information for a table
Let's say that the human resources department of your organization needs you to create a table that stores information for all sales staff who earn commissions and bonuses.
CREATE TABLE[SCHEMA.] table (column datatype [DEFAULT expr] [column_constraint], ... [table_constraint][,...]);
a unique order identification number that an employee enters for each sale the identification number for each employee the bonus percentage that each employee earns the monthly sales for each employee the salary for each employee
To prevent users from entering invalid data in the table, you need to include some constraint definitions when you create the table.
Step 1 of 6
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 2 of 12
You need to create a table named commission to store data for all employees who earn commission. Type the command to complete the code that will create the table.
Result
Step 2 of 6
Now you want to define a column of the VARCHAR2 datatype called order_id with a size of 10. You also want to define a column-level PRIMARY KEY constraint on this column with a system-assigned name. Identify the code you need to enter to do this.
CREATE TABLE commission ( MISSING CODE Options: 1. order_id VARCHAR2 CONSTRAINT order_order_id_pk PRIMARY KEY, 2. order_id VARCHAR2(10) CONSTRAINT PRIMARY KEY(order_id), 3. order_id VARCHAR2(10) PRIMARY KEY;
Result
You use the code order_id VARCHAR2(10) PRIMARY KEY to define a column of the VARCHAR2 datatype called order_id with a size of 10 and to include a column-level PRIMARY KEY constraint with a system-assigned name on this column. Option 1 is incorrect. This code creates a named PRIMARY KEY constraint on the order_id column with the name order_order_id_pk. Furthermore, because the code does not specify the size of the VARCHAR2 datatype, Oracle assumes the default size of 1.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 3 of 12
Option 2 is incorrect. This is invalid syntax for a column level constraint. At the column level you use the CONSTRAINT keyword only when you want to specify a name for the constraint. Additionally, it is only when you declare a PRIMARY KEY constraint at the table level that you need to specify the columns in the key. Option 3 is correct. You omit the CONSTRAINT constraint_name part of the syntax to allow the system to generate a name for the constraint using the format SYS_Cn, where n is a unique integer.
Step 3 of 6
Next you need to define the second of five columns, a column of the NUMBER datatype called employee_id that can store numbers with six digits. Enter the code to complete the column definition.
CREATE TABLE commission ( order_id VARCHAR2(10) PRIMARY KEY, employee_id MISSING CODE
Result
To complete the column specification you specify the NUMBER(6) datatype with the column size in parentheses, followed by a comma.
Next you define a column to hold the bonus percentage that each employee earns.
CREATE TABLE commission( order_id VARCHAR2 (10) PRIMARY KEY, employee_id NUMBER (6), bonus_pct NUMBER(2,2)
Step 4 of 6
You want to define a column-level constraint on the bonus_pct column that specifies that the column cannot contain null values. Type the code to complete the constraint definition.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 4 of 12
order_id VARCHAR2 (10) PRIMARY KEY, employee_id NUMBER(6), bonus_pct NUMBER(2,2) CONSTRAINT comm_bonus_pct_nn MISSING CODE,
Result
To complete the constraint definition, you specify the constraint type NOT NULL.
You enter the column definitions for the monthly_sales and salary columns. The code for this is:
CREATE TABLE commission( order_id VARCHAR2 (10) PRIMARY KEY, employee_id NUMBER(6), bonus_pct NUMBER(2,2) CONSTRAINT comm_bonus_pct_nn NOT NULL, monthly_sales NUMBER(10,2) salary NUMBER(8,2),
Step 5 of 6
Next you need to define a referential constraint on the employee_id column. Type the keywords to indicate that the employee_id column is a foreign key.
CREATE TABLE commission( order_id VARCHAR2 (10) PRIMARY KEY, employee_id NUMBER(6), bonus_pct NUMBER(2,2) CONSTRAINT comm_bonus_pct_nn NOT NULL, monthly_sales NUMBER(10,2) salary NUMBER(8,2), CONSTRAINT emp_emp_id_fk MISSING CODE (employee_id)
Result
You use the FOREIGN KEY keyword to specify that the employee_id column is a foreign key.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 5 of 12
Now you need to complete the FOREIGN KEY constraint by specifying that it references the employee_id column in the employees table. Choose the code that will do this.
CREATE TABLE commission( order_id VARCHAR2 (10) PRIMARY KEY, employee_id NUMBER(6), bonus_pct NUMBER(2,2) CONSTRAINT comm_bonus_pct_nn NOT NULL, monthly_sales NUMBER(10,2) salary NUMBER(8,2), CONSTRAINT emp_emp_id_fk FOREIGN KEY (employee_id) MISSING CODE Options: 1. REFERENCES employees(employee_id) 2. PRIMARY KEY employees (employee_id) 3. REFERENCES employees.employee_id
Result
You use the code REFERENCES employees(employee_id) to specify that the FOREIGN KEY constraint references the employee_id column in the employees table. Option 1 is correct. The FOREIGN KEY constraint includes the REFERENCES keyword, which specifies the parent table and the column or set of columns to which the FOREIGN KEY refers. Option 2 is incorrect. You use the keyword REFERENCES to identify the parent table and the column or set of columns in the parent table which makes up its primary key. The parent table's primary key would have been created with a PRIMARY KEY constraint. Option 3 is incorrect. You specify the column (or columns) of the primary key in the parent table within parentheses, not with dot notation.
You close the parentheses around the table definition and execute the code. Oracle then creates the new table and applies the constraints you've defined.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 6 of 12
order_id VARCHAR2 (10) PRIMARY KEY, employee_id NUMBER(6), bonus_pct NUMBER(2,2) CONSTRAINT comm_bonus_pct_nn NOT NULL, monthly_sales NUMBER(10,2) salary NUMBER(8,2), CONSTRAINT emp_emp_id_fk FOREIGN KEY (employee_id) REFERENCES employees(employee_id));
Step 1 of 2
Because a salary column already exists in the employees table in the database, you decide to drop the salary column in the commission table. Enter the command to make changes to the table.
Result
Step 2 of 2
To complete the code, you need to specify that you want to remove the salary column. Identify the code to do this.
ALTER TABLE commission MISSING CODE Options: 1. DELETE (salary); 2. DROP (salary);
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 7 of 12
3. TRUNCATE (salary);
Result
You use the DROP clause and supply the column name in parentheses to drop the salary column from the table. Option 1 is incorrect. DELETE is not a clause of the ALTER TABLE command but a command in its own right that you use to remove data from a row. Option 2 is correct. To remove one or more columns in one ALTER TABLE statement, you use the DROP clause followed by the list of columns enclosed in parentheses. Alternatively, to drop only one column you can use the DROP COLUMN clause. Option 3 is incorrect. TRUNCATE is not a clause of the ALTER TABLE command but a command in its own right that you use to remove all the rows from a table.
You execute the code and Oracle drops the salary column.
Step 1 of 2
You need to make a new column in the commission table to hold the commissions that employees earn each month. Type the command to make a new column.
Result
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 8 of 12
Step 2 of 2
You need to specify that the new monthly_commission column must be of the NUMBER datatype and that it must hold 10-digit values with two digits to the right of the decimal point. Identify the code that specifies the name, size, and datatype of the new column.
ALTER TABLE commission ADD MISSING CODE Options: 1. 2. 3. 4. (monthly_commission, NUMBER, 10, 2) monthly_commission NUMBER(10,2) (monthly_commission NUMBER(8,2)) (monthly_commission NUMBER(10,2))
Result
You use the code (monthly_commission NUMBER(10,2)) to specify the required name, size, and datatype of the new column. Option 1 is incorrect. You do not separate the column name and its datatype with a comma. Additionally, you specify the precision and scale of the NUMBER datatype within parentheses after the NUMBER keyword. Option 2 is incorrect. The list of columns that you add to a table with the ADD clause needs to be enclosed within parentheses. Option 3 is incorrect. The size specification here is for an eight-digit number with two of the eight digits appearing to the right of the decimal point. Option 4 is correct. The optional attributes you set for a NUMBER datatype are its precision and scale. The first value is the precision, or total number of digits, and the second value is the scale, or number of digits to the right of the decimal point.
You execute the code and Oracle adds the new column to the commission table.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 9 of 12
Step 1 of 4
You need to define a new constraint on the commission table to ensure that the bonus percentage for any employee does not exceed 10 percent. Type the command to modify the existing constraints on the commission table.
Result
You use the ALTER TABLE command to modify the existing constraints on a table.
Step 2 of 4
Now you need to specify that you want to define a new constraint named on the commission table. Type the command to do this.
Result
You use the ADD CONSTRAINT command to specify a new constraint for a table.
Step 3 of 4
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 10 of 12
The new constraint named bonus_bonus_pct_ck needs to impose a condition on the bonus_pct column. Which constraint type do you need to specify to do this?
ALTER TABLE commission ADD CONSTRAINT comm_bonus_pct_ck MISSING CODE Options: 1. 2. 3. 4. NOT NULL UNIQUE CHECK PRIMARY KEY
Result
You specify that the new constraint is a CHECK constraint because it needs to impose a condition on the bonus_pct column. Option 1 is incorrect. A NOT NULL constraint forbids the entry of null values in a column. Option 2 is incorrect. A UNIQUE constraint specifies that each value in a column must be unique. Option 3 is correct. A column with a CHECK constraint specifies a condition that must be true for all values in a column. For instance, you can declare a CHECK constraint that only allows a value within a certain range or a string of a certain length. Option 4 is incorrect. A PRIMARY KEY constraint designates a column in which the values uniquely identify each row in a table.
Step 4 of 4
Now you need to specify the condition that the value in the bonus_pct column cannot exceed 10. Type the condition for the CHECK constraint.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 11 of 12
Result
You enter the condition BONUS_PCT <=10 to ensure that the bonus_pct column can't store values that exceed 10.
You execute the code and Oracle adds the new constraint to the commission table.
ALTER TABLE commission ADD CONSTRAINT comm_bonus_pct_ck CHECK (bonus_pct <= 10);
Step 1 of 2
Now that you have created and modified the commission table, you want to view all the constraint information for the table to check that the constraints are correct. Type the name of the view that you query to do this.
Result
You query the USER_CONSTRAINTS view in the data dictionary to view all constraint information for a table.
Step 2 of 2
You need to specify a condition in a WHERE clause so that you view the constraint information for the commission table only.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007
Page 12 of 12
Type the expression that enters the condition in the WHERE clause.
SELECT constraint_name, constraint_type, search_condition FROM USER_CONSTRAINTS WHERE table_name = MISSING CODE;
Result
You type the table name 'COMMISSION' using all uppercase letters in single quotation marks.
You execute the code and Oracle displays the constraint information for the commission table.
Table of Contents
| Top of page | | Learning objective | | Exercise overview | | Task 1: Creating tables with constraints | | Task 2: Dropping a column | | Task 3: Adding a column | | Task 4: Adding a constraint | | Task 5: Viewing constraint information |
Copyright 2003 SkillSoft. All rights reserved. SkillSoft and the SkillSoft logo are trademarks or registered trademarks of SkillSoft in the United States and certain other countries. All other logos or trademarks are the property of their respective owners.
http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html
5/11/2007