简体   繁体   English

无法在 Mysql 中使用外键创建新表

[英]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, search_id int auto_increment=1000 主键,
f_name varchar(100) not null, f_name varchar(100) 不是 null,
f_lo varchar(300) not null, f_lo varchar(300) 不是 null,
u_id varchar(50) not null unique, u_id varchar(50) 不是 null 唯一的,
foreign key(u_id) references userinfo(user_id)外键(u_id)引用用户信息(user_id)
); );

This is the error i'm facing这是我面临的错误
ERROR 1064 (42000): You have an error in your SQL syntax;错误 1064 (42000):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '=1000 primary key,查看与您的 MySQL 服务器版本相对应的手册,了解在 '=1000 主键附近使用的正确语法,
f_name varchar(100) not null, f_name varchar(100) 不是 null,
f_lo varchar(300) not null, f_lo varchar(300) 不是 null,
u_i' at line 2 u_i' 在第 2 行

Refer this image:- https://i.stack.imgur.com/uXeoO.png参考这张图片:- 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. AUTO_INCREMENT 值是表选项,而不是列选项或默认值。 See CREATE TABLE Statement .请参阅CREATE TABLE 语句

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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