简体   繁体   English

无法添加外键约束

[英]Cannot add foreign key constraint

First, here's my main reference table mysql dump 首先,这是我的主要参考表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 */;

and the second table where the hopefully the foreign key resides 和希望外键所在的第二个表

-- 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 */;

the foreign key (not set up due to mysql error "Cannot add foreign key constraint ") in the users_details is the 'user_id' and the reference table is the 'users' table where the reference key for the foreign key is the 'user_id' on the users table and I tried this users_details中的外键(由于mysql错误“无法添加外键约束”而设置)是'user_id',而引用表是'users'表,其中外键的引用键是'user_id'在用户表上我尝试了这个

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

but sadly and unfortunately it throws me an error 但遗憾的是,它让我错了

Error SQL query: 错误SQL查询:

ALTER TABLE user_details ADD CONSTRAINT fk1 FOREIGN KEY ( user_id ) REFERENCES j_inventory . ALTER TABLE user_details ADD CONSTRAINT fk1 FOREIGN KEY( user_id )REFERENCES j_inventory users ( user_id ) ON DELETE CASCADE ON UPDATE NO ACTION; usersuser_id )ON DELETE CASCADE ON UPDATE NO ACTION; MySQL said: Documentation MySQL说:文档

1215 - Cannot add foreign key constraint Documentation 1215 - 无法添加外键约束文档

any help, clues, ideas, suggestions, recommendations please? 请给我任何帮助,线索,想法,建议和建议?

Your user id is unsigned on the users table. 您的用户ID在users表上未签名。 Remove 'unsigned' from this field in your users table create statement and see if it works. 从users table create语句中删除此字段中的“unsigned”,看看它是否有效。 I think what you're seeing is an error caused by a mismatch between the user_id field types on your users and user_details tables 我认为您所看到的是由用户和user_details表上的user_id字段类型不匹配导致的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM