我在添加约束时遇到错误。
我正在使用 MYSQL 工作台创建表
来自 MYSQL 工作台的消息错误:
错误代码:1064 您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在“ADD CONSTRAINT user_contact_ibfk1
FOREIGN KEY(user_id
) REFERENCES `user_ac' at line 1
这可能只是我遇到的版本问题。我正在使用 5.2.4.7
错误:
22:47:35 ADD CONSTRAINT
user_contact_ibfk_1
FOREIGN KEY (user_id
) REFERENCESuser_account
(user_id
) ON DELETE CASCADE ON UPDATE CASCADE Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD CONSTRAINTuser_contact_ibfk_1
FOREIGN KEY (user_id
) REFERENCES `user_ac' at line 1 0.000 sec
--
-- 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;
最佳答案
我唯一能想到的是 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;
运行本帖结果
SELECT @@SESSION.sql_mode;
或者直接运行:
SET SESSION SQL_MODE='NO_ENGINE_SUBSTITUTION';
关于php - MYSQL 添加约束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19482822/