来自:http://www.cnblogs.com/AaronYang/archive/2012/04/24/2468093.html

Sql Server 基础语法

-- 查看数据表

select * from Student

-- 使用Sql查询数据

--1、查询表中所有类容

Select * From TableName

--2、查询表中指定字段类容

Select ColumnName,…, From TableName

Select stuName,stuNo,stuSex From stuInfo

--3、带Where条件的查询

Select *|ColumnName From TableName Where condition

select * From stuInfo where stuSex='女'

--4、带排序的查询(Order By ColumnName)

-- 语法: Select *|ColumnName From TableName Order By ColumnName Desc|Asc

Select * From stuInfo Order By stuAge,stuSeat desc

-- 5、选择指定数量的记录,通常配合order By使用

-- 语法: Select Top num *|ColumnName From TableName

-- 语法: Select Top num *|ColumnName From TableName Order By Desc|Asc

Select Top 2 * From stuInfo Order By stuAge Desc

-- 6、分组查询 Group By

-- 分组查询中包含的列必须包含在聚合函数或 GROUP BY 子句中

Select * From stuInfo

Select stuSex, Max(stuAge) As '平均年龄' From stuInfo Group By stuSex

--7、对分组后的结果进行过滤

-- having(相当于Where)

Select * From stuInfo

Select stuSex, Avg(stuAge) As '平均年龄' From stuInfo Group By stuSex having Avg(stuAge)>20

--8、Group By 配合 Where 使用

Select * From stuInfo

Select stuSex, Avg(stuAge) As '平均年龄' From stuInfo where stuAge > 18 Group By stuSex having Avg(stuAge)>20

-----------------------------------------------------------

--使用Sql插入数据

--1、不指定列插入数据

语法:Insert Into TableName Values(值列表)

Insert Into stuInfo Values('小八','S25311','男',24,'北京')

Select * from stuInfo

-- 2、指定列名对数据插入

--语法:Insert Into TableName(列名列表) Values(值列表)

--注意:列名列表顺序可自己指定,但值列表的顺序应该和列名列表相同。

Insert Into stuInfo(stuName,stuNo,stuSex,stuAddress,stuAge) Values('小九','S25312','男','上海',25)

select * from stuInfo

--3、一次插入多条记录

--1) Insert Into TableName(列名类表) Select…From  插入到现存的表中

--注意:列名的数据类型,个数必须相同

Insert Into stuInfoCopy(stuName,stuNo,stuSex,stuAge,stuSeat,stuAddress)

Select * From stuInfo

Select * From stuInfoCopy

--3、一次插入多条记录

--2) Select 列名列表 Into 新表名 From SourceTable 插入到现存的表中

--注意:列名的数据类型,个数必须相同,新表必须不存在

Select Identity(int,1,1) As 'ID',stuName,stuNo,stuSex,stuAge,stuAddress

Into #temp

From stuInfo

select * from #temp

--3、一次插入多行记录

--3)使用Union合并数据行

Insert #temp(stuName,stuNo,stuSex,stuAge,stuAddress)

Select '宝贝','S25318','男',22,'湖北' Union

Select '宝贝2','S25318','女',23,'湖南'

select * from #temp

--4、更改数据

--语法: Update TableName Set ColumnName=值 where Condititon

Update #temp Set stuName = '宝贝3' Where stuName = '宝贝'

Select * from #temp

--5删除数据

-- 语法: Delete From TableName Where Condition

Delete From #temp Where Id=8

Select * From #temp

--5删除数据

--语法: Truncate Table TableName(在删除表中所有数据时,比Delete效率高,但不能

--删除包含外键约束的表

Truncate Table stuMarks

-- Where 条件种类

--1、ColumnName Between 低值 And 高值

Select * from stuInfo Where stuAge Between 20 And 25

--2、And Or Not(与,或,非)

--3、In(值列表)

Select * from stuInfo Where stuAge IN (21,25)

--4、Like(模糊查询)

-- % 表示任意数量字符 _ 一个字符 [] 一个范围 [^]不在某个范围

Select * from stuInfo Where stuName like '小%'

Sql Server 基础语法的更多相关文章

  1. [SQL] SQL SERVER基础语法

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

  2. sql server 基础语法4 实践练习+子查询

    drop table class create table class ( classId ) primary key not null, cName ) ) insert into class ', ...

  3. sql server 基础语法2

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

  4. SQL Server基础知识

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

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

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

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

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

  7. 数据库开发基础-SQl Server 基础

    SQL Server 基础 1.什么是SQL Server SQL:Structured Query Language  结构化查询语言 SQL Server是一个以客户/服务器(c/s)模式访问.使 ...

  8. 【SQL Server】SQL Server基础之存储过程

    SQL Server基础之存储过程  阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...

  9. Sql Server 基础知识

    Sql Server 基础知识: http://blog.csdn.net/t6786780/article/details/4525652 Sql Server 语句大全: http://www.c ...

随机推荐

  1. error C4996: 'setmode': The POSIX name for this item is deprecated解决方案

    在使用VS2012编译zlib库官方提供的案例程序 zpipe.c 中代码时报错: 信息如下: 错误 1 error C4996: 'setmode': The POSIX name for this ...

  2. Windows10通过VNC远程连接Ubuntu18.04

    1.打开终端输入:sudo apt-get install xrdp vnc4server xbase-clients dconf-editor 2.接着在终端输入: 进入到下面这个界面: 接着按照这 ...

  3. 20180929 北京大学 人工智能实践:Tensorflow笔记07

    (完)

  4. unity 5.6.1 Oculus手柄输入问题

    unity文档中提到 轴的 ID 是5和6,但是测试后发现,ID是6和7,很坑 void Update () { if (Input.GetKeyDown(KeyCode.JoystickButton ...

  5. 用C3P0建立server与数据库的连接

    1:在MyEclipse建立 Web Service Project 2:在project中建立servlets包 3:在包中新建Servlet文件(採用new Servlet方法可省去配置web.x ...

  6. 文件的默认权限:umask

    1. 文件的默认权限 linux下当我们新建一个文件和文件夹时,该文件和文件夹的默认权限是什么? 通过umask命令来查看: $ umask 0002 $ umask -S u=rwx,g=rwx,o ...

  7. shrio 身份认证流程-Realm

    身份认证流程 流程如下: 1.首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtils. setSecuri ...

  8. How Hystrix Works?--官方

    https://github.com/Netflix/Hystrix/wiki/How-it-Works Contents Flow Chart Circuit Breaker Isolation T ...

  9. codeforces 710C Magic Odd Square(构造或者n阶幻方)

    Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both ma ...

  10. 整个shuffle的流程图

    整个shuffle的流程图   Paste_Image.png Map Shuffle的作用以及相应的设置 partition 过程:输入的<key,value>对经过map()处理后输出 ...