mariadb在更新时丢失主键

时间:2016-02-12 12:16:31

标签: mysql zend-framework mariadb

每次在特定表上进行更新时,我的一个表都会丢失主键。

Describe zizi_card_household

在更新后给我这个结果。

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   |     | NULL    | auto_increment |
| householdnumber   | varchar(45)  | NO   |     |         |                |
| cardnumber        | varchar(45)  | YES  | MUL | NULL    |                |
| startdate         | datetime     | YES  |     | NULL    |                |
| enddate           | datetime     | YES  |     | NULL    |                |
| assignedby        | int(11)      | YES  |     | NULL    |                |
| assigneddate      | datetime     | YES  |     | NULL    |                |

通常应该

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| householdnumber   | varchar(45)  | NO   | MUL |         |                |
| cardnumber        | varchar(45)  | YES  | MUL | NULL    |                |
| startdate         | datetime     | YES  |     | NULL    |                |
| enddate           | datetime     | YES  |     | NULL    |                |
| assignedby        | int(11)      | YES  |     | NULL    |                |


SHOW CREATE TABLE

将给出此语句,该语句显示已定义主键。每当表被更改时,条目都可能会更新,因此它会显示上次更改或创建的表。

CREATE TABLE `zizi_card_household` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `householdnumber` varchar(45) NOT NULL DEFAULT '',
  `cardnumber` varchar(45) DEFAULT NULL,
  `startdate` datetime DEFAULT NULL,
  `enddate` datetime DEFAULT NULL,
  `assignedby` int(11) DEFAULT NULL,
  `assigneddate` datetime DEFAULT NULL,
  `deassignedby` int(11) DEFAULT NULL,
.
.
.
.
 `modifiedby` int(11) DEFAULT NULL,
  `reprintstatus` tinyint(4) DEFAULT NULL,
  `printeddate` datetime DEFAULT NULL,
  `printedby` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_householdnumber` (`householdnumber`),
  KEY `idx_cardnumber` (`cardnumber`),
  KEY `idx_deassignedby` (`deassignedby`),
.
.
.
.
 KEY `idx_reprintstatus` (`reprintstatus`)
) ENGINE=InnoDB AUTO_INCREMENT=860137 DEFAULT CHARSET=latin1 

尝试添加主键

ALTER TABLE `labour`.`zizi_card_household` 
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY

此时给了我

ERROR 1068 (42000): Multiple primary key defined

修复表

repair table zizi_card_household;

给我这个,但确实解决了这个问题。主键返回但在下次更新表时会被破坏。

+----------------------------+--------+----------+---------------------------------------------------------+
| Table                      | Op     | Msg_type | Msg_text                                                |
+----------------------------+--------+----------+---------------------------------------------------------+
| labour.zizi_card_household | repair | note     | The storage engine for the table doesn't support repair |
+----------------------------+--------+----------+---------------------------------------------------------+

表和其他表具有更新摘要表的触发器。它删除相关的行并重建它们。它确实像

 FOR EACH ROW

begin
delete from zizi_card_summary where householdnumber=new.householdnumber;
insert into zizi_card_summary(
    cardnumber,cardhouseholdid,householdnumber,startdate,enddate,assignedby
.
.
.
)
select ch.cardnumber,ch.id,b.householdnumber,
    ch.startdate,ch.enddate,ch.assignedby,
.
.
.
.
where b.householdnumber=new.householdnumber and b.beneficiary_type=0;
end

所有其他表格都运行正常。谢谢。一直在玩这个一周。该应用程序是Zend框架1.12。

db版本是10.1.11-MariaDB-log。操作系统是Linux 2.6.32-573.8.1.el6.x86_64 x86_64。

1 个答案:

答案 0 :(得分:0)

我可以确认Timothy所描述的错误和解决方法:删除触发器会在更新记录后保留主键。 错误解决进度位于https://jira.mariadb.org/browse/MDEV-9558