【问题标题】:Having trouble with creating tables using foreign key [duplicate]使用外键创建表时遇到问题[重复]
【发布时间】:2019-05-05 00:37:26
【问题描述】:

我正在尝试创建两个表,aluno = student 和 usuario = user。 MySQL Workbench 不断向我显示表 usario 中的 codigo 不存在。谁能帮帮我?

drop database `web2`;

CREATE DATABASE `web2` DEFAULT CHARSET latin1;

USE `web2`;

CREATE TABLE `aluno` (
   `id_aluno` bigint(20) not null auto_increment,
   `nome` varchar(100) not null,
   `cpf` varchar(20) not null,
   `rg` varchar(20) not null,
   `dataDeNascimento` date not null,
   `endereco` varchar(50),   
   `cidade` varchar(150),   
   `telefoneFixo` varchar(14),
   `telefoneCelular` varchar(14),
   `email` varchar(30) not null,   
   PRIMARY KEY (`id_aluno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
 `telefoneCelular`,`email`) VALUES ('1', 'Marcela', '255665696363', '2153263699',
 '1985-07-08', 'Rua Hum', 'Belo Horizonte', '(35)54321-9876', '(35)54321-9876','marcela@bh.mg');

INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
 `telefoneCelular`,`email`) VALUES ('2', 'Paulo', '275865696361', '2183255599','1983-02-05', 'Rua Dois', 'Bela Vista', '(11)12345-6789',
 '(11)12345-6789', 'paulo@saopaulo.sp');

 INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
 `telefoneCelular`,`email`) VALUES ('3', 'Marcos', '275812656361', '2183255599','1983-02-12', 'Rua Dois', 'Bela Vista', '(11)12345-6790',
 '(11)12345-6789', 'paulo@saopaulo.sp');

INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
 `telefoneCelular`,`email`) VALUES ('4', 'Rodolfo', '569865696361', '2183255599','1983-05-28', 'Rua Dois', 'Bela Vista', '(11)12345-6791',
 '(11)12345-6789', 'paulo@saopaulo.sp');

INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
 `telefoneCelular`,`email`) VALUES ('5', 'Larissa', '275865696361', '2183255599','1983-02-01', 'Rua Dois', 'Bela Vista', '(11)12345-6792',
 '(11)12345-6789', 'paulo@saopaulo.sp');


CREATE TABLE `usuario` (
   `id` bigint(20) not null auto_increment,   
   `login` varchar(10) not null,
   `senha` varchar(10) not null,   
   PRIMARY KEY (`id`),
   FOREIGN KEY (`codigo`) REFERENCES aluno (`id_aluno`),
   UNIQUE KEY (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('1', 'marcela', '54321', '1');

INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('2', 'paulo', '12345', '2');

INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('3', 'gustavo', '52321', '3');

INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('4', 'leandro', '19315', '4');

INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('5', 'bruna', '14045', '5');

【问题讨论】:

  • 您尚未将该列添加到您的usario 表中。您只创建了 idloginsenha 列。创建codigo 列与这三个相同,以便FOREIGN KEY 定义可以看到它。

标签: mysql sql foreign-keys


【解决方案1】:

您是否缺少“CREATE TABLE usuario”中的“codigo”列名

...
`codigo`    bigint(20)
...

【讨论】:

    【解决方案2】:

    你需要在create table语句中定义codigo

     CREATE TABLE `usuario` (
       `id` bigint(20) not null auto_increment,   
       `login` varchar(10) not null,
       `senha` varchar(10) not null,
       `codigo` bigint(20)
       PRIMARY KEY (`id`),
       FOREIGN KEY (`codigo`) REFERENCES aluno (`id_aluno`),
       UNIQUE KEY (`login`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
    

    原因是外键是应用列的约束,所以表首先需要应用外键约束的列。

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 2019-04-26
      相关资源
      最近更新 更多