Related
I have three tables, two are created independently, and the third one is created to include some inputs from the first two. First two tables have no problems, however, when I try to create the third one, I get an error:
Error report -
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
The thing is, I created the third table by copy/pasting the column names/definitions right from the first two tables, yet I still get this ridiculous error message. Now I wonder if the order of the columns and the especially the order of constrains is important.
The tables:
comm_Customers
CREATE TABLE comm_Customers (
custID NUMBER(6) NOT NULL,
FirstName VARCHAR2(10) NOT NULL,
LastName VARCHAR2(15) NOT NULL,
HomeCountry VARCHAR2(2) NOT NULL,
HomeState_Prov VARCHAR2(2) NOT NULL,
HomeCity VARCHAR2(20) NOT NULL,
HomeAddress VARCHAR2(25) NOT NULL,
Phone NUMBER(10) NOT NULL,
Email VARCHAR2(15) NOT NULL,
ShippCountry VARCHAR2(2) NOT NULL,
ShippState_Prov VARCHAR2(2) NOT NULL,
ShippCity VARCHAR2(10) NOT NULL,
ShippAddress VARCHAR2(15) NOT NULL,
CONSTRAINT comm_customers_custid_pk PRIMARY KEY (custID)
);
comm_Items
CREATE TABLE comm_Items (
itemID NUMBER(4) NOT NULL,
ItemCat VARCHAR2(3) NOT NULL,
ItemQty NUMBER(4) NOT NULL,
SalePrice NUMBER(6,2) NOT NULL,
CostPrice NUMBER(6,2) NOT NULL,
ItemDesc VARCHAR2(15),
CONSTRAINT comm_items_itemid_pk PRIMARY KEY (itemID)
);
comm_Orders which gives the error
CREATE TABLE comm_Orders (
orderID NUMBER(10) NOT NULL,
OrderQty NUMBER(4) NOT NULL,
OrderDate DATE NOT NULL,
Shipped VARCHAR2(1),
ShippedDate DATE,
custID NUMBER(6) NOT NULL,
Phone NUMBER(10) NOT NULL,
Email VARCHAR2(15) NOT NULL,
ShippCountry VARCHAR2(2) NOT NULL,
ShippState_Prov VARCHAR2(2) NOT NULL,
ShippCity VARCHAR2(10) NOT NULL,
ShippAddress VARCHAR2(15) NOT NULL,
itemID NUMBER(4) NOT NULL,
SalePrice NUMBER(6,2) NOT NULL,
CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID)
REFERENCES comm_Customers(custID),
CONSTRAINT comm_order_phone_fk FOREIGN KEY (Phone)
REFERENCES comm_Customers(Phone),
CONSTRAINT comm_order_email_fk FOREIGN KEY (Email)
REFERENCES comm_Customers(Email),
CONSTRAINT comm_order_shippcountry_fk FOREIGN KEY (ShippCountry)
REFERENCES comm_Customers(ShippCountry),
CONSTRAINT comm_order_shippstate_prov_fk FOREIGN KEY (ShippState_Prov)
REFERENCES comm_Customers(ShippState_Prov),
CONSTRAINT comm_order_shippcity_fk FOREIGN KEY (ShippCity)
REFERENCES comm_Customers(ShippCity),
CONSTRAINT comm_order_shippaddress_fk FOREIGN KEY (ShippAddress)
REFERENCES comm_Customers(ShippAddress),
CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID)
REFERENCES comm_Items(itemID),
CONSTRAINT comm_order_saleprice_fk FOREIGN KEY (SalePrice)
REFERENCES comm_Items(SalePrice)
ON DELETE CASCADE,
CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);
The references to non-primary key columns of customers and items do raise the error.
Bottom-line, you should not be duplicating the information from the referential tables. A single foreign key is sufficient.
So:
CREATE TABLE comm_Orders (
orderID NUMBER(10) NOT NULL,
OrderQty NUMBER(4) NOT NULL,
OrderDate DATE NOT NULL,
Shipped VARCHAR2(1),
ShippedDate DATE,
custID NUMBER(6) NOT NULL,
itemID NUMBER(4) NOT NULL,
CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID) REFERENCES comm_Customers(custID),
CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID) REFERENCES comm_Items(itemID),
CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);
Then, whenever you need to recover an information from a referential table, you join it using the foreign key. Say you want the phone of a customer:
select o.*, c.phone
from comm_orders o
inner join comm_customers c on c.custid = o.custid
I have ran this Script File on Oracle Application Express and it does not create my tables and does not state that I have errors. If you can't help that is okay, but I have been at this for hours and cannot figure out why it is not working
Here is my Script File:
CREATE TABLE GUEST_T
(GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR(25),
GUEST_ADDRESS VARCHAR(30),
CONSTRAINT GUEST_PK PRIMARY KEY (GUEST_ID));
CREATE TABLE COMPANY_T
(GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR(25),
COMPANY_NAME VARCHAR(25),
ADDRESS VARCHAR(30),
NO_OF_GUESTS INT,
CONSTRAINT COMPANY_PK PRIMARY KEY (GUEST_ID),
CONSTRAINT COMPANY_FK FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID));
CREATE TABLE ACCOMMODATION_T
(ACCOMMODATION_ID NUMBER NOT NULL,
ACCOMMODATION_TYPE VARCHAR(25),
PRICE DECIMAL (6,2),
CONSTRAINT ACCOMMODATION_PK PRIMARY KEY (ACCOMMODATION_ID));
CREATE TABLE EMPLOYEE_T
(EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR(25),
HOURS_WORKED INT,
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (EMPLOYEE_ID));
CREATE TABLE RECEPTIONIST_T
(EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR(25),
HOURS_WORKED INT,
HOURLY_WAGE DECIMAL (4,2),
CONSTRAINT RECEPTIONIST_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT EMPLOYEE_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE MANAGER_T
(EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR(25),
HOURS_WORKED INT,
SALARY DECIMAL (10,2)
CONSTRAINT MANAGER_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT EMPLOYEE_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE ACCOMMODATION_REQUEST_T
(ACCOMMODATION_ID NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
TIME REQUESTED NUMBER,
DATE_OF_USE DATE DEFAULTSYSDATE,
CONSTRAINT ACCOMMODATION_REQUEST_PK PRIMARY KEY (ACCOMMODATION_ID, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK1 FOREIGN KEY (ACCOMMODATION_ID) REFERENCES ACCOMMODATION_T(ACCOMMODATION_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE ROOM_T
(ROOM_NO NUMBER NOT NULL,
FLOOR_NO NUMBER,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR(20) CHECK (ROOM_STATUS IN (‘Vacant’, ‘Occupied’)),
CONSTRAINT ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT ROOM_FK FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T(FLOOR_NO));
CREATE TABLE DOUBLE_ROOM_T
(ROOM_NO NUMBER NOT NULL,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR(20) CHECK (ROOM_STATUS IN (‘Vacant’, ‘Occupied’)),
ADJOINED VARCHAR(5) CHECK (ADJOINED IN (‘Yes’, ’No’)),
CONSTRAINT DOUBLE_ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT DOUBLE_ROOM_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T (ROOM_NO),
CONSTRAINT DOUBLE_ROOM_FK2 FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T (FLOOR_NO));
CREATE TABLE RESERVATION_T
(ROOM_NO NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
CHECK_IN_DATE DATE DEFAULTSYSDATE,
CHECK_OUT_DATE DATE DEFAULTSYSDATE,
NO_OF_ROOMS INT,
CONSTRAINT RESERVATION_PK PRIMARY KEY (ROOM_NO, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT RESERVATION_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T(ROOM_NO),
CONSTRAINT RESERVATION_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT RESERVATION_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE FLOOR_T
(FLOOR_NO NUMBER NOT NULL,
NO_OF_ROOMS INT,
CONSTRAINT FLOOR_PK PRIMARY KEY (FLOOR_NO));
CREATE TABLE FACILITY_T
(FACILITY_ID NUMBER NOT NULL,
FACILITY_NAME VARCHAR(25),
CONSTRAINT FACILITY_PK PRIMARY KEY (FACILITY_ID));
I have run this Script File on Oracle Application Express and it does not create my tables and does not state that I have errors. If you can't help that is okay, but I have been at this for hours and cannot figure out why it is not working.
There are a lot of issues in your script (and they do show up when the scripts runs). The most proeminent ones are:
the same constraint name is used in several tables; a constraint name must be unique in a schema, you can prefix them with the table name to avoid clashes
various typos : missing underscore, missing spaces, missing commas
funky quotes ‘’ instead of regular quotes
wrong table creation sequence: a parent table must be created before its dependant tables
side note: you want to use VARCHAR2, that Oracle recommends to replace VARCHAR
Here is a new version of your script that works fine in this DB fiddle. All the changes are commented:
CREATE TABLE GUEST_T (
GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR2(25),
GUEST_ADDRESS VARCHAR2(30),
CONSTRAINT GUEST_PK PRIMARY KEY (GUEST_ID)
);
CREATE TABLE COMPANY_T (
GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR2(25),
COMPANY_NAME VARCHAR2(25),
ADDRESS VARCHAR2(30),
NO_OF_GUESTS INT,
CONSTRAINT COMPANY_PK PRIMARY KEY (GUEST_ID),
CONSTRAINT COMPANY_FK FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID)
);
CREATE TABLE ACCOMMODATION_T (
ACCOMMODATION_ID NUMBER NOT NULL,
ACCOMMODATION_TYPE VARCHAR2(25),
PRICE DECIMAL (6,2),
CONSTRAINT ACCOMMODATION_PK PRIMARY KEY (ACCOMMODATION_ID)
);
CREATE TABLE EMPLOYEE_T (
EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR2(25),
HOURS_WORKED INT,
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (EMPLOYEE_ID)
);
CREATE TABLE RECEPTIONIST_T (
EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR2(25),
HOURS_WORKED INT,
HOURLY_WAGE DECIMAL (4,2),
CONSTRAINT RECEPTIONIST_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT RECEPTIONIST_T_EMPLOYEE_FK -- constraint name must be unique
FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
CREATE TABLE MANAGER_T (
EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR2(25),
HOURS_WORKED INT,
SALARY DECIMAL (10,2), -- missing comma here
CONSTRAINT MANAGER_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT MANAGER_T_EMPLOYEE_FK -- constraint name must be unique
FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
CREATE TABLE ACCOMMODATION_REQUEST_T (
ACCOMMODATION_ID NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
TIME_REQUESTED NUMBER, -- missing underscore between TIME and REQUESTED
DATE_OF_USE DATE DEFAULT SYSDATE, -- missing space between DEFAULT and SYSDATE
CONSTRAINT ACCOMMODATION_REQUEST_PK PRIMARY KEY (ACCOMMODATION_ID, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK1 FOREIGN KEY (ACCOMMODATION_ID) REFERENCES ACCOMMODATION_T(ACCOMMODATION_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
-- must be created before the dependant tables (ROOM_T, DOUBLE_ROOM_T, ...)
CREATE TABLE FLOOR_T (
FLOOR_NO NUMBER NOT NULL,
NO_OF_ROOMS INT,
CONSTRAINT FLOOR_PK PRIMARY KEY (FLOOR_NO)
);
CREATE TABLE ROOM_T (
ROOM_NO NUMBER NOT NULL,
FLOOR_NO NUMBER,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR2(20) CHECK (ROOM_STATUS IN ('Vacant', 'Occupied')), -- funky quotes ‘’
CONSTRAINT ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT ROOM_FK FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T(FLOOR_NO)
);
CREATE TABLE DOUBLE_ROOM_T (
ROOM_NO NUMBER NOT NULL,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR2(20) CHECK (ROOM_STATUS IN ('Vacant', 'Occupied')), -- funky quotes ‘’
ADJOINED VARCHAR2(5) CHECK (ADJOINED IN ('Yes', 'No')), -- funky quote ‘’
CONSTRAINT DOUBLE_ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT DOUBLE_ROOM_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T (ROOM_NO)
-- CONSTRAINT DOUBLE_ROOM_FK2 FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T (FLOOR_NO) -- there is no such column
);
CREATE TABLE RESERVATION_T (
ROOM_NO NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
CHECK_IN_DATE DATE DEFAULT SYSDATE, -- missing space between DEFAULT and SYSDATE
CHECK_OUT_DATE DATE DEFAULT SYSDATE, -- missing space between DEFAULT and SYSDATE
NO_OF_ROOMS INT,
CONSTRAINT RESERVATION_PK PRIMARY KEY (ROOM_NO, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT RESERVATION_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T(ROOM_NO),
CONSTRAINT RESERVATION_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT RESERVATION_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
CREATE TABLE FACILITY_T (
FACILITY_ID NUMBER NOT NULL,
FACILITY_NAME VARCHAR2(25),
CONSTRAINT FACILITY_PK PRIMARY KEY (FACILITY_ID)
);
I've create two tables with some columns and all that stuff. And I get the error "no matching unique or primary key for this column-list", but I've no clue what I am missing...
It's a basic primary and foreign keys tables and it won't compile. I have check on internet for hours and found out nothing.
create table TP2_ITEM_FAVORI (
NOM_UTILISATEUR varchar2(30) not null,
NO_ITEM number(6) not null,
constraint PK_ITEM_FAV primary key(NOM_UTILISATEUR, NO_ITEM));
create table TP2_UTILISATEUR (
NOM_UTILISATEUR varchar2(30) not null,
NO_ENCAN number(6) not null,
MOT_DE_PASSE_UTI varchar(30) not null,
NOM_UTI varchar2(20) not null,
PRENOM_UTI varchar2(20) not null,
TEL_UTI char(13) not null,
COURRIEL_UTI varchar2(25) not null,
TYPE_UTI varchar(20) not null,
NOM_UTILISATEUR_PARENT varchar2(30) not null,
constraint PK_UTILISATEUR primary key(NOM_UTILISATEUR),
constraint AK_NOM_PRENOM_TEL_UTI unique(NOM_UTI, PRENOM_UTI, TEL_UTI),
constraint AK_COURR_UTI unique(COURRIEL_UTI),
constraint FK_NOM_UTILISATEUR_UTI foreign key(NOM_UTILISATEUR) references
TP2_ITEM_FAVORI(NOM_UTILISATEUR));
A foreign key needs to be a primary key or unique column of another table: https://www.techonthenet.com/oracle/errors/ora02270.php.
In TP2_UTILISATEUR, you references TP2_ITEM_FAVORI(NOM_UTILISATEUR) as a foreign key, but NOM_UTILISATEUR is not the primary key of TP2_ITEM_FAVORI. TP2_ITEM_FAVORI's primary key is (NOM_UTILISATEUR, NO_ITEM).
Resolution is to change TP2_ITEM_FAVORI's primary key to NOM_UTILISATEUR instead of both columns.
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID)
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
Here is my code and I can not figure out why comes the error.
CREATE TABLE Customer_T
(CustomerID NUMBER(11,0) NOT NULL,
CustomerFname VARCHAR2(25),
CustomerMname VARCHAR2(25),
CustomerLname VARCHAR2(25),
CustomerAddress1 VARCHAR2(50),
CustomerAddress2 VARCHAR2(50),
CustomerCity VARCHAR2(25),
CustomerState CHAR(2),
CustomerZipCode NUMBER(5,0),
CustomerPhoneNumber NUMBER(11,0),
CONSTRAINT Customer_PK PRIMARY KEY (CustomerID)
);
CREATE TABLE Pool_T
(PoolID NUMBER(11,0) NOT NULL,
PoolAddress1 VARCHAR2(25),
PoolAddress2 VARCHAR2(25),
PoolCity VARCHAR2(25),
PoolState VARCHAR2(25),
PoolZipCode NUMBER(5,0),
TypeSurface VARCHAR2(9),
Gallon INTEGER,
Filters VARCHAR2(25),
VisitPerMonth INTEGER,
Dates DATE DEFAULT SYSDATE,
Fee NUMBER(11,0),
CONSTRAINT Pool_PK PRIMARY KEY (PoolID)
);
CREATE TABLE PoolVisit_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
LengthStay INTEGER,
CONSTRAINT Poolvisit_PK PRIMARY KEY (PoolID,ServiceDate)
CONSTRAINT PoolVisit_FK FOREIGN KEY (PoolID)
REFERENCES Pool_T (PoolID)
);
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID)
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
CREATE TABLE PoolChemical_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ChemicalName VARCHAR2(25) NOT NULL,
Quantity INTEGER,
Cost NUMBER(11,2),
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName)
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID)
CONSTRAINT PoolChemical_FK FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
);
CREATE TABLE Activity_T
(ActivityID NUMBER(11,0) NOT NULL,
ActivityDesc VARCHAR2(500),
CONSTRAINT Activity_PK PRIMARY KEY (ActivityID));
CREATE TABLE Chemical_T
(ChemicalName VARCHAR2(25) NOT NULL,
CONSTRAINT Chemical_PK PRIMARY KEY (ChemicalName));
The error isn't just in the table you highlighted, it's also in the one before, PoolVisit_T. In both, you're just missing a comma after the primary key constraint:
CREATE TABLE PoolVisit_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
LengthStay INTEGER,
CONSTRAINT Poolvisit_PK PRIMARY KEY (PoolID,ServiceDate), -- was missing comma
CONSTRAINT PoolVisit_FK FOREIGN KEY (PoolID)
REFERENCES Pool_T (PoolID)
);
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID), -- was missing
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
And you're missing two in the one after that:
CREATE TABLE PoolChemical_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ChemicalName VARCHAR2(25) NOT NULL,
Quantity INTEGER,
Cost NUMBER(11,2),
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName), -- was missing
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID), -- was missing
CONSTRAINT PoolChemical_FK FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
);
If you use an IDE like SQL Developer it will highlight syntax problems like this as you go.
But you still have issues; your PoolActivity_T foreign key:
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
is referring only to the PoolID, but the primary key on PoolVisit_T and the primary key has two columns, so that needs to be:
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID,ServiceDate)
REFERENCES PoolVisit_T(PoolID,ServiceDate),
And you've used the same FK name for two constraints, so the next bit is:
CONSTRAINT PoolActivity_FK2 FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
(but pick more descriptive FK names, perhaps), and you have to create the Activity_T table before this one.
And similar issues with PoolChemical_T:
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName),
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID,ServiceDate)
REFERENCES PoolVisit_T(PoolID,ServiceDate),
CONSTRAINT PoolChemical_FK2 FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
and create Chemical_T first.
SQL Fiddle with all tables created.
When I run the booking table, it show the message "No unique index found for the referenced field of the primary table".
Create Table Customer
CREATE TABLE CUSTOMER
(
CUSTID INTEGER NOT NULL,
FNAME CHAR(18) NOT NULL,
LNAME CHAR(18) NOT NULL,
STREET CHAR(6) NOT NULL,
CITY CHAR(18) NOT NULL,
PROVINCE CHAR(8) NOT NULL,
COUNTRY CHAR(8) NOT NULL,
POSTCODE CHAR(6) NOT NULL,
GENDER CHAR(6) NOT NULL,
PRIMARY KEY (CUSTID)
);
Create Table Booking
CREATE TABLE BOOKING
(
BKGNO INTEGER NOT NULL,
CUSTID INTEGER NOT NULL,
FNO INTEGER NOT NULL,
STATUSID CHAR(3) NOT NULL,
CLASSID CHAR(4) NOT NULL,
ORIG CHAR(18) NOT NULL,
DEST CHAR(18),
DEPTTIME DATE NOT NULL,
ARRTIME DATE NOT NULL,
BKGCITY CHAR(18) NOT NULL,
PAIDBY CHAR(18) NOT NULL,
FPRICE CURRENCY NOT NULL,
TOTPRICE CURRENCY NOT NULL,
PAIDAMT CURRENCY NOT NULL,
BAL CURRENCY NOT NULL,
BKGDATE DATE NOT NULL,
PRIMARY KEY (BKGNO),
INDEX (ORIG,DEST,DEPTTIME, ARRTIME),
INDEX (CUSTID),
FOREIGN KEY (CUSTID) REFERENCES CUSTOMER,
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO),
FOREIGN KEY(DEST) REFERENCES FLIGHT_AVAILABILITY(DEST),
FOREIGN KEY(DEPTTIME) REFERENCES FLIGHT_AVAILABILITY(DEPTTIME),
FOREIGN KEY(ARRTIME) REFERENCES FLIGHT_AVAILABILITY(ARRTIME),
FOREIGN KEY(DEST,DEPTTIME,ARRTIME) REFERENCES FLIGHT_AVAILABILITY(DEST, DEPTTIME, ARRTIME),
FOREIGN KEY (ORIG) REFERENCES AIRPORT(AIRPORTCD)
);
Create Table Flight Availability
CREATE TABLE FLIGHT_AVAILABILITY
(
FNO INTEGER NOT NULL,
ORIG CHAR(18) NOT NULL,
DEST CHAR(18),
DEPTTIME DATE NOT NULL,
ARRTIME DATE NOT NULL,
FLENGTH INTEGER NOT NULL,
PRIMARY KEY (FNO,ORIG,DEST,DEPTTIME,ARRTIME)
);
Create TableAirport
CREATE TABLE AIRPORT
(
AIRPORTCD CHAR(18) NOT NULL,
CITYID CHAR(18) NOT NULL,
AIRPORTNM CHAR(18) NOT NULL,
AIRPORTTAX CURRENCY,
PRIMARY KEY (AIRPORTCD)
);
This part causes errors:
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO),
FOREIGN KEY(DEST) REFERENCES FLIGHT_AVAILABILITY(DEST),
FOREIGN KEY(DEPTTIME) REFERENCES FLIGHT_AVAILABILITY(DEPTTIME),
FOREIGN KEY(ARRTIME) REFERENCES FLIGHT_AVAILABILITY(ARRTIME),
FOREIGN KEY(DEST,DEPTTIME,ARRTIME) REFERENCES FLIGHT_AVAILABILITY(DEST, DEPTTIME, ARRTIME)
A foreign key constraint (also called a referential integrity
constraint) designates a column as the foreign key and establishes a
relationship between that foreign key and a specified primary or
unique key, called the referenced key. A composite foreign key
designates a combination of columns as the foreign key.
So a referenced key should be either unique or primary key.
For example:
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO)
Referenced key = FNO which is not unique and is not a primary key. Although FNO is part of the composite PRIMARY KEY (FNO,ORIG,DEST,DEPTTIME,ARRTIME), it's not unique (the combination of all the columns of the primary key is unique).
P.S. you really don't need so many columns in your primary key in FLIGHT_AVAILABILITY table. You may create a composite unique index on those columns (if it's really required) and create a NOT NULL constraint on each column. In this case you'll have the same checks as in your current composit primary key. But I don't think you need this.