gpt4 book ai didi

mysql - 这个建表语句有什么问题?

转载 作者:行者123 更新时间:2023-11-29 04:53:22 25 4
gpt4 key购买 nike

我目前正在为学校项目编写数据库。我在 xampp 上使用 MySQL 并尝试将此表添加到我的数据库中。我仍然没有 100% 掌握我的 SQL 语法,这里有一个错误我似乎无法弄清楚:

CREATE TABLE photoDB(
U_id INT UNSIGNED NOT NULL FOREIGN KEY REFERENCES userDB(U_id),
P_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
C_id INT UNSIGNED NOT NULL FOREIGN KEY REFERENCES table_comments(C_id),

PhotoName VARCHAR(50),
Description TEXT NOT NULL,
File VARCHAR,
Views BIGINT UNSIGNED,
Rep DOUBLE (100000, 2),
UploadDate DATETIME,
EditDate DATETIME,
EditVersion INT UNSIGNED,
LatestEditVerion INT UNSIGNED

);

我尝试创建的所有表都遇到了同样的问题。

错误信息如下:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY REFERENCES userDB(U_id), P_id INT UNSIGNED NOT NULL AUTO_INCREMENT ' at line 2

提前致谢

最佳答案

前面的回答都是对的。您还有其他问题(例如,您不能有那么大的两倍;最大值 = 255)。

你有问题,伙计。

这是一个您或许可以扩展的简单示例。它有两个表,它们之间是多对多关系。连接表有两个外键。它适用于 MySQL - 我刚刚创建了一个数据库并添加了这些表。

use stackoverflow;

create table if not exists stackoverflow.product
(
product_id int not null auto_increment,
name varchar(80) not null,
primary key(product_id)
);

create table if not exists stackoverflow.category
(
category_id int not null auto_increment,
name varchar(80) not null,
primary key(category_id)
);

create table if not exists stackoverflow.product_category
(
product_id int,
category_id int,
primary key(product_id, category_id),
constraint product_id_fkey
foreign key(product_id) references product(product_id)
on delete cascade
on update no action,
constraint category_id_fkey
foreign key(category_id) references category(category_id)
on delete cascade
on update no action
);

insert into stackoverflow.product(name) values('teddy bear');
insert into stackoverflow.category(name) values('toy');
insert into stackoverflow.product_category
select p.product_id, c.category_id from product as p, category as c
where p.name = 'teddy bear' and c.name = 'toy';

关于mysql - 这个建表语句有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9186948/

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