32
Copyright © 2009, Oracle. All rights reserved. Modifying a Table

Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

Copyright © 2009, Oracle. All rights reserved.

Modifying a Table

Page 2: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

2

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

What Will I Learn?In this lesson, you will learn to:

• 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 FLASHBACK TABLE operations

• Explain and perform FLASHBACK QUERY on a table

Page 3: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

3

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

What Will I Learn?In this lesson, you will learn to:

• Explain the rationale for using TRUNCATE vs. 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

Page 4: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

4

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Why Learn It?Remember the statement, "There is nothing permanent except change"? Wouldn't it be nice if we never made mistakes or needed to change anything? As you know by now, databases are dynamic entities. They probably wouldn't be very useful if they couldn't be changed.

Up to now you've created tables and made changes to the row data inside tables, but how do you make changes to the tables themselves? This lesson presents the DDL commands that are used to alter, rename, empty, or simply eliminate a table altogether.

Page 5: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

5

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeALTER TABLE

ALTER TABLE statements are used to:• Add a new column• Modify an existing column• Define a DEFAULT value for a column• Drop a column.

You can add or modify a column in a table, but you cannot specify where the column appears. A newly added column always becomes the last column of the table.

Also, if a table already has rows of data and you add a new column to the table, the new column is initially null for all the rows.

sdp_s09_l01_a01

Presenter
Presentation Notes
Several other Data Definition Language commands are available for you to use: ALTER TABLE, DROP TABLE, RENAME, TRUNCATE, or COMMENT. These commands allow you to change the structure of a table. These types of commands, along with a CREATE TABLE command, which we’ve used already, are called Data Definition Language commands or D-D-L commands. Data Definition Language commands are always irreversible. Once they are executed, there is no going back to the previous conditions. So, use them with care. The first one we’ll examine is the ALTER TABLE command.
Page 6: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

6

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeALTER TABLE: ADDING A COLUMN

To add a new column, use the SQL syntax shown.

ALTER TABLE tablenameADD (column name datatype [DEFAULT expression],column name datatype [DEFAULT expression], ...

For example:

ALTER TABLE copy_f_staffsADD (hire_date DATE DEFAULT SYSDATE);

ALTER TABLE copy_f_staffsADD (e_mail_address VARCHAR2(80));

Presenter
Presentation Notes
We use ALTER TABLE to add new columns, modify columns, define default values for columns, drop columns, or set columns to be unused. We’ll talk about each one of these in a little more detail. We use ADD to place a new column in a table. This column is always placed at the end.
Page 7: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

7

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show Me

ALTER TABLE: MODIFYING A COLUMN

Modifying a column can include changes to a column's data type, size, and DEFAULT value. Rules and restrictions when modifying a column are:

• You can increase the width or precision of a numeric column.• You can increase the width of a character column.• You can decrease the width of a column only if the column

contains only null values or if the table has no rows.

Page 8: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

8

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show Me

ALTER TABLE: MODIFYING A COLUMN

• You can change the data type only if the column contains null values.

• You can convert a CHAR column to VARCHAR2 or convert a VARCHAR2 column to CHAR only if the column contains null values or if you do not change the size.

• A change to the DEFAULT value of a column affects only later insertions to the table.

Page 9: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

9

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeALTER TABLE: MODIFYING A COLUMN

Example: a table has been created with two columns:

CREATE TABLE mod_emp(last_name VARCHAR2(20),salary NUMBER(8,2));

Which of these modifications would be allowed, and which would not?1. ALTER TABLE mod_emp MODIFY (last_name VARCHAR2(30));2. ALTER TABLE mod_emp MODIFY (last_name VARCHAR2(10));3. ALTER TABLE mod_emp MODIFY (salary NUMBER(10,2));4. ALTER TABLE mod_emp MODIFY (salary NUMBER(8,2)

DEFAULT 50);

Presenter
Presentation Notes
We use MODIFY to change the definition of an existing column. Remember, this does not change the data in the column, rather, it changes the structure of the column itself. We can change data type, size, or change default values. There are restrictions, however. The data in the existing table must match the change or you will get an error. DEFAULT allows you to change a column so that you have a default value in the column where none existed before. The first example would be successful since the definition enlarges the column structure. The second example would be successful if no data existed in this column previously; otherwise this statement would fail with data in the column. Both of the next two statements would be successful. Note that the last statement would only affect default values for future entries.
Page 10: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

10

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeALTER TABLE: DROPPING A COLUMN

When dropping a column the following rules apply:• A column to be dropped may or may not contain

data.• Only one column can be dropped at a time.• You can't drop all of the columns in a table; at least

one column must remain.• Once a column is dropped, the data values in it cannot

be recovered.SQL syntax:ALTER TABLE tablename DROP COLUMN column name;For example:ALTER TABLE copy_f_staffs DROP COLUMN manager_target;

sdp_s09_l01_a02

Presenter
Presentation Notes
DROP COLUMN allows you to remove a column so that it cannot be accessed any more. You can drop only one column at a time, but not all columns in the table.
Page 11: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

11

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeSET UNUSED COLUMNS

Dropping a column from a large table can take a long time. A quicker alternative is to mark the column as unusable. The column values remain in the database but cannot be accessed in any way, so the effect is the same as dropping the column.

In fact, you could add a new column to the database with the same name as the unused column. The unused columns are there, but invisible!

Syntax:ALTER TABLE tablename SET UNUSED (column name);

Page 12: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

12

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeSET UNUSED COLUMNSExample:ALTER TABLE copy_f_staffs

SET UNUSED (manager_budget);

DROP UNUSED COLUMNS removes all columns currently marked as unused. You use this statement when you want to reclaim the extra disk space from unused columns in a table.ALTER TABLE tablename DROP UNUSED COLUMNS;

Example:ALTER TABLE copy_f_staffs DROP UNUSED COLUMNS;

Presenter
Presentation Notes
Dropping a column from a table can sometimes take a long time. So, we sometimes use the SET UNUSED command to flag a column so that it can be formally dropped later. Once a column is SET UNUSED, no one can access it with a SELECT statement, or an INSERT statement, or any other type of statement. The data is still there but it is unavailable. Then, later on, when the database is not being used so much, the database administrator could execute DROP UNUSED. And, this will drop all the columns and corresponding data from the database. I’m going to now give you a set of examples that you can try with a new table called items. The table will have three columns, part_number, description, and qty_on_hand. First, create the table by typing: CREATE TABLE items(part_number VARCHAR2(10), description VARCHAR2(10), qty_on_hand NUMBER DEFAULT 0). Now, create a new column called price by typing: ALTER TABLE items ADD left parenthesis price NUMBER left parenthesis eight comma two right parenthesis right parenthesis. This command creates a new column in the items table called ‘price.’ It is a numeric column that can fit up to eight digits. Let’s now try to change the structure of that column. Type: ALTER TABLE items MODIFY left parenthesis price number left parenthesis seven right parenthesis right parenthesis. We have now changed price column so that only seven digits can be entered in the column. This command will work only if the column does not contain any values that already have eight digits. Now let’s try to drop the column price from the items table. Type: ALTER TABLE items DROP COLUMN price. The price column and all of the data is now removed from this table. Now let’s set the qty_on_hand column UNUSED. Type: ALTER TABLE items SET UNUSED left parenthesis q-t-y underscore o-n underscore h-a-n-d right parenthesis. We have now removed the ability to access any data in the quantity on hand column. The data does still exist in the database even though it is not accessible. We can remove it by this command: ALTER TABLE items DROP UNUSED columns.
Page 13: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

13

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show Me

ALTER TABLEThis chart summarizes the uses of the ALTER TABLE command.

Syntax Outcomes Concerns

ALTER TABLE tablename ADD (column name datatype [DEFAULT expression], column name datatype [DEFAULT expression], …

Adds a new column to a table You cannot specify where the column is to appear in the table. It becomes the last column.

ALTER TABLE tablename MODIFY (column name datatype [DEFAULT expression], column name datatype, …

Used to change a column’s datatype, size, and default value

A change to the default value of a column affects only subsequent insertions to the table.

ALTER TABLE tablename DROP COLUMN column name;

Used to drop a column from a table The table must have at least one column remaining in it after it is altered. Once dropped, the column cannot be recovered.

ALTER TABLE tablename SET UNUSED (column name);

Used to mark one or more columns so they can be dropped later

Does not restore disk space. Columns are treated as if they were dropped.

ALTER TABLE tablename DROP UNUSED COLUMNS

Removes from the table all columns currently marked as unused

Once set unused, there is no access to the columns; no data displayed using DESCRIBE. Permanent removal; no rollback.

Page 14: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

14

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeDROP TABLE

The DROP TABLE statement removes the definition of an Oracle table. The database loses all the data in the table and all the indexes associated with it. When a DROP TABLE statement is issued:

• All data is deleted from the table.• The table’s description is removed from the

Data Dictionary• It may be irreversible! The Oracle Server

does not question your decision.

Page 15: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

15

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeDROP TABLE

Only the creator of the table or a user with DROP ANY TABLE privilege (usually only the DBA) can remove a table.

DROP TABLE tablename;

DROP TABLE copy_f_staffs;

Presenter
Presentation Notes
This slide demonstrates the results of a DROP TABLE command. The command is simple. DROP TABLE then tablename. It removes all references to the table. If there is existing data in the table, it is removed. This command is executed immediately and is irreversible in Application Express. Sometimes a DROP TABLE command will not work, for example, if there are foreign key dependencies upon the table. There is a way to still drop the table, and we’ll talk about that when we discuss constraints.
Page 16: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

16

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK TABLE

If you drop a table by mistake, in Oracle 10g, there is a way of bringing that table and its data back.Each database user has their own recyclebin into which dropped objects are now moved, and they can be recovered from here with the FLASHBACK TABLE command.This command can be used to restore a table, a view or an index that was dropped in error.The Syntax is quite simple:FLASHBACK TABLE tablename TO BEFORE DROP;

Presenter
Presentation Notes
If a table is dropped by mistake, Oracle 10g and later can recover the table definition and data by implementing the FLASHBACK TABLE command. The recycle bin is Oracle’s way of keeping track of dropped objects so that they may be recovered later. You may identify dropped objects by using the Data Dictionary view USER underscore RECYCLEBIN. Note the table recovery is not guaranteed. The database administrator can purge dropped objects to free up space. And, once a user’s table space is full, dropped objects are purged as new objects are created.
Page 17: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

17

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK TABLE

So if you have dropped the EMPLOYEES table in error, you can restore it by simply issuing the command:

FLASHBACK TABLE employees TO BEFORE DROP;

As the owner of a table you can issue the flashback command, and if the table that you are restoring had any indexes, then these are also restored. It is possible to see what objects you have that you can restore, by simply querying the data dictionary view USER_RECYCLEBIN.

Page 18: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

18

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK TABLE

The USER_RECYCLEBIN view can be queried like all other data dictionary views:

SELECT original_name, operation, droptimeFROM user_recyclebin

ORIGINAL_NAME OPERATION DROPTIME

EMPLOYEES DROP 2007-12-05:12.34.24

EMP_PK DROP 2007-12-05:12.34.24

Once a table has been restored by the FLASHBACK TABLE command, it is no longer visible in the USER_RECYCLEBIN view.Any indexes that were dropped along with the original table, will also have been restored.

Page 19: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

19

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeRENAME

To change the name of a table, use the RENAMEstatement. This can be done only by the owner ofthe object or by the DBA.

RENAME old_name to new_name;

Example:

RENAME copy_f_staffs to copy_fastfood_staffs;

We will see later that we can rename other types ofobject, such as views, sequences and synonyms.

Presenter
Presentation Notes
This slide demonstrates the way to rename a table. The syntax is: RENAME TABLE tablename TO new tablename. To practice this, first check to see that you have a copy_items table by typing DESC copy_items. If you do not have a copy_items table available, you can create one by typing CREATE TABLE copy_items AS SELECT * FROM items. When your ‘copy_items’ table is available, try this command now and rename the table to ‘inventory_items’.
Page 20: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

20

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeTRUNCATE

Truncating a table removes all rows from a table and releases the storage space used by that table. When using the TRUNCATE TABLE statement:

• You cannot roll back row removal.• You must be the owner of the table or have

been given DROP ANY TABLE system privileges.

Syntax: TRUNCATE TABLE tablename;

The DELETE statement can also remove rows from a table, but it does not release storage space. TRUNCATE is faster than DELETE because it does not generate rollback information.

Presenter
Presentation Notes
We’re now ready to examine the TRUNCATE command. The TRUNCATE command removes all the rows from the table and releases the storage space. The syntax is: TRUNCATE TABLE tablename. This is different from deleting every column in the table. When we delete the columns from a table, we do not release the storage space from the database. It is still held and unavailable to be used for any other purpose. When we use TRUNCATE, it removes every row of data, and releases the storage space. Note that although all the data is removed from the table and storage space is regained, the total table structure remains intact.
Page 21: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

21

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeCOMMENT ON TABLE

You can add a comment of up to 2,000 characters about a column, table, or view by using the COMMENT statement. The comment is stored in the data dictionary and can be viewed in one of the following data dictionary views in the COMMENTS column:Comments on columns:• ALL_COL_COMMENTS• USER_COL_COMMENTS

Comments on tables:• ALL_TAB_COMMENTS• USER_TAB_COMMENTS

Page 22: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

22

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeCOMMENT ON TABLE: EXAMPLES

Syntax:COMMENT ON TABLE tablename | COLUMN table.column IS 'place your comment here';

Example:COMMENT ON TABLE employeesIS 'Western Region only';

Presenter
Presentation Notes
COMMENT ON TABLE allows you to enter comments about the structure of a table or really, anything else that you want to make note of. You can also comment on individual columns in the database table. For this, use COMMENT ON, and instead of using the keyword TABLE, use the name of the table and then the name of the column. The word IS is used next, then in single quotation marks, the comment that is to be added. You can view the comments on columns by using the describe command in the SQL command processor or by browsing the database tables USER_TAB_COMMENTS or ALL_TAB_COMMENTS, and USER_COL_COMMENTS or ALL_COL_COMMENTS. To delete a previously existing comment, use the COMMENT ON command, and after the keyword IS, use two single quotation marks. This effectively changes the value to null.
Page 23: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

23

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeCOMMENT ON TABLE: EXAMPLES

SELECT table_name, comments FROM user_tab_comments;

TABLE_NAME COMMENTS-------------------- ------------------------------EMPLOYEES Western Region only

If you want to drop a comment previously made on a table or column, use the empty string ( ' ' ):

COMMENT ON TABLE employees IS ' ' ;

Page 24: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

24

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK QUERY

You may discover that somehow data in a table has been inappropriately changed. Luckily, Oracle has a facility that allows us to view row data at specific points in time, so we can compare different versions of a row over time.

This facility is very useful. Imagine for instance that someone accidently performed some DML on a table, and COMMIT’ed those changes. Oracle Application Express commits automatically, so mistakes are easily made.

We can use this facility to look at what the rows looked like BEFORE those changes were applied.

Page 25: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

25

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK QUERY

When Oracle changes data, it always keeps a copy of what the amended data looked like before any changes were made. So it keeps a copy of the old column value for a column update, it keeps the entire row for a delete and it keeps nothing for a insert statement. These old copies are held in a special place called the UNTO tablespace. Users have no normal access to this area of the Database, but we can use the data it contains for FLASHBACK QUERIES.

Page 26: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

26

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK QUERY

A new clause is used in a SELECT statement to allow us to look at older versions of the rows: VERSIONS.

A typical flashback query might look like this:

SELECT versions_starttime AS "START_DATE", versions_endtime AS "END_DATE", salary

FROM employeesVERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

WHERE department_id = 90;

Page 27: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

27

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK QUERYThis is easiest demonstrated via an example:The contents of the employees is as follows for department_id 90.LAST_NAME SALARY DEPARTMENT_ID

King 24000 90

Kochhar 17000 90

De Haan 17000 90

update employeesset salary = 1where department_id = 90;commit;LAST_NAME SALARY DEPARTMENT_ID

King 1 90

Kochhar 1 90

De Haan 1 90

Page 28: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

28

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK QUERYSELECT versions_starttime "START_DATE",

versions_endtime "END_DATE", salary

FROM employeesVERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

WHERE department_id=90

Rows from the ‘old’ updated version are in red in the output table below:START_DATE END_DATE SALARY

11-DEC-07 05.44.24 AM - 1

11-DEC-07 05.44.24 AM - 1

11-DEC-07 05.44.24 AM - 1

- 11-DEC-07 05.44.24 AM 2400- 11-DEC-07 05.44.24 AM 17000- 11-DEC-07 05.44.24 AM 17000

Page 29: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

29

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

Tell Me / Show MeFLASHBACK QUERY

So we could use a FLASHBACK QUERY to look at the data, as it was BEFORE the update took place, and use that to manually update the rows back to the way they were before the update was committed. Oracle is getting the FLASHBACK QUERY data from the “before images” held in the database in the UNDO tablespace. Typically, this UNDO data is only available for up to 15 minutes after the transaction was committed. Once the UNDO data has been overwritten by Oracle, Flashback queries are no longer possible, so if you want to use this functionality, you will have to do it quickly after the changes were committed.

Page 30: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

30

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

In this lesson you have learned to:

• 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 FLASHBACK TABLE operations

• Explain and perform FLASHBACK QUERY on a table

SummarySummary

Page 31: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

31

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

In this lesson you have learned to:

• Explain the rationale for using TRUNCATE vs. 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

SummarySummary

Page 32: Modifying a Table - Michael Easonmichaeleason.com/oracleacademy/pdfs/dp_s08_l03.pdf · • Explain the rationale for using TRUNCATE vs. DELETE for tables • Add a comment to a table

32

Modifying a Table

Copyright © 2009, Oracle. All rights reserved.

SummaryPractice Guide

The link for the lesson practice guide can be found in the course resources in Section 0.