Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 指定“删除时无操作”或“更新时无操作”无效_Sql Server - Fatal编程技术网

Sql server 指定“删除时无操作”或“更新时无操作”无效

Sql server 指定“删除时无操作”或“更新时无操作”无效,sql-server,Sql Server,当我创建表UV(包含外键的表)时,表可以正常工作 但是当我想更新或删除一些外键。。查询不起作用。因此,我发现我必须在每个外键上添加更新级联删除级联 问题是,我有两列外键引用用于表formatter()中的主键 那是我的创作 create table formateur ( num_formateur int primary key , nom_formateur varchar(30), prenom_formateur varchar(30), telephon

当我创建表UV(包含外键的表)时,表可以正常工作

但是当我想更新或删除一些外键。。查询不起作用。因此,我发现我必须在每个外键上添加更新级联
删除级联

问题是,我有两列外键引用用于表formatter()中的主键

那是我的创作

create table formateur
(
    num_formateur int primary key ,
    nom_formateur varchar(30),
    prenom_formateur varchar(30),
    telephone nvarchar(12),
    adresse nvarchar(300),
    typee varchar(30)
)

create table formation
(
    num_formation int primary key,
    nom_formation varchar(30),
    nombre_UV int,
    motdepasse nvarchar(30)
)

create table UV
(
    num_UV int primary key,
    nom_UV varchar(30),
    masse_horaire_prevue int,
    num_formateur_enseignant int foreign key  references formateur(num_formateur) on update cascade on delete cascade  ,
    num_formateur_responsable int foreign key   references formateur(num_formateur) on update cascade on delete cascade ,
    num_formation int foreign key   references formation(num_formation) on update cascade on delete cascade
)


insert into formateur values('1','nom_formateur','prenom_formateur','342343412','adresse','responsable')
insert into formateur values('2','nom_formateur','prenom_formateur','342343412','adresse','responsable')
insert into formateur values('3','nom_formateur','prenom_formateur','342343412','adresse','enseignant')
insert into formateur values('4','nom_formateur','prenom_formateur','342343412','adresse','enseignant')


insert into formation values ('1','nom_formation','123','1')
insert into formation values ('2','nom_formation','123','1')
insert into formation values ('3','nom_formation','123','1')
insert into formation values ('4','nom_formation','123','1')

insert into UV values('1','nom_UV','23','3','1','1')
insert into UV values('2','nom_uv','43','4','2','1')
insert into UV values('3','nom_uv','63','4','2','1')
insert into UV values('4','nom_uv','73','4','2','1')

您的引用尝试从同一个表中删除记录,而这两列可能会引用同一条记录(这是一个问题)

您可以在删除时创建触发器,而不是使用级联选项创建FKs,并从[formateur]中删除记录,或者只为每列创建单独的表(formateur1和formateur2,类似于此)

另外,我注意到UV中的数据在
num\u formatter\u ensignant
num\u formatter\u responsible
列中没有唯一的值

确实要删除这些记录,因为它们可以在不同的行中重复使用吗

如果你用扳机

CREATE OR ALTER TRIGGER TRG_UV_DELETE
on UV
INSTEAD OF DELETE
AS
BEGIN
    set nocount on

    DELETE UV
    FROM UV
        INNER JOIN DELETED
            ON UV.num_UV = DELETED.num_UV

    DELETE formation
    FROM formation
        INNER JOIN DELETED
            ON formation.num_formation = DELETED.num_formation

    delete formateur
    from formateur
    INNER JOIN DELETED
        ON formateur.num_formateur IN (DELETED.num_formateur_enseignant, DELETED.num_formateur_responsable) 
END
然后像这样填写您的表格:

insert into UV values('1','nom_UV','23','1','2','1')
insert into UV values('2','nom_uv','43','3','4','2')
触发器会工作的