关于删除级联-我必须在其中添加它 [英] On delete cascade - where I have to add it

查看:92
本文介绍了关于删除级联-我必须在其中添加它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个级联删除的示例。我的问题是,在哪里添加它?在表中创建PK,或者在其他表中以PK身份存储该PK?

I needed an example of cascade delete. My question is, where do I add it? In the table I create PK, or in other tables where this PK is sitting there as FK?

我可以使用更改表在删除级联上添加现有表?请举个例子吗?

Can I use "alter table" to add "on delete cascade" to existing table? An example please?

@edit
MYSQL,使用phpMyAdmin

@edit MYSQL, using phpMyAdmin

@edit 2

看起来不错吗?

alter table wplaty
  drop foreign key pesel,
  add constraint pesel foreign key (pesel)
    references baza_osob(pesel) on delete cascade on update restrict;

我的父表= baza_osob
我的子表= wplaty

My parent table = baza_osob My child table = wplaty

PK是peel,FK也是peel。

PK is pesel, FK is pesel as well.

@ edit3
有错误:

@edit3 Got error:


#1025-将'.\projekt\wplaty'重命名为'.\projekt#sql2-1300-6c'(错误:152)

#1025 - Error on rename of '.\projekt\wplaty' to '.\projekt#sql2-1300-6c' (errno: 152)


推荐答案

级联指令进入子表,例如

cascade directives go into the "child" tables, e.g.

create table parent (
   id int primary key
)

create table child (
   id int primary key
   parent_id int,
   foreign key (parent_id) references parent (id)
      on delete cascade
)

从未尝试对外键进行更改以更改其 设置,但是最坏的情况是,您只是放弃现有的FK并使用新的 on 设置对其进行重新定义。

Never tried doing an alter on a foreign key to change its on settings, but worst case, you simply drop the existing FK and redefine it with the new on settings in place.

这篇关于关于删除级联-我必须在其中添加它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆