sybase的数据操纵语言_第1页
sybase的数据操纵语言_第2页
sybase的数据操纵语言_第3页
sybase的数据操纵语言_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Sybase 的数据操纵语言 1Select 语句 基本语法: SELECTall|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*2total_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 advance5000 or total_sales between 500 and 1000 例 7:用集合函数小结查询结果 集合函数用特定列的数据来计算小结值。 集合函数结 果 Sum(all|distinctexpression)数值列中(不重复)值的总和 Avg(all|distinctexpression)数值列中(不重复)值的平均 count(all|distinctexpression)列中(不重复)非空值的数目 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 2Insert 语句 用 Insert 命令向数据库中添加行有两种方法:使用关键词 values 或使用 select 语句。 Insert 语句的基本语法为: Insertinto表名(字段列表) 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 3Delete 语句 Delete 可以对一行或多行进行操作。 Delete 语句的基本语法为: Delete 表名 from 表名列表 where 条件表达式 举例:Delete publishers where pub_name=”Jardin,Inc.” Delete titles From authors, titles Where titles.title_id=authors.title_id 4Update 语句 可以使用 Update 命令来改动表中的单个行、一组行或所有行。 Update 语句的基本语法为: Update 表名 Set column_name1=expression1|null|(select_statement) ,column_name2=expression2|null|(select_statement) from 表名列表 where

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论