数据库的简单查询和连接查询实验
        【摘要】 
                    
                        
                    
                    
 文章目录
 数据插入准备:数据库简单查询和连接查询实验3
 
 
数据插入准备: 
查看表的结构 
sp_help S1 
1 
查看表头 
select *from S1
1 
– 查看数据库中...
    
    
    
    数据插入准备:
- 查看表的结构
 
sp_help S1 
  
 - 1
 
- 查看表头
 
select *from S1
  
 - 1
 
– 查看数据库中自己建立的表
select table_name from stu_db.information_schema.TABLES where TABLE_TYPE='base table'
select *from information_schema.tables 
  
 - 1
 - 2
 
- sql 插入完整的行
 
insert into S values('201215121','李勇','男','20','CS')
insert into S(sno,sname,ssex,sage,sdept) values ('2012115122','刘晨','女','19','CS')
select *from S
  
 - 1
 - 2
 - 3
 
- sql 一次插入多条数据
 
写法一:
insert into S values ('201215123','王敏','女','18','MA'),('201215125','张立','男','19','IS')
写法二:
insert into S select '201215124','刘妮','女','18','DS'
union all select '201215128','何宝','男','21','IS'
union all select '201215126','平凡','男','24','IS'
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 
数据库简单查询和连接查询实验3
select *from information_schema.tables
select *from C
insert into C(cno,cname,cpno,credit) select '1','数据库','5','4'
union all select '2','数学','null','2'
union all select '3','信息系统','1','4'
union all select '4','操作系统','6','3'
union all select '5','数据结构','7','4'
union all select '6','数据处理','null','2'
union all select '7','C语言','6','4'
select *from SC
insert into SC values ('201215124','3','null')
insert into SC values ('201215124','2','100'),('201215124','1','99'),('201215128','4','100'),('201215126','3','88')
select *from S
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 
- 求数学系学生的学号和姓名
 
insert into S values('201215129','小何子','男','24','数学'),('201215120','嘻嘻','女','22','英语')
insert into S values('201215132','张三丰','男','100','计算机系')
select sno,sname from S where sdept='数学'
  
 - 1
 - 2
 - 3
 - 4
 
- 求选修了课程的学生学号
 
select distinct sno from SC 
  
 - 1
 
- 求选修课程号为‘C1’的学生号和成绩, 并要求对查询结果按成绩的降序排列,如果成绩相同按学号的升序排列。
 
select sno,grage from SC where cno='1' order by grage desc,sno
  
 - 1
 - 2
 
- 求选修课程号为‘C1’且成绩在80~100之间的学生学号和成绩,并成绩乘以0.8输出
 
select sno 学号,grage*0.8 成绩 from SC where cno='1' and grage between 80 and 100
  
 - 1
 - 2
 
- 求数学系或计算机系姓张的学生的信息
 
select *from S  where  sname like '张%' and sdept in ('数学','计算机')
  
 - 1
 - 2
 
- 求缺少了成绩的学生的学号和课程号
 
select sno,cno from SC where grage is null
  
 - 1
 
- 求缺少了成绩的学生的学号和课程号
 
select sno,cno from SC where grage is null
  
 - 1
 
- 查询每个学生的情况以及他所选修的课程
 
select S.*,C.*,grage from S left join SC on S.sno=SC.sno left join C on C.cno=SC.cno
  
 - 1
 
- 求学生的学号、姓名、选修的课程及成绩
 
select S.sno,s.sname,C.cname,SC.grage from S 
left join SC on S.sno=SC.cno
left join C on C.cno=SC.cno
  
 - 1
 - 2
 - 3
 
- 求选修课程号为‘C1’且成绩在90以上的学生学号、姓名和成绩
 
select S.sno,S.sname,SC.grage
from S,SC,C
where S.sno=SC.sno and C.cno=sc.cno and C.cno='1' and grage>90
  
 - 1
 - 2
 - 3
 - 4
 
- 查询每一门课程的间接先行课(即先行课的先行课)
 
select C1.cno,C2.cpno from C C1,C C2 where C1.cpno=C2.cno
select *from C
select *from S
select *from SC
  
 - 1
 - 2
 - 3
 - 4
 
文章来源: rivers.blog.csdn.net,作者:宝山的博客,版权归原作者所有,如需转载,请联系作者。
原文链接:rivers.blog.csdn.net/article/details/115619841
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)