REM Basic table storing flight details
Basic table storing flight details
create table flights (
flight_id integer not null primary key,
flight_number varchar2(6) not null,
departure_airport_code varchar2(3) not null,
destination_airport_code varchar2(3) not null,
departure_datetime timestamp with time zone not null,
flight_duration interval day to second(0) not null,
operating_carrier_code varchar2(2) not null
) partition by range (departure_airport_code) (
partition pA_E values less than ('F'),
partition pF_J values less than ('K'),
partition pK_O values less than ('P'),
partition pP_T values less than ('U'),
partition pU_Z values less than ('a')
)
Table created.
REM Sample data
Sample data
insert into flights
values (1, 'BA123', 'LHR', 'JFK', timestamp'2015-01-01 10:00:00 -00:00', interval '5' hour, 'BA')
1 row(s) inserted.
insert into flights
values (2, 'BA123', 'LHR', 'JFK', timestamp'2015-01-02 10:00:00 -00:00', interval '5' hour, 'BA')
1 row(s) inserted.
insert into flights
values (3, 'AA567', 'LHR', 'JFK', timestamp'2015-01-01 13:00:00 -00:00', interval '6' hour, 'AA')
1 row(s) inserted.
insert into flights
values (4, 'BA124', 'JFK', 'LHR', timestamp'2015-01-01 22:00:00 -05:00', interval '5' hour, 'BA')
1 row(s) inserted.
insert into flights
values (5, 'BA124', 'JFK', 'LHR', timestamp'2015-01-02 22:00:00 -05:00', interval '5' hour, 'BA')
1 row(s) inserted.
insert into flights
values (6, 'AA567', 'JFK', 'LHR', timestamp'2015-01-01 20:00:00 -05:00', interval '6' hour, 'AA')
1 row(s) inserted.
insert into flights
values (7, 'AA987', 'JFK', 'SFO', timestamp'2015-01-02 12:00:00 -05:00', interval '4' hour, 'AA')
1 row(s) inserted.
commit
Statement processed.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | BA124 | JFK | LHR | 01-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | AA | 7 | AA987 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 04:00:00 | AA | 1 | BA123 | LHR | JFK | 01-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | AA |
---|
REM Single row, single column update
Single row, single column update
REM Changing the flight number for flight id 7
Changing the flight number for flight id 7
update flights
set flight_number = 'AA986'
where flight_id = 7
1 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | BA124 | JFK | LHR | 01-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | AA | 7 | AA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 04:00:00 | AA | 1 | BA123 | LHR | JFK | 01-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | AA |
---|
REM Single row, multicolumn update
Single row, multicolumn update
REM Setting the flight number, duration and operating carrier for flight id 7
Setting the flight number, duration and operating carrier for flight id 7
update flights
set flight_number = 'BA986',
flight_duration = interval '5' hour,
operating_carrier_code = 'BA'
where flight_id = 7
1 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | BA124 | JFK | LHR | 01-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | AA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 1 | BA123 | LHR | JFK | 01-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | AA |
---|
REM Single row, single column self update
Single row, single column self update
REM Increase the time of flight_id 7 by thirty minutes
Increase the time of flight_id 7 by thirty minutes
update flights
set flight_duration = flight_duration + interval '30' minute
where flight_id = 7
1 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | BA124 | JFK | LHR | 01-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:00:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | AA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 1 | BA123 | LHR | JFK | 01-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:00:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | AA |
---|
REM Correlated subquery, multi-row update, single column self update
Correlated subquery, multi-row update, single column self update
REM increase the length of all BA flights by thirty minutes
increase the length of all BA flights by thirty minutes
update flights f1
set f1.flight_duration = f1.flight_duration + interval '30' minute
where f1.flight_id in (select f2.flight_id
from flights f2
where f2.operating_carrier_code = 'BA')
5 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | BA124 | JFK | LHR | 01-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | AA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | BA123 | LHR | JFK | 01-JAN-15 10.00.00.000000 AM +00:00 | +00 05:30:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:30:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | AA |
---|
REM Correlated update
Correlated update
REM Set the flight number and duration for BA flights departing on 1 Jan 2015
Set the flight number and duration for BA flights departing on 1 Jan 2015
REM to be the flight number appended with zero and duration of the AA flight
to be the flight number appended with zero and duration of the AA flight
REM on the same day and route
on the same day and route
REM Also set the departure times for these flights to 11AM GMT
Also set the departure times for these flights to 11AM GMT
update flights f1
set f1.departure_datetime = timestamp'2015-01-01 11:00:00 -00:00',
(f1.flight_number, f1.flight_duration) = (
select f2.flight_number || '0', f2.flight_duration
from flights f2
where f2.departure_airport_code = f1.departure_airport_code
and f2.destination_airport_code = f1.destination_airport_code
and trunc(f2.departure_datetime) = trunc(f2.departure_datetime)
and f2.operating_carrier_code = 'AA'
)
where f1.operating_carrier_code = 'BA'
and trunc(f1.departure_datetime) = date'2015-01-01'
2 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | AA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | AA5670 | LHR | JFK | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:30:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | AA |
---|
REM Update all rows, no where clause
Update all rows, no where clause
update flights
set operating_carrier_code = 'BA'
7 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | AA5670 | LHR | JFK | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 05:30:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 06:00:00 | BA |
---|
REM update all rows in partition pK_O (those departing from LHR)
update all rows in partition pK_O (those departing from LHR)
REM to have a flight duration of 7 hours
to have a flight duration of 7 hours
update flights partition (pK_O)
set flight_duration = interval '7' hour
3 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | AA5670 | LHR | JFK | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 07:00:00 | BA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 07:00:00 | BA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 07:00:00 | BA |
---|
REM Update a query
Update a query
REM Set all the flights that take 7 hours to be operated by AA
Set all the flights that take 7 hours to be operated by AA
update (select operating_carrier_code
from flights
where flight_duration = interval '7' hour)
set operating_carrier_code = 'AA'
3 row(s) updated.
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | AA5670 | LHR | JFK | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 07:00:00 | AA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 07:00:00 | AA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 07:00:00 | AA |
---|
REM Update a query with check operation.
Update a query with check operation.
REM This validates that the update will not set values that do not match the where clause
This validates that the update will not set values that do not match the where clause
REM Select all the rows with durations of at least 4 hours, preventing
Select all the rows with durations of at least 4 hours, preventing
REM any rows in the table having durations less than 4 hours after the update
any rows in the table having durations less than 4 hours after the update
REM Throws an exception because this will set some flight durations less than 4 hours
Throws an exception because this will set some flight durations less than 4 hours
update (select flight_duration
from flights
where flight_duration >= interval '4' hour
with check option)
set flight_duration = flight_duration - interval '2' hour
ORA-01402: view WITH CHECK OPTION where-clause violation
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | AA5670 | LHR | JFK | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 07:00:00 | AA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 07:00:00 | AA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 07:00:00 | AA |
---|
REM Create an error log table (err$_flights) for the flights table
Create an error log table (err$_flights) for the flights table
begin
DBMS_ERRLOG.create_error_log (dml_table_name => 'flights');
end;
Statement processed.
REM Update a query with check operation.
Update a query with check operation.
REM This validates that the update will not set values that do not match the where clause
This validates that the update will not set values that do not match the where clause
REM Select all the rows with durations of at least 4 hours, preventing
Select all the rows with durations of at least 4 hours, preventing
REM any rows in the table having durations less than 4 hours after the update
any rows in the table having durations less than 4 hours after the update
REM This time the statement will succeed because invalid rows are placed in the error table
This time the statement will succeed because invalid rows are placed in the error table
REM Only that will have an interval greater than or equal to 4 hours after the update are modified
Only that will have an interval greater than or equal to 4 hours after the update are modified
update (select flight_duration
from flights
where flight_duration >= interval '4' hour
with check option)
set flight_duration = flight_duration - interval '3' hour
log errors into err$_flights ('UDPATE') reject limit unlimited
3 row(s) updated.
select * from err$_flights
ORA_ERR_NUMBER$ | ORA_ERR_MESG$ | ORA_ERR_ROWID$ | ORA_ERR_OPTYP$ | ORA_ERR_TAG$ | FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 1402 | ORA-01402: view WITH CHECK OPTION where-clause violation | AAAaquAAOAAAACVAAA | U | UDPATE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000000 AM +00:00 | +000000000 03:00:00.000000000 | BA | 1402 | ORA-01402: view WITH CHECK OPTION where-clause violation | AAAaquAAOAAAACVAAB | U | UDPATE | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000000 PM -05:00 | +000000000 02:30:00.000000000 | BA | 1402 | ORA-01402: view WITH CHECK OPTION where-clause violation | AAAaquAAOAAAACVAAC | U | UDPATE | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000000 PM -05:00 | +000000000 03:00:00.000000000 | BA | 1402 | ORA-01402: view WITH CHECK OPTION where-clause violation | AAAaquAAOAAAACVAAD | U | UDPATE | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000000 PM -05:00 | +000000000 03:00:00.000000000 | BA |
---|
select * from flights
FLIGHT_ID | FLIGHT_NUMBER | DEPARTURE_AIRPORT_CODE | DESTINATION_AIRPORT_CODE | DEPARTURE_DATETIME | FLIGHT_DURATION | OPERATING_CARRIER_CODE | 4 | AA5670 | JFK | LHR | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 06:00:00 | BA | 5 | BA124 | JFK | LHR | 02-JAN-15 10.00.00.000000 PM -05:00 | +00 05:30:00 | BA | 6 | AA567 | JFK | LHR | 01-JAN-15 08.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 7 | BA986 | JFK | SFO | 02-JAN-15 12.00.00.000000 PM -05:00 | +00 06:00:00 | BA | 1 | AA5670 | LHR | JFK | 01-JAN-15 11.00.00.000000 AM +00:00 | +00 04:00:00 | AA | 2 | BA123 | LHR | JFK | 02-JAN-15 10.00.00.000000 AM +00:00 | +00 04:00:00 | AA | 3 | AA567 | LHR | JFK | 01-JAN-15 01.00.00.000000 PM +00:00 | +00 04:00:00 | AA |
---|