T-sql是标准的sql的加强版,并对sql命令做了许多扩充,提供类似于程序语言的基本功能。以下文章中出现的
该语言是由DML,DCL,DDL语言构成:
DML:数据操作语言(用来查询,插入,删除和修改数据库中的数据,如:select,insert,update及delete等)
DCL:数据控制语言(用来控制数据库组件的存取许可,存取权限等,如:grant,revoke等)
DDL:数据定义语言(用来建立数据库,数据库对象和定义其列,大部分是create table,create view,及drop table等)
表达式
1.条件表达式
1.比较运算
1.2通配符
2.逻辑表达式
1.3逻辑运算符
Select查询语句
Select select_list from table [where conditions] [order by order_list [asc|desc]];
Select_list:字段列表,样式为“字段1……字段N”。
Table:查询表,样式为“表1,表2…表N”。
Condiitions:由表字段组成的条件表达式或逻辑表达式。
Order_list:查询结果按照某字段排序的字段列表。
举例:
1.Select * from newsclass;
//查询newsclass表中所有的内容。
2.Select newsid from newsclass;
//查询newsclass表中newsid列中的所有内容。
3.Select * from newsclass where newid <(>; <=; >=; =)10;
//查询newsclass表所有列中的newsid小宇(大于,大于等于,小于等于,等于)的的所有内容。
4.Select * from newsclass where newstitle like 'c' ;
//查询newsclass表newstitle列中是C的所有内容。 like为模糊查询。这块自由发挥.自由度很大。
5.Select * from newsclass where newssource is not NULL;
//查询newsclass 表中newssource不为空的所有内容。
6.Select * from newclass where id > 10 and newssource ='博客';
//查询newsclass 表中id列大于10并且新闻源为博客的所有内容。
7.Select TOP 3 * from newsclass;
//查询newsclass表中的所有内容,只输出全部内容的前3行。
8.Select * from newsclass order by id ASC(DESC);
//查询newsclass 表中所有内容,并且将查询的结果根据id列执行升序(降序)的排序输出。
Insert 插入语句
格式:Insert [into] <表名> [列名] values <值列表>;
表名是必须的,表的列名可选,如果省略,<值列表>中栓许与数据表字段顺序保持一致。
多个列名和多个值列表用逗号分割。
举例:
1.insert into newsclass (id,username,password) values ('1','root','root123')
//向newsclass表中的id,username,password三列插入一条记录,内容为id是1,名字是root,密码是:root123。
2.Insert into newsMost(标题,日期,点击率)Select newstitle,newsdate,hitsFrom newsclass
//将从newsclass 表中查询出的newstitle,newsdate,hits三列的内容插入到newsmost中作为标题,日期,点击率三列。(Newsmost表需要在插入之前就建立好)
Cast用法,cast意思就是进行数据类型的转换。
3.Insert into newsMost (新闻标题,新闻日期,点击率)Select newstitle, newsdate, hits From newsclass where newsdate > cast ('2008-06-01' as datetime)
//将从newsclass 表中查询出的newstitle,newsdate,hits三列的内容插入到newsmost中作为标题,日期,点击率三列,并且将newsclass表中的newsdate列的一个值为‘2008-06-01’的数据类型改变为datetime类型。
4.Select NEWSTITLE,NEWSDATE,HITS into newsMost1 from newsclass;
//将查询newsclass表中的newstitle,newsdate,hits三列的内容插入到新的newsmost1中。 (Newsmost1表不需要在插入之前就建立好)
5.Select identity (int,1,1) As ID, newstitle AS 标题,newsdate AS 日期, hits AS 点击率 into newsMost2 from newsclass;
//将查询newsclass表中的newstitle ,newsdate,hits 三列的内容插入到新的newsmost2中。并且在插入的时候进行转换,把newstitle 作为新表中的标题,newsdate 作为新表中的日期,hits作为新表中的点击率。并且增加ID作为一个标识列。
[identity是标识的意思,其中的(int,1,1)表示的意思是标识列是一个整数型的,第一个1代表的从1开始,第二个1代表的是每次增加1]
Union 用法 union 意思是将前一个结果和后一个结果联合使用
6.Insert newsMost (标题,日期,点击率)
Select '两会会议','2009-03-06',100 union Select '汽车.购车','2009-03-06',120 union Select NEWSTITLE,NEWSDATE,HITS From t_news union select 标题,日期,点击率 from newsMost2
//将从T_NEWS表中查询的三列newstitle,newsdate,hits 和从newsmost2 中查询的三列标题,日期,点击率联合起来,并且联合两次的输入结果一起插入到一张新的表newsmost中作为标题,日期,点击率。
Update 语句用法
格式:UPDATE <表名> SET <列名 = 更新值> WHERE <更新条件>
举例:
1. Update t_News_User set Power = 'False';
将t_news_user 表中power列中所有的值都更新为false
2. Update t_News set newssource='博客' where newssource is NULL;
将t_news表中newssource 列为空的都更新为‘博客’。
Delete 删除语句
格式:Delete from <表名> [where <删除条件>]
举例:
1. Delete from t_news where newssource like '%新浪网%'
//删除t_news表中新闻来源类似于新浪网的内容。
Truncate 用法 意思是清除表中的所有行
Truncate table t_News;
//清空t_news表中的所有数据
Create 的用法 意思是创建,可以是创建数据库可以是创建表 举例:
1. Create database newsclass;
//创建一个newsclass的数据库。
2. Create table newsclass (id int,name nvarchar(20),password nvarchar(20));
//创建一个newclass表,表中有三列 id,name,password。
Drop 的用法 意思是删除数据库或者表的
举例:
1. Drop table newsclass;
//删除表newsclass。
2. Drop database news;
//删除数据库news
Alter的用法 意思是表更改属性。
举例:
1. Alter table news alter column newsclass money;
//更改news表中列名为newsclass 的数据类型为money类型。
2. Alter table news add (newsid nvarchar(max));
//添加news表中名为newsid的列。
3. alter table news drop column newsid;
//删除news表中名为newsid的列。
数据类型