如果符合某些条件,在表格(如下表所示)获得新插入时,我要自动删除行。
何时:
- There are rows referring to the same field with the same user_id
- Their field , display and search columns are the same
简而言之,当行将成为重复的行时(组代列除外),非无号组代号应予删除,否则应更新或插入一行。
是否有办法在 Mysql (以“ ON DUPLIATE < em> do stuff em> stuff < / em> 和独有的密钥等 ” 的精神) 中设置此功能, 或者我是否必须在php( 包含多个查询) 中明确检查?
Additional info: There should always be a row with NULL group_id for every possible field (there s a limited set, defined elsewhere). On the other hand there might not be one with a non null group_id .
CREATE TABLE `Views` (
`user_id` SMALLINT(5) UNSIGNED NOT NULL,
`db` ENUM( db_a , db_b ) NOT NULL COLLATE utf8_swedish_ci ,
`field` VARCHAR(40) NOT NULL COLLATE utf8_swedish_ci ,
`display` TINYINT(1) UNSIGNED NOT NULL,
`search` TINYINT(1) UNSIGNED NOT NULL,
`group_id` SMALLINT(6) UNSIGNED NULL DEFAULT NULL,
UNIQUE INDEX `user_id` (`field`, `db`, `user_id`),
INDEX `Views_ibfk_1` (`user_id`),
INDEX `group_id` (`group_id`),
CONSTRAINT `Views_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `User` (`id`) ON
UPDATE CASCADE ON DELETE CASCADE
)
COLLATE= utf8_swedish_ci
ENGINE=InnoDB;