• Oracle SQL 核心


    5个核心的SQL语句

    SELECT 、INSERT、UPDATE、DELETE 、MERGE

    1.SELECT 语句

    1. SELECT <column list>
    2. FROM <source object list>
    3. FROM <left source object> <join type>
    4. JOIN <right source object> ON <on predicates>
    5. WHERE <where predicates>
    6. GROUP BY <group by expression(s)>
    7. HAVING <having predicates>
    8. ORDER BY <order by list>

    例1 下订单超过4次的女顾客查询语句

    1. oe@orclpdb1:orclcdb>
    2. oe@orclpdb1:orclcdb> select c.customer_id,count(o.order_id) as orders_ct
    3. 2 from oe.customers c
    4. 3 join oe.orders o
    5. 4 on c.customer_id = o.customer_id
    6. 5 where c.gender = 'F'
    7. 6 group by c.customer_id
    8. 7 having count(o.order_id) > 4
    9. 8 order by orders_ct,c.customer_id
    10. 9 /
    11. CUSTOMER_ID ORDERS_CT
    12. ----------- ----------
    13. 146 5
    14. 147 5
    15. 2 rows selected.
    16. oe@orclpdb1:orclcdb>

    1.1 FROM 子句

    FROM子句列出了查询数据的源对象。这个子句可以包含表、视图、物化视图、分区或子分区,或者你可以建立一个子查询生成子对象。

    注意在处理联结语句时是按照下面的顺序来进行的:

    • 交叉联结
    • 内联结
    • 外联结

    仅包含FROM子句的查询语句

    1. oe@orclpdb1:orclcdb> select c.customer_id cust_id,o.order_id ord_id,c.gender
    2. 2 from oe.customers c
    3. 3 join oe.orders o
    4. 4 on c.customer_id = o.customer_id;
    5. CUST_ID ORD_ID G
    6. ---------- ---------- -
    7. 101 2458 M
    8. 101 2447 M
    9. 101 2413 M
    10. 101 2430 M
    11. 102 2397 M
    12. 102 2432 M
    13. 102 2414 M
    14. 102 2431 M
    15. 103 2454 F
    16. 103 2433 F
    17. 103 2415 F
    18. 103 2437 F
    19. 104 2354 F
    20. 104 2355 F
    21. 104 2416 F
    22. 104 2438 F
    23. 105 2358 F
    24. 105 2356 F
    25. 105 2417 F
    26. 105 2439 F
    27. 106 2381 M
    28. 106 2359 M
    29. 106 2418 M
    30. 106 2441 M
    31. 107 2440 F
    32. 107 2360 F
    33. 107 2419 F
    34. 107 2442 F
    35. 108 2357 M
    36. 108 2361 M
    37. 108 2420 M
    38. 108 2443 M
    39. 109 2394 M
    40. 109 2362 M
    41. 109 2421 M
    42. 109 2444 M
    43. 116 2436 M
    44. 116 2369 M
    45. 116 2428 M
    46. 116 2453 M
    47. 117 2446 M
    48. 117 2370 M
    49. 117 2429 M
    50. 117 2456 M
    51. 118 2371 M
    52. 118 2457 M
    53. 119 2372 M
    54. 120 2373 M
    55. 121 2374 M
    56. 122 2375 M
    57. 141 2377 M
    58. 143 2380 M
    59. 144 2435 M
    60. 144 2363 M
    61. 144 2382 M
    62. 144 2422 M
    63. 144 2445 M
    64. 145 2455 M
    65. 145 2364 M
    66. 145 2383 M
    67. 145 2423 M
    68. 145 2448 M
    69. 146 2379 F
    70. 146 2365 F
    71. 146 2384 F
    72. 146 2424 F
    73. 146 2449 F
    74. 147 2396 F
    75. 147 2366 F
    76. 147 2385 F
    77. 147 2425 F
    78. 147 2450 F
    79. 148 2406 M
    80. 148 2367 M
    81. 148 2386 M
    82. 148 2426 M
    83. 148 2451 M
    84. 149 2434 M
    85. 149 2368 M
    86. 149 2387 M
    87. 149 2427 M
    88. 149 2452 M
    89. 150 2388 M
    90. 151 2389 M
    91. 152 2390 M
    92. 153 2391 M
    93. 154 2392 F
    94. 155 2393 M
    95. 156 2395 F
    96. 157 2398 M
    97. 158 2399 M
    98. 159 2400 M
    99. 160 2401 M
    100. 161 2402 M
    101. 162 2403 M
    102. 164 2405 M
    103. 165 2407 M
    104. 166 2408 F
    105. 167 2409 M
    106. 169 2411 F
    107. 170 2412 M
    108. 123 2376 F
    109. 142 2378 M
    110. 163 2404 M
    111. 168 2410 M
    112. 105 rows selected.
    113. oe@orclpdb1:orclcdb>

    1.2 WHERE子句

    目的是限制或者减少结果集。

    1.3GROUP BY 子句

    group by子句将执行完FROM和WHERE 子句后得到的经过筛选后的结果集进行聚合。查询出来的结果按照GROUP BY子句中列出的表达式进行分组,为每个分组得出一行汇总结果。

    GROUP BY子句中还可以包含两个附加的运算:ROLLUP和CUBE。ROLLUP运算用来产生部分求和值,CUBE运算用来求得交互分类值。

    记住一点:GROUP BY 子句并不确保排序结果数据。如果你需要结果按照特定的顺序排序,则必须指定ORDER BY 子句。

    1. oe@orclpdb1:orclcdb> select c.customer_id,count(o.order_id) as orders_ct
    2. 2 from oe.customers c
    3. 3 join oe.orders o
    4. 4 on c.customer_id = o.customer_id
    5. 5 where c.gender = 'F'
    6. 6 group by c.customer_id;
    7. CUSTOMER_ID ORDERS_CT
    8. ----------- ----------
    9. 107 4
    10. 166 1
    11. 105 4
    12. 104 4
    13. 103 4
    14. 156 1
    15. 169 1
    16. 146 5
    17. 147 5
    18. 154 1
    19. 123 1
    20. 11 rows selected.
    21. oe@orclpdb1:orclcdb>

    1.4 HAVING 子句

    HAVING 子句将分组汇总后的查询结果限定为只有该子句中的条件为真的数据行。

    1.5 SELECT 列表

    展现SELECT 列表各种可能情况的查询实例

    1. hr@orclpdb1:orclcdb> conn oe/oe@ORCLPDB1
    2. Connected.
    3. oe@orclpdb1:orclcdb> select c.customer_id,c.cust_first_name||' '||c.cust_last_name,
    4. 2 (select e.last_name from hr.employees e where e.employee_id = c.account_mgr_id) acct_mgr
    5. 3 from oe.customers c;
    6. CUSTOMER_ID C.CUST_FIRST_NAME||''||C.CUST_LAST_NAME ACCT_MGR
    7. ----------- ----------------------------------------- -------------------------
    8. 188 Charlotte Buckley Zlotkey
    9. 189 Gena Harris Zlotkey
    10. 190 Gena Curtis Zlotkey
    11. 191 Maureen Sanders Zlotkey
    12. 192 Sean Stockwell Zlotkey
    13. 193 Harry dean Kinski Zlotkey
    14. 194 Kathleen Garcia Zlotkey
    15. 195 Sean Olin Zlotkey
    16. 196 Gerard Dench Zlotkey
    17. 197 Gerard Altman Zlotkey
    18. 198 Maureen de Funes Zlotkey
    19. 199 Clint Chapman Zlotkey
    20. 200 Clint Gielgud Zlotkey
    21. 201 Eric Prashant Zlotkey
    22. 202 Ingrid Welles Zlotkey
    23. 203 Ingrid Rampling Zlotkey
    24. 204 Cliff Puri Zlotkey
    25. 205 Emily Pollack Zlotkey
    26. 206 Fritz Hackman Zlotkey
    27. 207 Cybill Laughton Zlotkey
    28. 208 Cyndi Griem Zlotkey
    29. 209 Cyndi Collins Zlotkey
    30. 210 Cybill Clapton Zlotkey
    31. 212 Luchino Falk Zlotkey
    32. 213 Luchino Bradford Zlotkey
    33. 214 Robin Danson Zlotkey
    34. 215 Orson Perkins Zlotkey
    35. 217 Bryan Huston Zlotkey
    36. 218 Bryan Dvrrie Zlotkey
    37. 219 Ajay Sen Zlotkey
    38. 221 Carol Bradford Zlotkey
    39. 222 Cary Stockwell Zlotkey
    40. 223 Cary Olin Zlotkey
    41. 224 Clara Krige Zlotkey
    42. 226 Ajay Andrews Zlotkey
    43. 227 Kathy Prashant Zlotkey
    44. 228 Graham Neeson Zlotkey
    45. 229 Ian Chapman Zlotkey
    46. 231 Danny Rourke Russell
    47. 232 Donald Hunter Russell
    48. 277 Don Siegel Zlotkey
    49. 278 Gvtz Bradford Zlotkey
    50. 279 Holly Kurosawa Zlotkey
    51. 280 Rob MacLaine Zlotkey
    52. 281 Don Barkin Zlotkey
    53. 282 Kurt Danson Russell
    54. 283 Kurt Heard Russell
    55. 308 Glenda Dunaway Cambrault
    56. 309 Glenda Bates Cambrault
    57. 323 Goetz Falk Cambrault
    58. 326 Hal Olin Errazuriz
    59. 327 Hannah Kanth Errazuriz
    60. 328 Hannah Field Errazuriz
    61. 333 Margret Powell Errazuriz
    62. 334 Harry Mean Taylor Errazuriz
    63. 335 Margrit Garner Errazuriz
    64. 337 Maria Warden Errazuriz
    65. 339 Marilou Landis Errazuriz
    66. 361 Marilou Chapman Errazuriz
    67. 363 Kathy Lambert Errazuriz
    68. 360 Helmut Capshaw Cambrault
    69. 342 Marlon Laughton Errazuriz
    70. 343 Keir Chandar Errazuriz
    71. 345 Keir Weaver Errazuriz
    72. 346 Marlon Clapton Errazuriz
    73. 347 Kelly Quinlan Errazuriz
    74. 349 Ken Glenn Errazuriz
    75. 350 Ken Chopra Errazuriz
    76. 351 Ken Wenders Errazuriz
    77. 380 Meryl Holden Cambrault
    78. 447 Richard Coppola Errazuriz
    79. 448 Richard Winters Errazuriz
    80. 450 Rick Lyon Errazuriz
    81. 451 Ridley Hackman Errazuriz
    82. 452 Ridley Coyote Errazuriz
    83. 454 Rob Russell Cambrault
    84. 458 Robert de Niro Cambrault
    85. 466 Rodolfo Hershey Cambrault
    86. 467 Rodolfo Dench Cambrault
    87. 468 Rodolfo Altman Cambrault
    88. 233 Graham Spielberg Russell
    89. 234 Dan Roberts Russell
    90. 235 Edward Oates Russell
    91. 236 Edward Julius Russell
    92. 237 Farrah Quinlan Russell
    93. 238 Farrah Lange Russell
    94. 239 Hal Stockwell Russell
    95. 240 Malcolm Kanth Russell
    96. 241 Malcolm Broderick Russell
    97. 242 Mary Lemmon Russell
    98. 243 Mary Collins Russell
    99. 244 Matt Gueney Russell
    100. 245 Max von Sydow Russell
    101. 246 Max Schell Russell
    102. 247 Cynda Whitcraft Russell
    103. 248 Donald Minnelli Russell
    104. 249 Hannah Broderick Russell
    105. 250 Dan Williams Russell
    106. 251 Raul Wilder Russell
    107. 252 Shah Rukh Field Russell
    108. 253 Sally Bogart Russell
    109. 254 Bruce Bates Russell
    110. 256 Ben de Niro Russell
    111. 257 Emmet Walken Russell
    112. 258 Ellen Palin Russell
    113. 259 Denholm von Sydow Russell
    114. 261 Emmet Garcia Zlotkey
    115. 262 Fred Reynolds Zlotkey
    116. 263 Fred Lithgow Zlotkey
    117. 265 Irene Laughton Zlotkey
    118. 266 Prem Cardinale Zlotkey
    119. 267 Prem Walken Russell
    120. 268 Kyle Schneider Russell
    121. 270 Meg Derek Russell
    122. 271 Shelley Peckinpah Russell
    123. 272 Prem Garcia Russell
    124. 274 Bob McCarthy Zlotkey
    125. 275 Dom McQueen Zlotkey
    126. 276 Dom Hoskins Zlotkey
    127. 101 Constantin Welles Russell
    128. 102 Harrison Pacino Russell
    129. 103 Manisha Taylor Russell
    130. 104 Harrison Sutherland Russell
    131. 105 Matthias MacGraw Russell
    132. 106 Matthias Hannah Russell
    133. 107 Matthias Cruise Russell
    134. 108 Meenakshi Mason Russell
    135. 109 Christian Cage Russell
    136. 110 Charlie Sutherland Russell
    137. 111 Charlie Pacino Russell
    138. 112 Guillaume Jackson Russell
    139. 113 Daniel Costner Russell
    140. 114 Dianne Derek Russell
    141. 115 Geraldine Schneider Russell
    142. 116 Geraldine Martin Russell
    143. 117 Guillaume Edwards Russell
    144. 118 Maurice Mahoney Russell
    145. 119 Maurice Hasan Russell
    146. 120 Diane Higgins Russell
    147. 121 Dianne Sen Russell
    148. 122 Maurice Daltrey Russell
    149. 124 Diane Mason Russell
    150. 125 Dianne Andrews Russell
    151. 126 Charles Field Russell
    152. 128 Isabella Reed Russell
    153. 129 Louis Jackson Russell
    154. 130 Louis Edwards Russell
    155. 131 Doris Dutt Russell
    156. 133 Kristin Malden Russell
    157. 134 Sissy Puri Russell
    158. 135 Doris Bel Geddes Russell
    159. 136 Sissy Warden Russell
    160. 138 Mani Fonda Russell
    161. 139 Placido Kubrick Russell
    162. 140 Claudia Kurosawa Russell
    163. 141 Maximilian Henner Russell
    164. 143 Sachin Neeson Russell
    165. 144 Sivaji Landis Russell
    166. 145 Mammutti Pacino Russell
    167. 146 Elia Fawcett Russell
    168. 147 Ishwarya Roberts Russell
    169. 148 Gustav Steenburgen Russell
    170. 149 Markus Rampling Russell
    171. 150 Goldie Slater Russell
    172. 151 Divine Aykroyd Russell
    173. 152 Dieter Matthau Russell
    174. 153 Divine Sheen Russell
    175. 154 Frederic Grodin Russell
    176. 155 Frederico Romero Russell
    177. 156 Goldie Montand Russell
    178. 157 Sidney Capshaw Russell
    179. 158 Frederico Lyon Russell
    180. 159 Eddie Boyer Russell
    181. 160 Eddie Stern Russell
    182. 161 Ernest Weaver Russell
    183. 162 Ernest George Russell
    184. 164 Charlotte Kazan Russell
    185. 165 Charlotte Fonda Russell
    186. 166 Dheeraj Alexander Russell
    187. 167 Gerard Hershey Russell
    188. 169 Dheeraj Davis Russell
    189. 170 Harry Dean Fonda Russell
    190. 171 Hema Powell Russell
    191. 172 Harry Mean Peckinpah Russell
    192. 174 Blake Seignier Zlotkey
    193. 175 Claude Powell Zlotkey
    194. 176 Faye Glenn Zlotkey
    195. 177 Gerhard Seignier Zlotkey
    196. 179 Harry dean Forrest Zlotkey
    197. 180 Harry dean Cage Zlotkey
    198. 181 Lauren Hershey Zlotkey
    199. 183 Lauren Altman Zlotkey
    200. 184 Mary Beth Roberts Zlotkey
    201. 185 Matthew Wright Zlotkey
    202. 186 Meena Alexander Zlotkey
    203. 767 Klaus Maria Russell Errazuriz
    204. 771 Kris de Niro Errazuriz
    205. 826 Alain Barkin Errazuriz
    206. 830 Albert Dutt Errazuriz
    207. 850 Amanda Finney Errazuriz
    208. 905 Billy Hershey Cambrault
    209. 911 Bo Dickinson Cambrault
    210. 919 Brooke Michalkow Cambrault
    211. 924 Bruno Montand Cambrault
    212. 930 Buster Jackson Cambrault
    213. 980 Daniel Loren Cambrault
    214. 473 Rolf Ashby Cambrault
    215. 474 Romy Sharif Cambrault
    216. 475 Romy McCarthy Cambrault
    217. 476 Rosanne Hopkins Cambrault
    218. 477 Rosanne Douglas Cambrault
    219. 478 Rosanne Baldwin Cambrault
    220. 479 Roxanne Shepherd Cambrault
    221. 480 Roxanne Michalkow Cambrault
    222. 481 Roy Hulce Cambrault
    223. 482 Roy Dunaway Cambrault
    224. 483 Roy Bates Cambrault
    225. 487 Rufus Dvrrie Cambrault
    226. 488 Rufus Belushi Cambrault
    227. 492 Sally Edwards Cambrault
    228. 496 Scott Jordan Cambrault
    229. 605 Shammi Pacino Cambrault
    230. 606 Sharmila Kazan Cambrault
    231. 609 Shelley Taylor Cambrault
    232. 615 Shyam Plummer Cambrault
    233. 621 Silk Kurosawa Cambrault
    234. 712 M. Emmet Stockwell Errazuriz
    235. 713 M. Emmet Olin Errazuriz
    236. 717 Mammutti Sutherland Errazuriz
    237. 719 Mani Kazan Errazuriz
    238. 721 Mani Buckley Errazuriz
    239. 729 Margaux Krige Errazuriz
    240. 731 Margaux Capshaw Errazuriz
    241. 754 Kevin Goodman Errazuriz
    242. 756 Kevin Wilder Errazuriz
    243. 757 Kiefer Reynolds Errazuriz
    244. 766 Klaus Young Errazuriz
    245. 768 Klaus Maria MacLaine Errazuriz
    246. 769 Kris Harris Errazuriz
    247. 770 Kris Curtis Errazuriz
    248. 772 Kristin Savage Errazuriz
    249. 782 Laurence Seignier Zlotkey
    250. 825 Alain Dreyfuss Errazuriz
    251. 827 Alain Siegel Errazuriz
    252. 828 Alan Minnelli Errazuriz
    253. 829 Alan Hunter Errazuriz
    254. 831 Albert Bel Geddes Errazuriz
    255. 832 Albert Spacek Errazuriz
    256. 833 Alec Moranis Errazuriz
    257. 834 Alec Idle Errazuriz
    258. 835 Alexander Eastwood Errazuriz
    259. 836 Alexander Berenger Errazuriz
    260. 837 Alexander Stanton Errazuriz
    261. 838 Alfred Nicholson Errazuriz
    262. 839 Alfred Johnson Errazuriz
    263. 840 Ali Elliott Errazuriz
    264. 841 Ali Boyer Errazuriz
    265. 842 Ali Stern Errazuriz
    266. 843 Alice Oates Errazuriz
    267. 844 Alice Julius Errazuriz
    268. 845 Ally Fawcett Errazuriz
    269. 846 Ally Brando Errazuriz
    270. 847 Ally Streep Errazuriz
    271. 848 Alonso Olmos Errazuriz
    272. 849 Alonso Kaurusmdki Errazuriz
    273. 851 Amanda Brown Errazuriz
    274. 852 Amanda Tanner Errazuriz
    275. 853 Amrish Palin Errazuriz
    276. 906 Billy Dench Cambrault
    277. 909 Blake Mastroianni Cambrault
    278. 912 Bo Ashby Cambrault
    279. 913 Bob Sharif Cambrault
    280. 916 Brian Douglas Cambrault
    281. 917 Brian Baldwin Cambrault
    282. 920 Bruce Hulce Cambrault
    283. 921 Bruce Dunaway Cambrault
    284. 923 Bruno Slater Cambrault
    285. 927 Bryan Belushi Cambrault
    286. 928 Burt Spielberg Cambrault
    287. 929 Burt Neeson Cambrault
    288. 931 Buster Edwards Cambrault
    289. 932 Buster Bogart Cambrault
    290. 934 C. Thomas Nolte Cambrault
    291. 981 Daniel Gueney Cambrault
    292. 123 Elizabeth Brown Russell
    293. 127 Charles Broderick Russell
    294. 132 Doris Spacek Russell
    295. 137 Elia Brando Russell
    296. 142 Sachin Spielberg Russell
    297. 163 Ernest Chandar Russell
    298. 168 Hema Voight Russell
    299. 173 Kathleen Walken Russell
    300. 178 Grace Belushi Zlotkey
    301. 182 Lauren Dench Zlotkey
    302. 187 Grace Dvrrie Zlotkey
    303. 211 Luchino Jordan Zlotkey
    304. 216 Orson Koirala Zlotkey
    305. 220 Carol Jordan Zlotkey
    306. 225 Clara Ganesan Zlotkey
    307. 230 Danny Wright Zlotkey
    308. 255 Brooke Shepherd Russell
    309. 260 Ellen Khan Zlotkey
    310. 264 George Adjani Zlotkey
    311. 269 Kyle Martin Russell
    312. 273 Bo Hitchcock Russell
    313. 341 Keir George Errazuriz
    314. 344 Marlon Godard Cambrault
    315. 348 Kelly Lange Errazuriz
    316. 352 Kenneth Redford Errazuriz
    317. 378 Meg Sen Zlotkey
    318. 449 Rick Romero Errazuriz
    319. 453 Ridley Young Cambrault
    320. 463 Robin Adjani Cambrault
    321. 470 Roger Mastroianni Cambrault
    322. 607 Sharmila Fonda Cambrault
    323. 627 Sivaji Gielgud Cambrault
    324. 715 Malcolm Field Errazuriz
    325. 727 Margaret Ustinov Errazuriz
    326. 755 Kevin Cleveland Errazuriz
    327. 319 rows selected.
    328. oe@orclpdb1:orclcdb>

    1.6 ORDER BY子句

    ORDER BY 子句用来将查询最终返回的结果集排序。

    示例查询的最终输出

    1. oe@orclpdb1:orclcdb>
    2. oe@orclpdb1:orclcdb> select c.customer_id,count(o.order_id) as orders_ct
    3. 2 from oe.customers c
    4. 3 join oe.orders o
    5. 4 on c.customer_id = o.customer_id
    6. 5 where c.gender = 'F'
    7. 6 group by c.customer_id
    8. 7 having count(o.order_id) > 4
    9. 8 order by orders_ct,c.customer_id
    10. 9 /
    11. CUSTOMER_ID ORDERS_CT
    12. ----------- ----------
    13. 146 5
    14. 147 5
    15. 2 rows selected.
    16. oe@orclpdb1:orclcdb>

    2.INSERT语句

    2.1 单表插入

    单表插入

    1. oe@orclpdb1:orclcdb>
    2. hr@orclpdb1:orclcdb>
    3. hr@orclpdb1:orclcdb> insert into hr.jobs(job_id,job_title,min_salary, max_salary)
    4. 2 values ('IT_PM', 'Project Manager', 5000, 11000);
    5. 1 row created.
    6. scott@orclpdb1:orclcdb> insert into scott.bonus(ename,job,sal)
    7. 2 select ename,job,sal * .10
    8. 3 from scott.emp;
    9. 14 rows created.
    10. scott@orclpdb1:orclcdb>
    11. scott@orclpdb1:orclcdb> rollback;
    12. Rollback complete.
    13. scott@orclpdb1:orclcdb>

    2.2多表插入

    1. oe@orclpdb1:orclcdb> create table small_customers as select customer_id,sum(order_total) sum_orders from oe.orders where 1=0 group by customer_id;
    2. Table created.
    3. oe@orclpdb1:orclcdb> select * from small_customers;
    4. no rows selected
    5. oe@orclpdb1:orclcdb> desc small_customers;
    6. Name Null? Type
    7. ----------------------------------------------------- -------- ------------------------------------
    8. CUSTOMER_ID NOT NULL NUMBER(6)
    9. SUM_ORDERS NUMBER
    10. oe@orclpdb1:orclcdb> create table medium_customers as select customer_id,sum(order_total) sum_orders from oe.orders where 1=0 group by customer_id;
    11. Table created.
    12. oe@orclpdb1:orclcdb> create table large_customers as select customer_id,sum(order_total) sum_orders from oe.orders where 1=0 group by customer_id;
    13. Table created.
    14. oe@orclpdb1:orclcdb>
    15. oe@orclpdb1:orclcdb>
    16. oe@orclpdb1:orclcdb> insert all
    17. 2 when sum_orders < 10000 then
    18. 3 into small_customers
    19. 4 when sum_orders >= 10000 and sum_orders < 100000 then
    20. 5 into medium_customers
    21. 6 else
    22. 7 into large_customers
    23. 8 select customer_id,sum(order_total) sum_orders
    24. 9 from oe.orders
    25. 10 group by customer_id;
    26. 47 rows created.
    27. oe@orclpdb1:orclcdb> select * from small_customers;
    28. CUSTOMER_ID SUM_ORDERS
    29. ----------- ----------
    30. 120 416
    31. 121 4797
    32. 152 7616.8
    33. 157 7110.3
    34. 160 969.2
    35. 161 600
    36. 162 220
    37. 163 510
    38. 164 1233
    39. 165 2519
    40. 166 309
    41. 167 48
    42. 12 rows selected.
    43. oe@orclpdb1:orclcdb> select * from medium_customers;
    44. CUSTOMER_ID SUM_ORDERS
    45. ----------- ----------
    46. 102 69211.4
    47. 103 20591.4
    48. 105 61376.5
    49. 106 36199.5
    50. 116 32307
    51. 119 16447.2
    52. 123 11006.2
    53. 141 38017.8
    54. 142 25691.3
    55. 143 27132.6
    56. 145 71717.9
    57. 146 88462.6
    58. 151 17620
    59. 153 48070.6
    60. 154 26632
    61. 155 23431.9
    62. 156 68501
    63. 158 25270.3
    64. 159 69286.4
    65. 168 45175
    66. 169 15760.5
    67. 170 66816
    68. 22 rows selected.
    69. oe@orclpdb1:orclcdb> select * from large_customers;
    70. CUSTOMER_ID SUM_ORDERS
    71. ----------- ----------
    72. 101 190395.1
    73. 104 146605.5
    74. 107 155613.2
    75. 108 213399.7
    76. 109 265255.6
    77. 117 157808.7
    78. 118 100991.8
    79. 122 103834.4
    80. 144 160284.6
    81. 147 371278.2
    82. 148 185700.5
    83. 149 403119.7
    84. 150 282694.3
    85. 13 rows selected.
    86. oe@orclpdb1:orclcdb>

    3.UPDATE 语句

    UPDATE \SET \WHERE

    UPDATE子句用来指定要更新的表

    SET子句用来指明哪些列改变了以及调整的值

    WHERE 子句用来按条件筛选需要更新的行.WHERE子句可选的

    1. hr@orclpdb1:orclcdb>
    2. hr@orclpdb1:orclcdb> -- create duplicate employees tables
    3. hr@orclpdb1:orclcdb> create table employees1 as select * from employees;
    4. Table created.
    5. hr@orclpdb1:orclcdb> create table employees2 as select * from employees;
    6. Table created.
    7. hr@orclpdb1:orclcdb> -- add a primary key
    8. hr@orclpdb1:orclcdb>
    9. hr@orclpdb1:orclcdb> alter table employees2 add constraint emp2_emp_id_pk primary key (employee_id);
    10. Table altered.
    11. hr@orclpdb1:orclcdb> -- retrieve list of employees in department 90
    12. hr@orclpdb1:orclcdb> select employee_id, last_name,salary
    13. 2 from employees1 where department_id = 90;
    14. EMPLOYEE_ID LAST_NAME SALARY
    15. ----------- ------------------------- ----------
    16. 100 King 24000
    17. 101 Kochhar 17000
    18. 102 De Haan 17000
    19. 3 rows selected.
    20. hr@orclpdb1:orclcdb> -- Exp1: Update a single column value using an expression
    21. hr@orclpdb1:orclcdb> update employee2
    22. 2
    23. hr@orclpdb1:orclcdb> update employees2
    24. 2 set salary = salary * 1.10 -- increase salary by 10%
    25. 3 where department_id = 90;
    26. 3 rows updated.
    27. hr@orclpdb1:orclcdb> commit;
    28. Commit complete.
    29. hr@orclpdb1:orclcdb> select employee_id,last_name,salary
    30. 2 from employees2 where department_id = 90;
    31. EMPLOYEE_ID LAST_NAME SALARY
    32. ----------- ------------------------- ----------
    33. 100 King 26400
    34. 101 Kochhar 18700
    35. 102 De Haan 18700
    36. 3 rows selected.
    37. hr@orclpdb1:orclcdb> -- Exp2 : Update a single column value using a subquery
    38. hr@orclpdb1:orclcdb> update employees1
    39. 2 set salary = (select employees2.salary
    40. 3 from employees2
    41. 4 where employees2.employee_id = employees1.employee_id
    42. 5 and employees1.salary != employees2.salary)
    43. 6 where department_id = 90;
    44. 3 rows updated.
    45. hr@orclpdb1:orclcdb> select employee_id,last_name,salary
    46. 2 from employees1 where department_id = 90;
    47. EMPLOYEE_ID LAST_NAME SALARY
    48. ----------- ------------------------- ----------
    49. 100 King 26400
    50. 101 Kochhar 18700
    51. 102 De Haan 18700
    52. 3 rows selected.
    53. hr@orclpdb1:orclcdb> rollback;
    54. Rollback complete.
    55. hr@orclpdb1:orclcdb> -- Exp 4 : Update a table using a select statement
    56. hr@orclpdb1:orclcdb> -- to define the table and column values
    57. hr@orclpdb1:orclcdb>
    58. hr@orclpdb1:orclcdb> update (select e1.salary,e2.salary new_sal
    59. 2 from employees1 e1, employees2 e2
    60. 3 where e1.employee_id = e2.employee_id
    61. 4 and e1.department_id = 90)
    62. 5 set salary = new_sal;
    63. 3 rows updated.
    64. hr@orclpdb1:orclcdb> select employee_id,last_name,salary,commission_pct
    65. 2 from employees1 where department_id = 90;
    66. EMPLOYEE_ID LAST_NAME SALARY COMMISSION_PCT
    67. ----------- ------------------------- ---------- --------------
    68. 100 King 26400
    69. 101 Kochhar 18700
    70. 102 De Haan 18700
    71. 3 rows selected.
    72. hr@orclpdb1:orclcdb> rollback
    73. 2 ;
    74. Rollback complete.
    75. hr@orclpdb1:orclcdb> -- Exp 5: Update multiple columns using a subquery
    76. hr@orclpdb1:orclcdb>
    77. hr@orclpdb1:orclcdb> update employees1
    78. 2 set (salary, commission_pct) = (select employees2.salary, .10 comm_pct
    79. 3 from employees2
    80. 4 where employees2.employee_id = employees1.employee_id
    81. 5 and employees1.salary != employees2.salary)
    82. 6 where department_id = 90;
    83. 3 rows updated.
    84. hr@orclpdb1:orclcdb> select employee_id,last_name,salary,commission_pct
    85. 2 from employees1 where department_id = 90;
    86. EMPLOYEE_ID LAST_NAME SALARY COMMISSION_PCT
    87. ----------- ------------------------- ---------- --------------
    88. 100 King 26400 .1
    89. 101 Kochhar 18700 .1
    90. 102 De Haan 18700 .1
    91. 3 rows selected.
    92. hr@orclpdb1:orclcdb> rollback;
    93. Rollback complete.
    94. hr@orclpdb1:orclcdb>

    4.DELETE语句

    DELETE语句用来从表中移除数据行

    DELETE \FROM\WHERE

    1. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    2. 2 from employees2
    3. 3 where department_id = 90;
    4. EMPLOYEE_ID DEPARTMENT_ID LAST_NAME SALARY
    5. ----------- ------------- ------------------------- ----------
    6. 100 90 King 26400
    7. 101 90 Kochhar 18700
    8. 102 90 De Haan 18700
    9. 3 rows selected.
    10. hr@orclpdb1:orclcdb> -- Exp 1: Delete rows from specified table using
    11. hr@orclpdb1:orclcdb> -- a filter condition in the WHERE clause
    12. hr@orclpdb1:orclcdb> delete from employees2
    13. 2 where department_id = 90;
    14. 3 rows deleted.
    15. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    16. 2 from employees2
    17. 3 where department_id = 90;
    18. no rows selected
    19. hr@orclpdb1:orclcdb> rollback;
    20. Rollback complete.
    21. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    22. 2 from employees2
    23. 3 where department_id = 90;
    24. EMPLOYEE_ID DEPARTMENT_ID LAST_NAME SALARY
    25. ----------- ------------- ------------------------- ----------
    26. 100 90 King 26400
    27. 101 90 Kochhar 18700
    28. 102 90 De Haan 18700
    29. 3 rows selected.
    30. hr@orclpdb1:orclcdb> -- Exp 2: Delete rows using a subquery in the FROM clause
    31. hr@orclpdb1:orclcdb> delete from (select * from employees2 where department_id = 90);
    32. 3 rows deleted.
    33. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    34. 2
    35. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    36. 2 from employees2
    37. 3 where department_id = 90;
    38. no rows selected
    39. hr@orclpdb1:orclcdb> rollback;
    40. Rollback complete.
    41. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    42. 2 from employees2
    43. 3 where department_id = 90;
    44. EMPLOYEE_ID DEPARTMENT_ID LAST_NAME SALARY
    45. ----------- ------------- ------------------------- ----------
    46. 100 90 King 26400
    47. 101 90 Kochhar 18700
    48. 102 90 De Haan 18700
    49. 3 rows selected.
    50. hr@orclpdb1:orclcdb> -- Exp 3 : Delete rows from specified table using
    51. hr@orclpdb1:orclcdb> -- a subquery in the WHERE clause
    52. hr@orclpdb1:orclcdb> delete from employees2
    53. 2 where department_id in (select department_id
    54. 3 from departments
    55. 4 where department_name = 'Executive');
    56. 3 rows deleted.
    57. hr@orclpdb1:orclcdb> select employee_id,department_id,last_name,salary
    58. 2 from employees2
    59. 3 where department_id = 90;
    60. no rows selected
    61. hr@orclpdb1:orclcdb> rollback;
    62. Rollback complete.
    63. hr@orclpdb1:orclcdb>

    5.MERGE语句

    MERGE语句可以按条件获取要更新或插入到表中的数据行,然后从1个或多个源头对表进行更新或向表中插入行。

    MERGE语句的语法

    1. MERGE <hint>
    2. INTO <table_name>
    3. USING <table_view_or_query>
    4. ON (<condition>)
    5. WHEN MATCHED THEN <update_clause>
    6. DELETE <where_clause>
    7. WHEN NOT MATCHED THEN <insert_clause>
    8. [LOG ERRORS <log_errors_clause> <reject limit <integer | unlimited>];

    1. hr@orclpdb1:orclcdb>
    2. hr@orclpdb1:orclcdb> create table dept60_bonuses
    3. 2 (employee_id number,
    4. 3 bonus_amt number);
    5. Table created.
    6. hr@orclpdb1:orclcdb> insert into dept60_bonuses values(103, 0);
    7. 1 row created.
    8. hr@orclpdb1:orclcdb> insert into dept60_bonuses values(104, 100);
    9. 1 row created.
    10. hr@orclpdb1:orclcdb> insert into dept60_bonuses values(105, 0);
    11. 1 row created.
    12. hr@orclpdb1:orclcdb> commit;
    13. Commit complete.
    14. hr@orclpdb1:orclcdb> select employee_id,last_name,salary
    15. 2 from employees
    16. 3 where department_id = 60;
    17. EMPLOYEE_ID LAST_NAME SALARY
    18. ----------- ------------------------- ----------
    19. 103 Hunold 9000
    20. 104 Ernst 6000
    21. 105 Austin 4800
    22. 106 Pataballa 4800
    23. 107 Lorentz 4200
    24. 5 rows selected.
    25. hr@orclpdb1:orclcdb> select * from dept60_bonuses;
    26. EMPLOYEE_ID BONUS_AMT
    27. ----------- ----------
    28. 103 0
    29. 104 100
    30. 105 0
    31. 3 rows selected.
    32. hr@orclpdb1:orclcdb> merge into dept60_bonuses b
    33. 2 using (
    34. 3 select employee_id,salary,department_id
    35. 4 from employees
    36. 5 where department_id = 60) e
    37. 6 on (b.employee_id = e.employee_id)
    38. 7 when matched then
    39. 8 update set b.bonus_amt = e.salary * 0.2
    40. 9 where b.bonus_amt = 0
    41. 10 delete where (e.salary > 7500)
    42. 11 when not matched then
    43. 12 insert (b.employee_id,b.bonus_amt)
    44. 13 values(e.employee_id, e.salary * 0.1)
    45. 14 where (e.salary < 7500);
    46. 4 rows merged.
    47. hr@orclpdb1:orclcdb> select * from dept60_bonuses;
    48. EMPLOYEE_ID BONUS_AMT
    49. ----------- ----------
    50. 104 100
    51. 105 960
    52. 107 420
    53. 106 480
    54. 4 rows selected.
    55. hr@orclpdb1:orclcdb> rollback;
    56. Rollback complete.
    57. hr@orclpdb1:orclcdb>
  • 相关阅读:
    随身wifi编译Openwrt的ImmortalWrt分支
    1366 - Incorrect string value: ‘\xE5\xB9\xBF\xE5\x85\xB0...‘ for column编码错误
    R实现动态条件相关模型与GARCH模型结合研究中美股市动态相关性(DCC-GARCH模型)
    numpy生成0和1数组方法,从已有数组生成新数组方法、生成固定范围内数组、生成随机数组,绘制指定均值和标准差正态分布图、均匀分布图绘制
    团队Git规范文档(操作规范及提交规范)
    C#与Java计算俩个时间的差的方法
    SAP-QM-UD取消程序
    C#笔记:C#程序基本内容
    给大家分享一份适合练手的软件测试实战项目
    mysql源码分析——InnoDB的磁盘结构
  • 原文地址:https://blog.csdn.net/u011868279/article/details/127135284