【问题标题】:How to add or insert foreign key value in child table from parent table using insert statement如何使用插入语句在父表的子表中添加或插入外键值
【发布时间】:2022-01-17 20:24:33
【问题描述】:

我想在我的子表中使用父表的主键。我创建了一个外键来连接 QUESTION 和 ANSWER 表。 以下是我的数据库代码:

用户表

CREATE TABLE user (
  user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, -- unique index
  username VARCHAR(255) NOT NULL, 
  password VARBINARY(255) NOT NULL,                -- password hash, binary
  firstname VARCHAR(255) NOT NULL,
  lastname VARCHAR(255) DEFAULT NULL,
  email VARCHAR(255) DEFAULT NULL,
  address VARCHAR(255) DEFAULT NULL,
  phone BIGINT DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE question (
   question_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,  -- unique index
   questions TEXT NOT NULL,    
   question_text TEXT NOT NULL,        
   user_id INT NOT NULL,                                 -- id of the user who have asked
   CONSTRAINT user_to_question FOREIGN KEY (user_id) REFERENCES user (user_id)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

 CREATE TABLE answer (
  answer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,  -- unique index
  answer_text TEXT NOT NULL,
  question_id INT NOT NULL,                           -- what question it answers on
  user_id INT NOT NULL,                               -- id of the user who have answered
  CONSTRAINT user_to_answer FOREIGN KEY (user_id) REFERENCES user (user_id),
  CONSTRAINT question_to_answer FOREIGN KEY (question_id) REFERENCES question (question_id)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

所以,在 USER 表中我添加了一个新值:

  insert into user values(1,"krish","123456789","Krish","D","krish@gmail.com","Mumbai MH","98776");

现在在 QUESTION 表中,我想添加一个新值,并且我想使用 USER 表中的 user_id 值。由于 user_id 是 QUESTION 表中的外键。所以,到目前为止,我已经尝试过这个查询,但它不起作用。

 insert into question (question_id,questions,question_text)
 SELECT user_id
 FROM user
 where user_id = 1;
 values (1,"What is java","Please Explain in details");

我已经提到了这个问题和解决方案,但它不起作用。 inserting data from parent table to child table in postgres

【问题讨论】:

  • 如果你知道user_id = 1,你可以把1放在VALUES列表中,你不需要SELECT
  • 嘿@Barmar 在我的例子中 user_id 是自动增量主键。所以假设在 USER 表中有一个 user_id = 1 的用户提出了一个会生成 question_id 的问题,所以我应该能够从 USER 表中检索该 user_id 并插入到 QUESTION 表中。 user_id 和 question_id 不必相同。 user_id 为 2 的用户可以提出问题,该问题可能有也可能没有 question_id = 2。
  • 如果您通过用户名查找用户 ID 会更有意义。

标签: mysql sql


【解决方案1】:

您正在尝试的查询类似于:

insert into question ( question_id,
                       questions,
                       question_text
                     )
SELECT user_id as question_id,
       "What is java" as questions,
       "Please Explain in details" as question_text
FROM user
where user_id = 1 ;

但据我所知,你不能只插入这个值,因为 on

CREATE TABLE answer (
  answer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
  answer_text TEXT NOT NULL,
  question_id INT NOT NULL,                           
  user_id INT NOT NULL,  

你有 user_id INT NOT NULL 所以它需要一个值。

类似:

insert into question ( question_id,
                       questions,
                       question_text,
                       user_id
                      )
SELECT user_id as question_id,
       "What is java" as questions,
       "Please Explain in details" as question_text,
       user_id
FROM user
where user_id = 1 ;

【讨论】:

  • 嗨@ergest-basha 在我的例子中,user_id 是自动递增主键。所以假设在 USER 表中有一个 user_id = 1 的用户提出了一个会生成 question_id 的问题,所以我应该能够从 USER 表中检索该 user_id 并插入到 QUESTION 表中。 user_id 和 question_id 不必相同。 user_id 为 2 的用户可以提出问题,该问题可能有也可能没有 question_id = 2。
  • 我希望这能让我的问题更加清晰。
【解决方案2】:

如果您知道从user 表中选择的user_id,则根本不需要SELECT,只需将其放入VALUES 列表中即可。

INSERT INTO question (question_id,questions,question_text, user_id)
VALUES (1,"What is java","Please Explain in details", 1);

如果您需要使用其他列查找user_id,您可以使用SELECT。在这种情况下,您可以将固定值添加到 SELECT 列表中。

INSERT INTO question (question_id,questions,question_text, user_id)
SELECT 1,"What is java","Please Explain in details", user_id
FROM users
WHERE username = 'krish';

如果您在创建用户后立即执行此操作,您还可以使用LAST_INSERT_ID() 函数来获取使用自动增量分配的user_id

INSERT INTO question (question_id,questions,question_text, user_id)
VALUES (1,"What is java","Please Explain in details", LAST_INSERT_ID());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多