Answer the question
In order to leave comments, you need to log in
How to properly organize foreign keys in MySQL?
There are two tables .. the first table is "People", which will contain:
CREATE TABLE peoples(
id_people INT(255) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARACHAR(15),
surname VARACHAR(15),
patronymic VARACHAR(15)
);
CREATE TABLE comments(
id_comment INT(255) NOT NULL AUTO_INCREMENT PRIMARY KEY,
comment TEXT(),
...(и как здесь связать?)
);
Answer the question
In order to leave comments, you need to log in
10,255 people ? Where do you get so many?
Creating a foreign key:
ALTER TABLE `comments`
ADD COLUMN `peoples_id` INT(255) NOT NULL AFTER `comment`,
ADD INDEX `peoples_id_idx` (`peoples_id` ASC);
ALTER TABLE `comments`
ADD CONSTRAINT `comments_peoples`
FOREIGN KEY (`peoples_id_fk`)
REFERENCES `peoples` (`id_people`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
The next question is how to organize the transfer of this very id_people to the comments table?
When creating a table:
CREATE TABLE comments(
id_comment INT(255) NOT NULL AUTO_INCREMENT PRIMARY KEY,
comment TEXT not null,
id_people int(8) unsigned not null,
foreign key(id_people) references peoples(people_id) on update cascade on delete cascade);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question