I can not put foreign key in PHPMYADMIN

Asked

Viewed 1,607 times

1

I have my bank already created in phpmyadmin, are two tables, payments and students. I have seen several tutorials and read various ways of how to put a foreign key (fk_alunos) id_students in my payments table , nothing works.

Table pagamentos

CREATE TABLE IF NOT EXISTS `horus`.`pagamentos` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `situacao_aluno` VARCHAR(100) NOT NULL,
  `validade_plano` VARCHAR(15) NOT NULL,
  `planos` VARCHAR(100) NOT NULL,
  `vencimento` VARCHAR(50) NOT NULL,
  `cpf_amigo` VARCHAR(50) NOT NULL,
  `forma_pagamento` VARCHAR(100) NOT NULL,
  `data_matricula` VARCHAR(20) NOT NULL,
  `numero_documento` VARCHAR(50) NOT NULL,
  `data_documento` VARCHAR(15) NOT NULL,
  `valor` VARCHAR(6) NOT NULL,
  `status` VARCHAR(100) NOT NULL,
  `status_mensalidade` VARCHAR(100) NOT NULL,
  `alunos_id` INT(11) NOT NULL )

Table alunos

CREATE TABLE IF NOT EXISTS `horus`.`alunos` (
 `id` INT(11) NOT NULL AUTO_INCREMENT,
 `nome` VARCHAR(100) NOT NULL,
 `cpf` VARCHAR(15) NOT NULL,
 `rg` VARCHAR(15) NOT NULL,
 `nascimento` VARCHAR(50) NOT NULL,
 `sexo` VARCHAR(100) NOT NULL,
 `fone` VARCHAR(15) NOT NULL,
 `email` VARCHAR(100) NOT NULL,
 `endereco` VARCHAR(100) NOT NULL,
 `bairro` VARCHAR(100) NOT NULL,
 `cep` VARCHAR(10) NOT NULL,
 `estado` VARCHAR(50) NOT NULL,
 `cidade` VARCHAR(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `cpf` (`cpf` ASC),
  UNIQUE INDEX `rg` (`rg` ASC))
  ENGINE = InnoDB
 AUTO_INCREMENT = 2
 DEFAULT CHARACTER SET = latin1;

In the payments table I put another field being the id_alunos In the indexes of the payments table I added the name of the key fk_alunos, spine id_pagamentos.

After that, when I try to execute the code "ALTER TABLE pagamentos ADD CONSTRAINT fk_alunos FOREIGN KEY(id_alunos) REFERENCES alunos (id_alunos)", only brings me mistakes saying :

" #1452 - Cannot add or update a Child Row: a Foreign key Constraint fails (horus.#sql-32d0_181, CONSTRAINT id_alunos FOREIGN KEY (id_alunos) REFERENCES alunos (id_alunos)) "

I tried to make relationship model by Workbench to then import into phpmyadmin, but not even so the foreign key appears.

  • And are there already records in the tables? Can the foreign key be null? Ask the question the definition of both tables, please.

  • already exist yes records...

2 answers

2

Simple: REFERENCES alunos (id_alunos) is incorrect because the table alunos does not have a column called id_alunos. I believe the right thing would be REFERENCES alunos (id).

Another detail is that you own alunos_id on your table payments while you reference the column id_alunos, that doesn’t exist at your FK.

  • ah sorry my error, actually there is yes.... i who did not change ... CREATE TABLE IF NOT EXISTS horus.alunos ( id_alunos INT(11) NOT NULL AUTO_INCREMENT, nome VARCHAR(100) NOT NULL, cpf VARCHAR(15) NOT NULL, rg VARCHAR(15) NOT NULL, .... etc

  • 1

    yet the error remains..

  • You use id_alunos and alunos_id, this may be causing the mistake.

0

Guys I managed to find the solution! Thank you all...

The mistake was because my phpmyadmin was not updated, not even the drawing of the foreign key appeared.... I ended up reassembling the payments table again by writing in SQL and mentioning the Foreign key right there!

Thank you all.

Browser other questions tagged

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