MySQL表创建错误

时间:2013-11-29 16:36:12

标签: mysql sql unique primary-key create-table

我想创建一个Date表(数据用葡萄牙语),我在dia(day),mes(月)和ano(year)前面有“unique”属性,但我决定删除它们,因为它没有意义,因为表中可能有很多天“25”和很多“januarys”,但是当我删除“unique”属性时,我得到了以下错误

  

ERROR 1005(HY000):无法创建表'ist169515.Disponivel'(错误号:150)
  ERROR 1005(HY000):无法创建表'ist169515.Venda'(错误号:150)
  查询正常,0行受影响(0.07秒)
  ERROR 1005(HY000):无法创建表'ist169515.Encomenda'(错误号:150)
  ERROR 1005(HY000):无法创建表'ist169515.RegistoEnc'(错误号:150)

完整代码如下:

create table Alimento
    (nomeA varchar(80) not null unique,
    vegetariano boolean not null,
    PRIMARY KEY(nomeA));

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


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

create table Composto
    (nomeAgg varchar(80) not null unique,
    nomeS varchar (80) not null unique,
    quantidade numeric(4),
    PRIMARY KEY(nomeAgg, nomeS),
    FOREIGN KEY(nomeAgg) REFERENCES Agregado(nomeA),
    FOREIGN KEY(nomeS) REFERENCES Simples(nomeA));

create table Prato
    (nomeA varchar(80) not null,
     preco numeric(5,2) not null,
     PRIMARY KEY(nomeA),
     FOREIGN KEY(nomeA) REFERENCES Alimento(nomeA));

create table Restaurante
     (nomeR varchar(80) not null unique,
      PRIMARY KEY(nomeR));

create table Data
     (dia numeric(2) not null,
     mes varchar(45) not null,
     ano numeric(4) not null,
     PRIMARY KEY(dia, mes, ano));

create table Disponivel
     (nomeA varchar(80) not null,
     nomeR varchar(80) not null,
     dia numeric(2) not null,
     mes varchar(45) not null,
     ano numeric(4) not null,
     PRIMARY KEY(nomeA, nomeR, dia, mes, ano),
     FOREIGN KEY(nomeA) REFERENCES Prato(nomeA),
     FOREIGN KEY(nomeR) REFERENCES Restaurante(nomeR),
     FOREIGN KEY(dia) REFERENCES Data(dia),
     FOREIGN KEY(mes) REFERENCES Data(mes),
     FOREIGN KEY(ano) REFERENCES Data(ano));

create table Venda
     (nomeA varchar(80) not null,
     dia numeric(2) not null,
     mes varchar(45) not null,
     ano numeric(4) not null,
     nomeR varchar(80) not null,
     num numeric(5) not null,
     PRIMARY KEY(nomeA, dia, mes, ano, nomeR),
     FOREIGN KEY(nomeA) REFERENCES Prato(nomeA),
     FOREIGN KEY(dia) REFERENCES Data(dia),
     FOREIGN KEY(mes) REFERENCES Data(mes),
     FOREIGN KEY(ano) REFERENCES Data(ano),
     FOREIGN KEY(nomeR) REFERENCES Restaurante(nomeR));

 create table Cliente
     (email varchar(255) not null unique,
     PRIMARY KEY Cliente(email));

 create table Encomenda
     (email varchar(255) not null,
     nEnc numeric(5) not null unique,
     precoTotal numeric(5,2) not null,
     desconto double(20,2) not null,
     nomeR varchar(80) not null,
     dia numeric(2) not null,
     mes varchar(45) not null,
     ano numeric(4) not null,
     PRIMARY KEY(email, nEnc),
     FOREIGN KEY(email) REFERENCES Cliente(email),
     FOREIGN KEY(nomeR) REFERENCES Restaurante(nomeR),
     FOREIGN KEY(dia) REFERENCES Data(dia),
     FOREIGN KEY(mes) REFERENCES Data(mes),
     FOREIGN KEY(ano) REFERENCES Data(ano));

  create table RegistoEnc
     (email varchar(255) not null,
     nEnc numeric(5) not null,
     nomeA varchar(80) not null,
     PRIMARY KEY(email, nEnc, nomeA),
     FOREIGN KEY(email) REFERENCES Encomenda(email),
     FOREIGN KEY(nEnc) REFERENCES Encomenda(nEnc),
     FOREIGN KEY(nomeA) REFERENCES Prato(nomeA));

1 个答案:

答案 0 :(得分:0)

编辑:

 FOREIGN KEY(dia, mes, ano) REFERENCES Data(dia, mes, ano)

而不是

 FOREIGN KEY(dia) REFERENCES Data(dia),
 FOREIGN KEY(mes) REFERENCES Data(mes),
 FOREIGN KEY(ano) REFERENCES Data(ano),