无法添加外键约束

时间:2015-11-17 02:25:32

标签: mysql foreign-keys foreign-key-relationship

首先,这是我的主要参考表mysql dump

-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 17, 2015 at 03:15 AM
-- Server version: 5.6.17
-- PHP Version: 5.5.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */;

--
-- Database: `j_inventory`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (   `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `username` varchar(200) COLLATE utf8_unicode_ci NOT NULL,   `password` varchar(500) COLLATE utf8_unicode_ci NOT NULL,  `real_password` varchar(250) COLLATE utf8_unicode_ci NOT NULL,   `role` varchar(50) COLLATE utf8_unicode_ci NOT NULL,   `full_name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,   `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,   `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',   `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',   `status` varchar(100) COLLATE utf8_unicode_ci NOT NULL,   PRIMARY KEY (`user_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=44 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

和希望外键所在的第二个表

-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 17, 2015 at 03:14 AM
-- Server version: 5.6.17
-- PHP Version: 5.5.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */;

--
-- Database: `j_inventory`
--

-- --------------------------------------------------------

--
-- Table structure for table `user_details`
--

CREATE TABLE IF NOT EXISTS `user_details` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `phone` varchar(200) NOT NULL,   `age` int(100) NOT NULL,   `gender` varchar(50) NOT NULL,   `address` varchar(250) NOT NULL,   `course` varchar(250) NOT NULL,   `college` varchar(200) NOT NULL,   `year` int(11) NOT NULL,   `user_id` int(100) NOT NULL,   `updated_at` timestamp NOT NULL,   `created_at` timestamp NOT NULL,   PRIMARY KEY (`id`),   KEY `user_id` (`user_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

外键(由于mysql错误未设置"无法添加外键约束")在users_details中是' user_id'参考表是'用户'表中外键的引用键是' user_id'在用户表上我尝试了这个

  

ALTER TABLE user_details ADD FOREIGN KEY fk1(user_id)REFERENCES   users(user_id)ON DELETE CASCADE ON UPDATE NO ACTION;

但遗憾的是,不幸的是它给我一个错误

  

错误SQL查询:

     

ALTER TABLE user_details ADD CONSTRAINT fk1 FOREIGN KEY   (user_id)参考j_inventoryusersuser_id)ON DELETE   CASCADE ON UPDATE NO ACTION; MySQL说:文档

     

1215 - 无法添加外键约束文档

请提供任何帮助,线索,想法,建议和建议吗?

1 个答案:

答案 0 :(得分:3)

您的用户ID在users表上未签名。从users table create语句中删除此字段中的“unsigned”,看看它是否有效。我认为您所看到的是由用户和user_details表上的user_id字段类型不匹配导致的错误