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 ...
随机推荐
- session 存入 redis
<?php header('content-type:text/html;charset=utf-8'); /* * 更改 session 存储位置及存储方式. */ ini_set('sess ...
- 记intel杯比赛中各种bug与debug【其一】:安装intel caffe
因为intel杯创新软件比赛过程中,并没有任何记录.现在用一点时间把全过程重演一次用作记录. 学习 pytorch 一段时间后,intel比赛突然不让用 pytoch 了,于是打算转战intel ca ...
- 【Henu ACM Round#19 F】Dispute
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这一题和这一题很像 (链接 ) 会发现如果a[i]!=b[i]那么就按下i就好了. 然后改变和他相邻的点. 此后a[i]再也不可能和 ...
- ZooKeeper 配置注意事项 zoo.cfg
一 平台 二 软件环境 1) JDK 1.6 以上 (最好1.7 Hadoop 某一项安装时候需要 1.7) 2) 至少 3 个节点 (2m +1 ...
- Hive Cilent数据操作
Hive运行命令方式有cli,jdbc.hwi.beeline.而我们经常使用的往往是cli shell 操作. cli shell hive -help hive --help 注:命令脚本必须在集 ...
- 为OLED屏添加GUI支持2:2D图形库
为OLED屏添加GUI支持2:2D图形库 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:MDK5.13 MCU:S ...
- Please ensure that adb is correctly located at 'D:\Android\android-sdk\platform-tools\adb.exe' and
1.启动任务管理器 2.找到百度安全组件杀掉进程. 3.一般都是组件给禁止了.
- thinkphp5项目--企业单车网站(六)
thinkphp5项目--企业单车网站(六) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...
- 分贝(dB)的理解
分贝(dB,decibels)表达的是功率比(power ratio,P2/P1),而不是一个amount,P2>P1,分贝为正值,否则为负值.分贝是对数形式的,而不是线性形式的,也即 20 d ...
- input[type=date]
::-webkit-datetime-edit – 控制编辑区域的 ::-webkit-datetime-edit-fields-wrapper – 控制年月日这个区域的 ::-webkit-date ...