php - MYSQL 添加约束错误

标签 php mysql sql database

我在添加约束时遇到错误。

我正在使用 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) REFERENCES user_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 CONSTRAINT user_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/

相关文章:

sql - 索引 View 的两列上的唯一聚集索引

asp.net - 将 varchar 转换为 int SQl server 2005

php - 使用 OOP PHP 的 mysql.php 中出现 fatal error : Cannot access empty property,

php - 通过 PHP 创建 mysql 触发器?

在 SELECT 中使用时 CASE 'NOT NULL' 的 mysql 语法

mysql - 检测锁定表(由 LOCK TABLE 锁定)

PHP 或 MySQL 正在从 tags 表中获取所有行,而不仅仅是中间 term_tags 表所说的

php - 解析错误: "syntax error, unexpected ' ;', expecting T_FUNCTION"

php - RGB 到最接近的预定义颜色

php - 获取没有任何符号的日期