OCP课程3:SQL之使用SELECT语句检索数据

select语句有三个功能:投影、选择和多表联合,投影指选择表的某些列,选择指选择表的某些行,多表联合指从多张表获取数据。这一章主要讲投影的功能。

clipboard

 

 

1、基本SELECT语句语法

clipboard[1]

包含select子句和from子句:

  • select子句列出要选择的列,其中*表示选择表中的所有列,distinct关键字表示去掉重复的列,多列之间使用逗号隔开,也可以使用表达式,可以为列或者表达式指定一个别名
  • from子句指明这些列来自于哪张表

 

例子:使用*选择所有表中列

SQL> select * from departments;

 

例子:使用列名字选择指定的列

SQL> select department_id,location_id from departments;

 

 

2、SQL语句的一些书写规范

  • SQL语句不区分大小写
  • SQL语句可以写在一行,也可以跨行
  • 关键字不能缩写及跨行
  • 子句一般单独一行
  • 可以使用缩进提高可读性
  • 在SQL*PLUS中使用分号作为语句的结束


3、列的默认显示

在SQL Developer中:

  • 列的对齐方式:居中
  • 列头显示方式:大写

在SQL*PLUS和PL/SQL Developer中:

  • 字符和日期列左对齐
  • 数字列右对齐
  • 列头显示方式:大写

 

 

4、算术表达式

使用算术运算符创建表达式。

clipboard[2]

 

例子:使用+

SQL> SELECT last_name,salary,salary+300 from employees;

 

例子:使用*和+

SQL> SELECT last_name,salary,12*salary+100 from employees;

 

例子:使用括号改变运算优先级

SQL> SELECT last_name,salary,12*(salary+100) from employees;

 

 

5、null

null表示一个未知的值,既不是零也不是空格。

 

例子:查看人员表中的提成,其中有的人的提成为null

SQL> SELECT last_name,job_id,salary,commission_pct from employees;

 

null具有传染性,也就是说null和任何值进行运算都为null。

 

例子:使用包含null的提成字段进行算术运算,结果也是null

SQL> select last_name,12*salary*commission_pct from employees;

 

 

6、定义列别名

前面的SQL语句一个列的名字为12*SALARY*COMMISSION_PCT,很长,我们可以使用一个有意义的简短别名来替代他,更易读。

可以直接在列名字后面加上别名,也可以使用as关键字。

 

例子:使用as关键字加列别名以及直接加上列别名

SQL> select last_name as name,commission_pct comm from employees;

NAME                            COMM

------------------------- ----------

OConnell

 

别名和列名一样,默认都是以大写显示,如果别名包含空格,特殊字符或者不想使用大写显示,就需要加上双引号。

 

例子:加上双引号的别名

SQL> select last_name "Name",salary*12 "Annual Salary" from employees;

Name                      Annual Salary

------------------------- -------------

OConnell                          31200

 

 

7、连接操作符||

Oracle中的连接操作符使用两个竖杠表示,可以将列或者字符串与其他列连接起来,可以把多个字段连接成一个字段来显示。

 

例子:2个字段连接成1个字段显示

SQL> select last_name||job_id as "Employees" from employees;

Employees

-----------------------------------

AbelSA_REP

AndeSA_REP

AtkinsonST_CLERK

 

 

8、字变量

  • 字变量是select语句中个一个字符,一个数字或者一个日期
  • 日期和字符字变量的值必须使用单引号括起来
  • 输出的每一行都会显示一次字变量字符

 

例子:使用单引号将字符字变量括起来,每一行都会显示一次这个字变量

SQL> select last_name||' is a '||job_id as "Employee Details" from employees;

Employee Details

-----------------------------------------

Abel is a SA_REP

Ande is a SA_REP

Atkinson is a ST_CLERK

 

例子:把人员表某些数据转换成insert语句,拷贝到其他数据库去执行,满足临时少量数据迁移的需求

SQL> select 'insert into employee(employee_id,last_name) values(' || employee_id || ',''' || last_name || ''');' as ttt from employees;

TTT

--------------------------------------------------------------------------------

insert into employee(employee_id,last_name) values(174,'Abel');

insert into employee(employee_id,last_name) values(166,'Ande');

 

这里如果要显示字变量里面的单引号,那就使用两个单引号,也可以使用q操作符,同时还需要加上分割符号,可以是方框,问号,小括号,但是必须要配对。

clipboard[3]

 

例子:使用q操作符显示字变量里面的单引号

SQL> select department_name||q'[,it's assigned Manager Id: ]'||manager_id AS "Department and Manager" from departments;

Department and Manager

--------------------------------------------------------------------------------

Administration,it's assigned Manager Id: 200

Marketing,it's assigned Manager Id: 201

这种方式平常用得少一些

 

 

9、使用distinct去掉重复的结果

使用select语句查询的结果默认显示所有的行,包括重复的行,可以使用distinct关键字去重。

 

例子:对比不使用与使用distinct的结果

SQL> select department_id from employees;

DEPARTMENT_ID

-------------

           50

不使用distinct,结果有107行

SQL> select distinct department_id from employees;

DEPARTMENT_ID

-------------

          100

使用distinct,结果只有12行

 

例子:distinct后面跟多个字段,表示多个字段联合起来唯一

SQL> select distinct department_id,manager_id from employees;

DEPARTMENT_ID MANAGER_ID

------------- ----------

           40        101

 

 

10、SQL开发环境

我们平常用得更多的SQL开发环境是PL/SQL Developer,这里介绍了Oracle自己的SQL Developer,我们简单看一下。

首先启动SQL Developer。

[root@oracletest1 ~]# su - oracle
[oracle@oracletest1 ~]$ export DISPLAY=192.168.230.1:0.0

[oracle@oracletest1 ~]$ cd /u01/app/oracle/product/11.2.0/dbhome_1/sqldeveloper/
[oracle@oracletest1 sqldeveloper]$ ./sqldeveloper.sh 

新建连接。

clipboard[4]

输入连接名称、用户名、密码、服务器地址、端口及SID,点击“Test”,测试成功,点击“Connect”,进行连接。

clipboard[5]

就可以看到相关对象了。

clipboard[6]

这个SQL Developer功能和PL/SQL Developer差不多,平时用得也比较少,就不多讲了。

 

 

11、相关习题

(1)Evaluate the following SQL statement: SELECT product_name ||'it's not available for order' FROM product_information WHERE product_status = 'obsolete';You received the following error while executing the above query: ERROR: ORA-01756: quoted string not properly terminated;What would you do to execute the query successfully?
A.Enclose the character literal string in the SELECT clause within the double quotation marks.
B.Do not enclose the character literal string in the SELECT clause within the single quotation marks.
C.Use Quote (q) operator and delimiter to allow the use of single quotation mark in the literal character string.
D.Use escape character to negate the single quotation mark inside the literal character string in the SELECT clause.

答案:C

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28536251/viewspace-1831090/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28536251/viewspace-1831090/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值