D
D
Dilik Pulatov2018-05-02 14:36:14
MySQL
Dilik Pulatov, 2018-05-02 14:36:14

How to make a connection in MySQL?

Hello!
I read articles on the Internet and watched video tutorials, but I can’t create a link between tables
, so let’s say there are two tables
1 - post (id, status, date)
2 - post_content (id, parent_id, language, name, content)
when deleting a post I need to delete it all the contents that are in different languages,
how can I do this?
I have MySQL version 5.7
in phpMyAdmin how to do it? or you need a request
in my opinion I didn’t understand very well link them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-05-02
@dilikpulatov

If I understood correctly, then here you have a one-to-many relationship, the `post_content`.`parent_id` field points to `post`.`id`.
In order to create such a relationship (foreign key, FOREIGN KEY) in MySQL, you first need to make sure that both of these fields are of the same type and size and that there are no values ​​in the `post_content`.`parent_id` column that are missing in `post`.`id `.
Then you need to create the connection itself

ALTER TABLE `post_content`
  ADD CONSTRAINT FOREIGN KEY `fk_parent_id` (`parent_id`)
    REFERENCES `post` (`id`)
    ON DELETE CASCADE;

This query adds a foreign key `fk_parent_id` from the `parent_id` field to `post`.`id` and says that deleting a record from the `post` table will delete related records from `post_content`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question