mysql is not using the mentioned foreign key constraint name
I faced a strange situation, so I thought to share it with you, in case someone else finds himself in similar situation.
I added a column to an existing table in MySQL. I also added a foreign key constraint on that column. So what is strange about that. Let me show you.
First of all let me display the original structure of the table.
Now the SQL statement that I used to alter the structure of the above table.
No error and the new column and foreign key added successfully. But what is strange here is that I provided fk_t_dresultantstate_t_dlocking as foreign key contraint name but MySQL is using its auto generated value t_device_resultantstate_ibfk_1 as constraint name, although the associated key name is as defined in the SQL statement. See the table structure below.
I found out that the correct way to specify foreign key constraint in MySQL table is as follows:
You can check it by issuing SHOW CREATE TABLE T_DEVICE_RESULTANTSTATE\G on mysql prompt.
I added a column to an existing table in MySQL. I also added a foreign key constraint on that column. So what is strange about that. Let me show you.
First of all let me display the original structure of the table.
mysql> SHOW CREATE TABLE T_DEVICE_RESULTANTSTATE\G
*************************** 1. row ***************************
Table: T_DEVICE_RESULTANTSTATE
Create Table: CREATE TABLE `t_device_resultantstate` (
`RESULTANTSTATEID` int(11) NOT NULL AUTO_INCREMENT,
`RESULTANTSTATE` blob,
`DEVICEID` int(11) NOT NULL,
`CREATEDBY` varchar(50) NOT NULL DEFAULT 'ADMIN',
`CREATEDATE` datetime DEFAULT NULL,
`MODIFIEDBY` varchar(50) NOT NULL DEFAULT 'ADMIN',
`MODIFIEDDATE` datetime DEFAULT NULL,
PRIMARY KEY (`RESULTANTSTATEID`),
KEY `FK_T_DEVICE_RESULTANT_STATE_T_DEVICE` (`DEVICEID`),
CONSTRAINT `FK_T_DEVICE_RESULTANT_STATE_T_DEVICE` FOREIGN KEY (`DEVICEID`) REFERENCES `t_device` (`DeviceID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
Now the SQL statement that I used to alter the structure of the above table.
mysql> alter table t_device_resultantstate add devicelockingid int;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table t_device_resultantstate add foreign key fk_t_dresultantstate_t_dlocking(devicelockingid) references t_device_locking (devicelockingid) on delete no action on upda
te cascade;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
No error and the new column and foreign key added successfully. But what is strange here is that I provided fk_t_dresultantstate_t_dlocking as foreign key contraint name but MySQL is using its auto generated value t_device_resultantstate_ibfk_1 as constraint name, although the associated key name is as defined in the SQL statement. See the table structure below.
mysql> SHOW CREATE TABLE T_DEVICE_RESULTANTSTATE\G
*************************** 1. row ***************************
Table: T_DEVICE_RESULTANTSTATE
Create Table: CREATE TABLE `t_device_resultantstate` (
`RESULTANTSTATEID` int(11) NOT NULL AUTO_INCREMENT,
`RESULTANTSTATE` blob,
`DEVICEID` int(11) NOT NULL,
`CREATEDBY` varchar(50) NOT NULL DEFAULT 'ADMIN',
`CREATEDATE` datetime DEFAULT NULL,
`MODIFIEDBY` varchar(50) NOT NULL DEFAULT 'ADMIN',
`MODIFIEDDATE` datetime DEFAULT NULL,
`devicelockingid` int(11) DEFAULT NULL,
PRIMARY KEY (`RESULTANTSTATEID`),
KEY `FK_T_DEVICE_RESULTANT_STATE_T_DEVICE` (`DEVICEID`),
KEY `fk_t_dresultantstate_t_dlocking` (`devicelockingid`),
CONSTRAINT `t_device_resultantstate_ibfk_1` FOREIGN KEY (`devicelockingid`) REFERENCES `t_device_locking` (`devicelockingid`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_T_DEVICE_RESULTANT_STATE_T_DEVICE` FOREIGN KEY (`DEVICEID`) REFERENCES `t_device` (`DeviceID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
I found out that the correct way to specify foreign key constraint in MySQL table is as follows:
alter table t_device_resultantstate add devicelockingid int;
alter table t_device_resultantstate add CONSTRAINT fk_t_dresultantstate_t_dlocking foreign key (devicelockingid) references t_device_locking (devicelockingid) on delete no action on update cascade;
You can check it by issuing SHOW CREATE TABLE T_DEVICE_RESULTANTSTATE\G on mysql prompt.
Comments
Post a Comment