create DATABASE 教务管理系统
on (
name=教务管理系统_data,
filename='E:\sql\教务管理系统_data.mdf ',
size=5mb,
maxsize=20mb

)
log on(

name=教务管理系统_log,
filename='E:\sql\教务管理系统_log.ldf',
size=2mb,
maxsize=10mb

)

create database myTest
on (
name=myTest_data,
filename='E:\sql\myTest_data.mdf ',
size=5mb,
maxsize=20mb

)
log on(

name=myTest_log,
filename='E:\sql\myTest_log.ldf',
size=2mb,
maxsize=10mb

)

<创建数据库快照》
create database student_info
on
(name=教务管理系统_data,filename='E:\sql\教务管理系统_data_snop.mdf '),
(name=shuju,filename='E:\sql\教务管理系统_data_snop2.mdf ')

as snapshot of 教务管理系统

《/创建数据库快照>

《还原数据库》
Restore database 教务管理系统
from database_snapshot ='student_info'

《?还原数据库》

《创建表>
use mysql
create table meng
(userNo int identity not null,
username nvarchar(10) null,
pohoto image null,
birthday dateTime null
)

use myTest
create table meng
(userNo int identity not null,
username nvarchar(10) null,
pohoto image null,
birthday dateTime null
)

</创建表>

<创建临时表>

<创建本地临时表>
create table #tempLocal
(
name1 varchar null,
age int null

)

可查询
select * from ##tempLocal

</创建本地临时表>

<创建全局临时表>
create table #tempLocal
(
name1 varchar null,
age int null

)

可查询
select * from ##tempLocal

</创建全局临时表>

</创建临时表>

将列设为标识字段将不需要插入该列,系统会自动生成记录,通过管理器设置
<创建表>
create table mytest2
(
id int primary key identity(3,1),--主键,自动+1 ,初始值为3
name varchar(20) unique not null,--不允许重复,不允许为空
Age tinyint,
notetime smalldatetime default getdate()
)
</创建表>
<更改数据表的结构>
alter table mytest2

alter COLUMN [notetime] smalldatetime not null

修改字段:
ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL

重命名表:(Access 重命名表,请参考文章:在Access数据库中重命名表)
sp_rename '表名', '新表名', 'OBJECT'

</更改数据表的结构>

<对数据表的重命名>
sp_rename 'mytest2', 'mytest'
</对数据表的重命名>

<数据表中增加字段>
alter table mytest2
add sex nvarchar(5) null
</数据表中增加字段>

<增加数据类型>
表->可编程性-》类型->用户自定义数据类型
</增加数据类型>

<三张表的创建>
1.
create table classInfo
(
className nvarchar(20) null

)

2.
create table GradeInfo
(
stuNo nvarchar(20) primary key not null,
id int identity(1,1),
chineseGrade int null,
java int null,
math int null,
english int null

)

3.
create table stuInfo2
(
stuNo nvarchar(20) primary key not null,
id int identity(1,1),
username nvarchar(20) null,
sex varchar(5) null,
birthday date null,
class nvarchar(20) null

)

</三张表的创建>

<select>

select * from tablename where a and b or c
select * from tablename where name like'x%'通配符以x开头
select * from tablename where name like '%x'

<排序>
SELECT * from myTest.dbo.mytest2 order by Age desc

SELECT * from myTest.dbo.mytest2 order by Age asc,id desc 先按照年龄按升序,年龄相同然后以id降序排列
SELECT * from myTest.dbo.mytest2 order by Age ,id desc 先按照年龄按降序,年龄相同然后以id降序排列
</排序>

select stuNo , max(english),AVG(java) from myTest.dbo.GradeInfo group by rollup (java,stuNo ,id ) having java>90 order by id desc

select * from stuInfo where class in('软工11001班','软工11002班')

</select>

<group by>
select AVG(Age),name from myTest.dbo.mytest2 group by rollup (name)
</group by>

<having>
select max(Age),AVG(Age) ,name from myTest.dbo.mytest2 group by rollup (name,Age) having Age>20
</having>

<where>
where 后+集合函数 是先where 后集合,所以where AVG(COLNUM)是错的,where 后分组的的,先where后统计
</where>

<grouping>
获取产生的空值
select max(Age) ,GROUPING(Age) S1 from myTest.dbo.mytest2 group by rollup (Age )
</grouping>

<compute >
use myTest

select username,stuNo from stuInfo group by username,stuNo compute count(username )
</compute>

<内连接查询 内连接把两个表中的数据连接生成第三个表,在第三个表中仅包含那些满足连接条件的数据行。
在连接中使用inner join 连接运算符,如果在Join关键字前面没明确指定连接类型,那么默认的连接类型是内连接>

use myTest

select a.username as'姓名' from stuInfo a inner join GradeInfo b on a.stuNo=b.stuNo on b.java >85

</内连接查询>

<外连接查询 在外连接中,不仅包括那些满足的条件的数据,而且某些不满足条件的数据也会显示在结果集中。
也就是说,外连接中只限制其中一个表的数据行,而不限制另外一个表中的数据,有三种方式:
left outer join --左连接是对连接条件中左边的表不加限制
right outer join--右连接是对连接条件中右边的表不加限制
full outer join--全连接是对连接条件中左右两边边的表不加限制

>
<left outer join>
select a.username as'姓名' ,b.stuNo as '成绩学号' from stuInfo a left outer join GradeInfo b on a.stuNo=b.stuNo and b.java >85

</left outer join>

<right outer join>
select a.username as'姓名' ,b.stuNo as '成绩学号' from stuInfo a left outer join GradeInfo b on a.stuNo=b.stuNo and b.java >85

</right outer join>

<full outer join>
select a.username as'姓名' ,b.stuNo as '成绩学号' from stuInfo a full outer join GradeInfo b on a.stuNo=b.stuNo and b.java >85

</full outer join>

</外连接查询>

<子查询>

select username from stuInfo where class=(
select className from classInfo where className='软工11002班' //clasa后面是'=',所以子查询(select className
from classInfo where className='软工11002班')
返回的必须是一个值,不能是多个值,还有>,<也是
)

select username from stuInfo where class in(
select className from classInfo //in后面是个集合
)

<exists 如果内层的查询结果不为空,则外层的where 的子条件为ture 即第一个where查询的条件为真,相当于select class from stuInfo,否则第一个where查询的条件为假>

select class from stuInfo where exists(
select className from classInfo
)

select stuNo from stuInfo where exists(
select * from GradeInfo
)

</exists>

</子查询>

<联合查询>
<存储查询结果>
select class into class from stuInfo where username='孟志军'
</存储查询结果>

<if...else begin...end>

if(select AVG(java) from GradeInfo)>80

begin
print 'Bucuo'
print '不错哦'

end
else
if exists(select * from classInfo where className='软工11003班')
print '后来--Bucuo'
print '懂得--不错哦'

</if...else begin...end>

<case ..when 返回第一个为true的表达式(若为true将不继续判断),若没有一个表达式为真则返回Null>

select username ,
case class
when '软工11001班' then 'uername:软工1001班'
when '软工11002班' then 'uername:软工1002班'
when '软工11003班' then 'uername:软工1003班'
end as '班级'
from stuInfo

select stuNo ,
case
when AVG(english)>80 then 'uername:软工1001班'
when AVG(math)>80then 'uername:软工1002班'
when AVG(chineseGrade)>80 then 'uername:软工1003班'
end as '班级'
from GradeInfo
group by stuNo

</case ..when>

<cast>
select cast (count(java) as nvarchar(10)) meng ,
CAST(AVG(english) as int) english2

from GradeInfo
</cast>

<over>

select max(java) max_java ,rank() over (order by math) meng_rowNow ,ROW_NUMBER() over (order by math) wo from GradeInfo group by math

select * from(
select max(java) max_java ,rank() over (order by math) meng_rowNow ,ROW_NUMBER() over (order by math) wo
from GradeInfo group by math
)hahh where meng_rowNow=3 and hahh.max_java=(select MAX(java) from GradeInfo)

select * from(
select className , ROW_NUMBER() over(order by className) r from classInfo )t
where t.r>0
<over>

</联合查询>

<insert ...select>
insert into classInfo (className) select className FROM classInfo where className is not null
</insert...select>

<delete 删除最后十行>
delete top(10) classInfo from (
select * from(
select className , max(ROW_NUMBER() over(order by className )) r from classInfo )t
where t.r>0
)z where z.r>10
<delete>

follow me learning sqlserver transql的更多相关文章

  1. 【转】The most comprehensive Data Science learning plan for 2017

    I joined Analytics Vidhya as an intern last summer. I had no clue what was in store for me. I had be ...

  2. 【深度学习Deep Learning】资料大全

    最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books  by Yoshua Bengio, Ian Goodfellow and Aaron C ...

  3. Machine Learning

    Recently, I am studying Maching Learning which is our course. My English is not good but this course ...

  4. (转) Awesome - Most Cited Deep Learning Papers

    转自:https://github.com/terryum/awesome-deep-learning-papers Awesome - Most Cited Deep Learning Papers ...

  5. (转) The major advancements in Deep Learning in 2016

    The major advancements in Deep Learning in 2016 Pablo Tue, Dec 6, 2016 in MACHINE LEARNING DEEP LEAR ...

  6. (转) Deep Learning Research Review Week 2: Reinforcement Learning

      Deep Learning Research Review Week 2: Reinforcement Learning 转载自: https://adeshpande3.github.io/ad ...

  7. 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)

    Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...

  8. Deep Learning in a Nutshell: History and Training

    Deep Learning in a Nutshell: History and Training This series of blog posts aims to provide an intui ...

  9. Deep Learning 9_深度学习UFLDL教程:linear decoder_exercise(斯坦福大学深度学习教程)

    前言 实验内容:Exercise:Learning color features with Sparse Autoencoders.即:利用线性解码器,从100000张8*8的RGB图像块中提取颜色特 ...

随机推荐

  1. [课程设计]Scrum 1.5 多鱼点餐系统开发进度(点餐页面框架修复及继续布置)

    Scrum 1.5 多鱼点餐系统开发进度(点餐页面框架修复及继续布置)  1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅 ...

  2. [课程设计]Scrum 2.3 多鱼点餐系统开发进度 (订单一览设计)

    Scrum 2.3 多鱼点餐系统开发进度  (订单一览设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统 ...

  3. IntelliJ IDEA14 配置 SVN

    最新升级IDEA13到14版本,升级后发现IDEA中SVN无法正常使用,但文件夹下能够正常使用. 并且报错:svn: E204899: Cannot run program "svn&quo ...

  4. Android中插件开发篇之----动态加载Activity(免安装运行程序)

    一.前言 又到周末了,时间过的很快,今天我们来看一下Android中插件开发篇的最后一篇文章的内容:动态加载Activity(免安装运行程序),在上一篇文章中说道了,如何动态加载资源(应用换肤原理解析 ...

  5. Android 编译时注解解析框架

    2.注解 说道注解,竟然还有各种分类,得,这记不住,我们从注解的作用来反推其分类,帮助大家记忆,然后举例强化大家的记忆,话说注解的作用: 1.标记一些信息,这么说可能太抽象,那么我说,你见过@Over ...

  6. ArcGIS Javascript查询数据库并添加到地图上

    将数据存放到数据库中,动态的调取比较灵活,数据变动后不需要改变图层的属性表. 此处采用的方法是通过jquery查询数据库,并将数据库的结果生产json串返回给js,在js中动态解析json串增加点至地 ...

  7. [问题2014A06] 复旦高等代数 I(14级)每周一题(第八教学周)

    [问题2014A06]  若 \(n\) 阶实方阵 \(A\) 满足 \(AA'=I_n\), 则称为正交矩阵. 证明: 不存在 \(n\) 阶正交矩阵 \(A,B\) 满足 \(A^2=cAB+B^ ...

  8. EXCEL计算数字、汉字、英文单元格的计数

    1.数字COUNT(A1:A100)2.汉字{=SUMPRODUCT(IF(LEN(A1:A100)LENB(A1:A100),1,0)*1)}3.英文{=SUMPRODUCT(IF(ISTEXT(A ...

  9. Python 基础练习

    今天接触了python,了解了一下 python 的基础语法,于是想着手训练一下,在本习题集中,参考代码为提供的参考答案,前面的代码为自己思考的代码,最后每道题给出练习的时间. Python 基础练习 ...

  10. JavaWeb学习总结_Servlet开发

    一. Servlet简介 二.Servlet的运行过程 Servlet程序是由Web服务器调用,web服务器收到客户端的Servlet访问请求后: WEB服务器首先检查是否已经装载并创建了该Servl ...