zoukankan      html  css  js  c++  java
  • SQL语句练习二

    use LIANXI
    Create table student--学生表
    (
    ssno    varchar(50) not null primary key,--主键学生学号
    ssname varchar(50) not null,--学生姓名
    sssex  varchar(50) not null,--学生性别
    sbirthday date,--学生出生年月
    ssclass varchar(50) not null,--学生班级
    )
    --primary key identity(1,1)自增长加入后设置的列无需输入也可自动成序号并排序,此列若需手动输入,就不
    insert into student values('108','曾华','','1977-09-01','95033')
    insert into student values('105','匡明','','1975-10-02','95031')
    insert into student values('107','王丽','','1976-01-23','95033')--学生信息
    insert into student values('101','李军','','1977-02-20','95033')
    insert into student values('109','王芳','','1975-02-10','95031')
    insert into student values('103','陆君','','1974-06-03','95031')
    
    create table teacher--教师表
    (能用自增长
    jsno varchar(50) not null primary key,--主键 教师工号
    jsname varchar(50) not null ,--教师名字
    jssex varchar(50) not null,--教师性别
    tbirthday    date,--教师出生年月
    jsprof varchar(50) not null,--教师职称
    jsdepart varchar(50) not null,--教师所在部门
    )
    insert into teacher values('804','李诚','','1958-12-02','副教授','计算机系')
    insert into teacher values('856','张旭','','1969-03-12','讲师','电子工程系')
    insert into teacher values('825','王萍','','1972-05-05','助教','计算机系')--教师信息
    insert into teacher values('831','刘冰','','1977-08-14','助教','电子工程系')
      
    create table course --课程表
    (
    kcno varchar(50) not null primary key,--课程号 主键
    kcname varchar(50) not null,--课程名称
    jsno varchar(50) references teacher(jsno) not null,--教师工号
    )
    insert into course values('3-105','计算机导论','825')
    insert into course values('3-245','操作系统','804')
    insert into course values('6-166','数字电路','856')--输入课程
    insert into course values('9-888','高等数学','831')
    
    create table score --成绩表
    (
    ssno varchar(50) references student(ssno) not null,--学生学号外键
    kcno varchar(50) references course(kcno) not null,--课程外键
    degree int not null,--课程成绩
    )
    insert into score values('103','3-245',86)
    insert into score values('105','3-245',75)
    insert into score values('109','3-245',68)--输入成绩
    insert into score values('103','3-105',92)
    insert into score values('105','3-105',88)
    insert into score values('109','3-105',76)
    insert into score values('101','3-105',64)
    insert into score values('107','3-105',91)
    insert into score values('108','3-105',78)
    insert into score values('101','6-166',85)
    insert into score values('107','6-166',79)
    insert into score values('108','6-166',81)
    
    
    --22
    --查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
    select ssno,ssname,sbirthday from student where YEAR(sbirthday)=(select YEAR(sbirthday)from student where ssno='108')
     --YEAR(sbirthday)提取年
     --month()提取月
     --day()提取天
     --dayofyear 
     print datediff(day,'1994-3-12','2015-4-23')--计算两个日期之间的天数 datediff,datepart返回一个int数值
     print datename(weekday,'2015-4-23')--计算今天值周几,datename返回一个字符串 
     print getdate()--获取当前系统时间
     --23、查询“张旭“教师任课的学生成绩
    select *from score where kcno in
    (select kcno from course where jsno in (select jsno from teacher where jsname='张旭'))
     --24、查询选修某课程的同学人数多于5人的教师姓名。
    select jsname from teacher where jsno in 
    (select jsno from course where kcno in 
    (select kcno from score group by kcno having COUNT(*)>5))
    --25、查询95033班和95031班全体学生的记录。
     select *from student where ssclass in('95033','95031')
    26、  查询存在有85分以上成绩的课程Cno.
    select kcno from score where degree>85
    27、查询出“计算机系“教师所教课程的成绩表。
    select * from score
    select * from teacher
    select * from course
    select * from score where kcno in(
    select kcno from course where jsno in(
    select jsno from teacher where jsdepart='计算机系'))
    
    28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
    select jsname,jsprof from teacher where jsprof not in
    (select jsprof from teacher group by jsprof having  count(*)>1)
    select jsname,jsprof from teacher where jsprof not in 
    (select jsprof from teacher group by jsprof having  count(*)>1)
    
    select jsname,jsprof  from teacher where jsprof  not in(select jsprof  from teacher where jsprof  in (select jsprof from teacher where jsdepart='计算机系') and jsdepart='电子工程系')and jsdepart  in('计算机系','电子工程系')
    
    29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
    select * from score where kcno='3-105' and degree>(select min (degree) from score where kcno='3-245')
    order by degree desc
    select * from score where kcno='3-105' and degree>any(select degree from score where kcno='3-245')
    --any前面必须跟比较值 any比较任何一个数 也就是3-245中所有的数 包括最小的数
    30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
    select * from score where kcno='3-105' and degree>(select max(degree) from score where kcno='3-245')
     31、 查询所有教师和同学的name、sex和birthday.
    select ssname,sssex,sbirthday from student 
    union
    select jsname,jssex,tbirthday from teacher
    32、查询所有“女”教师和“女”同学的name、sex和birthday.
    select ssname,sssex,sbirthday from student where sssex=''
    union
    select jsname,jssex,tbirthday from teacher where jssex=''
    --union 纵向连接 与 join on 横向连接 不同 两个标的数据类型必须一致不然连不到一起去
    --列数也要对应 
    33、 查询成绩比该课程平均成绩低的同学的成绩表。
    
    select * from score a where degree <(select avg(degree)from score b where a.kcno=b.kcno)
    34、 查询所有任课教师的Tname和Depart.
    select * from course
    select *from score 
    select *from teacher 
    select jsname,jsdepart from teacher where jsno in(select jsno from course where kcno in(select kcno from score))
    35 、 查询所有未讲课的教师的Tname和Depart. 
    select jsname,jsdepart from teacher where jsno not in(select jsno from course where kcno in(select kcno from score))
    
    36、查询至少有2名男生的班号。
    select *from student
    select ssclass from student where sssex=''  group by ssclass having COUNT(*)>=2
    
    37、查询Student表中不姓“王”的同学记录。
    select *from student where ssname not like '王%'
    38、查询Student表中每个学生的姓名和年龄。
    select ssname,YEAR(GETDATE())-YEAR(sbirthday) as 年龄 from student
    39、查询Student表中最大和最小的Sbirthday日期值。
    select MAX(sbirthday),MIN(sbirthday) from student
    
    40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
    select *from student order by ssclass desc,sbirthday asc
    
     --41查询“男”教师及其所上的课程
    select kcname from course where jsno in(select jsno from teacher where jssex='')
    
     42、查询最高分同学的Sno、Cno和Degree列。
     select ssno,kcno,DEGREE from score where degree IN(select max(degree) from score)
     
    43、查询和“李军”同性别的所有同学的Sname.
    select ssname from student where sssex in(select sssex from student where ssname='李军')
    
    44、查询和“李军”同性别并同班的同学Sname.
    select ssname from student where sssex in(select sssex from student where ssname='李军')
    and ssclass in (select ssclass from student where ssname='李军')
    
    45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
    select * from score where ssno in (select ssno from student where sssex='') and
    kcno in (select kcno from course where kcname='计算机导论')
    
    
    select LEFT('abcdef',2)--从左到右截取固定长度
    select RIGHT('abcdef',2)--从右往左截取固定长度
    select LOWER('ABC')--转换为小写
    select UPPER('abc')--转换为大写
    select LEN('qweqwe')--返回一个字符串 长度(int值)
    print LTRIM('                          adasj dajk                       ')--LTRIM去除字符串左边的空格
    print RTRIM('                          adasj dajk                       ')--RTRIM去除字符串右边的空格
    print substring('daskdjaskdjwioado',6,2)--从起始位置开始截取固定长度 从6 开始 6为1 所以第二个数是 a
    print replace('ssdadasrwfa','ss','jk')--第一个是字符串 第二个是被替换的目标 第三个是要替换成的字符串
    print replicate('sd',10)--复制 把 sd 复制10遍
    print str(123.456,2,2)
    
    
    
    
    --存储过程
    --定义变量
    declare @bianliang int
    set @bianliang=12--变量赋值
    select @bianliang--结果框查看
    print @bianliang--消息框查看
    -----------------定义两个变量求和
    declare @bianliang1 int,@bianliang2 int
    set @bianliang1=1
    set @bianliang2=2
    select @bianliang1+@bianliang2
    ---创建存储过程
    create proc  chaxun
    as--as...go中间写存储过程
       select *from student
    go
    --存储过程创建后,会存储到对应数据库的可编程性》存储过程文件夹下
    exec chaxun--执行存储过程
    
    --带返回值的存储过程
    
    create proc returncount
    as
       declare @a int
       select @a=COUNT(*) from student--查询结果赋值给了变量@a
       return @a--存储过程返回值
    go
    
    declare @jieshou int--定义一个变量接受存储过程返回值
    exec @jieshou=returncount--正在执行存储过程时,用接受变量去接受返回值
    print @jieshou--输出接受结果
    
    -----------带输入参数的存储过程
    create proc Jiafa
    @a int,--输入参数,(用逗号隔开,放在as和存储过程名之间)
    @b int--输入参数
    as
       return @a+@b
    go
    declare @a int
    exec @a=Jiafa 1,7 --调用带参数的存储过程,需要在存储过程名后输入参数,空格隔开,参数之间“,”隔开
    print @a
    
    ------输入一个0--100之间的数,判断是一位数还是两位数,返回数字1,2 不在范围内返回 -1
    create proc pandingshuzi--创建存储过程
    @a int--存储过程的输入参数
    as
       if @a>0 and @a<10
       begin
          return 1
       end
       else if @a>=10 and @a<100
       begin
          return 2
       end
       else 
       begin
          return -1
       end
    go
    declare @a2 int--在过程外定义一个接受变量a 用来接受 存储过程
    exec @a2=pandingshuzi 999--@a2
    print @a
  • 相关阅读:
    读写excel的组件
    一个关于C#语言中的Property的低级BUG,花了我二十分钟
    使用wwAppConfiguration类库来轻松读写应用程序配置信息
    为因地震死难的同胞默哀
    页面执行时生成静态文件的方法
    【转载】sp_spaceused2,看库里全部表占用的物理空间
    在微软中文技术论坛 CSDN cnblogs 三个微软社区中提问
    在内网服务器中获得真正的客户端ip的方法
    深拷贝的通用方法
    你知道在word中如何将段落标记替换成其他字符吗?
  • 原文地址:https://www.cnblogs.com/wwJ0312/p/4463199.html
Copyright © 2011-2022 走看看