N
N
nkorobkov2017-04-01 16:05:54
MySQL
nkorobkov, 2017-04-01 16:05:54

How to link tables in SQL?

Good evening. There is such a database structure:
62c4744614944bde8e570fa27b69dc1b.pngQuestion: how to connect tables in the MySQL Command Line Client through the Organizations_services linking table by the service_id and org_id fields? For this purpose it is necessary to make these fields secondary keys? Or just through REFERENCE you can somehow set the link?
The bottom line is that I don’t need to get data from these tables yet, I just need to make a connection between them so that the fields of the linking table are also filled in as data appears in the other two tables.
Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2017-04-17
@idShura

Using FOREIGN KEY Constraints

ALTER TABLE `Услуги_предприятий`
  ADD CONSTRAINT `<CONSTRAINT_NAME1>` FOREIGN KEY (`org_id`) REFERENCES `Предприятия` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `<CONSTRAINT_NAME2>` FOREIGN KEY (`service_id`) REFERENCES `Список_услуг` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question