Sql Server 基础语法
来自: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 基础语法的更多相关文章
- [SQL] SQL SERVER基础语法
Struct Query Language 1.3NF a.原子性 b.不能数据冗余 c.引用其他表的主键 2.约束 a.非空约束 b.主键约束 c.唯一约束 d.默认约束 e.检查约束 f.外键约束 ...
- sql server 基础语法4 实践练习+子查询
drop table class create table class ( classId ) primary key not null, cName ) ) insert into class ', ...
- sql server 基础语法2
别名,选择,查询,排序,去重,筛选 select * from UserInfo as ui --起别名 select UserName,UserPwd --指定选择的列 from UserInfo ...
- SQL Server基础知识
1.SQL Server表名为什么要加方括号? 这个不是必须要加,但表名或字段名如果引用了sqlserver中的关键字,数据库会不识别这到底是关键字还是表名(或字段名)时就必须要加. 比如,一个表名叫 ...
- SQL server存储过程语法及实例(转)
存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...
- SQL server基础知识(表操作、数据约束、多表链接查询)
SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...
- 数据库开发基础-SQl Server 基础
SQL Server 基础 1.什么是SQL Server SQL:Structured Query Language 结构化查询语言 SQL Server是一个以客户/服务器(c/s)模式访问.使 ...
- 【SQL Server】SQL Server基础之存储过程
SQL Server基础之存储过程 阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...
- Sql Server 基础知识
Sql Server 基础知识: http://blog.csdn.net/t6786780/article/details/4525652 Sql Server 语句大全: http://www.c ...
随机推荐
- dedecms4张关键表解析之1
虽然dedecms默认共有87张表,但是只有4张最核心,最最要的表. 1.第一张表:dede_arctype 栏目表 dede设计者认为不管存放什么样的数据(文章,商品,电影)都应该属于某个栏目(类 ...
- LeetCode(6)ZigZag Conversion
题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...
- php八大设计模式之单例模式
单例模式的好处: 实例化后只得到一个对象,减少内存的开销. 实现单例模式: 提供一个私有的属性用来存储实例后的对象. 禁止外部实例化对象,提供公共的的方法,返回实例化后的对象. 避免继承此类,然后重写 ...
- man帮助
man命令是Linux下的帮助指令,通过man指令可以查看Linux中的指令帮助.配置文件帮助和编程帮助等信息.
- [TJOI2017]城市(树的直径)
[TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达, ...
- MapReduce JOB 的输出与输出笔记。
提高 MapReduce 价值,自定义输入和输出. 比如跳过存储到 HDFS 中这个耗时的布置. 而只是从原始数据源接受数据,或者直接将数据发送给某些处理程序. 这些处理程序在 MapReduce 作 ...
- Gradle编译spring3.x报错找不到itextpdf4.2.2解决方案
google搜到一篇文章:http://www.bdtool.net/blog_356.html 试了文章里的两个方法,方法一不行,方法二有点搞头,但是还有些错.试着试着,突然成功了~ 我是这么做的 ...
- swift学习之数组
首先数组的定义:以有序的方式存储同样类型的值 (1)数组的简写(shorthand)语法 你能够通过Array<Element>,在这里,Element时数组存储元素的值的类型.也能够通过 ...
- javascript模拟类的最佳实践
1:怎样模拟一个类 在sencha touch2 系列里面定义一个类和new出这个类的对象 Ext.define( "Animal", { config: { name: null ...
- iOS学习9_事件分发&响应链
iOS的三种事件:触摸事件/运动事件/远程控制事件 typedef enum { UIEventTypeTouches, UIEventTypeMotion, UIEventTypeRemoteCon ...