S
S
sacred12014-11-17 17:13:44
PHP
sacred1, 2014-11-17 17:13:44

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)
);

And the second table is the actual "Comments" of these same users:
CREATE TABLE comments(
id_comment INT(255) NOT NULL AUTO_INCREMENT PRIMARY KEY,
comment TEXT(),
...(и как здесь связать?)
);

The foreign key is supposed to be id_people, respectively, and I would like to understand how best to organize this bundle.
The next question is how to organize the transfer of this very id_people to the comments table? As I understand it, we are accessing two tables, and id_people must somehow be transferred to the table with reviews, and how to do this? mysql functions (something like LAST_INSERT_ID), or stored procedures or otherwise.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2014-11-17
@sacred1

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;

Depending on what you specify in ON DELETE and ON UPDATE, the behavior of the database will change when you try to delete an entry in `peoples` or change `peoples`.`id_people`.

V
Vicom, 2016-09-10
@vicom

The next question is how to organize the transfer of this very id_people to the comments table?

the question is not quite correct, but somewhere in six months you will not even think in this way, so let's go. I think you need information about the ability to make a conditional selection from one table by the found value from another. if so, then dig in the direction of JOIN, the other day I mastered it, 20 links, a slight restructuring of the head and a temporary change in the type of brain activity help, if you don’t immediately stick it.
PS in order to approach from the right side, first define for yourself in your head such concepts as:
- relational DBMS
- table structure of a relational DBMS
- table fields
- table columns
- a foreign key (one of those columns that connects the same fields of some tables with others through value)
- MySQL a little about JOINs
- Visual explanation of the principle of joining tables in ...
- Explanation of SQL joins JOIN: LEFT/RIGHT/INNER/OUTER
- MySQl: using JOIN statements with examples
- Development → MySQL and JOINs , and when you pull yourself
up already - Top 20+ MySQL Best Practices PDF and buy in real life - the very thing for a normal understanding of the essence. good luck!

V
Vyacheslav Uspensky, 2014-11-17
@Kwisatz

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);

Well, or as a friend suggested above.
Threat Do not get confused in such names? First, Middle, Last name not better? )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question