简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM