作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在为学校项目编写数据库。我在 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/
我是一名优秀的程序员,十分优秀!