Oracle parents key not found - sql

Im trying to add a constraint to a reservation table, the reservation can either be for a flight or accommodation or both.
First 4 records booked inward flight, outward flight and accommodation
Next 4 records booked a flight only and have acc_id set to NULL
Following 2 records booked only accommodation, hence in flight, out flight and seats are set to null.
Here are my constraints for this table
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT HOLIDAY_PK PRIMARY KEY (RESV_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT CUSTOMER_FK FOREIGN KEY (BOOKING_CUS_ID) REFERENCES CUSTOMER (CUS_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT STAFF_FK3 FOREIGN KEY (EMP_ID) REFERENCES STAFF (EMP_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK FOREIGN KEY (IN_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK1 FOREIGN KEY (OUT_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT ACC_FK FOREIGN KEY (ACC_ID) REFERENCES ACCOMMODATION (ACC_ID);
and the only constraint that is yielding an error is;
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK1 FOREIGN KEY (OUT_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
I get
ERROR at line 1:
ORA-02298: cannot validate (U1146815.FLIGHT_FK1) - parent keys not found
What seems to be the problem? i understand that it has to do with orphan childs, but i am setting nulls so i dont understand, please advise

The error indicates that the FLIGHT table does not have an entry for at least one of the FLI_ID values between 11 and 18. You'd need to insert a row in the FLIGHT table for whatever flights are missing or update your table to have a different OUT_FLIGHT_ID.

Delete all the rows from the child table which is being used to alter the column for having the references.
ALTER TABLE sales ADD CONSTRAINT sales_time_fk
FOREIGN KEY (time_id) REFERENCES times (time_id)
RELY DISABLE NOVALIDATE;

Related

Can I use the same foreign key in multiple child tables from the parent table in XAMPP phpmyadmin?

The books are academic and do not specify what to do with the foreign keys. For the college assignment I am building an SQL database for a dental clinic.
If I had to create foreign keys, do I need one per table or can a table specialist dental referral be left with no foreign key.
I have 5 tables in dental clinic database.
appointment PK (primary key) app_id
patient PK pat_id
payment PK pay_id
treatment PK treat_id
referral PK ref_id
-- Constraints for table `appointment`
--
ALTER TABLE `appointment`
ADD CONSTRAINT `patient` FOREIGN KEY (`pat_id`) REFERENCES `patient` (`pat_id`),
ADD CONSTRAINT `pay_id` FOREIGN KEY (`pay_id`) REFERENCES `payment` (`pay_id`);
--
-- Constraints for table `patient`
--
ALTER TABLE `patient`
ADD CONSTRAINT `appointment` FOREIGN KEY (`app_id`) REFERENCES `appointment` (`app_id`),
ADD CONSTRAINT `ref` FOREIGN KEY (`ref_id`) REFERENCES `referral` (`ref_id`),
ADD CONSTRAINT `treat` FOREIGN KEY (`treat_id`) REFERENCES `treatment` (`treat_id`);
--
-- Constraints for table `payment`
--
ALTER TABLE `payment`
ADD CONSTRAINT `app` FOREIGN KEY (`app_id`) REFERENCES `appointment` (`app_id`),
ADD CONSTRAINT `pat` FOREIGN KEY (`pat_id`) REFERENCES `patient` (`pat_id`);
COMMIT;
Foreign keys are used to make relationship between tables and ensure the integrity of the data in that field. A table may have zero or more foreign keys. The process of creating multiple tables and adding foreign keys and all is called Normalisation. It would be nice for you to read on it and understand it properly. This will help in your whole life.
In your case, the way I see it, assuming a patient can exist independently, the patient do not have any dependencies.
A patient has one or more appointments. However an appointment cannot exist without a patient. An appointment has only one patient, so in this case, there must be a foreign key of Patient in the table Appointment.
The appointment can have one or many payments (depending on your business case). A payment cannot exist without an appointment. However linking payment with patient is kinda redundant given that you can find the patient in the appointment associated with the payment. Still this is a case to case basis based on the business requirement.
Now let's say you have a patient with patient id 1. You are adding a new appointment but and by mistake, instead of using 1 for the foreign key patient id, another value has been used, like 100. And in the system, there is no patient with id 100. When doing the insert, you will get a foreign key constraint error, informing you no patient exists with the id 100. The foreign key ensures data integrity for the foreign keys used in the table, like the foreign key patient id in the table appointment.

Create a table in SQL where the Composite Primary Key is also a Foreign Key

I need to create a table called LFM_Enroll in SQL that has a composite primary key of Student_ID and Section_Number. Student_ID is also a foreign key, it references Student_ID in the LFM_Student table and Section_Number is also a foreign key, it references Section_Number in the LFM_Section table. How do I write the constraints and foreign keys? I've attached an image of the tables and below is what I have done so far. After the LFM_Enroll table is created I need to update one row. I tried doing so but kept getting the below error.
: Error starting at line : 173 in command -
UPDATE LFM_Enroll
SET Student_ID = 1234567,
Section_Number = 01234
WHERE Student_ID = 900000 AND Section_Number = 4138
Error report -
ORA-02291: integrity constraint (SYSTEM.FK_LFM_ENROLL_SECTION_NUMBER) violated - parent key not found.
Tables Thanks in advance for all your help.
CREATE TABLE LFM_Enroll (
Student_ID char(7),
Section_Number char(4),
constraint PK_LFM_Enroll Primary Key (Student_ID,Section_Number),
constraint FK_LFM_Enroll_Student_ID
Foreign Key (Student_ID,Section_Number) references LFM_Student (Student_ID),
constraint FK_LFM_Enroll_Section_Number
Foreign Key (Student_ID,Section_Number) references LFM_Section (Section_Number)
);
Your foreign key constraints are not right. You are trying to map two columns {Student_ID,Section_Number} to a single column LFM_Student.Student_ID.
The number of columns in the principal key must match the number of columns in the foreign key. In other words, the key LFM_Student is one column (Student_ID), so the foreign key also needs to be a single matching column - in this case LFM_Enroll.Student_ID. Correct DDL would be:
constraint FK_LFM_Enroll_Student_ID
Foreign Key (Student_ID) references LFM_Student (Student_ID),
constraint FK_LFM_Enroll_Section_Number
Foreign Key (Section_Number) references LFM_Section (Section_Number)
I'm not quite sure why your RDBMS is allowing what you have, but it may be using the first column and simply ignoring the second. In which case FK_LFM_Enroll_Section_Number is creating a foreign key LFM_Enroll.Student_ID => LFM_Section.Section_Number.
The error indicates that the values with which you are trying to update the two columns may not exist in Student and / or Sections tables i.e. 1234567 doesn't exists in the student table and / or 01234 doesn't exist in your section table . You should try inserting new rows or updating existing ones with the new values you are trying to update your foreign keys with.
[Edit: For defining constraints refer lc.'s post]

How to add foreign key to an existing column in SQL Server 2012

I am trying to add foreign key to my existing column using below query
ALTER TABLE Sub_Category_Master
ADD FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
but I'm getting an error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Sub_Categ__Categ__5812160E". The conflict occurred in database "shaadikarbefikar_new", table "shaadikarbefikar.Category_Master", column 'Category_ID'.
Well, the error clearly tells you that Category_ID in your Sub_Category_Master table contains some values that are not present in Category_Master (column Category_ID). But that's exactly the point of having a foreign key constraint - making sure your child table (Sub_Category_Master) only uses defined values from its parent table.
Therefore, you must fix those "voodoo" values first, before you're able to establish this foreign key relationship. I would also strongly recommend to explicitly name that constraint yourself, to avoid those system-generated, but not really very useful constraint names like FK__Sub_Categ__Categ__5812160E:
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FK_SubCategoryMaster_CategoryMaster
FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FKSub_Category_Master_Category_ID FOREIGN KEY (Category_ID)
REFERENCES Category_Master(Category_ID);
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

two columns referencing a single column in another table

A similar question is asked here multiple foreign keys referencing single column in other table
but the syntax is not shown in the answer. I would like to know how this can be accomplished in SQL server. The following syntax gives error
ALTER TABLE ItemIssue ADD CONSTRAINT FK_ItemIssue_Person
FOREIGN KEY (PersonID, AdvisorID) REFERENCES Person (PersonID)
;
ERROR: Number of referencing columns in foreign key differs from number of referenced columns, table 'ItemIssue'.
-- Create Tables
CREATE TABLE ItemIssue (
ItemIssueID int identity(1,1) NOT NULL,
PersonID int,
AdvisorID int,
)
;
CREATE TABLE Person (
PersonID int NOT NULL,
Name nvarchar(500),
)
;
You need to define two foreign keys, one for each column:
ALTER TABLE ItemIssue ADD CONSTRAINT FK_ItemIssue_Person
FOREIGN KEY (PersonID) REFERENCES Person (PersonID)
;
ALTER TABLE ItemIssue ADD CONSTRAINT FK_ItemAdvisor_Person
FOREIGN KEY (AdvisorID) REFERENCES Person (PersonID)
;
It is impossible to create one foreign key for two columns referencing one column. Create them seperate:
ALTER TABLE ItemIssue
ADD CONSTRAINT FK_ItemIssue_Person_Person FOREIGN KEY (PersonID) REFERENCES Person (PersonID),
ADD CONSTRAINT FK_ItemIssue_Advisor_Person FOREIGN KEY (AdvisorID) REFERENCES Person (PersonID);
To define two foreign keys, one for each column-
Table
Contract - HospidPharmacyId Column
Hospice- HospiceID PK
Pharmacy PharmacyId Pk
Using Following Query we can apply 2 Foreign Key for 1 column.
Alter Table Contract
Add Constraint fk_pharmacyID Foreign Key ([HospIDPharmID]) references Pharmacy([PharmacyID])
Alter TAble contract
Add Constraint Fk_hospId Foreign key ([HospIDPharmID]) references Hospice(HospiceID)
In the Contract Table for column-HospidPharmacyId we can insert common value in both the
tables. those which are present in hospice & not in Pharmacy then we cant insert that value in
contract table & vice versa.

Removing a primary key column in SQL Server 2008

I have a table named master_employee with a column empid as the primary key, and it has 12 rows in it. This empid has been mapped as a foreign key to another table named dep_child. I want to delete the 12 records in the master_employee table but I was unable to do it.
You were unable to do the deletes from master_employees, as there is a referential integrity with dep_child. You would need to either disable the constraint or delete the records from dep_child, before being able to delete records from master_employees
Add this constraints to your master_employee table
ALTER TABLE "master_employee"
ADD CONSTRAINT "fk_emp11"
FOREIGN KEY ("emp_id")
REFERENCES "dep_child" ("emp_id")
ON DELETE CASCADE;