您的位置:首页 > 数据库

第2部分 T-SQL 语法基础

2010-12-18 10:49 295 查看
一.SQL简介
1.SQL和T-SQL
SQL语言是1974年由,Boyce和Chamberlin提出来的,T-SQL是标准的SQL德加强版
2.T-SQL的组成
DML(数据操作语言:用来查询,插入,删除,和修改数据库中的数据,如Secect, Insert,Update,Delete)
DCL(数据控制语言:用来控制数据库组件的存取许可,存取权限)
DDL(数据定义语言:用来建立数据库,数据库对象和定义其列,大部分是以Create开头的命令)
二.使用T-SQL查询数据
1. 查询所有行列
use news_manage
select * from dbo.t_News
2. 查询部分行列
use news_manage
select newstitle,newsdate from dbo.t_News
where newstitle = '换车族"的三大类型'
use news_manage
select newstitle,newsdate from dbo.t_News
where newstitle <> '换车族"的三大类型'
3. 查询中使用列名
use news_manage
select newstitle as 标题,newsdate as 日期from dbo.t_News
where newstitle <> '换车族"的三大类型'
use news_manage
select newstitle + '来源于' +newsource as 标题from dbo.t_News
use news_manage
select 标题= newstitle + '来源于' + newsource from dbo.t_News
4. 查询空行
use news_manage
select newstitle,newscounent from dbo.t_News
where newsource is null
use news_manage
select newstitle,newscounent from dbo.t_News
where newsource is not null
5. 查询中使用常量
use news_manage
select newstitle as 新闻标题, '北大青鸟' as 新闻来源
from dbo.t_News
where newsource is null
6. 查询返回限制的行数
use news_manage
select top 3 newstitle as 新闻标题from dbo.t_News
use news_manage
select top 20 percent newstitle as 新闻标题from dbo.t_News
7.select单表查询
use news_manage
select * from dbo.t_Class
use news_manage
select classID,classname from dbo.t_Class
use news_manage
select * from dbo.t_News where hits>10
use news_manage
select * from dbo.t_News where hits between 10 and 12
use new_manage
select * from dbo.t_News
where newsource like '新浪%'
use news_manage
select * from dbo.t_News
where newsource like '腾讯%'
use news_manage
select newstitle,newsdate,hits from dbo.t_News
order by hits desc 点击从高到底查询
8.通过insert select语句将现有表中的数据添加到新表中
insert into newsmost (newstitle,newsdate,hits)
select newstitle,newsdate,hits
from dbo.t_News
where newsdate >cast('2008-06-01' as datetime)
通过select into语句将现有表中的数据添加到新表中
select newstitle,newsdate,hits
into newsmost21
from dbo.t_News
通过union关键字合并数据进行插入
insert dbo.newsmost
select '123','2009-03-06','100' union
select '456','2009-03-06','120' union
select newstitle,newsdate,hits
from dbo.newsmost1
union
select newstitle,newsdate,hits
from dbo.newsmost2
9.使用Update更新数据
use news_manage
update dbo.t_News set newsource='北大青鸟'
where newsource is null
use news_manage
update dbo.t_News
set hits='+100'
where newsource like '腾讯%'
use news_manage
update dbo.t_News_User
set userpassword='123'
where power='false'
use news_manage
update dbo.t_News_User
set userpassword='administratro123'
where power='true'
10 .使用Delete删除数据
use news_manage
delete from dbo.t_News
where newsdate = '2007-11-16 0:00:00'

use news_manage
delete from dbo.t_News
where newsource like '腾讯%'
use news_manage
truncate table dbo.t_Log


本文出自 “张书兴” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: