完整约束的案例:
老师告诉我,答案是最没用的东西。

分析能力是一个最大的能力
运动员与项目之间是属于多对多关系。
多对多是怎么设计的?

第一个操作是,数据表的建立。

编写一个数据库脚本

--删除数据表
drop table grade;
drop table sporter;
drop table item;
purge recyclebin;
--创建表
create table sporter(
sporterid number(4),
name varchar2(30) not null,
sex varchar2(10) not null,
department varchar2(30) not null,
constraint pk_sporterid primary key (sporterid),
constraint ck_sex check(sex in ('' ,''))
)

create table item (
itemid varchar2(4),
itname varchar2(30) not null,
location varchar2(30) not null,
constraint pk_itemid primary key (itemid)
)

create table grade(
sporterid number(4),
itemid varchar2(4),
mark number(1),
constraint fk_sporterid foreign key(sporterid) references sporter (sporterid) on delete cascade,
constraint fk_itemid foreign key (itemid) references item (itemid) on delete cascade,
constraint ck_mark check ( mark in (6,4,2,0))
)
--测试数据
insert into sporter(sporterid,name,sex,department)values ( 1001 ,'李逵','','计算机系');
insert into sporter(sporterid,name,sex,department)values ( 1002 ,'李妹','','计算机系');
insert into sporter(sporterid,name,sex,department)values ( 1003 ,'李比','','会计系');
insert into sporter(sporterid,name,sex,department)values ( 1004 ,'李好','','金融系');


insert into item (itemid , itname , location )values ('x001','标杆','01');
insert into item (itemid , itname , location )values ('x002','长跑','02');
insert into item (itemid , itname , location )values ('x003','短跑','03');
insert into item (itemid , itname , location )values ('x004','飞镖','01');
insert into item (itemid , itname , location )values ('x005','射箭','02');
insert into item (itemid , itname , location )values ('x006','三级跳','03');

insert into grade(sporterid,itemid,mark)values (1001,'x001',6);
insert into grade(sporterid,itemid,mark)values (1002,'x001',6);
insert into grade(sporterid,itemid,mark)values (1001,'x002',2);
insert into grade(sporterid,itemid,mark)values (1004,'x001',2);
insert into grade(sporterid,itemid,mark)values (1003,'x003',6);
insert into grade(sporterid,itemid,mark)values (1001,'x001',2);
insert into grade(sporterid,itemid,mark)values (1004,'x003',2);
insert into grade(sporterid,itemid,mark)values (1003,'x002',6);

--事务提交
commit

 

 

第一道题目:求出总积分最高的系名及积分


确定所需要的设计表:
sporter表
grade 表:
确定已知的关联字段:
id=id

首先,求出所有系的分数:
select s.department , sum(g.mark)
from sporter s, grade g
where s.sporterid= g.sporterid
group by s.department

第三步:最高积分
select max(sum(g.mark))
from sporter s, grade g
where s.sporterid= g.sporterid
group by s.department

第四步:
select s.department , sum(g.mark)
from sporter s, grade g
where s.sporterid= g.sporterid
group by s.department
having sum(g.mark)=(
select max(sum(g.mark))
from sporter s, grade g
where s.sporterid= g.sporterid
group by s.department
)

第二题:找出一操场的各项比赛名称及运动员姓名
item 表 项目名字
sportor 表 冠军名字
grade 表 冠军分数
确定已知的关联字段:
项目和成绩:
运动员和 成绩

先找到一操场比赛的各个项目编号和成绩
select i.itemid ,max(g.mark)
from item i ,grade g
where i.itemid=g.itemid and i.location='01'
group by i.itemid

第三步,
select i.itname, s.name ,g.mark
from sporter s, item i , grade g ,(
select i.itemid iid,max(g.mark) max
from item i ,grade g
where i.itemid=g.itemid and i.location='01'
group by i.itemid
) temp
where s.sporterid=g.sporterid and i.itemid=g.itemid and i.location ='01'
and i.itemid=iid and g.mark=max

第三题:找出参加了张三所参加过的项目的其他同学的姓名
确定所需要的表,
sporter 表
grade 表

第一步:找到李逵运动员编号;
select *
from sporter
where name='李逵'

第二步:
select g.itemid
from grade g
where sporterid =(
select sporterid
from sporter
where name='李逵'
)

第三步:
select g.sporterid
from grade g
where g.itemid in(
select g.itemid
from grade g
where sporterid =(
select sporterid
from sporter
where name='李逵'
)
);
第四步:
select distinct s.name
from sporter s
where s.sporterid in (
select g.sporterid
from grade g
where g.itemid in(
select g.itemid
from grade g
where sporterid =(
select sporterid
from sporter
where name='李逵'
)
)
)
and name <>'李逵';

第四题:经查李逵因为使用了违禁药品,其成绩变成0
update grade set mark =0 where sporterid=(
select sporterid from sporter where name='李逵'
)

select * from grade

第五题,删除女子跳高项目
delete from item where itname ='跳远'