如何将数据从父表更新或插入到子表?

时间:2019-05-06 04:30:08

标签: mysql sql

好吧,所以我想尽办法解决这个问题,但是我们有两个表,其中Book表是Author表的父表,而Author表使用book表中的两个外键,然后有三个作者的名字,中间名和姓氏列;首先,当我输入图书信息时,如何用相同的isbn和副本号更新作者表?在AF,AM,AL列中插入值时?

VALUES ("1234567980605", 2, "Chaos Theory", "Mathematics", 19900910, "Steven Stugart", 450);
INSERT INTO AUTHOR (isbn, copy) SELECT ISBN FROM BOOK;
INSERT INTO AUTHOR (AF, AM, AL) VALUES ("Tony", "W", "Stark");

这是我精心设计的查询,认为它可以工作,但是没有用。

预订表

+---------------+------+----------------+-------------+------------+-------------------+------+
|     ISBN      | COPY |     TITLE      |    GENRE    |   PDATE    |     PUBLISHER     | SIZE |
+---------------+------+----------------+-------------+------------+-------------------+------+
| 1111111111111 |    1 | Calculus AB BC | Math        | 2000-01-02 | Pearson Education |  325 |
| 1111111111111 |    2 | Calculus AB BC | Math        | 2000-01-02 | Pearson Education |  325 |
| 1112223334445 |    2 | Test           | tst         | 2019-05-05 | testaa            |  123 |
| 1212365454789 |    1 | Math           | Mathematics | 2000-02-02 | Pearson Education |  450 |
+---------------+------+----------------+-------------+------------+-------------------+------+

作者表

+---------------+------+--------+----+---------+
|     isbn      | copy |   AF   | AM |   AL    |
+---------------+------+--------+----+---------+
| 1111111111111 |    1 | John   | M  | Smith   |
| 1111111111111 |    2 | John   | M  | Smith   |
| 1231231231231 |    1 | Peyton | M  | Manning |
+---------------+------+--------+----+---------+

预期结果:

VALUES ("1234567980605", 2, "Chaos Theory", "Mathematics", 19900910, "Steven Stugart", 450);
INSERT INTO AUTHOR (isbn, copy) SELECT ISBN FROM BOOK;
INSERT INTO AUTHOR (AF, AM, AL) VALUES ("Tony", "W", "Stark");

书桌

+---------------+------+----------------+-------------+------------+-------------------+------+
|     ISBN      | COPY |     TITLE      |    GENRE    |   PDATE    |     PUBLISHER     | SIZE |
+---------------+------+----------------+-------------+------------+-------------------+------+
| 1111111111111 |    1 | Calculus AB BC | Math        | 2000-01-02 | Pearson Education |  325 |
| 1111111111111 |    2 | Calculus AB BC | Math        | 2000-01-02 | Pearson Education |  325 |
| 1112223334445 |    2 | Test           | tst         | 2019-05-05 | testaa            |  123 |
| 1212365454789 |    1 | Math           | Mathematics | 2000-02-02 | Pearson Education |  450 |
| 1234567980605 |    2 | Chaos Theory   | Mathematics | 1990-09-10 | Steven Stugart    |  450 |
+---------------+------+----------------+-------------+------------+-------------------+------+

作者表

+---------------+------+--------+----+---------+
|     isbn      | copy |   AF   | AM |   AL    |
+---------------+------+--------+----+---------+
| 1111111111111 |    1 | John   | M  | Smith   |
| 1111111111111 |    2 | John   | M  | Smith   |
| 1231231231231 |    1 | Peyton | M  | Manning |
| 1234567980605 |    2 | Tony   | W  | Stark   |
+---------------+------+--------+----+---------+

1 个答案:

答案 0 :(得分:0)

我认为您应该修改表的结构以使将来执行查询更加容易,例如,添加主键将有助于您跟踪记录

CREATE TABLE `test`.`book` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `isbn` VARCHAR(100) NOT NULL,
  `copy` INT NULL,
  `gener` VARCHAR(45) NULL,
  `title` VARCHAR(100) NULL,
  `pdate` DATETIME NULL,
  `publisher` VARCHAR(45) NULL,
  `size` INT NULL DEFAULT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE `test`.`author` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `book_id` INT NULL,
  `af` VARCHAR(45) NULL,
  `am` VARCHAR(45) NULL,
  `al` VARCHAR(45) NULL,
  PRIMARY KEY (`id`),
  INDEX `author_book_id_idx` (`book_id` ASC),
  CONSTRAINT `author_book_id`
    FOREIGN KEY (`book_id`)
    REFERENCES `test`.`book` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


INSERT INTO `book` (`id`, `isbn`, `copy`, `gener`, `title`, `pdate`, `publisher`, `size`) VALUES (NULL, 'test2', '2', 'math', 'test2', '2019-05-06 00:00:00', 'test', '2');

insert into author (`book_id`,`af`,`am`,`al`) select max(id),'test af','test am','test al' from book; 

一种更好的结构是将每本书与其作者联系起来,而不是相反,因为每位作者可能有一本以上的书

CREATE TABLE `test`.`book` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `isbn` VARCHAR(100) NOT NULL,
  `copy` INT NULL,
  `gener` VARCHAR(45) NULL,
  `title` VARCHAR(100) NULL,
  `pdate` DATETIME NULL,
  `publisher` VARCHAR(45) NULL,
  `size` INT NULL DEFAULT NULL,
`author_id` INT NULL,
  PRIMARY KEY (`id`)),
CONSTRAINT `author_book_id`
    FOREIGN KEY (`author_id`)
    REFERENCES `test`.`author` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

编辑: 如果您不想重新创建表格,则可以更改书本表格

ALTER TABLE `test`.`book` 
ADD COLUMN `created_at` DATETIME NULL AFTER `size`;

然后插入应该看起来像

insert into author (`isbn`, `copy`,`af`,`am`,`al`) select isbn, copy,'test af','test am','test al' from book where created_at=(select max(created_at) from book );