使用set null进行Mysql级联删除不起作用

时间:2017-12-28 08:19:08

标签: mysql cascading-deletes

我有表评论,其他表的外键具有一些关系动作(对于user_id的删除级联,对于mark_id和comment_id的delete set null)。 删除注释或标记时,将相应的fk in reviews table设置为null,但是当删除该行的用户时,只将mark_id和comment_id设置为null,但仍然存在查看行。看起来删除操作被中断了。在查询期间,没有显示错误。

评论表

create table reviews
(
    id int unsigned auto_increment
        primary key,
    user_id int unsigned not null,
    movie_id int unsigned not null,
    mark_id int unsigned null,
    comment_id int unsigned null,
    created_at timestamp null,
    updated_at timestamp null,
    comment_points tinyint(1) not null,
    mark_points tinyint(1) not null,
    constraint recalls_user_id_movie_id_unique
        unique (user_id, movie_id),
    constraint reviews_user_id_foreign
        foreign key (user_id) references users (id)
            on delete cascade,
    constraint reviews_mark_id_foreign
        foreign key (mark_id) references marks (id)
            on delete set null,
    constraint reviews_comment_id_foreign
        foreign key (comment_id) references comments (id)
            on delete set null
)
engine=InnoDB collate=utf8_unicode_ci
;

create index reviews_mark_id_foreign
    on reviews (mark_id)
;

create index reviews_comment_id_foreign
    on reviews (comment_id)
;

删除用户的SQL

-- for example
    DELETE FROM users WHERE id = 5

用户表

create table users
(
    id int unsigned auto_increment
        primary key,
    email varchar(128) null,
    phone varchar(60) null,
    password varchar(60) null,
    fb_id varchar(60) null,
    vk_id varchar(60) null,
    first_name varchar(60) null collate utf8mb4_unicode_ci,
    last_name varchar(60) null collate utf8mb4_unicode_ci,
    birthday date null,
    gender enum('male', 'female') null,
    city_id int null,
    status enum('wanttocinema', 'youpay', 'ipay') default 'wanttocinema' not null,
    latitude double(12,9) null,
    longitude double(12,9) null,
    is_online int unsigned null,
    is_vip int unsigned null,
    points int default '0' not null,
    blocked tinyint default '0' not null,
    unblock_date date null,
    vk_sharing int unsigned null,
    fb_sharing int unsigned null,
    filter_male tinyint(1) default '1' not null,
    filter_female tinyint(1) default '1' not null,
    secure_location tinyint(1) default '1' not null,
    secure_status tinyint(1) default '1' not null,
    secure_feed tinyint(1) default '0' not null,
    walk_message_sound tinyint(1) default '1' not null,
    walk_message_push tinyint(1) default '1' not null,
    walk_message_banner tinyint(1) default '0' not null,
    new_message_sound tinyint(1) default '1' not null,
    new_message_push tinyint(1) default '1' not null,
    new_message_banner tinyint(1) default '0' not null,
    walk_changes tinyint(1) default '1' not null,
    is_top tinyint(1) default '0' not null,
    public_at int unsigned null,
    deleted_by enum('user', 'admin') null,
    created_at timestamp null,
    updated_at timestamp null,
    deleted_at timestamp null,
    filter_age_min int default '16' not null,
    filter_age_max int default '100' not null,
    constraint users_email_unique
        unique (email),
    constraint users_phone_unique
        unique (phone),
    constraint users_fb_id_unique
        unique (fb_id),
    constraint users_vk_id_unique
        unique (vk_id)
)
engine=InnoDB collate=utf8_unicode_ci
;

create index users_city_id_index
    on users (city_id)
;

create index users_unblock_date_index
    on users (unblock_date)
;

create index users_public_at_index
    on users (public_at)
;

删除id = 5的用户之前 Before delete user with id = 5

删除id = 5的用户后 After delete user with id = 5

0 个答案:

没有答案