Oracle 检索数据

SELECT  *  |    {   [ DISTINCT  ]    column   |    expression   [   alias   ]  ,   ...    }

FROM  table;


•SELECT  标识 选择哪些列。
•FROM    标识从哪个表中选择。

column后面加上空格,同时跟上别名(alias),或者 as 别名,到下一行的内容时,要用逗号隔开,

默认的别名是小写的,如果想要让它是大写的,使用 "别名"

如果别名有多个单词的时候,用双引号别名的方式   比如 “annual  salary”

select employee_id id, first_name name from employees;
结果:

.....

    193 Britney
    194 Samuel
     id NAME
------- --------------------
    195 Vance
    196 Alana
    197 Kevin
107 rows selected



连接符:

•把列与列,列与字符连接在一起。
•用 ‘ ||’表示。
•可以用来‘合成’列。


select last_name||' `s eamil is '||email from employees;

类似于Java中的System.out.println(123+ “hello” + 123) ;//123hello123

默认情况下,查询会返回全部行,包括重复行。


SELECT last_name||' is a '||job_id 
       AS "Employee Details"
FROM   employees;

列的别名:

•重命名一个列。
•便于计算。
•紧跟列名, 也可以在列名和别名之间加入关键字‘ AS’ ,别名使用 双引号以便在别名中包含空格或特殊的字符并区分大小写
SQL中只有这儿用双引号(double quotation)

SELECT last_name AS name,commission_pctcomm

FROM   employees;

SELECT last_name "Name", salary*12 "AnnualSalary"

FROM  employees;


The first example displays the names and the commission percentages of all the employees. Notice that theoptionalASkeyword has been used before the columnalias name. The result of the query is the same whether the AS keyword is used or not. Also notice that the SQL statement has the column aliases, name and comm, in lowercase, whereas the result of the querydisplays the column headings inuppercase. As mentioned in a previous slide, column headingsappear inuppercase by default.

默认的这种没有引号的别名是大写的

Thesecond example displays the last names and annual salaries of all the employees. Because Annual Salarycontain a space, it has been enclosed in double quotation marks. Notice that the column heading in the output is exactly the same as the column alias.

用双引号的这种方式,可以将特殊的字符保留在引用的别名中,同时大小写和列的别名一致


在SELECT子句中使用关键字‘DISTINCT’删除重复行。

select distinct department_id from employees;
DEPARTMENT_ID
-------------
          100
           30
           20
           70
           90
          110
           50
           40
           80
           10
           60
12 rows selected


定义空值

空值是无效的,未指定的,未知的或不可预知的值
空值不是空格或者0。

包含空值的数学表达式的值都为空值





SQL 语句与 SQL*Plus命令

Structural query language

SQL

一种语言
ANSI 标准
关键字不能缩写
使用语句控制数据库中的表的定义信息和表中的数据


SQL*Plus

一种环境

Oracle的特性之一
关键字可以缩写   desc employees,desc是sql plus的关键字,全称是describe;
ed也是sql plus的关键字 ,全称是edit
命令不能改变数据库中的数据的值
集中运行

总结:

1. 对于日期型数据, 做 *, / 运算不合法


2. 包含空值的数学表达式的值都为空值


3. 别名使用双引号!


4. oracle 中连接字符串使用 "||", 而不是 java 中的 "+"


5. 日期和字符只能在单引号中出现. 输出 last_name`s email is email


select last_name || ' `s email is ' || email EMAIL
from employees


6. distinct 关键字, 以下语法错误


select last_name, distinct department_id
from employees

习题:

 SQL*PLUS命令可以控制数据库吗?否!SQL*PLUS只是一个运行环境,控制数据库的是SQL语言。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值