gpt4 book ai didi

php - 外键没有自动递增

转载 作者:行者123 更新时间:2023-11-29 21:31:30 26 4
gpt4 key购买 nike

我的代码:

enter image description here

    create table Products
(
ProductID int not null auto_increment primary key,
ProductName varchar(20),
Recommendedprice decimal(10,2),
Category varchar(10)

)



create table customers
(
CustomerID int not null auto_increment primary key,
FirstName varchar(50),
LastName varchar(50),
city varchar(50),
State char(2),
zip varchar(10)

)


create table sales
(
SalesID int not null auto_increment primary key,
ProductID int,
CustomerID int,
CONSTRAINT fk_PerProducts FOREIGN KEY (ProductId)
REFERENCES Products(ProductId),
CONSTRAINT fk_PerCustomers FOREIGN KEY (CustomerId)
REFERENCES customers(customerId),
SalesPrice decimal(10,2),
SalesDate date
)

我在表列表中得到空值。

最佳答案

当您进行插入时,您需要发送外键的 id。

像这样(paper_Author像您的sales表一样工作):

CREATE TABLE Paper (`paperId` int, `title` varchar(7));
INSERT INTO Paper (`paperId`, `title`)
VALUES (1, 'hello'),(2, 'hola'),(3, 'bonjour');

CREATE TABLE Author (`authorId` int, `authorName` varchar(3));
INSERT INTO Author (`authorId`, `authorName`)
VALUES (1, 'me'),(2, 'moi');

CREATE TABLE Paper_Author (`paperId` int, `authorId` int);
INSERT INTO Paper_Author (`paperId`, `authorId`)
VALUES (1, 1),(1, 2),(2, 2);

你可以像这样使用它: http://sqlfiddle.com/#!2/eb61f/2

<小时/>

其他认为你应该阅读:

Basics of Foreign Keys in MySQL?

Why use Foreign Key constraints in MySQL?

关于php - 外键没有自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com