You are on page 1of 3

www.oracle.

com/academy

Database Programming with SQL 14-2: PRIMARY KEY, FOREIGN


KEY, and CHECK Constraints Practice Activities
Objectives
• Define and give an example of PRIMARY KEY, FOREIGN KEY, and CHECK
constraints
• Explain the purpose of defining PRIMARY KEY, FOREIGN KEY, and CHECK
constraints on a table
• Demonstrate the creation of constraints at the column level and table level in a

m
CREATE TABLE statement

er as
• Evaluate a business problem requiring the addition of a PRIMARY KEY and FOREIGN

co
KEY constraint and write the code to execute the change

eH w
o.
Vocabulary
rs e
Identify the vocabulary word for each definition below.
ou urc
On delete cascade Allows a foreign key row that is referenced to a primary
key row to be deleted
o

Check constraint Explicitly defines a condition that must be met


aC s
vi y re

Primary key A column or set of columns that uniquely identifies each


row in a table
Not null Constraint ensures that the column contains no null
ed d

values
ar stu

On delete set null Allows a child row to remain in a table with null values
when a parent record has been deleted
Foreign key constraint Establishes a relationship between the foreign key column and a
is

primary key or unique key in the same table or a different table


Th

Try It / Solve It
sh

1. What is the purpose of a


a. PRIMARY KEY
i A unique column which identifies every row in the table
b. FOREIGN KEY
i Refers to the parent table’s primary key
c. CHECK CONSTRAINT
i Defines condition that the rows need to meet, will return as either true or unkown

This study source was downloaded by 100000809939014 from CourseHero.com on 05-05-2021 13:36:14 GMT -05:00

https://www.coursehero.com/file/46996684/DP-14-2-Practice-Ethan-Vijudocx/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

2. Using the column information for the animals table below, name constraints where
applicable at the table level, otherwise name them at the column level. Define the primary
key (animal_id). The license_tag_number must be unique. The admit_date and
vaccination_date columns cannot contain null values.

animal_id NUMBER(6) PK
Name VARCHAR2(25)
license_tag_number NUMBER(10)
UK
admit_date DATE NOT NULL
adoption_id NUMBER(5),

m
vaccination_date DATE NOT NULL

er as
co
eH w
3. Create the animals table. Write the syntax you will use to create the table.

o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu

4. Enter one row into the table. Execute a SELECT * statement to verify your input. Refer to
the graphic below for input.
is
Th

ANI- NAME LI- ADMIT_DATE ADOP- VACCINA-


MAL_ID CENSE_TAG_N TION_ID TION_DATE
UMBER
101 Spot 35540 10-Oct-2004 205 12-Oct-2004
sh

This study source was downloaded by 100000809939014 from CourseHero.com on 05-05-2021 13:36:14 GMT -05:00

https://www.coursehero.com/file/46996684/DP-14-2-Practice-Ethan-Vijudocx/
5. Write the syntax to create a foreign key (adoption_id) in the animals table that has a
corresponding primary- key reference in the adoptions table. Show both the column-level
and table-level syntax. Note that because you have not actually created an adoptions
table, no adoption_id primary key exists, so the foreign key cannot be added to the animals

m
table.

er as
co
Table:

eH w
Alter table animals

o.
Add adoption_id number(6) constraint animals_fk references animals(adoption_id)
rs e
ou urc
Column:
Alter table animals
Modify adoption_id number(6) constraint animals_fk references animals(adoption_id)
o
aC s
vi y re

6. What is the effect of setting the foreign key in the ANIMAL table as:
a. ON DELETE CASCADE
i The rows in the child table will be deleted when the rows in the parent table are
ed d

deleted
ar stu

b. ON DELETE SET NULL


i The child table’s rows will simply be filled with null values
is

7. What are the restrictions on defining a CHECK constraint?


Th
sh

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

This study source was downloaded by 100000809939014 from CourseHero.com on 05-05-2021 13:36:14 GMT -05:00

https://www.coursehero.com/file/46996684/DP-14-2-Practice-Ethan-Vijudocx/
Powered by TCPDF (www.tcpdf.org)

You might also like