简体   繁体   中英

Can't create new table in Mysql used foreign key

This is what i have used to create new table
CREATE TABLE history(
search_id int auto_increment=1000 primary key,
f_name varchar(100) not null,
f_lo varchar(300) not null,
u_id varchar(50) not null unique,
foreign key(u_id) references userinfo(user_id)
);

This is the error i'm facing
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 '=1000 primary key,
f_name varchar(100) not null,
f_lo varchar(300) not null,
u_i' at line 2

Refer this image:- https://i.stack.imgur.com/uXeoO.png

CREATE TABLE history(
search_id int auto_increment primary key,
f_name varchar(100) not null,
f_lo varchar(300) not null,
u_id varchar(50) not null unique,
foreign key(u_id) references userinfo(user_id)
);

Try not to initialize auto increment

Once you create the above table you can alter it by:

ALTER TABLE history AUTO_INCREMENT = 1000;

link to refer

AUTO_INCREMENT value is table option, not column option or default value. See CREATE TABLE Statement .

So

CREATE TABLE history(
    search_id int auto_increment primary key,
    f_name varchar(100) not null,
    f_lo varchar(300) not null,
    u_id varchar(50) not null unique,
    foreign key(u_id) references userinfo(user_id)
) AUTO_INCREMENT = 1000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM