Bo's Oracle Station

查看: 2004|回复: 0

课程第10次(2016-04-18星期一):WHERE,SORT和单行函数

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2016-4-19 08:55:38 | 显示全部楼层 |阅读模式
课程第10次(2016-04-18星期一):WHERE,SORT和单行函数
【上完1Z0-051的第2章】:WHERE, SORT

【1Z0-051】:共3章
【1Z0-052】:共3章

2016-04-18.txt:
  1. SQL> conn hr/oracle_4U
  2. Connected.
  3. SELECT last_name
  4. FROM   employees
  5.   3  WHERE  hire_date = '17-FEB-96' ;

  6. no rows selected

  7. SQL> SELECT last_name
  8.   2  FROM   employees
  9.   3  WHERE  hire_date = '2/17/1996 00:00:00 AM' ;
  10. WHERE  hire_date = '2/17/1996 00:00:00 AM'
  11.                    *
  12. ERROR at line 3:
  13. ORA-01843: not a valid month


  14. SELECT last_name
  15. FROM   employees
  16.   3  WHERE  hire_date  =  to_date ('17-FEB-96','DD-MON-RR') ;

  17. no rows selected

  18. SQL> show user
  19. USER is "HR"
  20. SQL> create table t05102_a( a varchar2(20)) ;

  21. Table created.

  22. SQL> insert  into  t05102_a  values ('A'||chr(37));

  23. 1 row created.

  24. SQL> insert  into  t05102_a  values ('B');

  25. 1 row created.

  26. SQL> commit;

  27. Commit complete.

  28. SQL> select  * from t05102_a  ;

  29. A
  30. --------------------
  31. A%
  32. B

  33. SQL> select  * from t05102_a   where a  like  '%%' ;

  34. A
  35. --------------------
  36. A%
  37. B

  38. SQL> select  * from t05102_a   where a  like  '%\%'  escape '\';

  39. A
  40. --------------------
  41. A%

  42. SQL> select  q'[']' from dual;

  43. Q
  44. -
  45. '

  46. SQL> select  q'["]' from dual;

  47. Q
  48. -
  49. "

  50. SQL> select  q'[%]' from dual;

  51. Q
  52. -
  53. %

  54. SQL> select  q'["]' from dual;

  55. Q
  56. -
  57. "

  58. SQL> create table t05102_b ( a varchar2(20))  ;

  59. Table created.

  60. SQL> insert into t05102_b values ('A'||chr(95)) ;

  61. 1 row created.

  62. SQL> insert into t05102_b values ('B') ;

  63. 1 row created.

  64. SQL> commit;

  65. Commit complete.

  66. SQL> select  * from t05102_b;

  67. A
  68. --------------------
  69. A_
  70. B

  71. SQL> update t05102_b set a='BB' where a='B';

  72. 1 row updated.

  73. SQL> commit;

  74. Commit complete.

  75. SQL> select  * from t05102_b;

  76. A
  77. --------------------
  78. A_
  79. BB

  80. SQL> select  * from t05102_b where a like '__';

  81. A
  82. --------------------
  83. A_
  84. BB

  85. SQL> select  * from t05102_b where a like '_\_' escpe '\';
  86. select        * from t05102_b where a like '_\_' escpe '\'
  87.                                            *
  88. ERROR at line 1:
  89. ORA-00933: SQL command not properly ended


  90. SQL> select  * from t05102_b where a like '_\_' escape '\';

  91. A
  92. --------------------
  93. A_

  94. SQL> insert into t05102_b values ('C') ;

  95. 1 row created.

  96. SQL> commit;

  97. Commit complete.

  98. SQL> select  * from t05102_b where a like '__';

  99. A
  100. --------------------
  101. A_
  102. BB

  103. SQL>


  104. ----------------------------------------------------
  105. SQL> set linesize 1000
  106. SQL> set pagesize 1000
  107. SQL> select  * from departments;

  108. DEPARTMENT_ID DEPARTMENT_NAME                     MANAGER_ID LOCATION_ID
  109. ------------- ------------------------------ ---------- -----------
  110.            10 Administration                            200        1700
  111.            20 Marketing                             201        1800
  112.            30 Purchasing                            114        1700
  113.            40 Human Resources                            203        2400
  114.            50 Shipping                                    121        1500
  115.            60 IT                                    103        1400
  116.            70 Public Relations                            204        2700
  117.            80 Sales                                    145        2500
  118.            90 Executive                             100        1700
  119.           100 Finance                                    108        1700
  120.           110 Accounting                            205        1700
  121.           120 Treasury                                               1700
  122.           130 Corporate Tax                                       1700
  123.           140 Control And Credit                               1700
  124.           150 Shareholder Services                               1700
  125.           160 Benefits                                               1700
  126.           170 Manufacturing                                       1700
  127.           180 Construction                                       1700
  128.           190 Contracting                                       1700
  129.           200 Operations                                       1700
  130.           210 IT Support                                       1700
  131.           220 NOC                                               1700
  132.           230 IT Helpdesk                                       1700
  133.           240 Government Sales                                       1700
  134.           250 Retail Sales                                       1700
  135.           260 Recruiting                                       1700
  136.           270 Payroll                                               1700

  137. 27 rows selected.

  138. SQL> set feedback off
  139. SQL> select  * from departments;

  140. DEPARTMENT_ID DEPARTMENT_NAME                     MANAGER_ID LOCATION_ID
  141. ------------- ------------------------------ ---------- -----------
  142.            10 Administration                            200        1700
  143.            20 Marketing                             201        1800
  144.            30 Purchasing                            114        1700
  145.            40 Human Resources                            203        2400
  146.            50 Shipping                                    121        1500
  147.            60 IT                                    103        1400
  148.            70 Public Relations                            204        2700
  149.            80 Sales                                    145        2500
  150.            90 Executive                             100        1700
  151.           100 Finance                                    108        1700
  152.           110 Accounting                            205        1700
  153.           120 Treasury                                               1700
  154.           130 Corporate Tax                                       1700
  155.           140 Control And Credit                               1700
  156.           150 Shareholder Services                               1700
  157.           160 Benefits                                               1700
  158.           170 Manufacturing                                       1700
  159.           180 Construction                                       1700
  160.           190 Contracting                                       1700
  161.           200 Operations                                       1700
  162.           210 IT Support                                       1700
  163.           220 NOC                                               1700
  164.           230 IT Helpdesk                                       1700
  165.           240 Government Sales                                       1700
  166.           250 Retail Sales                                       1700
  167.           260 Recruiting                                       1700
  168.           270 Payroll                                               1700
  169. SQL> select  * from v$datafile;
  170. select        * from v$datafile
  171.                *
  172. ERROR at line 1:
  173. ORA-00942: table or view does not exist


  174. SQL> conn / as sysdba
  175. Connected.
  176. SQL> select  * from v$datafile;

  177.      FILE# CREATION_CHANGE# CREATION_TIME              TS#     RFILE# STATUS  ENABLED        CHECKPOINT_CHANGE# CHECKPOINT_TIME    UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME LAST_CHANGE# LAST_TIME               OFFLINE_CHANGE# ONLINE_CHANGE# ONLINE_TIME              BYTES        BLOCKS CREATE_BYTES BLOCK_SIZE NAME                                                                                                                 PLUGGED_IN BLOCK1_OFFSET
  178. ---------- ---------------- ------------------ ---------- ---------- ------- ---------- ------------------ ------------------ --------------------- ------------------ ------------ ------------------ --------------- -------------- ------------------ ---------- ---------- ------------ ---------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------- -------------
  179. AUX_NAME                                                                                                                                  FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TI FOREIGN_DBID FOREIGN_CREATION_CHANGE# FOREIGN_CREATION_T PLU PLUGIN_CHANGE# PLUGIN_RESETLOGS_CHANGE# PLUGIN_RESETLOGS_T
  180. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------- ------------------ ------------ ------------------------ ------------------ --- -------------- ------------------------ ------------------
  181.          1                  7 15-AUG-09                        0           1 SYSTEM  READ WRITE            1313408 18-APR-16                          0                                                                945183               945184 23-MAY-13           723517440         88320                  0          8192 +DATA/orcl/datafile/system.256.816169553                                                                                   0    4294967295
  182. NONE                                                                                                                                            0                                    0                             0                          NO                   0                            0

  183.          2               2140 15-AUG-09                        1           2 ONLINE  READ WRITE            1313408 18-APR-16                          0                                                                945183               945184 23-MAY-13           629145600         76800                  0          8192 +DATA/orcl/datafile/sysaux.257.816169553                                                                                   0    4294967295
  184. NONE                                                                                                                                            0                                    0                             0                          NO                   0                            0

  185.          3             942603 15-AUG-09                        2           3 ONLINE  READ WRITE            1313408 18-APR-16                          0                                                                945183               945184 23-MAY-13           104857600         12800                  0          8192 +DATA/orcl/datafile/undotbs1.258.816169553                                                                                  0    4294967295
  186. NONE                                                                                                                                            0                                    0                             0                          NO                   0                            0

  187.          4              17993 15-AUG-09                        4           4 ONLINE  READ WRITE            1313408 18-APR-16                          0                                                                945183               945184 23-MAY-13             7864320           960                  0          8192 +DATA/orcl/datafile/users.259.816169553                                                                                          0    4294967295
  188. NONE                                                                                                                                            0                                    0                             0                          NO                   0                            0

  189.          5             973659 23-MAY-13                        6           5 ONLINE  READ WRITE            1313408 18-APR-16                            1118860 11-APR-16                                                        973659               973664 23-MAY-13           104857600         12800  104857600          8192 +DATA/orcl/datafile/example.265.816169651                                                                          0    4294967295
  190. NONE                                                                                                                                            0                           4023594539                        945509 15-AUG-09          NO              965811                       945184 23-MAY-13

  191. SQL> select  name from v$datafile;

  192. NAME
  193. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  194. +DATA/orcl/datafile/system.256.816169553
  195. +DATA/orcl/datafile/sysaux.257.816169553
  196. +DATA/orcl/datafile/undotbs1.258.816169553
  197. +DATA/orcl/datafile/users.259.816169553
  198. +DATA/orcl/datafile/example.265.816169651
  199. SQL> col name format a60 trunc
  200. SQL> select  name from v$datafile;

  201. NAME
  202. ------------------------------------------------------------
  203. +DATA/orcl/datafile/system.256.816169553
  204. +DATA/orcl/datafile/sysaux.257.816169553
  205. +DATA/orcl/datafile/undotbs1.258.816169553
  206. +DATA/orcl/datafile/users.259.816169553
  207. +DATA/orcl/datafile/example.265.816169651
  208. SQL> conn hr/oracle_4U
  209. Connected.
  210. SQL> select salary from employees where employee_id = &s_1;
  211. Enter value for s_1: 100
  212. old   1: select salary from employees where employee_id = &s_1
  213. new   1: select salary from employees where employee_id = 100

  214.     SALARY
  215. ----------
  216.       8000
  217. SQL> /
  218. Enter value for s_1: 101
  219. old   1: select salary from employees where employee_id = &s_1
  220. new   1: select salary from employees where employee_id = 101

  221.     SALARY
  222. ----------
  223.      17000
  224. SQL> /
  225. Enter value for s_1: 102
  226. old   1: select salary from employees where employee_id = &s_1
  227. new   1: select salary from employees where employee_id = 102

  228.     SALARY
  229. ----------
  230.      17000
  231. SQL> set feedback off
  232. SQL> set verify off
  233. SQL> /
  234. Enter value for s_1: 103

  235.     SALARY
  236. ----------
  237.       9000
  238. SQL> select salary from employees where  last_name like '&s_2%';
  239. Enter value for s_2: T

  240.     SALARY
  241. ----------
  242.       8600
  243.       3200
  244.       2800
  245.      10000
  246.       7000
  247. SQL> select last_name, salary from employees where  last_name like '&s_2%';
  248. Enter value for s_2: T

  249. LAST_NAME                      SALARY
  250. ------------------------- ----------
  251. Taylor                                8600
  252. Taylor                                3200
  253. Tobias                                2800
  254. Tucker                               10000
  255. Tuvault                         7000
  256. SQL> /
  257. Enter value for s_2: S

  258. LAST_NAME                      SALARY
  259. ------------------------- ----------
  260. Sarchand                        4200
  261. Sciarra                         7700
  262. Seo                                2700
  263. Sewall                                7000
  264. Smith                                8000
  265. Smith                                7400
  266. Stiles                                3200
  267. Sullivan                        2500
  268. Sully                                9500
  269. SQL> select last_name, salary from employees where  last_name like '&&s_3%';
  270. Enter value for s_3: T

  271. LAST_NAME                      SALARY
  272. ------------------------- ----------
  273. Taylor                                8600
  274. Taylor                                3200
  275. Tobias                                2800
  276. Tucker                               10000
  277. Tuvault                         7000
  278. SQL> /

  279. LAST_NAME                      SALARY
  280. ------------------------- ----------
  281. Taylor                                8600
  282. Taylor                                3200
  283. Tobias                                2800
  284. Tucker                               10000
  285. Tuvault                         7000
  286. SQL> undefine s_3
  287. SQL> /
  288. Enter value for s_3: T

  289. LAST_NAME                      SALARY
  290. ------------------------- ----------
  291. Taylor                                8600
  292. Taylor                                3200
  293. Tobias                                2800
  294. Tucker                               10000
  295. Tuvault                         7000
  296. SQL> /

  297. LAST_NAME                      SALARY
  298. ------------------------- ----------
  299. Taylor                                8600
  300. Taylor                                3200
  301. Tobias                                2800
  302. Tucker                               10000
  303. Tuvault                         7000
  304. SQL> undefine s_3
  305. SQL> define s_4 = G
  306. SQL> select last_name, salary from employees where  last_name like '&s_4%';

  307. LAST_NAME                      SALARY
  308. ------------------------- ----------
  309. Gates                                2900
  310. Gee                                2400
  311. Geoni                                2800
  312. Gietz                                8300
  313. Grant                                2600
  314. Grant                                7000
  315. Greenberg                       12008
  316. Greene                                9500
  317. SQL> select last_name, salary from employees where  last_name like '&&s_5%'  or first_name like '&s_5%';
  318. Enter value for s_5: A

  319. LAST_NAME                      SALARY
  320. ------------------------- ----------
  321. Hunold                                9000
  322. Austin                                4800
  323. Khoo                                3100
  324. Fripp                                8200
  325. Atkinson                        2800
  326. Errazuriz                       12000
  327. McEwen                                9000
  328. Ande                                6400
  329. Banda                                6200
  330. Abel                               11000
  331. Hutton                                8800
  332. Bull                                4100
  333. Cabrio                                3000
  334. Walsh                                3100
  335. SQL> select last_name, first_name, salary from employees where  last_name like '&&s_5%'  or first_name like '&s_5%';

  336. LAST_NAME                  FIRST_NAME                   SALARY
  337. ------------------------- -------------------- ----------
  338. Hunold                          Alexander                     9000
  339. Austin                          David                      4800
  340. Khoo                          Alexander                     3100
  341. Fripp                          Adam                             8200
  342. Atkinson                  Mozhe                      2800
  343. Errazuriz                  Alberto                    12000
  344. McEwen                          Allan                      9000
  345. Ande                          Sundar                     6400
  346. Banda                          Amit                             6200
  347. Abel                          Ellen                     11000
  348. Hutton                          Alyssa                     8800
  349. Bull                          Alexis                     4100
  350. Cabrio                          Anthony                     3000
  351. Walsh                          Alana                      3100
  352. SQL>

复制代码

2016-04-18.sql:

  1. select  sysdate from dual;

  2. SELECT last_name
  3. FROM   employees
  4. WHERE  hire_date  =  to_date ('17-FEB-96','DD-MON-RR') ;


  5. SELECT employee_id,last_name, manager_id
  6. FROM   employees
  7. WHERE  manager_id IS NULL ;

  8. --ERROR
  9. SELECT employee_id,last_name, manager_id
  10. FROM   employees
  11. WHERE  manager_id = NULL ;

  12. SELECT   last_name, job_id, department_id, hire_date
  13. FROM     employees
  14. ORDER BY hire_date DESC ;


  15. SELECT employee_id, last_name, salary*12 annsal , department_id
  16. FROM   employees
  17. ORDER BY  department_id  desc ;

  18. SELECT last_name, department_id, salary
  19. FROM   employees
  20. ORDER BY department_id, salary DESC;





  21. -----
  22. select  * from user_ind_columns c
  23. where c.TABLE_NAME='EMPLOYEES';

  24. select  * from user_indexes i
  25.   where i.INDEX_NAME='EMP_NAME_IX';
  26.   
  27.   select  
  28.   SUBSTR('HelloWorld',-1,5)
  29.    from dual;
  30.    
  31. select  
  32.   SUBSTR('HelloWorld',-5)
  33.   from dual;

  34. select  
  35.   SUBSTR('HelloWorld',5)
  36.   from dual;
  37.   
  38.   -----
  39.   select  * from database_properties;
  40.   
  41. select   INSTR('HelloWorld', 'Z')  from dual;

  42. select  replace ( 'HelloWorld','W' , '')
  43. from  dual;
  44.   
  45.   

  46. select  * from employees where employee_id =&s_7;
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-4-20 08:18 , Processed in 0.043464 second(s), 24 queries .

快速回复 返回顶部 返回列表