{"id":1575,"date":"2025-02-12T22:18:39","date_gmt":"2025-02-12T14:18:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1575"},"modified":"2025-02-12T22:18:40","modified_gmt":"2025-02-12T14:18:40","slug":"oracle-%e7%89%b9%e6%9c%89%e7%9a%84-sql-%e8%af%ad%e6%b3%95%e4%b8%8e%e4%ba%8b%e5%8a%a1%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/12\/oracle-%e7%89%b9%e6%9c%89%e7%9a%84-sql-%e8%af%ad%e6%b3%95%e4%b8%8e%e4%ba%8b%e5%8a%a1%e7%ae%a1%e7%90%86\/","title":{"rendered":"Oracle \u7279\u6709\u7684 SQL \u8bed\u6cd5\u4e0e\u4e8b\u52a1\u7ba1\u7406"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">1. <strong>Oracle \u7279\u6709\u7684 SQL \u8bed\u6cd5<\/strong><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u5206\u9875\u67e5\u8be2\uff1aROWNUM\u3001ROWID\u3001FETCH FIRST<\/strong><\/h5>\n\n\n\n<p>\u5728\u67e5\u8be2\u7ed3\u679c\u4e2d\u8fdb\u884c\u5206\u9875\u662f\u5e38\u89c1\u7684\u9700\u6c42\uff0cOracle \u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5206\u9875\u67e5\u8be2\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ROWNUM<\/strong>\uff1a<br><code>ROWNUM<\/code> \u4e3a\u67e5\u8be2\u7ed3\u679c\u7684\u884c\u53f7\uff0c\u8fd4\u56de\u7ed3\u679c\u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u7f16\u53f7\uff0c\u901a\u5e38\u7528\u4e8e\u5206\u9875\u67e5\u8be2\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT * FROM (SELECT first_name, last_name FROM employees ORDER BY salary DESC) \n  WHERE ROWNUM &lt;= 10;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0a\u8ff0\u67e5\u8be2\u8fd4\u56de\u6309 <code>salary<\/code> \u6392\u5e8f\u540e\u7684\u524d 10 \u6761\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong>ROWID<\/strong>\uff1a<br><code>ROWID<\/code> \u662f\u6570\u636e\u5e93\u4e2d\u7684\u7269\u7406\u5730\u5740\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u83b7\u53d6\u8868\u4e2d\u67d0\u4e00\u884c\u7684\u7269\u7406\u4f4d\u7f6e\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT ROWID, first_name, last_name FROM employees WHERE department_id = 10;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>FETCH FIRST<\/strong>\uff08Oracle 12c \u4ee5\u540e\u5f15\u5165\u7684\u5206\u9875\u8bed\u6cd5\uff09\uff1a<br><code>FETCH FIRST<\/code> \u53ef\u4ee5\u7528\u6765\u9650\u5236\u67e5\u8be2\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u91cf\uff0c\u652f\u6301 OFFSET \u548c FETCH \u5b50\u53e5\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT first_name, last_name FROM employees\n  ORDER BY salary DESC\n  FETCH FIRST 10 ROWS ONLY;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u5de5\u8d44\u6700\u9ad8\u7684 10 \u4f4d\u5458\u5de5\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u5206\u7ec4\u4e0e\u5206\u533a\u67e5\u8be2\uff1aGROUP BY\u3001HAVING\u3001RANK\u3001DENSE_RANK<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GROUP BY<\/strong>\uff1a<br><code>GROUP BY<\/code> \u7528\u4e8e\u5c06\u7ed3\u679c\u96c6\u4e2d\u7684\u6570\u636e\u6309\u67d0\u4e00\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department_id, AVG(salary)\n  FROM employees\n  GROUP BY department_id;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department_id<\/code> \u5206\u7ec4\uff0c\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u5de5\u8d44\u3002<\/li>\n\n\n\n<li><strong>HAVING<\/strong>\uff1a<br><code>HAVING<\/code> \u7528\u4e8e\u5728\u5206\u7ec4\u540e\u8fc7\u6ee4\u7ed3\u679c\u96c6\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department_id, AVG(salary)\n  FROM employees\n  GROUP BY department_id\n  HAVING AVG(salary) &gt; 5000;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ec5\u8fd4\u56de\u5e73\u5747\u5de5\u8d44\u5927\u4e8e 5000 \u7684\u90e8\u95e8\u3002<\/li>\n\n\n\n<li><strong>RANK<\/strong> \u548c <strong>DENSE_RANK<\/strong>\uff1a<br><code>RANK<\/code> \u548c <code>DENSE_RANK<\/code> \u7528\u4e8e\u4e3a\u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u7684\u6bcf\u4e00\u884c\u6570\u636e\u5206\u914d\u4e00\u4e2a\u6392\u540d\u3002<code>RANK<\/code> \u4f1a\u4e3a\u76f8\u540c\u7684\u503c\u5206\u914d\u76f8\u540c\u7684\u6392\u540d\uff0c\u4f46\u4f1a\u8df3\u8fc7\u4e0b\u4e00\u4e2a\u6392\u540d\uff0c\u800c <code>DENSE_RANK<\/code> \u5219\u4e0d\u4f1a\u8df3\u8fc7\u6392\u540d\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT first_name, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank\n  FROM employees;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT first_name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_dense_rank\n  FROM employees;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u4e8b\u52a1\u7ba1\u7406<\/strong><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4e8b\u52a1\u7684\u6982\u5ff5\u4e0e\u7279\u6027\uff08ACID\uff09<\/strong><\/h5>\n\n\n\n<p>\u4e8b\u52a1\u662f\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a\u6267\u884c\u5355\u5143\uff0c\u662f\u4e00\u7cfb\u5217\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u8981\u4e48\u5b8c\u5168\u6267\u884c\uff0c\u8981\u4e48\u5b8c\u5168\u4e0d\u6267\u884c\u3002ACID \u662f\u4e8b\u52a1\u7ba1\u7406\u7684\u56db\u4e2a\u57fa\u672c\u7279\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Atomicity\uff08\u539f\u5b50\u6027\uff09<\/strong>\uff1a\u4e8b\u52a1\u5185\u7684\u6240\u6709\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002<\/li>\n\n\n\n<li><strong>Consistency\uff08\u4e00\u81f4\u6027\uff09<\/strong>\uff1a\u4e8b\u52a1\u6267\u884c\u524d\u540e\uff0c\u6570\u636e\u5e93\u7684\u72b6\u6001\u5fc5\u987b\u4fdd\u6301\u4e00\u81f4\u3002<\/li>\n\n\n\n<li><strong>Isolation\uff08\u9694\u79bb\u6027\uff09<\/strong>\uff1a\u4e8b\u52a1\u7684\u6267\u884c\u4e0d\u53d7\u5176\u4ed6\u4e8b\u52a1\u7684\u5f71\u54cd\u3002<\/li>\n\n\n\n<li><strong>Durability\uff08\u6301\u4e45\u6027\uff09<\/strong>\uff1a\u4e00\u65e6\u4e8b\u52a1\u63d0\u4ea4\uff0c\u7ed3\u679c\u4f1a\u6c38\u4e45\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>COMMIT\u3001ROLLBACK\u3001SAVEPOINT \u7684\u4f7f\u7528<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>COMMIT<\/strong>\uff1a\u63d0\u4ea4\u4e8b\u52a1\uff0c\u5c06\u6240\u6709\u66f4\u6539\u6c38\u4e45\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  COMMIT;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ROLLBACK<\/strong>\uff1a\u56de\u6eda\u4e8b\u52a1\uff0c\u64a4\u9500\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u66f4\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  ROLLBACK;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SAVEPOINT<\/strong>\uff1a\u8bbe\u7f6e\u4fdd\u5b58\u70b9\uff0c\u5141\u8bb8\u5728\u4e8b\u52a1\u4e2d\u8fdb\u884c\u90e8\u5206\u56de\u6eda\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SAVEPOINT savepoint1;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u5728\u4e4b\u540e\u6267\u884c\u56de\u6eda\u5230\u67d0\u4e2a\u4fdd\u5b58\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  ROLLBACK TO savepoint1;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u9501\u673a\u5236\u4e0e\u5e76\u53d1\u63a7\u5236<\/strong><\/h5>\n\n\n\n<p>Oracle \u4f7f\u7528\u9501\u673a\u5236\u6765\u63a7\u5236\u591a\u4e2a\u4e8b\u52a1\u5e76\u53d1\u64cd\u4f5c\u65f6\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u907f\u514d\u6570\u636e\u7684\u4e0d\u4e00\u81f4\u6027\u3002\u9501\u6709\u591a\u79cd\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5171\u4eab\u9501\uff08Shared Lock\uff09<\/strong>\uff1a\u5141\u8bb8\u591a\u4e2a\u4e8b\u52a1\u8bfb\u53d6\u6570\u636e\uff0c\u4f46\u4e0d\u80fd\u4fee\u6539\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u6392\u4ed6\u9501\uff08Exclusive Lock\uff09<\/strong>\uff1a\u5f53\u4e8b\u52a1\u4fee\u6539\u6570\u636e\u65f6\uff0c\u5176\u4ed6\u4e8b\u52a1\u4e0d\u80fd\u5bf9\u8be5\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>Oracle \u63d0\u4f9b\u4e86\u5982\u4e0b\u673a\u5236\u6765\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u81ea\u52a8\u63d0\u4ea4<\/strong>\uff1aOracle \u9ed8\u8ba4\u6bcf\u4e2a SQL \u8bed\u53e5\u90fd\u4f1a\u81ea\u52a8\u63d0\u4ea4\u3002<\/li>\n\n\n\n<li><strong>\u663e\u5f0f\u4e8b\u52a1<\/strong>\uff1a\u901a\u8fc7 <code>BEGIN<\/code>\u3001<code>COMMIT<\/code> \u548c <code>ROLLBACK<\/code> \u6765\u63a7\u5236\u4e8b\u52a1\u7684\u8fb9\u754c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>Oracle \u6570\u636e\u5b57\u5178\u4e0e\u89c6\u56fe<\/strong><\/h4>\n\n\n\n<p>Oracle \u6570\u636e\u5b57\u5178\u662f\u5305\u542b\u6570\u636e\u5e93\u7ed3\u6784\u3001\u5b9a\u4e49\u3001\u7528\u6237\u548c\u6743\u9650\u7b49\u5143\u6570\u636e\u7684\u7cfb\u7edf\u8868\u3002\u901a\u8fc7\u67e5\u8be2\u8fd9\u4e9b\u6570\u636e\u5b57\u5178\u89c6\u56fe\uff0c\u53ef\u4ee5\u83b7\u53d6\u5173\u4e8e\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>DBA\u3001ALL\u3001USER \u89c6\u56fe<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>DBA \u89c6\u56fe<\/strong>\uff1a\u5305\u542b\u6570\u636e\u5e93\u7684\u6240\u6709\u5bf9\u8c61\u4fe1\u606f\uff0c\u901a\u5e38\u53ea\u6709 DBA \u7528\u6237\u624d\u6709\u6743\u9650\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a<code>DBA_TABLES<\/code>\u3001<code>DBA_USERS<\/code>\u3002<\/li>\n\n\n\n<li><strong>ALL \u89c6\u56fe<\/strong>\uff1a\u5305\u542b\u5f53\u524d\u7528\u6237\u6709\u6743\u9650\u8bbf\u95ee\u7684\u6240\u6709\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a<code>ALL_TABLES<\/code>\u3001<code>ALL_VIEWS<\/code>\u3002<\/li>\n\n\n\n<li><strong>USER \u89c6\u56fe<\/strong>\uff1a\u4ec5\u5305\u542b\u5f53\u524d\u7528\u6237\u81ea\u5df1\u62e5\u6709\u7684\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a<code>USER_TABLES<\/code>\u3001<code>USER_OBJECTS<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM DBA_USERS;\nSELECT * FROM ALL_TABLES WHERE owner = 'HR';\nSELECT * FROM USER_TAB_COLUMNS WHERE table_name = 'EMPLOYEES';<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u6570\u636e\u5b57\u5178\u8868\u7684\u4f5c\u7528<\/strong><\/h5>\n\n\n\n<p>\u6570\u636e\u5b57\u5178\u8868\u5b58\u50a8\u4e86\u5173\u4e8e\u6570\u636e\u5e93\u5bf9\u8c61\uff08\u5982\u8868\u3001\u89c6\u56fe\u3001\u7d22\u5f15\u7b49\uff09\u7684\u5143\u6570\u636e\u3002\u5b83\u4eec\u662f\u6570\u636e\u5e93\u7684\u201c\u84dd\u56fe\u201d\uff0c\u5728\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u548c\u7ba1\u7406\u4e2d\u8d77\u7740\u91cd\u8981\u4f5c\u7528\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u67e5\u8be2\u6570\u636e\u5e93\u5bf9\u8c61\u3001\u6743\u9650\u3001\u7528\u6237\u4fe1\u606f\u7b49\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT table_name, column_name, data_type\nFROM all_tab_columns\nWHERE owner = 'HR' AND table_name = 'EMPLOYEES';<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u67e5\u8be2\u6570\u636e\u5e93\u5bf9\u8c61\u5143\u6570\u636e<\/strong><\/h5>\n\n\n\n<p>\u4f7f\u7528\u6570\u636e\u5b57\u5178\u89c6\u56fe\u67e5\u8be2\u6709\u5173\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u5143\u6570\u636e\uff0c\u6709\u52a9\u4e8e\u6570\u636e\u5e93\u7ba1\u7406\u548c\u5f00\u53d1\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT object_name, object_type\nFROM all_objects\nWHERE owner = 'HR';<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b Oracle \u7279\u6709\u7684 SQL \u8bed\u6cd5\u548c\u4e8b\u52a1\u7ba1\u7406\uff0c\u4f60\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u4e0e\u6570\u636e\u64cd\u4f5c\u3002\u540c\u65f6\uff0c\u4e8b\u52a1\u7ba1\u7406\u548c\u9501\u673a\u5236\u4e3a\u591a\u7528\u6237\u5e76\u53d1\u8bbf\u95ee\u6570\u636e\u5e93\u63d0\u4f9b\u4e86\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Oracle \u7279\u6709\u7684 SQL \u8bed\u6cd5 \u5206\u9875\u67e5\u8be2\uff1aROWNUM\u3001ROWID\u3001FETCH FIRST \u5728\u67e5\u8be2\u7ed3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-1575","post","type-post","status-publish","format-standard","hentry","category-oracle"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1575"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1575\/revisions"}],"predecessor-version":[{"id":1576,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1575\/revisions\/1576"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}