当前位置:文档之家› oracle创建表视图 增删改查的实例

oracle创建表视图 增删改查的实例



_________________________________________________________________________________________________
创建3个表

表一
create table 职工(职工号 char(10) primary key,
姓名 char(8),
年龄 smallint default 20,
性别 char(20),constraint C1 check (性别 in ('男','女')));
表二
create table 社会团体(编号 char(10) primary key,
名称 char(8),
负责人 char(10),
活动地点 char(20),
constraint C2 foreign key (负责人) references 职工 (职工号));
表三
create table 参加(职工号 char(10),编号 char(10),
参加日期 smalldatetime,constraint C3 primary key (职工号,编号),
constraint C4 foreign key (职工号) references 职工 (职工号),
constraint C5 foreign key (编号) references 社会团体 (编号));
__________________________________________________________________________________________________
创建2个视图

(2)create view 社团负责人(编号,名称,负责人职工号,负责人姓名,负责人性别)as select 社会团体.编号,社会团体.名称,社会团体.负责人,
职工.职工号,职工.性别from 职工,社会团体,参加where 社会团体.编号=参加.编号 and 职工.职工号=参加.职工号
create view 参加人情况(职工号,姓名,社团编号,社团名称,参加日期)as select 参加.职工号,姓名,社会团体.编号,名称,参加日期from 职工,社会团体,参加where 职工.职工号=参加.职工号 and 参加.编号=社会团体.编号
_________________________________________________________________________________________________
查找参加歌唱队或篮球队的职工号和姓名

(3)select distinct 职工.职工号,姓名from 职工,社会团体,参加where 职工.职工号=参加.职工号 and 参加.编号=社会团体.编号and 社会团体.名称 in('歌唱队','篮球队');
__________________________________________________________________________________________________
查找没参加任何社会团体的职工情况

(4)select *from 职工where not exists (select *from 参加where 参加.职工号=职工.职工号);
__________________________________________________________________________________________________
查找参加了全部社会团体的职工情况

(5)select * from 职工where not exists (select *from 社会团体where not exists(select *from 参加where 参加.职工号=职工.职工号 and 参加.编号=社会团体.编号));
__________________________________________________________________________________________________
查找参加了职工号为“1001”的职工参加的全部社会团体的职工号

(6)select 职工号from 职工where not exists (select *from 参加 参加1where 参加1.职工号='001'and not exists(select *from 参加 参加2where 参加2.编号=参加1.编号 and 参加2.职工号=职工.职工号))
__________________________________________________________________________________________________
求每个社会团体的参加人数

(7)select 编号,count(职

工号) as 参加人数from 参加group by 编号;
__________________________________________________________________________________________________
求参加人数最多的社会团体的名称和参加人数

(8)select TOP 1 名称,count(*) 参加人数from 参加,社会团体where 参加.编号=社会团体.编号group by 名称order by 参加人数 desc
__________________________________________________________________________________________________
求参加人数超过100人的社会团体的名称和负责人

(9)select distinct 社会团体.名称,职工.姓名 as 负责人from 职工,社会团体,参加where 社会团体.编号=参加.编号and 社会团体.负责人=职工.职工号and 参加.编号 in(select 参加.编号from 参加group by 参加.编号 having count(参加.编号)>100)
__________________________________________________________________________________________________
把对社会团体和参加两个表的数据查看、插入和删除数据的权力赋给用户李平,并允许他再将此权力授予其他用户
(10)grant select,insert,delete on 社会团体 to 李平with grant option;grant select,insert,delete on 参加 to 李平with grant option;
__________________________________________________________________________________________________



相关主题
文本预览
相关文档 最新文档