MySQL创建表错误

时间:2013-11-29 14:42:11

标签: mysql sql primary-key create-table

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

  

错误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));

3 个答案:

答案 0 :(得分:3)

您忘记了tipo列的长度

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

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

答案 1 :(得分:1)

试试这个

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

除此之外,我建议使用INT字段ID作为主键,如果项目增长,它将使事情变得更容易......

答案 2 :(得分:0)

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