我正在为学校项目编写数据库。我在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
提前致谢
答案 0 :(得分:4)
以前的答案都是正确的。你也有其他问题(例如你不能有那么大的双倍;最大= 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';
答案 1 :(得分:0)
这一行末尾需要一个逗号
EditVersion INT UNSIGNED,
答案 2 :(得分:0)
您需要在关键字未签名的末尾显示昏迷。
答案 3 :(得分:0)
FOREIGN KEY( C_id ) REFERENCES table_comments( C_id ),
FOREIGN KEY( U_id ) REFERENCES userDB (U_id ),
尝试重写这样的外键