powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / написание триггеров
3 сообщений из 3, страница 1 из 1
написание триггеров
    #32053560
marko
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Помогите написать триггер, который поддерживает связь таблиц N к M (SQL Server 2000)
...
Рейтинг: 0 / 0
написание триггеров
    #32053570
Фотография Lexis
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
from "Inside SQL Server 2000 "
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
 -- 1. INSERT and UPDATE trigger on referencing table.
 
 -- Disallow any insert or update if the foreign key title_id 
 
 -- in the referencing table titleauthor does not match
 
 -- the primary key title_id in the referenced table titles.
 
CREATE TRIGGER INS_UPD_titleauthor
    ON titleauthor
    FOR INSERT, UPDATE
AS
 -- Do any rows exist in the inserted table that do not have 
 
 -- a matching ID in titles?
 
IF EXISTS
    (SELECT * FROM inserted WHERE inserted.title_id NOT IN
        (SELECT titles.title_id FROM titles) )
    BEGIN
    RAISERROR('No matching title found. Statement will be
aborted.',  16 ,  1 )
    ROLLBACK TRAN
    END
GO

 -- 2. If primary key in referenced table titles is changed,
 
 -- update any rows in titleauthor to the DEFAULT value.
 
 -- This implements ON UPDATE SET DEFAULT. Notice that this trigger could
 
 -- be easily changed to set the column to NULL instead of DEFAULT,
 
 -- which would implement ON UPDATE SET NULL.
 
CREATE TRIGGER UPD_titles
    ON titles 
    FOR UPDATE 
AS
    DECLARE @counter int
    IF UPDATE(title_id)
        BEGIN
        UPDATE titleauthor
        SET titleauthor.title_id=DEFAULT
        FROM titleauthor, deleted
        WHERE titleauthor.title_id=deleted.title_id
    
        SET @COUNTER=@@ROWCOUNT
         -- If the trigger resulted in modifying rows of
 
          -- titleauthor, raise an informational message
 
        IF (@counter >  0 )
            RAISERROR('%d rows of titleauthor were updated to
DEFAULT title_id as a result of an update to titles table',
 10 ,  1 , @counter)
        END
GO

 -- 3. DELETE of the referenced table titles will set to NULL the
 
 --  referencing titleauthor rows
 
CREATE TRIGGER DelCascadeTrig
    ON titles
    FOR DELETE
AS
    DECLARE @counter int
    UPDATE titleauthor
    SET title_id = NULL
    FROM titleauthor, deleted
    WHERE titleauthor.title_id=deleted.title_id
    SET @counter=@@ROWCOUNT
    
    IF (@counter >  0 )
        RAISERROR('%d rows of titleauthor were set to a
NULL title_id as a result
of a delete to the titles table',  10 ,  1 , @counter)
GO
...
Рейтинг: 0 / 0
написание триггеров
    #32053586
Фотография Jimmy
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Связь N к M проще всего реализовать введением таблицы ассоциаций, а не триггерами.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
create table n_table(id_n int identity( 1 , 1 )  primary key, n_value char( 10 ))
go
create table m_table(id_m int identity( 1 , 1 )  primary key, m_value char( 10 ))
go
create table n_m_associations(id_n int references n_table(id_n),id_m int references m_table(id_m),)
go
insert n_table (n_value) values('A-N')
insert n_table (n_value) values('B-N')
insert n_table (n_value) values('C-N')
go
insert m_table (m_value) values('A-M')
insert m_table (m_value) values('B-M')
insert m_table (m_value) values('C-M')
go
insert n_m_associations (id_n,id_m) values( 1 , 1 )
insert n_m_associations (id_n,id_m) values( 1 , 2 )
insert n_m_associations (id_n,id_m) values( 2 , 1 )
insert n_m_associations (id_n,id_m) values( 2 , 3 )
go
select a.id_n,a.id_m,n.n_value,m.m_value
from n_table n, m_table m, n_m_associations a
where n.id_n = a.id_n and m.id_m = a.id_m
order by  4 , 3 
go
 --- results ---
 
id_n        id_m        n_value    m_value    
 ----------- ----------- ---------- ---------- 
 
 1 . 00          1 . 00         A-N        A-M       
 2 . 00          1 . 00         B-N        A-M       
 1 . 00          2 . 00         A-N        B-M       
 2 . 00          3 . 00         B-N        C-M       

( 4  row(s) affected)


...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / написание триггеров
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]