Foreign key does not work! MYSQL

Asked

Viewed 193 times

2

Good afternoon guys, could someone help me? My Foreign key is not working in a database. I had already made a Foreign key in another database the same way, but now it is not working without apparent reason. I already changed the engine to INNODB, I tried an alter table, but nothing...

Follows the tables:

**Tabela cargos:**

create table cargos

(

cod_cargo int auto_increment primary key,

nome_cargo varchar(45)

)

**Tabela funcionarios:**

create table Funcionarios 

(

cracha int AUTO_INCREMENT PRIMARY KEY, 

nome varchar(50), 

sobrenome varchar(50), 

email varchar(50), 

senha varchar(20), 

cpf varchar(14), 

data_de_nascimento varchar(10),

sexo char(1),

cidade varchar(35),

tel_fixo char(13),

tel_movel char(14),

foto varchar(50),

cargo_funcionario int,

salario decimal (8,2),

observacoes text,

foreign key (cargo_funcionario) references cargos(cod_cargo)

)
  • Your table engine is Myisam or Innodb? Foreign Key does not respect referential integrity

  • The engine is Innodb

  • When you say it doesn’t work what exactly happens, is it an error? have the message? if you couldn’t describe it in more detail.

  • 1

    For "it’s not working " we must understand ...

  • 1

    I tested it here and it was quiet.

  • I have 20 lines inserted in the table positions, however, when I try to insert a value greater than 20 in the field cargo_funcionaio of the table employees it does not return error and insert normally, that is, when I insert a new employee with a higher post id than allowed he performs quietly, which was not supposed to happen.

  • Makes a show create table Funcionarios see if the engine is really innodb, just to confirm.

  • 1

    Yes, I just executed the command and in the end appeared "ENGINE=Innodb"

Show 3 more comments
No answers

Browser other questions tagged

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