create table STADIUM
( stad_location varchar2(20) primary key
, stad_name varchar2(10)
, stad_capacity number(5)
, match_ID char(8)
, stall_ID char(4)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
create table TRANSPORTATION
(registra_no char(6) primary key
, transp_type varchar2(10)
, capacity number(2)
);
create table CEREMONY
(cerem_type varchar2(10) primary key
, cerem_name varchar2(15)
, FIFA_theme_song varchar2(20)
, p_ID char(8)
, stad_location varchar2(20)
, foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
, foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
);
create table REFEREE( ref_ID char(8) primary key
, ref_name varchar2(20)
, yo_exp number(2)
, match_ID char(8)
, registra_no char(6)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729'); select * from CEREMONY;
select * from REFEREE;
Bunch of tables are missing, referenced by foreign keys so I'm creating them first:
SQL> -- Missing tables, referenced by foreign keys
SQL> create table match (match_id char(8) primary key);
Table created.
SQL> create table stall (stall_id char(4) primary key);
Table created.
SQL> create table performer (p_id char(8) primary key);
Table created.
SQL>
Your tables:
SQL> create table STADIUM
2 ( stad_location varchar2(20) primary key
3 , stad_name varchar2(10)
4 , stad_capacity number(5)
5 , match_ID char(8)
6 , stall_ID char(4)
7 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
8 , foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
Table created.
SQL> create table TRANSPORTATION
2 (registra_no char(6) primary key
3 , transp_type varchar2(10)
4 , capacity number(2)
5 );
Table created.
SQL> create table CEREMONY
2 (cerem_type varchar2(10) primary key
3 , cerem_name varchar2(15)
4 , FIFA_theme_song varchar2(20)
5 , p_ID char(8)
6 , stad_location varchar2(20)
7 , foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
8 , foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
9 );
Table created.
SQL> create table REFEREE( ref_ID char(8) primary key
2 , ref_name varchar2(20)
3 , yo_exp number(2)
4 , match_ID char(8)
5 , registra_no char(6)
6 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
7 , foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
Table created.
SQL>
Inserts into tables referenced by ceremony and referee:
SQL> insert into performer values ('PP561475');
1 row created.
SQL> insert into stadium (stad_location) values ('Al-Waab Street');
1 row created.
SQL> insert into match values ('MM129456');
1 row created.
SQL> insert into transportation (registra_no) values ('QLM729');
1 row created.
SQL>
Your inserts & result of your select statements:
SQL> insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
1 row created.
SQL> insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729');
1 row created.
SQL> select * from CEREMONY;
CEREM_TYPE CEREM_NAME FIFA_THEME_SONG P_ID STAD_LOCATION
---------- --------------- -------------------- -------- --------------------
Opening Speech Colors PP561475 Al-Waab Street
SQL> select * from REFEREE;
REF_ID REF_NAME YO_EXP MATCH_ID REGIST
-------- -------------------- ---------- -------- ------
RF503624 Mike Dean 25 MM129456 QLM729
SQL>
Therefore, if you do everything right, everything is right.
Related
I have problem with Enforcing Referential Integrity to a new table.
There are different tables in different schemas, each one has its primary key:
schema1.table1
schema2.table2
schema3.table3
I want to create a new table, which among other information and its primary id, has a column "reference_schema" and a column "reference_id".
I want the column "referencing id" to reference the id on the relevant table, that is of the "reference_schema"="schema1" to reference the primary key schema1.table1.id.
The primary keys on the 3 tables, aren't unique in a UNION.
I have tried synthesizing a primary key in a UNION ALL view, but Oracle does not enforce view constraints.
It is not the different schema that causes problems, but the fact that you can't create a foreign key constraint which would reference two (or more) different tables.
I mean, you can do it, using out-of-line constraint syntax, but that just won't work. Why? Because - if that value doesn't exist in all referenced tables, constraint will be violated.
create table new_table
(id number constraint pk_newtab primary key,
ref_schema varchar2(30),
ref_id number,
--
constraint fk_newtab_s1 foreign key (ref_id) references schema1.table1 (id),
constraint fk_newtab_s2 foreign key (ref_id) references schema2.table2 (id)
);
A simple example is Scott's sample schema (there is department number 10, but no employee has that EMPNO):
SQL> create table test
2 (id number,
3 constraint fk1 foreign key (id) references scott.dept (deptno),
4 constraint fk2 foreign key (id) references scott.emp (empno)
5 );
Table created.
SQL> insert into test (id) values (10);
insert into test (id) values (10)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK2) violated - parent key not found
SQL>
So, what can you do? Use a trigger. Something like this:
SQL> create or replace trigger trg_test
2 before insert or update on test
3 for each row
4 declare
5 l_cnt number;
6 begin
7 select deptno into l_cnt
8 from dept
9 where deptno = :new.id;
10
11 exception
12 when no_data_found then
13 begin
14 select empno into l_cnt
15 from emp
16 where empno = :new.id;
17
18 exception
19 when no_data_found then
20 raise_application_error(-20000, 'Foreign key does not exist in any referenced table');
21 end;
22 end;
23 /
Trigger created.
Testing:
SQL> insert into test (id) values (10); --> this is ACCOUNTING
1 row created.
SQL> insert into test (id) values (7369); --> this is SMITH
1 row created.
SQL> insert into test (id) values (99); --> this doesn't exist in any table
insert into test (id) values (99)
*
ERROR at line 1:
ORA-20000: Foreign key does not exist in any referenced table
ORA-06512: at "SCOTT.TRG_TEST", line 17
ORA-04088: error during execution of trigger 'SCOTT.TRG_TEST'
SQL>
From Oracle 12, you can use virtual columns and put the constraints on the virtual column.
For example, if you have the tables:
CREATE TABLE table1 (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);
CREATE TABLE table2 (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);
CREATE TABLE table3 (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);
Then you can create your table as:
CREATE TABLE new_table(
id NUMBER
GENERATED ALWAYS AS IDENTITY
PRIMARY KEY,
reference_table VARCHAR2(30)
CHECK (reference_table IN ('table1', 'table2', 'table3')),
reference_id NUMBER
NOT NULL,
t1_id NUMBER
INVISIBLE
AS (
CASE reference_table WHEN 'table1' THEN reference_id END
)
REFERENCES table1(id),
t2_id NUMBER
INVISIBLE
AS (
CASE reference_table WHEN 'table2' THEN reference_id END
)
REFERENCES table2(id),
t3_id NUMBER
INVISIBLE
AS (
CASE reference_table WHEN 'table3' THEN reference_id END
)
REFERENCES table3(id)
);
Note: If you want to change from different tables in the same schema to the same table in different schemas then just update the foreign key constraint to point to the correct location.
db<>fiddle here
Can't find the missing parenthesis or figure out why I'm getting the error.
create table course(
CourseNum number(10) constraint course_CourseNum_pk primary key,
courseName varchar2(40),
startDate date,
endDate date,
Ins_ID varchar2(10),
constraint course_Ins_ID_fk foreign key
references instructor(Ins_ID)
);
expecting a table with 5 columns to be created.
Should be like this:
SQL> create table instructor (ins_id varchar2(10) primary key);
Table created.
SQL> create table course(
2 CourseNum number(10) constraint course_CourseNum_pk primary key,
3 courseName varchar2(40),
4 startDate date,
5 endDate date,
6 Ins_ID varchar2(10),
7 constraint course_Ins_ID_fk foreign key (ins_id) --> you're missing "(ins_id)" here
8 references instructor (Ins_ID)
9 );
Table created.
SQL>
Or, alternatively:
SQL> create table course(
2 CourseNum number(10) constraint course_CourseNum_pk primary key,
3 courseName varchar2(40),
4 startDate date,
5 endDate date,
6 Ins_ID varchar2(10) constraint course_Ins_ID_fk references instructor (Ins_ID)
7 );
Table created.
SQL>
Create table A_15006977.vehicle. (
Vin varchar(20) primary key,
Vehicle_type char(20) not null,
Mileage number(20) not null,
Manufacturer char(20) not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values ('tf1bb2ve533093891','panel van',18 325,'man')
A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values
('tf1bb2ve533093822','standard van',79 885,'ford')
Select * from dual;
Create table A_15006977.vehicle (
Vin varchar(20) CONSTRAINT vehicle__vin__pk PRIMARY KEY,
Vehicle_type char(20) CONSTRAINT vehicle__vehicle_type__nn not null,
Mileage number(20) CONSTRAINT vehicle__mileage__nn not null,
Manufacturer char(20) CONSTRAINT vehicle__manufacturer__nn not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
VALUES ( 'tf1bb2ve533093891', 'panel van', 18325, 'man' )
INTO A_15006977.vehicle (vin,vehicle_type,mileage,manufacturer)
values ( 'tf1bb2ve533093822', 'standard van', 79885, 'ford' )
SELECT 1 FROM DUAL;
Or:
Insert Into A_15006977.vehicle( vin,vehicle_type,mileage,manufacturer )
SELECT 'tf1bb2ve533093891','panel van', 18325, 'man' FROM DUAL UNION ALL
SELECT 'tf1bb2ve533093822','standard van', 79885, 'ford' FROM DUAL;
Note:
You had an extra . after the table name in the DDL statement and spaces in the mileage (18 325 and 79 885) which need removing and you needed an INTO keyword before the second insert.
It is also useful to name your constraints (then you can easily determine which constraint has been violated in later statements).
I'm trying to run my databases but STUDENTS AND BATCHES tables are giving the error: 'ORA-00942: table or view does not exist'. I've tried dropping them in order, but I can't tell what exactly the problem is.
Forgive me if the answer is obvious, I'm new to batches
DROP TABLE students;
DROP TABLE batches;
DROP TABLE courses;
DROP TABLE faculty;
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
Create your faculty and courses tables first. Since batches depends on one of those tables, you would want to have those tables created first.
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Example: http://sqlfiddle.com/#!4/91909c/1 shows the sequence of table creation
I have two tables :
SQL> desc SEGMENT
Name Null? Type
----------------------------------------- -------- ----------------------------
INDIP NOT NULL VARCHAR2(11)
NOMSEGMENT NOT NULL VARCHAR2(20)
ETAGE
SQL> desc POSTE
Name Null? Type
----------------------------------------- -------- ----------------------------
NPOSTE NOT NULL VARCHAR2(7)
NOMPOSTE NOT NULL VARCHAR2(20)
INDIP VARCHAR2(11)
AD VARCHAR2(3)
TYPEPOSTE VARCHAR2(9)
NSALLE VARCHAR2(7)
I want to add a constraint as the following :
ALTER TABLE "POSTE" ADD CONSTRAINT "FK_POSTE_SEGMENT" FOREIGN KEY ("INDIP") REFERENCES "SEGMENT" ("INDIP") ENABLE;
But I got this error message :
ERROR at line 1: ORA-02298: cannot validate (AIMAD.FK_POSTE_SEGMENT) -
parent keys not found
How can I solve this
You should check what POSTE table does not contain values in INDIP column what don't exist in INDIP column of SEGMENT table.
Like
SQL> create table t (x int primary key);
SQL> insert into t values(1);
SQL> create table t_c (x int);
SQL> insert into t_c values(1);
SQL> insert into t_c values(2);
SQL> commit;
SQL> alter table t_c add constraint t_c_x foreign key(x)
2 references t(x);
alter table t_c add constraint t_c_x foreign key(x)
*
ORA-02298: cannot validate (SCOTT.T_C_X) - parent key not found
SQL> select * from t_c where not exists (select *
2 from t where t.x = t_c.x);
X
-----------------------
2
Also Oracle provides the ability to create constraint in NOVALIDATE status, this prevents Oracle from checking data during contraint creation:
SQL> alter table t_c add constraint t_c_x foreign key(x)
2 references t(x) enable novalidate;
Table altered.
but this can have undesirable side effects because data in both tables are not consistent.