Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

r.vinicius

Como resolver erro 1215: Cannot add foreign key constraint

Recommended Posts

CREATE TABLE IF NOT EXISTS `acervo` (
`id` int(11) NOT NULL,
  `isbn` varchar(15) DEFAULT NULL,
  `cod_barra` int(15) DEFAULT NULL,
  `retrito` tinyint(1) DEFAULT NULL,
  `edicao` tinyint(3) DEFAULT NULL,
  `categoria_id` int(11) DEFAULT NULL,
  `genero_id` int(11) DEFAULT NULL,
  `editora_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `pessoa` (
`id` int(11) NOT NULL,
  `nome` varchar(45) DEFAULT NULL,
  `endereco` varchar(45) DEFAULT NULL,
  `num.` varchar(5) DEFAULT NULL,
  `bairro` varchar(20) DEFAULT NULL,
  `cidade` varchar(20) DEFAULT NULL,
  `estado` varchar(15) DEFAULT NULL,
  `cpf` varchar(14) DEFAULT NULL,
  `aluno` tinyint(1) DEFAULT NULL,
  `funcionario` tinyint(1) DEFAULT NULL,
  `professor` tinyint(1) DEFAULT NULL,
  `data_nac` date DEFAULT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

Agora quando crio essa tabela

create table emprestimo(
id int auto_increment primary key,
data date,
hora time,
devolvido tinyint(1),
acervo_id int,
pessoa_id int,
foreign key(acervo_id) references acervo(id),
foreign key(pessoa_id) references pessoa(id)
)

Fica dando esse erro 1215! Já tentei de tudo e não consigo resolver esse problema

Alguem sabe como resolver isso?

 

Qq ajuda e bem vinda! Boa noite!

Compartilhar este post


Link para o post
Compartilhar em outros sites

tente o seguinte:

1. defina as chaves primárias das tabelas "acervo" e "pessoa";

2. defina as colunas da 3ª tabela que serão chaves estrangeiras com o mesmo nº de posições (11) e como "unsigned"; ex:

acervo_id int(11) unsigned,
pessoa_id int(11) unsigned,

Compartilhar este post


Link para o post
Compartilhar em outros sites

O erro ainda persiste!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Continua do mesmo jeito! Não consigo resolver isso de forma alguma!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Em meu server funcionou. Verifique algum erro. Alterei para

CREATE TABLE IF NOT EXISTS `acervo` (

`id` int(11) NOT NULL PRIMARY KEY,
`isbn` varchar(15) DEFAULT NULL,
`cod_barra` int(15) DEFAULT NULL,
`retrito` tinyint(1) DEFAULT NULL,
`edicao` tinyint(3) DEFAULT NULL,
`categoria_id` int(11) DEFAULT NULL,
`genero_id` int(11) DEFAULT NULL,
`editora_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 CREATE TABLE IF NOT EXISTS `pessoa` (
`id` int(11) NOT NULL PRIMARY KEY,
`nome` varchar(45) DEFAULT NULL,
`endereco` varchar(45) DEFAULT NULL,
`num.` varchar(5) DEFAULT NULL,
`bairro` varchar(20) DEFAULT NULL,
`cidade` varchar(20) DEFAULT NULL,
`estado` varchar(15) DEFAULT NULL,
`cpf` varchar(14) DEFAULT NULL,
`aluno` tinyint(1) DEFAULT NULL,
`funcionario` tinyint(1) DEFAULT NULL,
`professor` tinyint(1) DEFAULT NULL,
`data_nac` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;


create table emprestimo(
id int auto_increment primary key,
data date,
hora time,
devolvido tinyint(1),
acervo_id int,
pessoa_id int,
foreign key(acervo_id) references acervo(id),
foreign key(pessoa_id) references pessoa(id)
)

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.