table is not created in mySQL

0
create database mbsys
default character set utf8
default collate utf8_german2_ci;

'' this generates normal ''

create table PESSOA (
ID_PESSOA int (10) not null,
NOME varchar (40) not null,
CPF int (11) not null,
NASCIMENTO date not null,
SEXO enum ('M','F') not null,
constraint PK_ID_PESSOA PRIMARY KEY (ID_PESSOA),
constraint UK_CPF unique (CPF)
)default charset = utf8;

'' this generates normal ''

create table ENDERECO (
ID_PESSOA int (10) not null,
RUA varchar (40) not null,
NUMERO varchar (10) not null,
CEP int (11) not null,
BAIRRO varchar (10) not null,
CIDADE varchar (10) not null,
constraint FK_ID_PESSOA foreign key (ID_PESSOA)
references PESSOA (ID_PESSOA)
)default charset = utf8;

'' this gives the problem ''

create table VENDA (
ID_PESSOA int (10) not null,
ID_VENDA int (10) not null,
VALOR_TOTAL decimal (8,2),
constraint PK_ID_VENDA PRIMARY KEY (ID_VENDA),
constraint FK_ID_PESSOA foreign key (ID_PESSOA)
references PESSOA (ID_PESSOA)
)default charset = utf8;

Error LOG:

> 12:05:44    create table VENDA ( ID_PESSOA int (10) not null, ID_VENDA
> int (10) not null,  VALOR_TOTAL decimal (8,2), constraint PK_ID_VENDA
> PRIMARY KEY (ID_VENDA), constraint FK_ID_PESSOA foreign key
> (ID_PESSOA) references PESSOA (ID_PESSOA) )default charset =
> utf8    Error Code: 1022. Can't write; duplicate key in table
> 'venda' 0.375 sec
    
asked by anonymous 09.11.2017 / 15:11

1 answer

0

You have already created a constraint named FK_ID_PESSOA in the ENDERECO table, in the VENDA table you tried to create another constraint with the same name.

Change the creation code of the VENDA table to:

create table VENDA (
    ID_PESSOA int (10) not null,
    ID_VENDA int (10) not null,
    VALOR_TOTAL decimal (8,2),
    constraint PK_ID_VENDA PRIMARY KEY (ID_VENDA),
    constraint FK_ID_PESSOA2 foreign key (ID_PESSOA)
    references PESSOA (ID_PESSOA)
)default charset = utf8;

Named to FK_ID_PESSOA2 just change to what you want.

    
09.11.2017 / 15:24