打印本文 打印本文 关闭窗口 关闭窗口
sysbase基础知识(13)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1175  更新时间:2009/4/22 22:55:13  文章录入:mintao  责任编辑:mintao
4.视图
视图是查看多表中数据的方法,视图从基表派生,它并非物理存在,而是逻辑表;视图也系统提供管理表的一种安全机制。视图使得用户集中精力在感兴趣的数据集上。
创建视图的语法:
create view [[database.]owner.]view_name
[(column_name[,column_name]…)]
as select [distinct] select_statement
[with check option]
有distinct关键字的视图不能更新。当视图涉及关联时,定义视图要小心,这时是对多表操作,完整性显得很重要。
五、数据操纵语言
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

打印本文 打印本文 关闭窗口 关闭窗口