Duplicate key in mysql

1

I'm in the third bimester of the database and we started using the insert in our database but I came across the error

  

"Error Code: 1022. Can not write; duplicate key in table '# sql-aa4_a".

Is there a function that can solve this problem or is my code incorrect? follow it down.

-- drop database 'chapelao'; -- caso eu não use o alter table  devo dropar a tabela
CREATE DATABASE IF NOT EXISTS 'chapelao';

use 'chapelao';

create table if not exists empresa(
    idempresa int auto_increment not null,
    cnpj varchar (45) not null,
    primary key (idempresa)
)Engine = InnoDB;


create table if not exists clientes(
    idclientes int auto_increment not null,
    rg varchar (45) not null,
    cpf varchar (45) not null,
    datadenascimento date not null,
    empresaid int not null,
    primary key (idclientes)
)Engine = InnoDB;

create table if not exists colaboradoras (
    idcolaboradora int auto_increment not null,
    nome varchar (45) not null,
    idade int not null,
    balao bool not null,
    rg varchar (45) not null,
    cpf varchar (45) not null,
    empresaid int not null,
    primary key (idcolaboradora)
)Engine = InnoDB;

alter table colaboradoras 
    add constraint fk_empresa_colaboradoras
        foreign key (empresaid)
        references empresa (idempresa)
        on delete no action
        on update no action;

alter table clientes 
    add constraint fk_empresa_clientes
        foreign key (empresaid)
        references empresa (idempresa)
        on delete no action
        on update no action;

insert into empresa (idempresa ,cnpj) values (null ,"08812216904");
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Taiana","19",1,"15011","15022",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Debora","25",0,"15012","15023",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Veronica","30",0,"15013","15024",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Aline","22",0,"15014","150245",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Luciana","23",1,"15015","15026",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Carol","24",0,"15016","15027",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Thalya","18",1,"15017","15028",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Julinha","20",0,"15018","15029",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Maria","25",1,"15019","150210",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Polyanna","35",1,"150110","150212",1);
insert into colaboradoras (idcolaboradora, nome, idade, balao, rg, cpf, empresaid) values (null,"Giovana","20",0,"150111","150213",1);
    
asked by anonymous 09.08.2016 / 03:44

1 answer

1

In a brief survey, I found that MySQL's INSERT fault is similar to PostgreSQL: when there is a specified ID , the database does not update the inserted record number, by inserting records reporting the PK you should also enter the ID that is being entered. If you do not want to worry about ID remove it from INSERT .

It would look like this:

insert into colaboradoras (nome, idade, balao, rg, cpf, empresaid) values ("Taiana","19",1,"15011","15022",1);

Source: link

    
22.01.2018 / 20:16