Create Tables

download Create Tables

of 12

Transcript of Create Tables

  • 7/27/2019 Create Tables

    1/12

    | Print | Contents | Close |

    Creating tables and constraints in Oracle9i

    Learning objective

    fter completing this topic, you should be able to create a table, define constraints on

    a table, and view constraint information.

    l creating a table with column-level and table-level constraints

    l dropping a column from a table

    l adding a new column to a table

    l adding a new constraint to a table

    l viewing constraint information for a table

    l a unique order identification number that an employee enters for each sale

    l the identification number for each employee

    l the bonus percentage that each employee earnsl the monthly sales for each employee

    l the salary for each employee

    Step 1 of 6

    Exercise overview

    In this exercise, you are required to manage a table and its constraints.

    This involves the following tasks:

    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][,...]);

    The table needs columns to store

    To prevent users from entering invalid data in the table, you need to include some

    constraint definitions when you create the table.

    Task 1: Creating tables with constraints

    Let's say that the human resources department of your organization needs you to create a

    table with column-level and table-level constraints.

    Page 1 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    2/12

    Result

    Step 2 of 6

    Result

    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.

    MI SSI NG CODEcommission

    You use the CREATE TABLE command to create a table.

    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 (

    MI SSI NG 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;

    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 namedPRIMARY KEYconstraint 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.

    Page 2 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    3/12

    Step 3 of 6

    Result

    Step 4 of 6

    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.

    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_idMI SSI NG CODE

    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)

    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.

    CREATE TABLE commission(

    Page 3 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    4/12

    Result

    Step 5 of 6

    Result

    order_id VARCHAR2 (10) PRIMARY KEY,

    employee_id NUMBER(6),

    bonus_pct NUMBER(2,2) CONSTRAINT

    comm_bonus_pct_nn MI SSI NG CODE,

    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),

    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) CONSTRAINTcomm_bonus_pct_nn NOT NULL,

    monthly_sales NUMBER(10,2)

    salary NUMBER(8,2),

    CONSTRAINT emp_emp_id_fk MI SSI NG CODE(employee_id)

    You use the FOREIGN KEY keyword to specify that the employee_id column is aforeign key.

    Page 4 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    5/12

    Step 6 of 6

    Result

    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)

    MI SSI NG CODE

    Options:

    1. REFERENCES employees(employee_id)

    2. PRIMARY KEY employees (employee_id)

    3. REFERENCES employees.employee_id

    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 KEYconstraint includes the REFERENCES

    keyword, which specifies the parent table and the column or set of columns to

    which the FOREIGN KEYrefers.

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

    CREATE TABLE commission(

    Page 5 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    6/12

    Step 1 of 2

    Result

    Step 2 of 2

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

    Task 2: Dropping a column

    You are required to drop a column from thre table that stores information for all sales staff

    who earn commissions and bonuses.

    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.

    MI SSI NG CODEcommission

    You use the ALTER TABLE command to make changes to a table.

    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

    MI SSI NG CODE

    Options:

    1. DELETE (salary);

    2. DROP (salary);

    Page 6 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    7/12

    Result

    Step 1 of 2

    Result

    3. TRUNCATE (salary);

    You use the DROP clause and supply the column name in parentheses to drop

    the salary column from the table.

    Option 1 is incorrect. DELETEis not a clause of the ALTER TABLEcommand 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 DROPclause followed by the list of columns enclosed in

    parentheses. Alternatively, to drop only one column you can use the DROP

    COLUMNclause.

    Option 3 is incorrect. TRUNCATEis not a clause of the ALTER TABLEcommand

    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.

    ALTER TABLE commission

    DROP (salary);

    Task 3: Adding a column

    You are now required to add a new column to the table storing the information on sales

    staff that earning commission.

    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.

    ALTER TABLE commission

    MI SSI NG CODE

    Page 7 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    8/12

    Step 2 of 2

    Result

    You enter the ADD command to add a column to a table.

    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 MI SSI NG CODE

    Options:

    1. (monthly_commission, NUMBER, 10, 2)

    2. monthly_commission NUMBER(10,2)

    3. (monthly_commission NUMBER(8,2))

    4. (monthly_commission NUMBER(10,2))

    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.

    ALTER TABLE commission

    Page 8 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    9/12

    Step 1 of 4

    Result

    Step 2 of 4

    Result

    Step 3 of 4

    ADD (monthly_commission NUMBER(10,2));

    Task 4: Adding a constraint

    You are required a add a constraint on the commission table.

    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.

    MI SSI NG CODEcommission

    You use the ALTER TABLE command to modify the existing constraints on a

    table.

    Now you need to specify that you want to define a new constraint named on the

    commission table.

    Type the command to do this.

    ALTER TABLE commission

    MI SSI NG CODE

    You use the ADD CONSTRAINT command to specify a new constraint for a table.

    Page 9 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    10/12

    Result

    Step 4 of 4

    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 MI SSI NG CODE

    Options:

    1. NOT NULL

    2. UNIQUE

    3. CHECK

    4. PRIMARY KEY

    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 UNIQUEconstraint specifies that each value in a column

    must be unique.

    Option 3 is correct. A column with a CHECKconstraint 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 KEYconstraint designates a column in which

    the values uniquely identify each row in a table.

    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.

    ALTER TABLE commission

    ADD CONSTRAINT comm_bonus_pct_ck CHECKMI SSI NG CODE;

    Page 10 of 12SkillSoft Learning Object

    5/11/2007http://xlibrary.skillport.com/courseware/cbtlib/67002/68154/eng/consim/transcript.html

  • 7/27/2019 Create Tables

    11/12

    Result

    Step 1 of 2

    Result

    Step 2 of 2

    You enter the condition BONUS_PCT

  • 7/27/2019 Create Tables

    12/12

    Result

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

    Type the expression that enters the condition in the WHERE clause.

    SELECT constraint_name, constraint_type,

    search_condition

    FROM USER_CONSTRAINTS

    WHERE table_name = MI SSI NG CODE;

    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.

    SELECT constraint_name, constraint_type,

    search_condition

    FROM USER_CONSTRAINTS

    WHERE table_name = 'COMMISSION';

    Table of Contents

    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.

    Page 12 of 12SkillSoft Learning Object