mysql - 创建表时,右括号在此位置无效输入

标签 mysql sql mysql-workbench

我尝试在 MySQL Workbench 上创建一个表,但它显示错误:

"()" (closing parenthesis) is not valid input at this position when creating table

最后一个右括号:

create table generics (
    idGen int unsigned not null auto_increment,
    nameGen nvarchar (255) not null,
    type_id int not null,
    id2 int not null,
    pk int not null,
    active bit not null,
    created_at datetime null,
    updated_at datetime null,
    created_by nvarchar(255) null,
    updated_by nvarchar(255) null,

    constraint primary key(idGen),
    constraint foreign key(type_id)
)

我读过,有时在使用 BOM 保存为 UTF8 时遇到问题时会发生这种情况,但此查询尚未保存,而且我以前从未遇到过此问题,另外这是我第一次使用 nvarchar,所以我不知道它是否与此有关。

最佳答案

对于外键约束,您缺少 REFERENCES 部分。 该键应该指向另一个表中的主键。

A FOREIGN KEY in MySQL creates a link between two tables by one specific column of both tables. The specified column in one table must be a PRIMARY KEY and referred by the column of another table known as FOREIGN KEY.

请检查以下内容以供引用

create table generics1 (
    type_id int not null,
    constraint primary key(type_id)
    );

create table generics (
    idGen int unsigned not null auto_increment,
    nameGen nvarchar (255) not null,
    type_id int not null,
    id2 int not null,
    pk int not null,
    active bit not null,
    created_at datetime null,
    updated_at datetime null,
    created_by nvarchar(255) null,
    updated_by nvarchar(255) null,

    constraint primary key(idGen),
    constraint foreign key(type_id) REFERENCES generics1(type_id)
    );

关于mysql - 创建表时,右括号在此位置无效输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677565/

相关文章:

mysql - 在联接的派生表查询中使用 LIMIT

java - 如何在Hibernate中模拟 "ON UPDATE NO ACTION"

SQL查询一对多关系

c# - 无法使用 Dapper.NET 将文件流插入 SQL 文件表

php - 通过表单选择选项插入 mysql 外键值

mysql - 计算每月不同的活跃用户数

mysql - 在 MySQL 中转换数据

mysql - 将 CSV 导入 MySQL 和

mysql - 将用户和特权转移到新服务器

MySql。谁能回答为什么这会产生超出预期范围的结果?