create table deathstar_protocols (
id integer not null primary key,
label varchar2(256),
alert_level varchar2(16) not null,
defense_mode varchar2(32) not null,
power_level number(5,2) not null
)
Table created.
insert into deathstar_protocols
values (1, 'Everything easy', 'LOW', 'BE_KIND', 80)
1 row(s) inserted.
insert into deathstar_protocols
values (2, 'Be careful', 'MEDIUM', 'BE_SUSPICIOUS', 90)
1 row(s) inserted.
insert into deathstar_protocols
values (3, 'OMG the rebels!', 'VERY HIGH', 'SHOOT_FIRST_ASK_LATER', 120)
1 row(s) inserted.
select * from deathstar_protocols
ID | LABEL | ALERT_LEVEL | DEFENSE_MODE | POWER_LEVEL | 1 | Everything easy | LOW | BE_KIND | 80 | 2 | Be careful | MEDIUM | BE_SUSPICIOUS | 90 | 3 | OMG the rebels! | VERY HIGH | SHOOT_FIRST_ASK_LATER | 120 |
---|
create table deathstar_protocol_active (
id integer not null primary key,
only_one number(1) default 1 not null,
-- ID is also foreign key
constraint deathstar_prot_act_fk
foreign key ( id )
references deathstar_protocols ( id )
on delete cascade,
-- Make sure there can only be one row
constraint deathstar_prot_act_uq
unique ( only_one ),
-- by limiting the possible value of the
-- helper-column
constraint deathstar_prot_act_chk
check ( only_one = 1 )
)
Table created.
insert into deathstar_protocol_active ( id ) values (1 )
1 row(s) inserted.
insert into deathstar_protocol_active ( id ) values ( 2 )
ORA-00001: unique constraint (SQL_ABGVMCFJNOJBNCVIXAMSBJNZY.DEATHSTAR_PROT_ACT_UQ) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
create or replace view v_deathstar_protocols
as
select
prot.id, label, alert_level, defense_mode, power_level,
coalesce(active.only_one, 0) is_active
from
deathstar_protocols prot
left outer join deathstar_protocol_active active
on prot.id = active.id
View created.
select * from v_deathstar_protocols
ID | LABEL | ALERT_LEVEL | DEFENSE_MODE | POWER_LEVEL | IS_ACTIVE | 1 | Everything easy | LOW | BE_KIND | 80 | 1 | 2 | Be careful | MEDIUM | BE_SUSPICIOUS | 90 | 0 | 3 | OMG the rebels! | VERY HIGH | SHOOT_FIRST_ASK_LATER | 120 | 0 |
---|
update deathstar_protocol_active set id = 2
1 row(s) updated.
select * from v_deathstar_protocols
ID | LABEL | ALERT_LEVEL | DEFENSE_MODE | POWER_LEVEL | IS_ACTIVE | 1 | Everything easy | LOW | BE_KIND | 80 | 0 | 2 | Be careful | MEDIUM | BE_SUSPICIOUS | 90 | 1 | 3 | OMG the rebels! | VERY HIGH | SHOOT_FIRST_ASK_LATER | 120 | 0 |
---|