无法在mysql中分配On Delete Set null和更新后的set Null

时间:2017-03-28 10:46:46

标签: php mysql referential-integrity

bank_api_uat_user表

CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bank_name` varchar(255) DEFAULT NULL,
  `role` varchar(10) NOT NULL,
  `bank_code` char(10) NOT NULL,
  `user_name` varchar(255) NOT NULL,
  `password` varchar(20) NOT NULL,
  `api_key` varchar(255) NOT NULL,
  `Client_Secret` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_name` (`user_name`),
  KEY `user_name_2` (`user_name`),
  KEY `id` (`id`,`user_name`),
  KEY `role` (`role`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;

create_role表

CREATE TABLE IF NOT EXISTS `create_role` (
  `date` datetime NOT NULL,
  `Role_name` varchar(50) NOT NULL,
  `Role_code` varchar(5) NOT NULL,
  PRIMARY KEY (`Role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我想在每次添加约束时将引用完整性分配给bank_api_uat_user表,它给出了以下错误 MySQL说:文档

1215 - 无法添加外键约束

以下用于创建外键的查询。

ALTER TABLE `bank_api_uat_user` ADD  CONSTRAINT `const_file_role` FOREIGN KEY (`role`) REFERENCES `test`.`create_role`(`Role_code`) ON DELETE SET NULL ON UPDATE SET NULL;

1 个答案:

答案 0 :(得分:0)

使子列Null设置空引用选项非常重要。

如果将role声明为NULL

,则上述查询有效
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bank_name` varchar(255) DEFAULT NULL,
  `role` varchar(10) NULL,
  `bank_code` char(10) NOT NULL,
  `user_name` varchar(255) NOT NULL,
  `password` varchar(20) NOT NULL,
  `api_key` varchar(255) NOT NULL,
  `Client_Secret` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_name` (`user_name`),
  KEY `user_name_2` (`user_name`),
  KEY `id` (`id`,`user_name`),
  KEY `role` (`role`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;

可在此处找到完整说明:https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html