What is wrong with this create table statement?

﹥>﹥吖頭↗ 提交于 2021-02-11 05:17:23

问题


Im currently writing a database for a school project. Im using MySQL on xampp and trying to add this table to my database. Im still not 100% on my SQL syntax and theres an error here I cannot seem ti figure out:

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

    );

Im having the same issue with all of the tables Im trying to create.

Heres the error message:

#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

Thanks in advance


回答1:


The previous answers are true. You've got other problems, too (e.g. you can't have a DOUBLE that big; max = 255).

You've got issues, man.

Here's a simple example that perhaps you can extend. It has two tables with a many-to-many relationship between them. The join table has two foreign keys. It works in MySQL - I just created a database and added these tables.

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';



回答2:


You need a comma at the end of this line

EditVersion INT UNSIGNED,




回答3:


You need a coma at the end of keyword unsigned.




回答4:


 FOREIGN KEY(  C_id ) REFERENCES table_comments( C_id ),
 FOREIGN KEY( U_id ) REFERENCES userDB (U_id ),

Try rewriting your foreign keys like this



来源:https://stackoverflow.com/questions/9186948/what-is-wrong-with-this-create-table-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!