Home > Blockchain >  Can't create new table in Mysql used foreign key
Can't create new table in Mysql used foreign key

Time:02-01

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

CodePudding user response:

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

CodePudding user response:

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;
  •  Tags:  
  • Related