我的模块是关于用户权限的。
用户可以拥有设备。用户可以将此设备的权限授予其他用户。没有读/写权限:如果您有权限,则可以使用权限=>执行相同的操作。 孩子可以授予。
Properties Style :- japanese, russian, american || Outdoor Features :- music system in park,Basketball Court || Special Rooms :- Camere Special ||
当用户permission inheritance:
A gives B permission. B gives C permission. C gives D permission.
a --> b --> c --> d
table: USER_DEVICE
---------------------------------
| FK_USER | FK_DEVICE | FK_PARENT |
|---------|-----------|-----------|
| a | d | null |
| b | d | a |
| c | d | b |
| d | d | c |
---------------------------------
删除用户a
上设备d
的权限时,应该递归删除每个子项。
b
应该触发
DELETE USER_DEVICE WHERE FK_DEVICE = 'd' AND FK_USER = 'b'
应该触发
DELETE USER_DEVICE WHERE FK_DEVICE = 'd' AND FK_PARENT = 'b'
是否可以将此行为创建为删除级联?
答案 0 :(得分:2)
在这里工作:
CREATE table user_device(
fk_user varchar NOT NULL
, fk_device varchar NOT NULL
, fk_parent varchar
, PRIMARY KEY(fk_user,fk_device)
, FOREIGN KEY(fk_parent,fk_device) REFERENCES user_device (fk_user,fk_device) ON DELETE CASCADE
);
INSERT INTO user_device(fk_user,fk_device,fk_parent) VALUES
('a' , 'd' , null )
,('b' , 'd' , 'a' )
,('c' , 'd' , 'b' )
,('d' , 'd' , 'c' )
;
SELECT * FROM user_device ;
DELETE FROM user_device WHERE fk_user = 'a';
SELECT * FROM user_device ;
结果:
CREATE TABLE
INSERT 0 4
fk_user | fk_device | fk_parent
---------+-----------+-----------
a | d |
b | d | a
c | d | b
d | d | c
(4 rows)
DELETE 1
fk_user | fk_device | fk_parent
---------+-----------+-----------
(0 rows)
答案 1 :(得分:0)
编辑:ON DELETE CASCADE
的外键工作正常。有关详细信息,请参阅joops答案。
ON DELETE CASCADE
适用于外键,但不适用于这些类型的约束。相反,您可以为表创建AFTER DELETE
trigger,删除相关行。