第4章 数据库利器——SQL

 

第4章    数据库利器——SQL

 

4.1、       SQL有哪些特点,SQL的用途有哪些?

 

解答:SQL语言简单易学、风格统一,可以利用简单的几个英语单词的组合就可以完成所有的功能。它可以不加修改的嵌入到前端开发平台上,利用前端开发平台的计算能力和SQL的数据库操作能力,可以快速的建立数据库应用程序。

 

4.2、       Oracle 9i中,SQL如何访问数据表?

解答:标准格式是按照“用户名.数据表”的形式进行访问的,但通常可以简化为直接查询数据表的形式访问。

 

4.3、       SQL语句进行单表查询

解答:单表查询是相对于多表查询而言的,指从一个数据表中查询数据。

²         查询所有记录

语法:Select * From 用户名.数据表 (这里的“*”代表数据表中的所有字段)

²         查询所有记录的某些字段

语法:Select 字段名1,字段名2…. From 用户名.数据表 (注意这里的字段名之间的逗号是英文状态下的逗号)

²         查询某些字段不同记录

语法:Select distinct 字段名 From 用户名.数据表 (这里的保留字“distinct”指的是在显示时除去相同的记录,与之对应的是“all”,默认为“all”)

²         单查询条件

语法:Select 字段名 From 用户名.数据表 Where 字段名 比较运算符 ‘字符串’ (“字符串”的值为数字型时不需要加单引号,如是字符型则要加单引号。)

单查询条件使用的比较运算符如表

名称

实例

=(等于)

Select * from scott.emp where job = ‘MANAGER’

Select * from scott.emp where sal = 1100

!=(不等于)

Select * from scott.emp where job != ‘MANAGER’

Select * from scott.emp where sal != 1100

^=(不等于)

Select * from scott.emp where job ^= ‘MANAGER’

Select * from scott.emp where sal ^= 1100

<>(不等于)

Select * from scott.emp where job <> ‘MANAGER’

Select * from scott.emp where sal <> 1100

<(小于)

Select * from scott.emp where job < ‘MANAGER’

Select * from scott.emp where sal < 1100

>(大于)

Select * from scott.emp where job > ‘MANAGER’

Select * from scott.emp where sal > 1100

<=(小于等于)

Select * from scott.emp where job <= ‘MANAGER’

Select * from scott.emp where sal <= 1100

>=(大于等于)

Select * from scott.emp where job = ‘MANAGER’

Select * from scott.emp where sal = 1100

in(列表)

Select * from scott.emp where job in (‘MANAGER’, ‘CLERK’)

Select * from scott.emp where sal in (2000, 3000)

not in(不在列表)

Select * from scott.emp where job not in (‘MANAGER’, ‘CLERK’)

Select * from scott.emp where sal not in (2000, 3000)

between(介于之间)

Select * from scott.emp where job between ‘MANAGER’ and ‘CLERK’

Select * from scott.emp where sal between 2000 and 3000

not between(不介于之间)

Select * from scott.emp where job not between ‘MANAGER’ and ‘CLERK’

Select * from scott.emp where sal not between 2000 and 3000

like(模式匹配)

Select * from scott.emp where job like ‘M%’

Select * from scott.emp where job like ‘M_’

not like(模式不匹配)

Select * from scott.emp where job not like ‘M%’

Select * from scott.emp where job not like ‘M_’

is null(为空)

Select * from scott.emp where job is null

Select * from scott.emp where sal is null

is not null(不为空)

Select * from scott.emp where job is not null

Select * from scott.emp where sal is not null

(“like”和“not like”适合字符型字段的查询,%表示任意长度的字符串,_下划线表示一个任意的字符。like ’m%’表示以m开头的任意长度的字符串,like ‘m_’表示以m开头的长度为1的字符)

²         组合条件的查询

语法:Select 字段名 From 用户名.数据表

Where 字段名1 比较运算符 ‘字符串1逻辑比较符 字段名2 比较运算符 ‘字符串2

名称

实例

and(逻辑与)

Select * from scott.emp where job = ‘MANAGER’ and sal = 1100

or(逻辑或)

Select * from scott.emp where job = ‘MANAGER’ or sal = 1100

not(逻辑非)

Select * from scott.emp where not job = ‘MANAGER’

                   not job = ‘MANAGER’ 等价于 job <> ‘MANAGER’

²         排序查询

语法:Select 字段名 From 用户名.数据表 Where 字段名 比较运算符 ‘字符串’Order by 字段名 排序关键词 Order by可以指定查询结果如何排序,asc表示升序排序,desc表示降序排序,多个排序字段之间用逗号分割。若有where查询条件,order by要放在where语句后面。)

 

posted @ 2008-01-06 22:10  ゛無磿頭.㊣  阅读(559)  评论(0编辑  收藏  举报