選讀SQL經典實例筆記03_DML和元數據

来源:https://www.cnblogs.com/lying7/archive/2023/07/09/17531969.html
-Advertisement-
Play Games

![](https://img2023.cnblogs.com/blog/3076680/202307/3076680-20230706135746797-1984903730.png) # 1. 複製數據到另一個表 ## 1.1. sql ```sql insert into dept_east ...


1. 複製數據到另一個表

1.1.  sql

 insert into dept_east (deptno,dname,loc)
 select deptno,dname,loc
   from dept
  where loc in ( 'NEW YORK','BOSTON' )

2. 複製表定義

2.1. 複製表結構,而不複製數據

2.2. MySQL

2.3. PostgreSQL

2.4. Oracle

2.5. sql

create table dept_2
 as
 select *
   from dept
  where 1 = 0

2.6. SQL Server

2.6.1.  sql

select *
   into dept_2
   from dept
  where 1 = 0

2.7. DB2

2.7.1. create table dept_2 like dept

3. 刪除違反參照完整性的記錄

3.1. 從表裡刪除一些記錄,因為在另一個表裡不存在與這些記錄相匹配的數據

3.2. 刪除其實就是查詢,最重要的步驟是要寫出正確的WHERE子句條件,以找出要刪除哪些記錄

3.3. sql

delete from emp
 where not exists (
   select * from dept
    where dept.deptno = emp.deptno
)

4. 刪除重覆記錄

4.1. sql

create table dupes (id integer, name varchar(10))
insert into dupes values (1, 'NAPOLEON')
insert into dupes values (2, 'DYNAMITE')
insert into dupes values (3, 'DYNAMITE')
insert into dupes values (4, 'SHE SELLS')
insert into dupes values (5, 'SEA SHELLS')
insert into dupes values (6, 'SEA SHELLS')
insert into dupes values (7, 'SEA SHELLS')
select * from dupes order by 1

4.2. 如果要刪除重覆記錄,首先要明確兩行數據在什麼條件下才會被認為是“重覆的記錄”

4.2.1. sql

select min(id)
  from dupes
 group by name

4.2.2.   sql

delete from dupes
   where id not in ( select min(id)
                       from dupes
                      group by name )

5. 刪除被其他表參照的記錄

5.1. sql

create table dept_accidents
( deptno         integer,
  accident_name varchar(20) )
insert into dept_accidents values (10,'BROKEN FOOT')
insert into dept_accidents values (10,'FLESH WOUND')
insert into dept_accidents values (20,'FIRE')
insert into dept_accidents values (20,'FIRE')
insert into dept_accidents values (20,'FLOOD')
insert into dept_accidents values (30,'BRUISED GLUTE')
select * from dept_accidents

5.2. 識別哪些部門發生過3次以上事故

5.2.1. sql

select deptno
  from dept_accidents
 group by deptno
having count(*) >= 3

5.3. 刪除在上述部門工作的員工

5.3.1.  sql

delete from emp
  where deptno in ( select deptno
                      from dept_accidents
                     group by deptno
                    having count(*) >= 3 )

6. 元數據查詢

6.1. SMEAGOL 模式

7. 列舉模式中的表

7.1. MySQL

7.2. PostgreSQL

7.3. SQL Server

7.4. 信息模式

7.4.1. information schema,這是按照ISO SQL 標准定義的一組視圖

7.4.2.  sql

select table_name
   from information_schema.tables
  where table_schema = 'SMEAGOL'

7.5. Oracle

7.5.1. sql

select table_name
  from all_tables
 where owner = 'SMEAGOL'

7.6. DB2

7.6.1.  sql

select tabname
   from syscat.tables
  where tabschema = 'SMEAGOL'

8. 列舉欄位

8.1. MySQL

8.2. PostgreSQL

8.3. SQL Server

8.4. 信息模式

8.4.1.  sql

select column_name, data_type, ordinal_position
   from information_schema.columns
  where table_schema = 'SMEAGOL'
    and table_name   = 'EMP'

8.5. Oracle

8.5.1.   sql

select column_name, data_type, column_id
    from all_tab_columns
   where owner      = 'SMEAGOL'
     and table_name = 'EMP'

8.6. DB2

8.6.1.  sql

select colname, typename, colno
   from syscat.columns
  where tabname   = 'EMP'
    and tabschema = 'SMEAGOL'

9. 列舉索引列

9.1. MySQL

9.1.1. show index from emp

9.2. PostgreSQL

9.2.1.   sql

select a.tablename,a.indexname,b.column_name
    from pg_catalog.pg_indexes a,
         information_schema.columns b
   where a.schemaname = 'SMEAGOL'
     and a.tablename  = b.table_name

9.3. Oracle

9.3.1. sql

select table_name, index_name, column_name, column_position
  from sys.all_ind_columns
 where table_name  = 'EMP'
   and table_owner = 'SMEAGOL'

9.4. SQL Server

9.4.1.   sql

select a.name table_name,
         b.name index_name,
          d.name column_name,
          c.index_column_id
     from sys.tables a,
          sys.indexes b,
          sys.index_columns c,
          sys.columns d.
   where a.object_id = b.object_id
    and b.object_id = c.object_id
    and b.index_id  = c.index_id
    and c.object_id = d.object_id
   and c.column_id = d.column_id
    and a.name      = 'EMP'

9.5. DB2

9.5.1.   sql

select a.tabname, b.indname, b.colname, b.colseq
    from syscat.indexes a,
         syscat.indexcoluse b
   where a.tabname   = 'EMP'
     and a.tabschema = 'SMEAGOL'
     and a.indschema = b.indschema
     and a.indname   = b.indname

10. 列舉約束

10.1. MySQL

10.2. PostgreSQL

10.3. SQL Server

10.4. 信息模式

10.4.1.   sql

select a.table_name,
          a.constraint_name,
          b.column_name,
          a.constraint_type
     from information_schema.table_constraints a,
          information_schema.key_column_usage b
    where a.table_name      = 'EMP'
      and a.table_schem     = 'SMEAGOL'
      and a.table_name      = b.table_name
     and a.table_schema    = b.table_schema
     and a.constraint_name = b.constraint_name

10.5. Oracle

10.5.1.   sql

select a.table_name,
          a.constraint_name,
         b.column_name,
          a.constraint_type
     from all_constraints a,
          all_cons_columns b
    where a.table_name      = 'EMP'
      and a.owner           = 'SMEAGOL'
      and a.table_name      = b.table_name
     and a.owner           = b.owner
     and a.constraint_name = b.constraint_name

10.6. DB2

10.6.1.   sql

select a.tabname, a.constname, b.colname, a.type
    from syscat.tabconst a,
         syscat.columns b
   where a.tabname   = 'EMP'
     and a.tabschema = 'SMEAGOL'
     and a.tabname   = b.tabname
     and a.tabschema = b.tabschema

11. 列舉非索引外鍵

11.1. 如果通過外鍵實現父子關係,那麼為子表裡對應的列加上索引有助於減少鎖

11.2. 子表和父表常用外鍵列做連接查詢,因而加上索引有助於提升查詢性能

11.3. MySQL

11.3.1. 針對特定的表執行SHOW INDEX命令,並比較其輸出結果與INFORMATION_SCHEMA.KEY_COLUMN_USAGE.COLUMN_NAME的異同

11.3.2. 如果KEY_COLUMN_USAGE里有對應的COLUMN_NAME,但是SHOW INDEX輸出的結果里卻沒有,那麼就說明該列沒有索引

11.4. PostgreSQL

11.4.1.   sql

select fkeys.table_name,
          fkeys.constraint_name,
          fkeys.column_name,
          ind_cols.indexname
     from (
   select a.constraint_schema,
          a.table_name,
          a.constraint_name,
          a.column_name
    from information_schema.key_column_usage a,
         information_schema.referential_constraints b
   where a.constraint_name    = b.constraint_name
     and a.constraint_schema  = b.constraint_schema
     and a.constraint_schema  = 'SMEAGOL'
     and a.table_name         = 'EMP'
         ) fkeys
         left join
         (
  select a.schemaname, a.tablename, a.indexname, b.column_name
    from pg_catalog.pg_indexes a,
         information_schema.columns b
   where a.tablename  = b.table_name
     and a.schemaname = b.table_schema
         ) ind_cols
      on (    fkeys.constraint_schema = ind_cols.schemaname
          and fkeys.table_name        = ind_cols.tablename
          and fkeys.column_name       = ind_cols.column_name )
   where ind_cols.indexname is null

11.5. Oracle

11.5.1.   sql

select a.table_name,
          a.constraint_name,
          a.column_name,
          c.index_name
     from all_cons_columns a,
          all_constraints b,
          all_ind_columns c
    where a.table_name       = 'EMP'
      and a.owner            = 'SMEAGOL'
     and b.constraint_type  = 'R'
     and a.owner            = b.owner
     and a.table_name       = b.table_name
     and a.constraint_name  = b.constraint_name
     and a.owner            = c.table_owner  (+)
     and a.table_name       = c.table_name   (+)
   and a.column_name      = c.column_name  (+)
     and c.index_name       is null

11.6. SQL Server

11.6.1.   sql

select fkeys.table_name,
          fkeys.constraint_name,
          fkeys.column_name,
          ind_cols.index_name
     from (
   select a.object_id,
          d.column_id,
          a.name table_name,
          b.name constraint_name,
         d.name column_name
    from sys.tables a
         join
         sys.foreign_keys b
      on (   a.name      = 'EMP'
         and a.object_id = b.parent_object_id
         )
         join
         sys.foreign_key_columns c
         on ( b.object_id = c.constraint_object_id )
            join
            sys.columns d
         on (   c.constraint_column_id = d.column_id
         and a.object_id               = d.object_id
         )
         ) fkeys
         left join
         (
  select a.name index_name,
         b.object_id,
         b.column_id
    from sys.indexes a,
         sys.index_columns b
   where a.index_id = b.index_id
          ) ind_cols
      on (     fkeys.object_id = ind_cols.object_id
          and fkeys.column_id = ind_cols.column_id )
   where ind_cols.index_name is null

11.7. DB2

11.7.1.   sql

select fkeys.tabname,
          fkeys.constname,
          fkeys.colname,
          ind_cols.indname
     from (
   select a.tabschema, a.tabname, a.constname, b.colname
     from syscat.tabconst a,
          syscat.keycoluse b
    where a.tabname   = 'EMP'
     and a.tabschema = 'SMEAGOL'
     and a.type      = 'F'
     and a.tabname   = b.tabname     and a.tabschema = b.tabschema
         ) fkeys
         left join
         (
  select a.tabschema,
         a.tabname,
         a.indname,
         b.colname
    from syscat.indexes a,
         syscat.indexcoluse b
   where a.indschema = b.indschema
     and a.indname   = b.indname
         ) ind_cols
      on (     fkeys.tabschema = ind_cols.tabschema
           and fkeys.tabname   = ind_cols.tabname
          and fkeys.colname   = ind_cols.colname )
   where ind_cols.indname is null

12. 用SQL生成SQL

12.1. 使用字元串拼接SQL 語句,通過查詢某些表來獲取需要填入的數據(例如資料庫對象名稱)

12.2. Oracle示例

12.2.1. /* 生成SQL以計算各個表的行數 */

select 'select count(*) from '||table_name||';' cnts
  from user_tables;
CNTS
--------------------------------------
select count(*) from ANT;
select count(*) from BONUS;
select count(*) from DEMO1;
select count(*) from DEMO2;
select count(*) from DEPT;
select count(*) from DUMMY;

12.2.2.  /* 禁用所有表的外鍵約束 */

select 'alter table '||table_name||
        ' disable constraint '||constraint_name||';' cons
   from user_constraints
  where constraint_type = 'R';
CONS
--------------------------------------------------------
alter table ANT disable constraint ANT_FK;
alter table BONUS disable constraint BONUS_FK;
alter table DEMO1 disable constraint DEMO1_FK;
alter table DEMO2 disable constraint DEMO2_FK;
alter table DEPT disable constraint DEPT_FK;
alter table DUMMY disable constraint DUMMY_FK;

12.2.3.  /* 根據EMP表的某些列生成插入腳本 */

select 'insert into emp(empno,ename,hiredate) '||chr(10)||
       'values( '||empno||','||''''||ename
       ||''',to_date('||''''||hiredate||''') );' inserts
 from emp
where deptno = 10;
INSERTS
---------------------------------------------------------------
insert into emp(empno,ename,hiredate)
values( 7782,'CLARK',to_date('09-JUN-1981 00:00:00') );
insert into emp(empno,ename,hiredate)
values( 7839,'KING',to_date('17-NOV-1981 00:00:00') );
insert into emp(empno,ename,hiredate)
values( 7934,'MILLER',to_date('23-JAN-1982 00:00:00') );

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • IQueryable/IQueryable 和表達式樹 IQueryable有兩個組件 Expression:當前查詢的組件的與語言和數據源無關的表示形式,以表達式樹的形式表示。 Provider:LINQ 提供程式的實例,它知道如何將當前查詢具體化為一個值或一組值。 ![](https://img ...
  • 特性 跨平臺 Web API和MVC技術的統一 原生依賴的註入支持 更強的測試性 輕量、高性能的模塊 開源、有社區的支持 項目入口--Main Asp.Net Core應用程式最初作為控制台應用程式啟動,而Program.cs文件中的Main()方法就是入口 CreateHostBuilder()方 ...
  • ## 一:背景 ### 1. 講故事 很多朋友可能會有疑問,C# 是一門托管語言,怎麼可能會有非托管句柄泄露呢? 其實一旦 C# 程式與 C++ 語言交互之後,往往就會被後者拖入非托管泥潭,讓我們這些調試者被迫探究 `非托管領域問題`。 ## 二:非托管句柄泄露 ### 1. 測試案例 為了方便講述 ...
  • # Unity UGUI的所有組件的介紹及使用 本文將介紹Unity UGUI中的各個組件,包括它們的具體介紹、用途 ## 1. Text(文本) - 介紹:Text組件用於在UI界面上顯示文本內容。 - 用途:常用於顯示UI界面的標題、按鈕標簽、提示信息等。 ## 2. Image(圖片) - 介 ...
  • # Unity AssetPostprocessor中Model相關函數的實際應用 Unity AssetPostprocessor是Unity引擎中的一個重要功能,它可以在導入資源時自動一些腳本,以便對資源進行自定義處理。其中,Model相關的函數可以用於對導入的3D模型進行處理,包括修改模型的材 ...
  • 國產系統大勢所趨,如果你公司的winform界面軟體需要在linux上運行,如果軟體是用C#開發的,現在我有一個好的快速解決方案。 世界第一的微軟的Microsoft Visual Studio,確實好用,C# 開發起來確實效率高,不過微軟的開發語言開發的軟體的界面都是跟windows系統綁定的,現 ...
  • 2013年7月1日,痞子衡應屆畢業正式入職飛思卡爾半導體上海 Site,至今已經十年零七天。 上周六是整十年的日子,當時並沒有特別的感覺,但是過去的一周總有種情愫在醞釀,終於今天還是決定花點時間回憶下過去的十年,梳理下那些值得紀念的時刻。 ### Offer抉擇 時間撥回到 2012 年的秋天,痞子 ...
  • 一、影響 Linux 性能的各種因素 1、系統硬體資源 (1)CPU 如何判斷多核 CPU 與超線程 消耗 CPU 的業務:動態 web 服務、mail 服務 (2)記憶體 物理記憶體與 swap 的取捨 選擇 64 位 Linux 操作系統 消耗記憶體的業務:記憶體資料庫(redis/hbase/mong ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...