简体   繁体   English

MYSQL添加约束错误

[英]MYSQL ADD CONSTRAINT ERROR

I am getting errors for my add constraints. 我的添加约束出现错误。

I am using MYSQL workbench to create the tables 我正在使用MYSQL工作台创建表

Message Error from MYSQL workbench: Error Code: 1064 You have an error in your SQL syntax; 来自MYSQL工作台的消息错误:错误代码:1064您的SQL语法中有错误;请参见表11。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD CONSTRAINT user_contact_ibfk1 FOREIGN KEY( user_id ) REFERENCES `user_ac' at line 1 请检查与您的MySQL服务器版本相对应的手册,以获取在'ADD CONSTRAINT user_contact_ibfk1 FOREIGN KEY( user_id )参考'第1行中使用的正确语法的正确语法。

Could this just be a version problem I'm having. 这可能只是我遇到的版本问题。 I'm using 5.2.4.7 我正在使用5.2.4.7

Error: 错误:

22:47:35 ADD CONSTRAINT user_contact_ibfk_1 FOREIGN KEY ( user_id ) REFERENCES user_account ( user_id ) ON DELETE CASCADE ON UPDATE CASCADE Error Code: 1064. You have an error in your SQL syntax; 22:47:35添加约束user_contact_ibfk_1 FOREIGN KEY( user_id )参考user_accountuser_id )ON在更新CASCADE上删除CASCADE错误代码:1064。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD CONSTRAINT user_contact_ibfk_1 FOREIGN KEY ( user_id ) REFERENCES `user_ac' at line 1 0.000 sec 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在“ ADD CONSTRAINT user_contact_ibfk_1 ”附近使用FOREIGN KEY( user_id )参考`user_ac'在第1行0.000秒

--
-- Table structure for table `user_contact`
--
CREATE TABLE IF NOT EXISTS `user_contact` (
  `contact_id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(11) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `email` varchar(50) NOT NULL,
  `email_code` varchar(32) NOT NULL,
  `cellnumber` decimal(32,0) NOT NULL,
  `city` varchar(25) NOT NULL,
  `state` varchar(25) NOT NULL,
  `country` varchar(25) NOT NULL,
  `username` varchar(50) NOT NULL,
  PRIMARY KEY (`contact_id`),
  UNIQUE KEY `user_id` (`user_id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

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

--
-- Table structure for table `user_profile`
--

CREATE TABLE IF NOT EXISTS `user_profile` (
  `profile_id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  `about_me` text NOT NULL,
  `work_info` varchar(255) NOT NULL,
  `education_info` varchar(255) NOT NULL,
  `rating` float NOT NULL,
  `user_image` varchar(1024) NOT NULL,
  `friend_array` text NOT NULL,
  `online_status` tinyint(1) NOT NULL,
  PRIMARY KEY (`profile_id`),
  UNIQUE KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

--
-- Table structure for table `user_account`
--

CREATE TABLE IF NOT EXISTS `user_account` (
  `user_id` bigint(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  `type` int(11) NOT NULL,
  `online_status` tinyint(1) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

--
-- Constraints for table `user_contact`
--
ALTER TABLE `user_contact`
  ADD CONSTRAINT `user_contact_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES    `user_account` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `user_profile`
--

ALTER TABLE `user_profile`
 ADD CONSTRAINT `user_profile_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_account`     (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;

The only thing I can think of is that FOREIGN KEY name might be taken,try changing it. 我唯一想到的是可能会使用FOREIGN KEY名称,请尝试更改它。

ALTER TABLE `user_contact`
  ADD CONSTRAINT `user_contact_ibfk_stuff` FOREIGN KEY (`user_id`) REFERENCES    `user_account` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;

Run this post the result 运行此结果

SELECT @@SESSION.sql_mode;

Or directly run this: 或直接运行:

SET SESSION SQL_MODE='NO_ENGINE_SUBSTITUTION';

Turns out to be some internal bug with MYSQL workbench. 原来是MYSQL工作台的一些内部错误。 I used phpmyadmin and it worked fine. 我使用phpmyadmin,它工作正常。

暂无
暂无

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

相关问题 MySQL错误:创建表时出错:无法添加外键约束 - MySQL Error: Error creating table: Cannot add foreign key constraint PHP MySQL错误创建表:无法添加外键约束 - PHP MySQL Error creating table: Cannot add foreign key constraint 一般错误:1215 无法添加外键约束,Laravel 5 & MySQL - General error: 1215 Cannot add foreign key constraint, Laravel 5 & MySQL MySQL 错误:无法添加外键约束(PrestaShop 模块) - MySQL error: Cannot add foreign key constraint (PrestaShop module) mysql错误错误SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败? - mysql error error SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails? MySQL错误代码1215:无法在ALTER TABLE上添加外键约束:Mysql - MySQL Error Code 1215: Cannot add foreign key Constraint on ALTER TABLE : Mysql Mysql错误添加外键时无法添加外键约束 - Mysql error Cannot add foreign key constraint when i add foreign key 无法添加或更新子行,外键约束失败,mysql错误号:1452 - cannot add or update a child row a foreign key constraint fails mysql Error Number: 1452 无法添加或更新子行:外键约束失败PHP或MySQL错误? - Cannot add or update a child row: a foreign key constraint fails Error in PHP or MySQL? MySQL:插入和更新时出错:无法添加或更新子行:外键约束失败 - MySQL: Error when insert and update: Cannot add or update a child row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM