如何修改主键的值?急!!!
我采用的数据库是mysql的,大家先看一下内部表的结构,
CREATE TABLE BUSINESS_ENTITY
(
BUSINESS_KEY VARCHAR(41) NOT NULL,
AUTHORIZED_NAME VARCHAR(255) NOT NULL,
PUBLISHER_ID VARCHAR(20) NULL,
OPERATOR VARCHAR(255) NOT NULL,
LAST_UPDATE TIMESTAMP NOT NULL,
PRIMARY KEY (BUSINESS_KEY)
);
CREATE TABLE BUSINESS_DESCR
(
BUSINESS_KEY VARCHAR(41) NOT NULL,
BUSINESS_DESCR_ID INT NOT NULL,
LANG_CODE VARCHAR(5) NULL,
DESCR VARCHAR(255) NOT NULL,
PRIMARY KEY (BUSINESS_KEY,BUSINESS_DESCR_ID),
FOREIGN KEY (BUSINESS_KEY)
REFERENCES BUSINESS_ENTITY (BUSINESS_KEY)
);
CREATE TABLE BUSINESS_CATEGORY
(
BUSINESS_KEY VARCHAR(41) NOT NULL,
CATEGORY_ID INT NOT NULL,
TMODEL_KEY_REF VARCHAR(41) NULL,
KEY_NAME VARCHAR(255) NULL,
KEY_VALUE VARCHAR(255) NOT NULL,
PRIMARY KEY (BUSINESS_KEY,CATEGORY_ID),
FOREIGN KEY (BUSINESS_KEY)
REFERENCES BUSINESS_ENTITY (BUSINESS_KEY)
);
上面的表中都有一个business_key字段,这个字段都是一样的,我现在要对这些表中的这个字段进行修改,我听说主键修改成功了,那么其他的外键的值也会跟着更新,但是我今天在business_entity表中进行对于主键的值进行更新的时候,发生了以下的错误信息。
mysql> update business_entity set business_key= '3BC66810-B6A1-11DB-BE19-8FE9A30977AF ' where business_key= '8B29BD80-B6A1-11DB-BD80-F88467493279 ';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`juddi1/business_descr`, CONSTRAINT `business_descr_ibfk_1` FOREIGN KEY (`BUSINESS_KEY`) REFERENCES `business_entity` (`BUSINESS_KEY`))
我不知道为什么会发生修改不成功,请达人能否指点一二,我在csdn里看到有些朋友是通过先在business_entity中增加一个新的,然后再把其他应用到这个business_key主键的表中的记录进行相应的修改,然后删除business_entity中原先的那个记录,但是我这里应用到这个business_key主键的表太多了,所以觉得采用那种方法不是很切合实际,我想处理这种问题的最好的方法是什么,望达人告知,谢谢。
[解决办法]
把REFERENCES BUSINESS_ENTITY (BUSINESS_KEY)改成:
REFERENCES BUSINESS_ENTITY (BUSINESS_KEY) ON UPDATE CASCADE