65
Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Embed Size (px)

Citation preview

Page 1: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Virtual training week 8

Sections from 7 to 9

Page 2: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Create table copy_departments

As (select * from departments)

This syntax used to create table with same structure and same data type and all rows of the originL Table with other name.

_____________________________________________

Create table sales_reps

As (select * from employees

Where 1 = 2)

This syntax used to create table with same structure and same data type but without any row.

Page 3: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 4: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 5: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 6: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 7: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 8: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 9: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 10: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Multi-table inserts can be unconditional or conditional. •In an unconditional multitable insert Oracle will insert all rows returned by the subquery into all table insert clauses found in the statement.•In a conditional multi-table insert you can specify either ALL or FIRST.ALLIf you specify ALL, the default value, then the database evaluates each WHENclause regardless of the results of the evaluation of any other WHEN clause.For each WHEN clause whose condition evaluates to true, the database executesthe corresponding INTO clause list.FIRSTIf you specify FIRST, then the database evaluates each WHEN clause in the orderin which it appears in the statement. For the first WHEN clause that evaluates totrue, the database executes the corresponding INTO clause and skips subsequentWHEN clauses for the given row.ELSE clause For a given row, if no WHEN clause evaluates to true, then:If you have specified an ELSE clause, then the database executes the INTO clauselist associated with the ELSE clause.If you did not specify an else clause, then the database takes no action for that row.

Page 11: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

INSERT ALLINTO all_calls VALUES (caller_id, call_timestamp, call_duration, call_format)INTO police_record_calls VALUES (caller_id, call_timestamp, recipient_caller)SELECT caller_id, call_timestamp, call_duration, call_format , recipient_caller)FROM callsWHERE TRUNC(call_timestamp ) = TRUNC(SYSDATE )

Multitable insert Unconditional

Page 12: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Multi-Table Inserts ConditionalINSERT ALLWHEN call_ format IN (‘tlk’,’txt’,’pic’) THENINTO all_calls VALUES (caller_id, call_timestamp, call_duration, call_format)WHEN call_ format IN (‘tlk’,’txt’) THENINTO police_record_calls VALUES (caller_id, call_timestamp, recipient_caller)WHEN call_duration < 50 AND call_type = ‘tlk’THENINTO short_calls VALUES (caller_id, call_timestamp, call_duration)WHEN call_duration > = 50 AND call_type = ‘tlk’THENINTO long_calls VALUES (caller_id, call_timestamp, call_duration)SELECT caller_id, call_timestamp, call_duration, call_format , recipient_caller)FROM callsWHERE TRUNC(call_timestamp ) = TRUNC(SYSDATE )

Page 13: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 14: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 15: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

We can use single row subquery only to update rows

Page 16: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Department table didn't have department_id 55

Page 17: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Note/ If no where condition is used all rows in the table will be deleted

Page 18: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 19: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 20: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Data copied from items table to copy_items table

Page 21: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 22: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 23: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 24: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 25: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

If you don’t enter any value

a 00 will be entered, same time any other number (value) can be entered

The length of the character is up to 10 characters

Page 26: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

In this case 0 will be addedIn this case 0 will be added

Page 27: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

External Tables• an external table would be used to store data migrated from

older versions of the databases used by a company.• The syntax to create an external tableSame syntax for creating table in addition to the following …..ORGANIZATION EXTERNAL -- tells Oracle to create an external

table(TYPE ORACLE_LOADER -- of type Oracle Loader (an Oracle

Product)DEFAULT DIRECTORY def_dir1 -- what is the name of the

directory where the file exists ACCESS PARAMETERS -- how to read the file(RECORDS DELIMITED BY NEWLINE -- how to identify a new

row startsFIELDS – the start of the external file field name and datatype

specification...LOCATION – name of the actual file containing the data

Page 28: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

CREATE TABLE emp_load(employee_number CHAR(5),employee_dob CHAR(20),employee_last_name CHAR(20),employee_first_name CHAR(15),employee_middle_name CHAR(15),employee_hire_date DATE)ORGANIZATION EXTERNAL(TYPE ORACLE_LOADER DEFAULT DIRECTORY def_dir1 ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE FIELDS (employee_number CHAR(2),employee_dob CHAR(20), employee_last_name CHAR(18), employee_first_name CHAR(11), employee_middle_name CHAR(11), employee_hire_date CHAR(10) date_format DATE mask

"mm/dd/yyyy“))LOCATION ('info.dat'));

Page 29: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Data dictionary contains what is called meta data (data of data)

Page 30: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Show all available views in your schema

Show the names of all tables in your schema

Show all objects available in your schema

Show the names of the objects and their titles in your schema

Page 31: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 32: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 33: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 34: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 35: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 36: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 37: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Sometimes tables don’t be dropped if there’re dependences (PK and FK between two tables)

Page 38: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 39: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 40: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 41: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Flashback • This command can be used to restore a table, a

view or an index that was dropped in error.

• Also used to view row data at specific points in time, so we can compare different versions of a row over time.

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

Page 42: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

FLASHBACK TABLE

FLASHBACK TABLE tablename TO BEFORE DROP;

Note/ 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.

Page 43: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

FLASHBACK QUERY

A typical flashback query might look like this:A typical flashback query might look like this:

SELECT versions_starttime AS "START_DATE",

versions_endtime AS "END_DATE",

salary

FROM employees

VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

WHERE department_id = 90;

Page 44: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 45: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

FLASHBACK QUERYSELECT versions_starttime "START_DATE",versions_endtime "END_DATE",salaryFROM employeesVERSIONS BETWEEN SCN MINVALUE AND MAXVALUEWHERE department_id=90Rows from the ‘old’updated version are in red in the output table below:

Page 46: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

NOTEOracle 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 47: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 48: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 49: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

When the constraint is created, it can be given a name, such as clients_client_num_pkclients_client_num_pk, or given no name, in which case the system gives the constraint a name, such as SYS_C00585417.SYS_C00585417.

Note: If the word CONSTRAINT is used in the CREATE TABLE definition, you must give the constraint a name. It is best to name constraints yourself. Why?

The system-generated names are not intuitive. You can do better than the database when it comes to naming!

Page 50: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 51: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 52: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 53: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 54: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 55: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 56: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 57: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 58: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 59: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 60: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

clients_email_nn_not null clients_email_nn_not null

Page 61: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 62: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 63: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 64: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

Page 65: Oracle Academy Egypt Virtual training week 8 Sections from 7 to 9

Oracle Academy Egypt

GOOD LUCK

SEE YOU NEXT MEETING

Raafat [email protected]

[email protected]