VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 数据库 > MongoDB >
  • sql语句大全之数据操纵语言

数据操纵语言
1.Select语句
基本语法:
SELECT[all|distinct]字段列表
[into表名]
[from表名]
[where条件表达式]
[group by [all]字段列表]
[having筛选表达式]
[order by 字段列表[asc|desc]]
[compute聚集函数列表[by字段列表]]
注意:Select语句中的子句必须按照上述顺序使用。也就是说,若该语句包括一个group by子句和一个order by子句where,group by子句必须放在order by子句之前。
Having子句类似于where子句,不同之处有两点:(1)Having子句必须结合group by子句使用;(2)where子句不能用聚集函数,而Having子句可以。
下面通过实例来对Select的通常用法加以介绍。
例1:选择所有的列,语法为select * from table_list
如:select * from publishers
例2:选择指定的列,语法为
select column_name[,column_name]… 
from table_name
      如:select pub_id,pub_name from publishers
例3:重命名查询结果中的列,语法为
     select column_heading= column_name
     from table_name
     如:select Publisher=pub_name,pub_id
from publishers
例4:select列表中的计算值,可以对select列表中的数值数据进行计算,下面列出了算术运算符。

符号运算
+加
-减
/除
*乘
%取模
如select title_id,total_sales,total_sales*2 from titles
例5:使用distinct消除重复的查询结果
可选的关键词消除select语句的结果中的重复行。若不指定distinct,缺省值为all,将检索出包含重复行的所有行数据。
如:select distinct au_id from titleauthor
例6:选择行——where语句
select语句中的确切指定要检索哪些行的准则,其一般格式为:
select select_list from table_list where search_conditions
where子句中的搜索条件(或称限制)包括:
·比较运算符(=,<,>,!=等=
如:where advance*2>total_sales*price
·范围(between和not between)
  如:where total_sales between 5000 and 10000
·列表(in和not in)
  如:where state in(“CA”,”IN”,”MD”)
·匹配字符(like和not like)
  如:where phone like “0535%”
·未知值(is null和is not null)
  如:where advance is null
·以上各项的组合(and, or)
  如:where advance<5000 or total_sales between 500 and 1000
例7:用集合函数小结查询结果
集合函数用特定列的数据来计算小结值。
集合函数结  果
Sum([all|distinct]expression)数值列中(不重复)值的总和
Avg([all|distinct]expression)数值列中(不重复)值的平均
count([all|distinct]expression)列中(不重复)非空值的数目
Count(*)选定的行数
Max(expression)Expression的最大值
Min(expression)Expression的最小值
如:select avg(advance),sum(total_sales) 
from titles 
where type=”as”
select count(*) from titles
select avg(distinct price) from titles
select max(price) from books
例8:分组组织查询结果——group by 子句
      group by 子句用在select语句中将一张表分成若干组。
如:select type, advance from titles group by type
例9:选择分组数据——having子句
      having为group by 子句设置条件,与where为select语句设置条件一样。Having搜索条件与where相同,但having可包括集合函数,而where不能包括。
下列语句使用带集合函数having子句的例子。它把title表中的行按类型分组,但去掉了那只包含一本书的分组。
Select type from titles group by type having count(*)>1
下面是一个不带集合函数的having子句的例子。它把title表中的行按类型分组,但去掉了那些不以字母“p”开头的类型。
Select type from titles group by type having type like “p%”
例10:查询结果排序——order by子句
Order by子句允许按一列或多列对查询结果排序。每个排序可以是升序的(asc)或降序的(desc)。若不特别指明,则按升序进行。下列查询返回按pub_id排序的结果:
Select pub_id,type,title_id from titles order by pub_id
例11:连接——从多张表中检索数据
连接两张或两张以上的表是这样一个过程:比较指定字段中的数据,根据比较结果用符合条件的行组成一张新表。
举例:
select publishers.pub_id,publishers.pub_name,authors.*
from publishers,authors
where publishers.city=authors.city
例12:分组计算子句
Compute是Sybase对SQL标准中Group子句的扩充,可以将其看作带聚集计算的Group子句。例如:
Select type,price,advance
From titles
Order by type
Compute sum(price),sum(advance) by type
2.Insert语句
用Insert命令向数据库中添加行有两种方法:使用关键词values或使用select语句。
Insert语句的基本语法为:
Insert[into]表名[(字段列表)]
{values(值列表)|select_statement}
举例:insert into publishers
      values(‘1622’,’Jardin,Inc.’,’Camden’,’NJ’)
      Insert into publishers(pub_id,pub_name)
values(‘1756’,’The Health Center’)
          Insert authors select * from newauthors
          Insert authors(au_id,address,au_lname,au_fname)
Select * from newauthors
3.Delect语句
Delect可以对一行或多行进行操作。
Delect语句的基本语法为:
Delect 表名
[from 表名列表]
[where条件表达式]
举例:Delect publishers
        where pub_name=”Jardin,Inc.”
      Delect titles
        From authors, titles
        Where titles.title_id=authors.title_id
4.Update语句
可以使用Update命令来改动表中的单个行、一组行或所有行。
Update语句的基本语法为:
Update表名
  Set column_name1={expression1|null|(select_statement)}
   [,column_name2={expression2|null|(select_statement)}]
   [……]
   [from 表名列表]
   [where 条件表达式]
举例:
update authors set_au_lname=”Health”,aufname=”Goodbody”
  where au_lname=”Bloth”
update titles
  set total_sales=total_sales + qty
  from titles,sales
  where titles.title_id=sales.title_id
六、Sybase预定义函数
1.聚集函数
sum([all|distinct]表达式)
avg([all|distinct]表达式)
count([all|distinct]表达式)
count(*)
max(表达式)
min(表达式)
2.字符串函数
upper(字符表达式)
lower(字符表达式)
char(整型表达式)
char_length(字符表达式)
ltrim(字符表达式)
rtrim(字符表达式)
……
3.数学函数
abs(精确小数型表达式)
floor(精确小数型表达式)求小于或等于给定表达式值的最大整数(取底)
rand([整数型]
round(精确小数型表达式,整数)
sign(精确小数型表达式)
power(精确小数型表达式,整数幂)
……
4.日期函数
getdate()
datepart(日期部分,日期)
datediff(日期部分,日期1,日期2)
dateadd(日期部分,数值表达式,日期)
5.类型转换函数
convert(数据类型,表达式[,格式])
6.系统函数
db_name([数据库ID])
host_name()
isnull(表达式1,表达式2)
……
七、数据控制语言
用来控制数据的安全性,如权限控制语句GRANT和REVOKE等。
 

第七讲  数据库编程基础

一、批处理
SQL Server可以处理作为一批而提交的多个SQL语句,既可以是交互式的,也可以是一个文件。批处理SQL语句由批结束标志终止,该标志指示SQL Server从前面开始执行该批处理语句,对于独立的SQL实用程序isql而言,其批结束标志为单独占一行的“go”。
举例:选择表title及表authors的行数
select count(*) from titles
select count(*) from authors
go



相关教程