How to update primary key?

佐手、 提交于 2019-12-25 01:22:01

问题


I am writing a script that has to update some rows without changing the contents of another script that creates a few tables. Another condition is that you cannot alter or drop constraints.

Contents of Create table script:

CREATE TABLE TRUCK(
REGNUM VARCHAR(10) NOT NULL,
CAPACITY DECIMAL(7) NOT NULL,
WEIGHT  DECIMAL(5) NOT NULL,
STATUS  VARCHAR(10) NOT NULL,
CONSTRAINT TRUCK_PKEY PRIMARY KEY(REGNUM),
CONSTRAINT TRUCK_STATUS CHECK (STATUS IN ('AVAILABLE', 'MAINTAINED', 'USED'));

and there are some row insertion statements.

CREATE TABLE TRIP(
TNUM DECIMAL(10)  NOT NULL,
LNUM DECIMAL(8)   NOT NULL,
REGNUM VARCHAR(10) NOT NULL,
TRIP_DATE DATE NOT NULL,
CONSTRAINT TRIP_PKEY PRIMARY KEY(TNUM),
CONSTRAINT TRIP_FKEY1 FOREIGN KEY(LNUM) REFERENCES DRIVER(LNUM)
CONSTRAINT TRIP_FKEY2 FOREGIN KEY(REGNUM) REFERENCES TRUCK(REGNUM) );

and there are some row insertion statements too.

This script is given by the lecturer and has no error.

Now, I tried:

UPDATE TRIP
SET REGNUN = 'PKR856'
WHERE REGNUM = 'SST005';

UPDATE TRUCK
SET REGNUN = 'PKR856'
WHERE REGNUM = 'SST005';

and this will give me an error "cannot delete/ update parent row. foregin key constriant.". All the row insertion statements in the given script have full information and there are row with regnum = sst005. I tried to update truck first and it won't work either. HELP PLEASE!


回答1:


CONSTRAINT TRIP_FKEY1 FOREIGN KEY(LNUM) REFERENCES DRIVER(LNUM)

Check manual about MySQL foreign key:

RESTRICT: Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION) is the same as omitting the ON DELETE or ON UPDATE clause.

You don't set ON DELETE and ON UPDATE options, so they will be RESTRICT by default. And you cannot update parent table primary key while row in child table exists.

You can change your CREATE TABLE like this:

CREATE TABLE TRIP(
  TNUM DECIMAL(10)  NOT NULL,
  LNUM DECIMAL(8)   NOT NULL,
  REGNUM VARCHAR(10) NOT NULL,
  TRIP_DATE DATE NOT NULL,
  CONSTRAINT TRIP_PKEY PRIMARY KEY(TNUM),
  CONSTRAINT TRIP_FKEY1 FOREIGN KEY(LNUM) REFERENCES DRIVER(LNUM) ON UPDATE CASCADE
  CONSTRAINT TRIP_FKEY2 FOREGIN KEY(REGNUM) REFERENCES TRUCK(REGNUM) ON UPDATE CASCADE);

and query

UPDATE TRUCK SET REGNUN = 'PKR856' WHERE REGNUM = 'SST005';

will change keys in both tables, primary key in truck and foreign key in trip.




回答2:


The quick fix is to disable foreign key checks for the session, and then re-enable them. With the foreign key checks disabled, MySQL will allow DML changes (INSERT/UPDATE/DELETE) that would otherwise violated the foreign key constraints.

Make sure the changes you make put the database in a consistent state, where there are no rows that violate constraints.

SET FOREIGN_KEY_CHECKS = 0;

UPDATE TRIP
SET REGNUN = 'PKR856'
WHERE REGNUM = 'SST005';

UPDATE TRUCK
SET REGNUN = 'PKR856'
WHERE REGNUM = 'SST005';

SET FOREIGN_KEY_CHECKS = 1;

As another alternative, if the scripts don't specify ENGINE= for the tables, you could temporarily change the default storage engine for the session to MyISAM, before executing the script to create the tables.

SELECT @@session.default_storage_engine INTO @prev_default_storage_engine ;
SET default_storage_engine = MYIASM ;

-- execute table creation script 

SET default_storage_engine = @prev_default_storage_engine ;

(The "trick" here is that MyISAM storage engine doesn't enforce foreign key constraints.)




回答3:


Instead of updating the primary keys in the parent table, add new rows that copy all the data except the primary key. Then you can update the foreign key in the child table, and then delete the original row in the parent table.

INSERT INTO TRUCK (regnum, capacity, weight, status)
SELECT 'PKR856', capacity, weight, status
FROM TRUCK
WHERE regnum = 'SST005';

UPDATE TRIP
SET REGNUM = 'PKR856'
WHERE REGNUM = 'SST005';

DELETE FROM TRUCK WHERE regnum = 'SST005';


来源:https://stackoverflow.com/questions/36807004/how-to-update-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!