D
D
delongeroman2020-10-29 21:39:08
MySQL
delongeroman, 2020-10-29 21:39:08

How to bind a key in tables?

How can I do a substitution of multiple values ​​from one table to another?

Paint
5f9b0c3d4842f203469321.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2020-10-29
@IgorPI

The orders table must have a person_id foreign key that refers to the person table as the primary key.
Actually it looks like this

CREATE TABLE `persons` (
  `id` INT(10,0) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
  `phone` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
  PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;


CREATE TABLE `orders` (
  `id` INT(10,0) NOT NULL AUTO_INCREMENT,
  `date` DATETIME NULL DEFAULT NULL,
  `person_id` INT(10,0) NOT NULL DEFAULT '0',
  `street` VARCHAR(255) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_unicode_ci',
  `name` VARCHAR(255) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_unicode_ci',
  `phone` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_unicode_ci',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `person_id` (`person_id`) USING BTREE,
  CONSTRAINT `FK1_OP` FOREIGN KEY (`person_id`) REFERENCES `rmok`.`persons` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question