如何在MySQL中构建外键

发布于 2025-02-13 15:04:51 字数 1379 浏览 0 评论 0原文

我有2个表 - 第一表:

CREATE TABLE COFFEE_SHOP 
(
    shop_id INT,
    shop_name VARCHAR(50),
    city VARCHAR(500),
    state CHAR(2),
    PRIMARY KEY (shop_id)
);

INSERT INTO COFFEE_SHOP (shop_id, shop_name, city, state)
VALUES (1, "store g", "melbourne", "CA"),
       (6, "store x", "palms", "FL"),
       (4, "store b", "grandi", "TX"),
       (2, "store a", "flonce", "GA");

第二个表:

CREATE TABLE EMPLOYEE 
(
    employee_id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(30),
    last_name VARCHAR(30),
    hire_date DATE,
    job_title VARCHAR(50),
    shop_id INT references COFFEE_SHOP(shop_id)
);

INSERT INTO EMPLOYEE (first_name, last_name, hire_date, job_title)
VALUES("jim", "jordan", "2002-03-05", "barista"),
      ("george", "washington", "2015-03-02", "manager"),
      ("abraham", "lincon", "2020-04-07", "grinder"),
      ("barack", "obama", "1999-05-09", "front office"),
      ("george", "bush", "2150-03-10", "tender");

问题:为什么以下查询返回null以及如何修复它,以使第二个表实际上引用了第一个并将shop_id用作第二个表中的外键。

查询:

SELECT * 
FROM EMPLOYEE

此查询将返回所有shop_id值的null。如何连接两个表,以使每个员工都有一个引用的shop_id来自coffee_shop

另外,我尝试使用以下代码在雇员中制作外键,并且它也无法正常工作。

FOREIGN KEY(shop_id) REFERENCES COFFEE_SHOP(shop_id)

I have 2 tables - 1st table:

CREATE TABLE COFFEE_SHOP 
(
    shop_id INT,
    shop_name VARCHAR(50),
    city VARCHAR(500),
    state CHAR(2),
    PRIMARY KEY (shop_id)
);

INSERT INTO COFFEE_SHOP (shop_id, shop_name, city, state)
VALUES (1, "store g", "melbourne", "CA"),
       (6, "store x", "palms", "FL"),
       (4, "store b", "grandi", "TX"),
       (2, "store a", "flonce", "GA");

2nd table:

CREATE TABLE EMPLOYEE 
(
    employee_id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(30),
    last_name VARCHAR(30),
    hire_date DATE,
    job_title VARCHAR(50),
    shop_id INT references COFFEE_SHOP(shop_id)
);

INSERT INTO EMPLOYEE (first_name, last_name, hire_date, job_title)
VALUES("jim", "jordan", "2002-03-05", "barista"),
      ("george", "washington", "2015-03-02", "manager"),
      ("abraham", "lincon", "2020-04-07", "grinder"),
      ("barack", "obama", "1999-05-09", "front office"),
      ("george", "bush", "2150-03-10", "tender");

Question: why does the following query return null and how do I fix it such that the 2nd table actually references the first and uses the shop_id as a foreign key in the second table.

Query:

SELECT * 
FROM EMPLOYEE

This query returns null for all shop_id values. How do I connect the two tables so that each employee has a referenced shop_id from coffee_shop?

Also I tried making the foreign key in EMPLOYEE with the following code and it did not work that way either.

FOREIGN KEY(shop_id) REFERENCES COFFEE_SHOP(shop_id)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2025-02-20 15:04:51

例如,如果您想告诉MySQL Jim Jordan在墨尔本的商店商店G中工作,则需要以这种方式进行插入:

INSERT INTO EMPLOYEE (first_name, last_name, hire_date, job_title, shop_id) VALUES("jim", "jordan", "2002-03-05", "barista", 1)

外键需要知道哪个主要密钥适合您在另一个表中的需求。

由于外键不能为null,因此您必须提供相关的 shop_id

For example, if you wish to tell MySQL that Jim Jordan is working in the Melbourne's shop Store G, you need to do the insertion this way :

INSERT INTO EMPLOYEE (first_name, last_name, hire_date, job_title, shop_id) VALUES("jim", "jordan", "2002-03-05", "barista", 1)

The foreign key needs to know which primary key fits your needs in the other table.

As a foreign key can't be NULL, you must provide the related shop_id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文