【问题标题】:How to insert new records to mysql with no key name assigned and with auto increasing primary key?如何在未分配键名且自动增加主键的情况下向 mysql 插入新记录?
【发布时间】:2012-12-14 19:02:34
【问题描述】:

如何在没有指定键名且主键自动增加的情况下向 mysql 插入新记录?

参考PHP mySQL - Insert new record into table with auto-increment on primary key
将第一个值更改为 DEFAULT ,但仍然出现相同的错误

表结构:

DROP TABLE IF EXISTS `configuration_copy`;
CREATE TABLE `configuration_copy` (
  `configuration_id` int(11) NOT NULL auto_increment,
  `configuration_title` text NOT NULL,
  `configuration_key` varchar(255) NOT NULL default '',
  `configuration_value` text NOT NULL,
  `configuration_description` text NOT NULL,
  `configuration_group_id` int(11) NOT NULL default '0',
  `sort_order` int(5) default NULL,
  `last_modified` datetime default NULL,
  `date_added` datetime NOT NULL default '0001-01-01 00:00:00',
  `use_function` text,
  `set_function` text,
  PRIMARY KEY  (`configuration_id`),
  UNIQUE KEY `unq_config_key_zen` (`configuration_key`),
  KEY `idx_key_value_zen` (`configuration_key`,`configuration_value`(10)),
  KEY `idx_cfg_grp_id_zen` (`configuration_group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of configuration_copy
-- ----------------------------
INSERT INTO `configuration_copy` VALUES ('1', 'World Points', 'WORD_POINTS', '0', 'Points required upgrading to world grade', '1', '1', '0000-00-00 00:00:00', '2012-12-30 09:38:23', null, null);
INSERT INTO `configuration_copy` VALUES ('2', 'Iron Points', 'IRON_POINTS', '1000', 'Points required upgrading to iron grade', '1', '2', null, '0001-01-01 00:00:00', null, null);
INSERT INTO `configuration_copy` VALUES ('3', 'Bronze Points', 'BRONZE_POINTS', '2000', 'Points required upgrading to bronze grade', '1', '3', null, '0001-01-01 00:00:00', null, null);
INSERT INTO `configuration_copy` VALUES ('4', 'Silver Points', 'SILVER_POINTS', '3000', 'Points required upgrading to silver grade', '1', '4', null, '0001-01-01 00:00:00', null, null);
INSERT INTO `configuration_copy` VALUES ('5', 'Gold Points', 'GOLD_POINTS', '4000', 'Points required upgrading to gold grade', '1', '5', null, '0001-01-01 00:00:00', null, null);

【问题讨论】:

  • 错误信息中已经很清楚了。 configuration_key 列存在唯一约束,您违反了它。
  • configuration_key 不是主键,可以是同一个值...
  • 它不能:在你的表结构中查看UNIQUE KEY unq_config_key_zen (configuration_key)
  • 谢谢,我现在清楚了

标签: mysql


【解决方案1】:

configuration_key 列上有一个唯一键 unq_config_key_zen,因此您不能插入具有相同 configuration_key 值的行。

 UNIQUE KEY `unq_config_key_zen` (`configuration_key`),

【讨论】:

  • 不,不是主键,主键只是configurations_id
猜你喜欢
  • 1970-01-01
  • 2011-11-21
  • 2013-05-11
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 2022-01-02
  • 2016-12-12
  • 2010-12-16
相关资源
最近更新 更多