SQL SERVER 2005之表的操作 -赵文斌
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> ADP及SQL SERVER


SQL SERVER 2005之表的操作

发表时间:2011/11/17 17:21:35 评论(0) 浏览(5148)  评论 | 加入收藏 | 复制
   
摘 要:共享
正 文:

SQL SERVER 2005之表的操作

分类: Microsoft SQL Server 2000 9人阅读 评论(0) 收藏 举报

数据的添加、删除和修改
insert into 表名 values ()
update 表名 set 列名 = 列名值 where
delete 表名  where

查询 (select)

输出表中的所有列
select * from 表名

去掉重复行
select distinct  列名 from 表名

限制返回的行数
select top n * from 表名

常用的查询条件
比较大小
select * form 表名  where 表名> 20   !=20

确定范围
select * from 表名  where 列名  between 10 and 20

确定集合 利用  IN   NOT IN
select * from 表名  where 列名 in (2,5,6,4,7,8,9)  在此范围内

模糊查询  like   %:w任意长度的字符   _:单个字符
select * from 表名  where  列名 like '%dd_dd'

涉及空值的查询
select * from 表名 where not 列名   is null  查询列名不为空的列

用查询结果生成新表

select * into new table from 表  将查询的所有字段放入到new table 里
select 列1,列2,列3..... into new table from 表 将指定列查询到的结果放入new table里

如果表名前加#号表示查询的表为临时表
select 列1,列2,列3..... into #new table from 表   结果存放在temp库里

对结果进行分组 
select * from 表  group by 列名  having 列名<expression>   按照列名相同的分成一组

升序
select * from 表  order by 列名   asc
 
降序
select * form 表 order by 列名  desc

对数据进行统计
数据表操作要对结果集进行统计,例如求和、平均值、最小值、个数
select count(*) as 别名 from 表  返回整数
select count(列名) as 别名 from 表

统计总和
select sum(列名) as 别名  from 表

最大值
select max(列名) as 别名 from 表名

最小值
select min(列名) as 别名 from 表名

 平均值
 select avg(列名) as 别名 from 表名
 
 
 连接查询
 
 交叉连接:把A 表中的所有数据 ,跟B表中的每一条数据进行
 拼接,从而形成了新的数据集。形成的新表的数据记录数为 A*B
 select 表1.列1,表1.列2, 表2.列1,表2.列2 from  表1
 cross join 表2  where 表1.列 = 表2.列
 
 自然连接:
select 列表列名 from 表名1 join 表名2 on 表名.列名=表名2.列名
 
自身连接查询:连接操作既可以在多表之间进行,也可以是一个表与其自己进行连接,称为表的自身连接,但必须为表指定
两个别名以示区分
select  a.name,b.sex from table_name as a join table_name
as b on a.name= b.name

外连接查询:其结果集不但包含满足连接条件的行,还包括相应表中的所有行,即使 某些行不满足条件,但仍输出改行记录
有三种方式
左外连接:按照左表列输出
select * from table1 left join table2
    on table1.column1=table2.column1
右外连接:按照右表列输出
select * from table1 right join table2
    on table1.column1=table2.column1
完全外连接:把两表列全输出
select * from table1 full join table2
    on table1.column1=table2.column1

合并结果集:数据的列要相同、数据类型也要相同
select column1,column2 from table1
union
select column1,column2 from table2


Access软件网交流QQ群(群号:198465573)
 
 相关文章
SQL SERVER 2005之表的操作二  【赵文斌  2011/11/17】
安装完sql server 2005后,修改了机器名后造成发布、订...  【赵文斌  2012/5/4】
SQL Server 2005 控制用户权限访问表  【风行  2012/8/7】
SQL server 2005数据库的备份与还原  【缪炜  2012/9/20】
SQL Server 2005基础教程(PDF文档/电子书)  【周芳  2012/12/15】
常见问答
技术分类
相关资源
文章搜索
关于作者

赵文斌

文章分类

文章存档

友情链接