如何将另一个表与另一个表连接

时间:2019-09-13 10:48:10

标签: mysql

我正在创建一个类似trello的应用程序,因为我使用MySQL作为数据库,直到现在我已经使用MySQL创建了身份验证系统并表达了node js,现在我想为卡创建表以保存数据,所以我想知道如何将用户数据链接到用户名。

我用以下字段ID(主键,auto_increment),名称,电子邮件,密码创建了一个表用户 现在我想创建一个表卡,其中将显示用户卡,所以我如何链接用户表列对应于特定 使用,在此先感谢后援新手

1 个答案:

答案 0 :(得分:2)

也许这行代码可以对您有所帮助。为了更好地掌握这个话题。

-- This statement creates the cards table you may want to use (alternatively see Barmars Comment, this maybe a more professional solution)

CREATE TABLE Cards (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
customer_id INT,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)

-- This statement then "connects" the tables. It makes the customer_id in the cards table a foreign key of the primary key of the customers table.
FOREIGN KEY (customer_id) REFERENCES customers(customer_id) 

实现此功能后,您可以在查询中使用JOIN来查询多个表。

如果您想了解更多信息,此视频可以帮助您了解以下主题: https://www.youtube.com/watch?v=USaXlErI-QE

相关问题