第三章 sql编程 练习

本文展示了使用SQL进行编程的一些实际操作,包括利用循环打印直角三角形的两种方法,计算1到100的偶数和,以及使用CASE语句进行数据统计分析。此外,还提供了根据学生考试成绩进行等级划分和通过循环为不及格学生加分的案例。
--打印直角三角形(法1)
                  --声明列,赋值
                  declare @row int
					set @row=1
					--声明行,赋值
					declare @low int 
					set @low=1
					--声明符号,赋值
					declare @xx varchar(10)
					set @xx='*'
					
					while(@row<=4)
					begin 
					
					while(@low<=@row)
					begin 
					
					print @xx
					
					select @xx=@xx+'*'
				    set @low=@low+1
				    
					end
					
					set @row=@row+1
					
					end
--打印直角三角形(法2)
declare @str2 nvarchar(200)
 declare @count int
 declare @result nvarchar(200)
 set @str2='*'
 set @count=0
 set @result=''0
 while(@count<4)
	begin
		set @result+=@str2
		print @result
		set @count+=1
	end
					
	--1--100的偶数和
	 declare @sum int
        set @sum=0
    	declare @num int
    	set @num=1
    	
	  while(@num<=100)begin
		  if(@num%2=0)
		  begin
			 set @sum=@num+@sum
		  end
		set  @num=@num+1
	  end
	  print @sum	
	  
	  --case end 案例
	create table sf(shengfu varchar(10),date datetime)
	insert into sf values('胜','2017-1-2')
    insert into sf values('胜','2017-1-1')
    insert into sf values('胜','2017-1-1')
    insert into sf values('胜','2017-1-2')
    insert into sf values('负','2017-1-2') 
    insert into sf values('负','2017-1-2')
    insert into sf values('负','2017-1-1')
    insert into sf values('负','2017-1-1')
    select * from sf
    
   select date as 日期,
   sum(case  when shengfu='胜' then 1 else 0 end)as 胜,
   sum(case  when shengfu='负' then 1 else 0 end)as 负    from sf           
       group by date
    

	  
  create table tb(LocTion varchar(10),iwork varchar(10),imoney int,itime int,age int)
   insert into tb values('广州', '程序员', 1000 , 5  , 22 )
   insert into tb values('广州', '教师'  , 1700 , 10 , 22 )
 insert into tb values('广州', '警察'  , 1300 , 15 , 22 )
  insert into tb values('广州', '警察'  , 800  , 5  , 22 )
   insert into tb values('上海', '程序员', 1600 , 5  , 21 )
   insert into tb values('上海', '司机'  , 1200 , 15 , 21 )
   insert into tb values('北京', '程序员', 1400 , 5  , 29 )

  select * from tb
   select loction , iwork, age,
   sum(case itime when 5 then imoney else 0 end),
   sum(case itime when 10 then imoney else 0 end),
   sum(case itime when 15 then imoney else 0 end) from tb
       group by loction , iwork ,age

                                       

--Case End 经典练习**************************************
--采用美国ABCDE五级打分制显示学生'走进Java编程世界'课程最近一次考试成绩(姓名等级)
--A级:   90分以上
--B级:  80-分
--C级:   70-分
--D级:60-分
--E级:60分以下
--01.找一个变量存储科目编号 
declare @subid int 
select @subid=subjectid from subject
where subjectname='走进Java编程世界'


--02.找到一个变量 ,存储最近一次考试时间 

declare @maxdate datetime 
select @maxdate= max(examdate) from result
where subjectid=@subid
 
  select studentname,等级=
  case
     when StudentResult>90 then 'A' 
     when StudentResult>80 then 'B' 
     when StudentResult>=70 then 'C' 
     when StudentResult>60 then 'D' 
     else 'E'
  end 
  from student,result
  where student.StudentNo=result.StudentNo
  and subjectid=@subid
  and examdate=@maxdate
  


--经典while循环加分题目***************************************
--检查学生参加“oop”课最近一次考试是否有不及格(60分及格)的学生。
--如有,每人加2分,高于95分的学生不再加分,直至所有学生这次考试成绩均及格

--①
--01.找一个变量存储o科目编号 
declare @subid2 int 
select @subid2=subjectid from subject
where subjectname='走进Java编程世界'
--02.找到一个变量 ,存储最近一次考试时间 
declare @maxdate2 datetime 
select @maxdate2= max(examdate) from result
where subjectid=@subid2

select * from result
where subjectid=@subid2
and  examdate=@maxdate2


declare @su int
select @su=COUNT(Studentno) from Result
where subjectid=@subid2 
and  examdate=@maxdate2
and studentresult<70

while(@su>0)
begin
   --每人+2分
   update result set studentresult+=2
   where subjectid=@subid2 
	and  examdate=@maxdate2
    and studentresult<95
    
    select @su=COUNT(Studentno) from Result
where subjectid=@subid2
and  examdate=@maxdate2
and studentresult<95

end
--②
declare @maxdate2 datetime --找到一个变量 ,存储最近一次考试时间 
declare @subid2 int   --找一个变量存储o科目编号
select @subid2=subjectid from subject
where subjectname='走进Java编程世界'

select @maxdate2= max(examdate) from result
where subjectid=@subid2


declare @n int
while(@n>0)
begin
   select @n=COUNT(Studentno) from Result
   where SubjectId=@subid2 and ExamDate=@maxdate2 and StudentResult<60
   if(@n>0)
   update result set studentresult+=2 from Result--每人+2分
   where subjectid=@subid2 
	and  examdate=@maxdate2
    and studentresult<95
    else
    break
    end
    print '加分后的成绩如下:' select StudentNo,studentresult from Result
    where SubjectId=@subid2 and ExamDate=@maxdate2

SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言中需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以写出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用的SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值