【问题标题】:MySQL creating table errorMySQL建表错误
【发布时间】:2013-11-29 14:42:11
【问题描述】:

我在尝试创建表时遇到此错误

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 'not null, PRIMARY KEY(nomeA), FOREIGN KEY(nomeA) 附近使用的语法 参考 Alimento(nomeA))' 在第 4 行

这是代码

create table Alimento
    (nomeA varchar(255) not null unique,
    vegetariano tinyint(1) not null,
    PRIMARY KEY(nomeA));

create table Simples
    (nomeA varchar(255) not null,
    calgramas numeric(5,2) not null,
    tipo varchar not null,
    PRIMARY KEY(nomeA),
    FOREIGN KEY(nomeA) REFERENCES Alimento(nomeA));


create table Agregado
    (nomeA varchar(255) not null,
    calorias numeric(5,2) not null,
    PRIMARY KEY(nomeA),
    FOREIGN KEY(nomeA) REFERENCES Alimento(nomeA));

【问题讨论】:

    标签: mysql sql primary-key create-table


    【解决方案1】:

    您忘记了tipo 列的长度

    tipo varchar(100) not null
                ^^^^^---------------add something like this
    

    如果您使用像 Mysql Workbench 这样的 SQL 工具,那么此类错误将被突出显示并且很容易找到。

    【讨论】:

    • 哎呀,我感觉好笨,我猜是因为我最近工作这么晚,非常感谢!
    【解决方案2】:

    试试这个

    create table Alimento
        (nomeA varchar(255) not null unique primary key,
        vegetariano tinyint(1) not null);
    

    除此之外,我会推荐一个 INT 字段 ID 作为主键,如果项目增长,它会让事情变得更容易......

    【讨论】:

    • 在类型之后立即指定主键或最后指定主键的主要区别是什么?
    【解决方案3】:
    tipo varchar(20) not null
    
    and would like to suggest that for table Alimento you have already used primary key for nomeA then no need to define it as not null unique
    

    【讨论】:

    • 我认为没有必要但又不确定,所以当您将某物定义为主要时,没有必要具有“唯一性”?
    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2014-04-09
    • 2010-11-03
    • 2016-02-07
    相关资源
    最近更新 更多