CREATE DATABASE chaxun
ON PRIMARY
(NAME=chaxun_data,
FILENAME='D:\software\chaxun.mdf',
SIZE=10MB,
MAXSIZE=UNLIMITED,
FILEGROWTH=10%
)
LOG ON
(NAME=chaxun_log,
FILENAME='D:\software\chaxun.ldf',
SIZE=1MB,
MAXSIZE=5MB,
FILEGROWTH=1MB
)
use chaxun
create table KC
(
c_ID char(3) primary key,
c_Name char(10) not null,
c_Credit int
)
create table XS
(
s_ID char(4) primary key,
s_Name char(6) not null,
s_department char(20) not null,
s_telephoe char(11)
)
create table XS_KC
(
s_ID char(4) foreign key references XS(s_ID),
c_ID char(3) foreign key references KC(c_ID),
score int,
primary key(s_ID,c_ID)
)
insert into KC values('101','语文',4)
insert into KC values('102','数学',2)
insert into KC values('103','英语',3)
insert into KC values('104','体育',1)
insert into XS values('1001','杨颖','信息技术系','2346666')
insert into XS values('1002','王丽','信息技术系','2346666')
insert into XS values('1003','张亮','经管系','2315555')
insert into XS values('1004','刘强','会计系','2361111')
insert into XS_KC values('1001','101',89)
insert into XS_KC values('1001','102',97)
insert into XS_KC values('1001','103', 86)
insert into XS_KC values('1002', '101',56)
insert into XS_KC values('1003', '102',55)
select * from KC
where C_Credit<3 order by c_ID ASC
select s_ID,AVG(score) 平均分 from XS_KC
group by s_ID
order by AVG(score) DESC
select s_Name,XS.s_ID from XS_KC,XS where XS.s_ID=XS_KC.s_ID
group by XS.s_ID,s_Name having COUNT(XS_KC.c_ID)>=2
方法1
select XS.* from XS,XS_KC
where XS.s_ID=XS_KC.s_ID and score<60
方法2(子查询)
select * from XS where s_ID in (select s_ID from XS_KC where score<60)
子查询
select * from XS where s_ID in(select distinct s_ID from XS_KC
where XS_KC.c_ID='101' and score <60)
连接查询
select XS.* from XS join XS_KC on XS.s_ID=XS_KC.s_ID
where XS_KC.c_ID='101' and score <60
select * from XS where s_telephoe in
(select s_telephoe from XS group by s_telephoe having COUNT(s_telephoe)>=2)
select distinct s_department from XS
select COUNT(distinct s_ID ) 已选课人数 from XS_KC
方法1
select x.s_Name from XS as x,XS as y
where x.s_department=y.s_department and y.s_Name='杨颖' and x.s_Name<>'杨颖'
select s_Name from XS where s_department=
(select s_department from XS where s_Name='杨颖') and s_Name <>'杨颖'
方法1
select s_Name,c_Name,score from XS,KC,XS_KC
where XS.s_ID=XS_KC.s_ID and KC.c_ID=XS_KC.c_ID
select s_Name,c_Name,score from XS join XS_KC on XS.s_ID=XS_KC.s_ID
join KC on KC.c_ID=XS_KC.c_ID
update XS set s_telephoe='3615555' where s_department='经管系'
select c_ID,COUNT(s_ID) 选课人数,MAX(score) 最高分 from XS_KC group by c_ID
select s_ID,COUNT(c_ID) 选课门数,SUM(score) 总成绩 from XS_KC
group by s_ID order by COUNT(c_ID) DESC
select COUNT(s_ID) 未选课人数 from XS where s_ID not in
(select s_ID from XS_KC)
今天的数据库学习内容就到这里啦,如果对友友们有帮助的话,记得点赞收藏博客,关注后续的数据库学习内容哦~