ПРАКТИЧЕСКАЯ РАБОТА
7.1 Тема: Назначение взаимосвязей в таблицах
7.2 Цель: научиться устанавливать взаимосвязи в таблицах.
7.3 Оборудование, инструменты, расходные материалы:
1) Персональный компьютер
2) Программа SQL Server 2000 (утилита Query Analizer) и Erwin 4.0
7.4 Теоретическая часть
Для установления взаимосвязей в таблицах необходимо использовать механизм жестких ограничений ссылочной целостности или механизм каскадных воздействий.
7.5 Выполнение работы
1) Установите механизмы ссылочной целостности с помощью программы Erwin 4.0.
2) Создайте триггеры в базе данных при помощи утилиты Query Analizer.
7.6 Пример выполнения работы
create trigger tD_Author on Author for DELETE as
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
/* DELETE trigger on Author */
begin
declare @errno int,
@errmsg varchar(255)
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
/* Author R/1 AuthorBook ON PARENT DELETE CASCADE */
/* ERWIN_RELATION:PARENT_OWNER="", PARENT_TABLE="Author"
CHILD_OWNER="", CHILD_TABLE="AuthorBook"
P2C_VERB_PHRASE="R/1", C2P_VERB_PHRASE="",
FK_CONSTRAINT="R_1", FK_COLUMNS="au_id" */
delete AuthorBook
from AuthorBook,deleted
where
/* %JoinFKPK(AuthorBook,deleted," = "," and") */
AuthorBook.au_id = deleted.au_id
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tU_Author on Author for UPDATE as
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
/* UPDATE trigger on Author */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@insau_id int,
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
/* Author R/1 AuthorBook ON PARENT UPDATE CASCADE */
/* ERWIN_RELATION:PARENT_OWNER="", PARENT_TABLE="Author"
CHILD_OWNER="", CHILD_TABLE="AuthorBook"
P2C_VERB_PHRASE="R/1", C2P_VERB_PHRASE="",
FK_CONSTRAINT="R_1", FK_COLUMNS="au_id" */
if
/* %ParentPK(" or",update) */
update(au_id)
begin
if @numrows = 1
begin
select @insau_id = inserted.au_id
from inserted
update AuthorBook
set
/* %JoinFKPK(AuthorBook,@ins," = ",",") */
AuthorBook.au_id = @insau_id
from AuthorBook,inserted,deleted
where
/* %JoinFKPK(AuthorBook,deleted," = "," and") */
AuthorBook.au_id = deleted.au_id
end
else
begin
select @errno = 30006,
@errmsg = 'Cannot cascade Author UPDATE because more than one row has been affected.'
goto error
end
end
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tI_AuthorBook on AuthorBook for INSERT as
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
/* INSERT trigger on AuthorBook */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Dec 25 11:26:53 2008 */
/* fee R/8 AuthorBook ON CHILD INSERT SET NULL */
/* ERWIN_RELATION:PARENT_OWNER="", PARENT_TABLE="fee"
CHILD_OWNER="", CHILD_TABLE="AuthorBook"
P2C_VERB_PHRASE="R/8", C2P_VERB_PHRASE="",
FK_CONSTRAINT="R_8", FK_COLUMNS="pos" */
if
/* %ChildFK(" or",update) */
update(pos)
begin
update AuthorBook
set
/* %SetFK(AuthorBook,NULL) */
AuthorBook.pos = NULL
from AuthorBook,inserted
where
/* %JoinPKPK(AuthorBook,inserted," = "," and") */
AuthorBook.book_id = inserted.book_id and
AuthorBook.au_id = inserted.au_id
7.7 Вывод
Дата добавления: 2015-09-27 | Просмотры: 666 | Нарушение авторских прав
|