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. spring-boot 定时任务案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 pom.xml配置代码: <?xml versio ...

  2. php rtrim()函数 语法

    php rtrim()函数 语法 rtrim()函数怎么用? php rtrim()函数用于删除字符串右边的空格或其他预定义字符,语法是rtrim(string,charlist),返回经过charl ...

  3. 安装并配置前端自动化工具-gulp

    由于现在前端自动化已经很有必要了,所以我今天死皮烂脸的找了2位前端大咖帮助我安装和配置gulp,讲真,这一步步弄下来直到安装配置成功,到现在还是迷迷糊糊,不过我还是把这些步骤给记录下来,以防下次不记得 ...

  4. 超全Altium Designer16 总结--Altium Designer

    原址:http://blog.csdn.net/qq_29350001/article/details/52199356 以前是使用DXP2004来画图的,后来转行.想来已经有一年半的时间没有画过了. ...

  5. 【Linux】ssh执行远程命令awk 参数报错问题

    ssh  ip    sudo docker ps -a | grep none | awk '{print \$1}'| sed 's/%//g' $1前面加上转移符就好

  6. 3 August

    P1013 进制位 结论:加法必为 \(n-1\) 进制:\({(n-1)}^1\) 位必为数字 1:\(0+0=0\). 模拟.字符串. #include <cstdio> #inclu ...

  7. ibatis 的使用

    1. 文本的使用 select  ‘day’+Num from Table;//Sql select convert(varchar,'day')+Num from Table;//ibatis

  8. Windows-菜单太长点不到

    显示设置里显示器方向调整成纵向

  9. Laex/Delphi-OpenCV

    https://github.com/Laex/Delphi-OpenCV 66 Star119 Fork75 Laex/Delphi-OpenCV CodeIssues 3Pull requests ...

  10. python改变导入模块中的变量的问题

    Python中循环函数1调用函数2,函数2中import了一个模块,函数1: def run(): for a in b: runTest(a,patter) 函数2: def runTest(cas ...