drop table class
create table class (
classId nchar(6) primary key not null,
cName nvarchar(20)
)
insert into class
values('','自动化1班')
insert into class
values('','计算机1班'),
('','计算机2班'),
('','自动化2班') select * from class delete class where classId=''
delete class where classId=''
delete class where classId=''
delete class where classId='' update class set cName='自动化3班'
where classId=09034 alter table student
alter column classId nchar(6)not null alter table student --给student的classId属性增加一个外键
add constraint classId foreign key(classId) references class(classId) alter table student --删掉该外键约束
drop constraint classId --然后才能删掉class
drop table class select * from course
select * from score
select * from student
select * from teacher --检查学号为107的学生的课程名,任课老师
select ci.cName,ti.teaName from student as si
inner join score as sco on sco.stuId=si.stuId
inner join course as ci on ci.cId=sco.cId
inner join teacher as ti on ti.teaId=ci.teaId
where si.stuId=107 --检索王同学不学习且不助教的任课老师和课程名
select ti.teaName,ci.cName from course as ci
inner join teacher as ti on ti.teaId=ci.teaId
except (
--王同学学习的课程及任课老师
select ti.teaName,ci.cName from student as si
inner join score as sco on sco.stuId=si.stuId
inner join course as ci on ci.cId=sco.cId
inner join teacher as ti on ci.teaId=ti.teaId
where si.stuName like '王%'
union
--王同学助教的任课老师的课
select ti.teaName,ci.cName from course as ci
inner join teacher as ti on ci.teaId=ti.teaId
where ti.teaWork='助教' and ti.teaName like '王%'
) --检索至少选修两门课程的学生学号
select sco.stuId,COUNT(*) from score as sco
group by sco.stuId having COUNT(*)>1 --子查询
--按学号列出每个学生所选修课程中最高分的课程名称及其分数
select si.stuId,ci.cName,sco.score
from student as si
inner join score as sco on sco.stuId=si.stuId
inner join course as ci on ci.cId=sco.cId
inner join teacher as ti on ci.teaId=ti.teaId
where sco.score>=
(
select MAX(ssco.score) from student as ssi
inner join score as ssco on ssco.stuId=ssi.stuId
inner join course as cci on cci.cId=ssco.cId
inner join teacher as tti on cci.teaId=tti.teaId
where ssi.stuName=si.stuName
)
order by si.stuId asc

sql server 基础语法4 实践练习+子查询的更多相关文章

  1. Sql Server 基础语法

    来自:http://www.cnblogs.com/AaronYang/archive/2012/04/24/2468093.html Sql Server 基础语法 -- 查看数据表 select  ...

  2. [SQL] SQL SERVER基础语法

    Struct Query Language 1.3NF a.原子性 b.不能数据冗余 c.引用其他表的主键 2.约束 a.非空约束 b.主键约束 c.唯一约束 d.默认约束 e.检查约束 f.外键约束 ...

  3. 九、Sql Server 基础培训《进度9-复杂查询练习》(实际操作)

    知识点: 复杂查询1:统计全校有多少个男生.有多少个女生? 写法1(分组): select sex as 性别,count(*) as 人数 from student group by sex 写法2 ...

  4. SQL夯实基础(四):子查询及sql优化案例

    首先我们先明确一下sql语句的执行顺序,如下有前至后执行: (1)from  (2) on   (3) join  (4) where  (5)group by  (6) avg,sum...  (7 ...

  5. sql server 基础语法2

    别名,选择,查询,排序,去重,筛选 select * from UserInfo as ui --起别名 select UserName,UserPwd --指定选择的列 from UserInfo ...

  6. SQL Server基础知识

    1.SQL Server表名为什么要加方括号? 这个不是必须要加,但表名或字段名如果引用了sqlserver中的关键字,数据库会不识别这到底是关键字还是表名(或字段名)时就必须要加. 比如,一个表名叫 ...

  7. SQL SERVER With语法[转]

    今天在论坛上看到一个举例,关于sql server 的示例.1/25/50/100美分,多少种可能拼凑成2美元. 看了其中第一条语法,放在SQL SERVER中测试,发现真的列举出所有组合成2美元的方 ...

  8. SQL server存储过程语法及实例(转)

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...

  9. SQL server基础知识(表操作、数据约束、多表链接查询)

    SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...

随机推荐

  1. 【Dart学习】--之Iterable相关方法总结

    一,概述 按顺序访问的值或元素的集合, List集合也是继承于Iterable List和Set也是Iterable,dart:collection库中同样有很多 部分Iterable集合可以被修改 ...

  2. 【Elasticsearch】ElasticSearch基本查询

    学习elasticsearch查询用法的时候,发现这篇文章写得很详细,为以后方便查看,就直接搬过来了,原文链接在下面. 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...

  3. 【MySQL】实现自增函数sequence

    前言 当前数据库为:mysql由于mysql和oracle不太一样,不支持直接的sequence,所以需要创建一张table来模拟sequence的功能,理由sql语句如下: 步骤 1.创建seque ...

  4. Delphi读取和写入utf-8编码格式的文件

    读取UTF-8格式的文件内容 function LoadUTF8File(AFileName: string): string; var ffileStream:TFileStream; fAnsiB ...

  5. shell(计算机壳层)(二)

    shell 命令常用命令cat 文件名 输出文件内容到基本输出(屏幕 or 加>fileName 到另一个文件)cb 格式化源代码chmod //change mode,改变文件的权限cp co ...

  6. this.$nextTick 与window.setTimeout

    两个都可以设置运行先后.前者,方式: this.$nextTick(() => { this.$refs.orgAddOrUpdate.init(row, isAdd) }) 其中orgAddO ...

  7. selenium问题之定位不到元素(NoSuchElementException)

    在使用selenium+爬虫的时候,经常会遇到一个问题,就是NoSuchElementException,定位不到元素的问题 一,打开了新页面,一般selenium点击新链接跳转打开了一个新页面,那么 ...

  8. 执行hbase zkcli命令报错

    执行hbase zkcli后报错信息如下: 15/10/02 15:17:55 INFO zookeeper.ZooKeeper: Client environment:java.library.pa ...

  9. node js 操作redis promise

    连接 redis = require('redis') var client = redis.createClient('6379', '127.0.0.1'); client.on('connect ...

  10. 详解linux io flush

    通过本文你会清楚知道 fsync().fdatasync().sync().O_DIRECT.O_SYNC.REQ_PREFLUSH.REQ_FUA的区别和作用. fsync() fdatasync( ...