table is not created in mysql

Asked

Viewed 26 times

1

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;

''that 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

1 answer

0


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

Change the table creation code VENDA for:

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;

I appointed to FK_ID_PESSOA2 just change to the one you want.

  • Thank you very much did not know this particularity in the oracle it allows. Even valew

  • If you decided to kindly signal the answer as correct and resolved ;)

  • how does it I did not find the button.

  • @Lordsnow next to my question has a "0" and below zero has a symbol of "correct" "right" kkkk is there

Browser other questions tagged

You are not signed in. Login or sign up in order to post.