简体   繁体   中英

Get information to create ALTER TABLE ADD FOREIGN KEY

How to get the information to update an already existing foreign key..

When updating a foreign key referemce in phpmyadmin this query is sent to the server

ALTER TABLE `dimension` ADD FOREIGN KEY (`test_id`) REFERENCES `db`.`test`
(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

But how do I get the status ON DELETE and ON UPDATE to sending and updating the alter table query?

You can't. You have to drop the existing foreign key constraint and add a new one.

However, you can fetch the existing reference_option from the INFORMATION_SCHEMA :

SELECT CONSTRAINT_NAME, UNIQUE_CONSTRAINT_NAME, UPDATE_RULE, DELETE_RULE
FROM   INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE  CONSTRAINT_SCHEMA = DATABASE()
   AND UNIQUE_CONSTRAINT_SCHEMA = DATABASE()
   AND TABLE_NAME = 'dimension'
   AND REFERENCED_TABLE_NAME = 'test'

Or else from SHOW CREATE TABLE :

SHOW CREATE TABLE dimension

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM