Was creating some tables in SQL and got stuck when I had to design the following tables:
As you can see it is impossible to create Room details and services details Table without customer receipt table as they contain Receipt_no as the primary key.
Similarly, it is impossible to create a Customer Receipt table without Room_charges and Service_charges attributes without first creating the two details tables.
Hence, I first created a customer receipt table but without FK constraints on Room_Charges
and Service_charges
and then I created Services Details and Room Details tables.
Later, using ALTER command I tried to add FK Constraints on Customer Receipt table but it gives me this error
ORA-02270: no matching unique or primary key for this column-list
Now, after researching a bit about it on StackOverflow, there might be three possible cases as mentioned in the approved answer (@ Oracle (ORA-02270) : no matching unique or primary key for this column-list error )
I think my case is number 3 as I have ensured the first two cases to be implemented.
Can anyone help me resolve it?
I am attaching SQL Code as a reference:
CREATE TABLE Customer_Receipt
(
Receipt_no VARCHAR2(12) PRIMARY KEY,
Booking_no NUMBER NOT NULL,
Total_charges NUMBER(12,2),
CONSTRAINT bookingnocustrec
FOREIGN KEY(Booking_no) REFERENCES Room_booking (Booking_no)
);
CREATE TABLE Services_Details
(
Receipt_no VARCHAR2(12) NOT NULL,
Service_offered VARCHAR2(8) NOT NULL,
Service_charges NUMBER(12,2),
PRIMARY KEY(Receipt_no, Service_offered),
CONSTRAINT recno
FOREIGN KEY(Receipt_no) REFERENCES Customer_receipt (Receipt_no)
);
ALTER TABLE Services_Details
MODIFY Service_charges NOT NULL;
CREATE TABLE Room_Details
(
Receipt_no VARCHAR2(12) NOT NULL,
Category_name VARCHAR2(9) NOT NULL,
Days_stayed INT,
Room_charges NUMBER(12,2),
PRIMARY KEY(Receipt_no, Category_name),
CONSTRAINT recno1
FOREIGN KEY(Receipt_no) REFERENCES Customer_receipt (Receipt_no),
CONSTRAINT catname1
FOREIGN KEY(Category_name) REFERENCES Room_category (Category_name)
);
ALTER TABLE Customer_receipt
ADD Room_charges NUMBER(12,2) NOT NULL;
ALTER TABLE Customer_receipt
ADD CONSTRAINT FK_RC
FOREIGN KEY (Room_charges) REFERENCES Room_Details (Room_charges);