Database Programming With SQL 13-3: Modifying A Table Practice Activities

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Database Programming with SQL

13-3: Modifying a Table


Practice Activities
Objectives
• Explain why it is important to be able to modify a table
• Explain and provide an example for each of the DDL statements—ALTER, DROP, RENAME,
and TRUNCATE—and the effect each has on tables and columns
• Construct a query and execute the ALTER TABLE commands ADD, MODIFY, and DROP
• Explain and perform a FLASHBACK QUERY on a table
• Explain and perform FLASHBACK table operations
• Track the changes to data over a period of time
• Explain the rationale for using TRUNCATE versus DELETE for tables
• Add a comment to a table using the COMMENT ON TABLE command
• Name the changes that can and cannot be made to modify a column
• Explain when and why the SET UNUSED statement is advantageous

Try It / Solve It
Before beginning the practice exercises, execute a DESCRIBE for each of the following tables:
o_employees, o_departments and o_jobs. These tables will be used in the exercises. If they do not
exist in your account, create them as follows:

1. Create the three o_tables – jobs, employees, and departments – using the syntax:
CREATE TABLE o_jobs AS (SELECT * FROM jobs);
CREATE TABLE o_employees AS (SELECT * FROM employees);
CREATE TABLE o_departments AS (SELECT * FROM departments);

2. Add the Human Resources job to the jobs table:


INSERT INTO o_jobs (job_id, job_title, min_salary, max_salary)
VALUES('HR_MAN', 'Human Resources Manager', 4500, 5500);

3. Add the three new employees to the employees table:


INSERT INTO o_employees (employee_id, first_name, last_name, email, hire_date,
job_id)
VALUES(210, 'Ramon', 'Sanchez', 'RSANCHEZ', SYSDATE, 'HR_MAN');

Copyright © 2020, 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.
4. Add Human Resources to the departments table:
INSERT INTO o_departments(department_id, department_name)
VALUES (210,'Human Resources');

You will need to know which columns do not allow null values.

1. Why is it important to be able to modify a table?

2. CREATE a table called Artists.

a. Add the following to the table:


• artist ID
• first name
• last name
• band name
• email
• hourly rate
• song ID from d_songs table

b. INSERT one artist from the d_songs table.

c. INSERT one artist of your own choosing; leave song_id blank.

d. Give an example how each of the following may be used on the table that you have created:

1) ALTER TABLE
2) DROP TABLE
3) RENAME TABLE
4) TRUNCATE
5) COMMENT ON TABLE

3. In your o_employees table, enter a new column called “Termination.” The datatype for the new
column should be VARCHAR2. Set the DEFAULT for this column as SYSDATE to appear as
character data in the format: February 20th, 2003.

Copyright © 2020, 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
4. Create a new column in the o_employees table called start_date. Use the TIMESTAMP WITH
LOCAL TIME ZONE as the datatype.

5. Truncate the o_jobs table. Then do a SELECT * statement.


Are the columns still there? Is the data still there?

6. What is the distinction between TRUNCATE, DELETE, and DROP for tables?

7. List the changes that can and cannot be made to a column.

8. Add the following comment to the o_jobs table:


"New job description added"
View the data dictionary to view your comments.

9. Rename the o_jobs table to o_job_description.

10. F_staffs table exercises:

a. Create a copy of the f_staffs table called copy_f_staffs and use this copy table for the
remaining labs in this lesson.
b. Describe the new table to make sure it exists.
c. Drop the table.
d. Try to select from the table.
e. Investigate your recyclebin to see where the table went.
f. Try to select from the dropped table by using the value stored in the OBJECT_NAME column.
You will need to copy and paste the name as it is exactly, and enclose the new name in “ “
(double quotes). So if the dropped name returned to you is
BIN$Q+x1nJdcUnngQESYELVIdQ==$0, you need to write a query that refers to
“BIN$Q+x1nJdcUnngQESYELVIdQ==$0”.
g. Undrop the table.
h. Describe the table.

Copyright © 2020, 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.

3
11. Still working with the copy_f_staffs table, perform an update on the table.

a. Issue a select statement to see all rows and all columns from the copy_f_staffs table;
b. Change the salary for Sue Doe to 12 and commit the change.
c. Issue a select statement to see all rows and all columns from the copy_f_staffs table;
d. For Sue Doe, update the salary to 2 and commit the change.
e. Issue a select statement to see all rows and all columns from the copy_f_staffs table;
f. Now, issue a FLASHBACK QUERY statement against the copy_f_staffs table, so you can see
all the changes made.
g. Investigate the result of f), and find the original salary and update the copy_f_staffs table salary
column for Sue Doe back to her original salary.

Copyright © 2020, 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.

You might also like