1 创建数据库。
注:电脑上只有c盘,所以创建数据库在c:\project目录下)
1.1方式一通过ssms创建。
1.2方式二通过sql 查询分析器创建。
create database [dbstu] on primary
name = n'dbstu', filename = n'c:\project\' size = 3072kb , maxsize = unlimited, filegrowth = 15%)
log on
name = n'dbstu_log', filename = n'c:\project\dbstu_' size = 1024kb , maxsize = 2048gb , filegrowth = 15%)
go2 创建数据表和约束。
2.1 新建学生信息表(stuinfos)
use dbstu
gocreate table stuinfos
stuname nvarchar(10) not null,stuno nchar(7) not null check (stuno like 'msd07%')stusex nvarchar(1) not null default (n'男') check(stusex in(n'男',n'女'))stuage numeric(2, 0) not null check(stuage>=15 and stuage<=50) ,stuseat numeric(18, 0) identity(1,1) not null,stuaddress ntext null default (n'地址不详'),constraint pk_stuinfos primary key clustered
stuno asc
) with (ignore_dup_key = off) on [primary]
) on [primary] textimage_on [primary]
go2.2 新建学生成绩表(stumarks)
create table stumarks
examno nchar(11) not null check (examno like 'e200707%')stuno nchar(7) not null check (stuno like 'msd07%')writtenexam numeric(3, 0) not null default (n'0') check(writtenexam>=0 and writtenexam<=100) ,labexam numeric(3, 0) not null default (n'0') check(labexam>=0 and labexam<=100) ,foreign key(stuno) references stuinfos(stuno),constraint pk_stumarks primary key clustered
examno asc
) with (ignore_dup_key = on) on [primary]
go3 插入测试数据。
3.1 插入学生记录。
set xact_abort on
gobegin tran
insert into ("stuname","stuno","stusex","stuage","stuaddress") values(n'张秋丽','msd0701',n'男',18,n'北京海淀')
insert into ("stuname","stuno","stusex","stuage","stuaddress") values(n'李斯文','msd0703',n'男',22,n'河南洛阳')
insert into ("stuname","stuno","stusex","stuage","stuaddress") values(n'李文才','msd0702',n'女',31,n'')
insert into ("stuname","stuno","stusex","stuage","stuaddress") values(n'欧阳俊雄','msd0704',n'男',28,n'新疆威武哈')
commit tran
3.2 插入成绩记录。
begin tran
insert into "examno","stuno","writtenexam","labexam") values('e20070701','msd0703',80,58)
insert into "examno","stuno","writtenexam","labexam") values('e20070702','msd0702',50,99)
insert into "examno","stuno","writtenexam","labexam") values('e20070703','msd0702',65,0)
insert into "examno","stuno","writtenexam","labexam") values('e20070704','msd0701',77,82)
commit tran
4 修改表中数据。
update
set writtenexam = case when writtenexam=0 then writtenexam else (case when writtenexam<95 then writtenexam+5 else 100 end) end,labexam = case when labexam=0 then labexam else (case when labexam<95 then labexam+5 else 100 end) end
5 基本查询语句。
5.1 查询两表所有数据。
1)学生信息表。
select * from stuinfos(nolock)
2)成绩表。
select * from stumarks(nolock)
5.2 查询男学生名单。
select * from stuinfos(nolock)
where stusex=n'男'
5.3 查询笔试成绩优秀学员。
select a.*,from stuinfos a inner join stumarks b
on where between 75 and 100
5.4 统计本次参考人数。
select count(*)as 'stunumber' from
select distinct stuno from stumarks) a
5.5 查询没有通过考试的人数。
select count(*)as 'stunumber' from
select distinct stuno from stumarks
where writtenexam<60 or labexam<60) a
5.6 统计笔试和机试平均分(不含补考成绩)
select **g( as **gwrittten, **g( as **glab
from stumarks a
inner join (select stuno,min(examno) as examno
from stumarks
group by stuno) b
on and
5.7 查询参考学员笔试,机试和平均分。
select as **gexam
from stumarks a
inner join (select stuno,min(examno) as examno
from stumarks
group by stuno) b
on and
5.8 按平均分从低到高排名次。
select as **gexam
from stumarks a inner join stuinfos s
on inner join (select stuno,min(examno) as examno
from stumarks
group by stuno) b
on and
order by 3 desc
SQL作业
客户号客户名称联系人地址邮政编码 客户。产品号。客户号。订单号。订购日期。订购单。产品名称规格说明单价。产品。订单号序号产品号数量。产品。图1订单管理数据库。查询客户表中的所有记录 从订购单表中查询客户号信息 哪些客户有订购单 查询单价在x元以上 含 的产品信息。查询单价在x元以上 不含 的某产品 ...
SQL作业
sql server数据库。实验报告。罗洁思编写。班级 汽车服务工程 2 班 姓名 康俊伟 学号 0908082217 厦门理工学院机械工程系。实验一活期储蓄管理系统数据库设计。一 实验目的。1 掌握概念模型 关系模型和关系数据库的概念 2 掌握概念模型的设计方法 3 掌握关系模型的设计。二 实验内...
SQL作业
一 学生 课程 选修数据库。单表。1 检索学习课程号为0911的学生的学号和成绩。2 统计选修了课程号为0911的学生的人数。3 检索每个学生的出生年份,输出学生姓名和出生年份。4 检索选修课程号为0912或者0911的学生的学号。5 检索18 20岁的姓王的同学的学生姓名。6 检索学习课程中包含了...