I am using MYSQL and I have a table of devices. Each device has, among the others, an ID, called mac_address (this is not the pk), and a column named deleted_date.
If a device is not active, its deleted_date equals to a timestamp, and then it should be ok to add a new device with the same mac_address.
If it's active, its deleted_date equals to NULL and then it should not be ok to add a new device with the same mac_address.
I found out that it was possible to have two devices that are still active with the same mac_address, which is wrong. After some research, I figured out that I can't compare NULL to NULL since it does not count as the same value.
I would appreciate an idea for how to get around this.
- Two devices with the same
mac_address, anddeleted_dateis NULL: WRONG - Two devices with the same
mac_address, onedeleted_dateis NULL and the other has a value: OK - Two devices with the same
mac_address, anddeleted_datehas a value for both of them: OK
Right now this is the relevant part of the table creation and constraints:
'CREATE TABLE `iot_devices` (
`pk` int(11) NOT NULL AUTO_INCREMENT,
`device_id` int(11) DEFAULT NULL,
`device_name` varchar(100) NOT NULL,
`delete_date` timestamp NULL DEFAULT NULL,
`mac_address` varchar(12) DEFAULT NULL,
PRIMARY KEY (`pk`),
UNIQUE KEY `idx_device_id` (`device_id`),
UNIQUE KEY `idx_mac_address_delete_date` (`mac_address`,`delete_date`),
)