开发者

MYSQL FOREIGN KEY ISSUES

开发者 https://www.devze.com 2022-12-07 21:29 出处:网络
I am trying to create a table which references two other tables that I have planned to make, but have not made yet. I am wondering if that is the issue here or if there is a syntax error that I am mis

I am trying to create a table which references two other tables that I have planned to make, but have not made yet. I am wondering if that is the issue here or if there is a syntax error that I am missing. If anyone can help me out it would be greatly appreciated

mysql> CREATE TABLE items ( items$id INT NOT NULL AUTO_INCREMENT, sales$id INT NOT NULL AUTO_INCREMENT, img$id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, desc VARCHAR(255) NOT NULL, PRIMARY KEY(items$id),

FOREIGN KEY(sales$id) REFERENCES sales(sal开发者_JAVA百科es$id),

FOREIGN KEY(img$id) REFERENCES image(img$id)

ERROR 1064 (42000): 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 'desc VARCHAR(255) NOT NULL, PRIMARY KEY(items$id), FOREIGN KEY(sales$id) REFEREN' at line 6

I tried to remove the references, as in just do 'FOREIGN KEY(sales$id)' and 'FOREIGN KEY(img$id)' to see if that would work (I am new to mysql), but that also did not work. Any help is appreciated.


The problem is your field name: 'desc'

'desc' is a keyword in mysql see here

Your sql would be better like this:

CREATE TABLE items ( items$id INT NOT NULL AUTO_INCREMENT, sales$id INT NOT NULL AUTO_INCREMENT, img$id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, descr VARCHAR(255) NOT NULL, PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFERENCES sales(sales$id),
FOREIGN KEY(img$id) REFERENCES image(img$id))

Ps: you missed closing bracket ')' at the end of your sql query.

0

精彩评论

暂无评论...
验证码 换一张
取 消