Posted on 2008-08-12 15:52
Prayer 阅读(215)
评论(0) 编辑 收藏 引用 所属分类:
数据库,SQL
*****************************************************************************************
视图,存储过程和触发器
*****************************************************************************************
创建视图:(查询数据,用于系统统计报表)
create view dept_sum_vu
(name,minsal,maxsal,avgsal)
as select d.department_name,min(e.salary),max(e.salary),avg(e.salary)
from employees e,departments d
where e.department_id = d.department_id
group by d.department_name;
存贮过程,触发器和删除外键
1).存贮过程:
存贮过程的路径:打开Stored Procedures中new一个新的Stored Procedures,中间填写代码,代码如下:
CREATE PROCEDURE sa.bbsTest AS
select Bbsid ,content , area_id , name FROM BBs a JOIN bbs_area b
on a.area_id = b.id
CREATE PROCEDURE bbsTest AS
select Bbsid ,content , area_id , name FROM BBs a JOIN bbs_area b
on a.area_id = b.id
2).触发器
打开用户表项,点设计表,选中其中一个字段,右键task点manager triggers,在弹出的表单中写代码:
CREATE TRIGGER [tD_bbs_area] ON [dbo].[bbs_area]
FOR DELETE
AS
BEGIN
DELETE rebbs
FROM bbs, deleted
WHERE rebbs.bbs_id = bbs.bbsid
AND bbs.area_id = deleted.id
DELETE bbs
FROM deleted
WHERE bbs.area_id = deleted.id
END
3).删除外键
打开用户表项,点设计表,选中其中一个字段,右键relationships,在弹出的主键和外键中选择就可以了.
删除外键,需要先删除主键表里面的有关外键的字段,再来删除外键的有关字段.