使用sql獲取各數據庫(MySQL、PostgreSQL、Oracle、MsSQL)的表結構

一般咱們提到數據庫表結構的時候,比較關心的幾個屬性:
字段名稱、類型、長度、是否主鍵、是否自增、是否爲空、默認值、備註。
那麼,使用SQL語句獲取這些屬性,在各主流數據庫下是什麼樣的呢?

MySQL數據庫

做爲時下最流行的數據庫,MySQL的獲取SQL是最簡單的。
通常咱們用下面一行語句就能搞定:git

SHOW FULL COLUMNS FROM xxx;

固然,也能夠用MySQL自帶的數據庫information_schema中的表,例如:columns等查詢更全的信息。github

PostgreSQL數據庫

使用其兩個最重要的Schema(information_schema、pg_catalog)下表,進行關聯查詢,獲取經常使用屬性。數據庫

SELECT d.column_name AS "Field", d.udt_name AS "Type", COALESCE(d.character_maximum_length, d.numeric_precision, d.datetime_precision) AS "Length", 
  CASE WHEN t.conname IS NOT NULL THEN 'PRI' ELSE '' END AS "Key", 
  CASE WHEN s.extra IS NOT NULL THEN 'auto_increment' ELSE '' END "Extra", 
  d.is_nullable AS "Null", f.adsrc AS "Default", col_description(a.attrelid, a.attnum) AS "Comment"
 FROM information_schema.columns d, pg_class c, pg_attribute a
 LEFT JOIN pg_constraint t ON (a.attrelid = t.conrelid AND t.contype = 'p' AND a.attnum = t.conkey[1])
 LEFT JOIN pg_attrdef f ON (a.attrelid = f.adrelid AND a.attnum = f.adnum)
 LEFT JOIN (SELECT 'nextval(''' || c.relname || '''::regclass)' AS extra FROM pg_class c WHERE c.relkind = 'S') s ON f.adsrc = s.extra
WHERE a.attrelid = c.oid
AND a.attnum > 0
AND c.relname = d.table_name
AND d.column_name = a.attname
AND c.relname = 'xxx'
ORDER BY a.attnum;

Oracle數據庫

系統表:user_col_comments能提供大部分信息,只有:備註(Comment)、主鍵(Key)須要關聯其餘表進行獲取。框架

SELECT t.column_name AS "Field", t.data_type AS "Type", t.data_length AS "Length", 
    CASE WHEN k.column_name IS NOT NULL THEN 'PRI' ELSE '' END AS "Key", 
    CASE WHEN t.identity_column = 'YES' THEN 'auto_increment' ELSE '' END AS "Extra",
    CASE WHEN t.nullable = 'N' THEN 'NO' ELSE 'YES' END AS "Null",
    t.data_default AS "Default", c.comments AS "Comment"
 FROM user_col_comments c, user_tab_cols t
LEFT JOIN (
    SELECT u.table_name, c.column_name 
     FROM user_constraints u, user_cons_columns c
    WHERE u.table_name = c.table_name
    AND u.constraint_name = c.constraint_name
    AND u.constraint_type = 'P') k ON t.table_name = k.table_name AND t.column_name = k.column_name
WHERE t.table_name = c.table_name
AND t.column_name = c.column_name 
AND t.table_name = 'xxx'
ORDER BY t.column_id;

MsSQL數據庫

MsSQL即SQL Server數據庫。MsSQL相對更加複雜一些,關聯的系統表也最多。ide

SELECT a.name AS 'Field', b.name AS 'Type', COLUMNPROPERTY(a.id,a.name,'PRECISION') AS 'Length', 
  CASE WHEN p.name IS NOT NULL THEN 'PRI' ELSE '' END 'Key',
  CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN 'auto_increment' ELSE '' END AS 'Extra',
  CASE WHEN a.isnullable=1 THEN 'YES' ELSE 'NO' END AS 'Null',
  e.text AS 'Default', g.[value] AS 'Comment'
FROM sysobjects d, syscolumns a
LEFT JOIN systypes b ON a.xusertype = b.xusertype
LEFT JOIN syscomments e ON a.cdefault = e.id
LEFT JOIN sys.extended_properties g ON (a.id = G.major_id AND a.colid = g.minor_id)
LEFT JOIN (
 SELECT s.name, k.id, k.colid FROM sysindexkeys k, sysindexes i, sysobjects s
 WHERE k.indid = i.indid
 AND s.name = i.name
 AND s.xtype = 'PK'
 ) p ON (p.id = a.id AND p.colid = a.colid)
WHERE d.id = a.id
AND d.xtype = 'U'
AND d.name = 'xxx'
ORDER BY a.colorder;

注:以上均參考MySQL的屬性名輸出。xxx爲須要查詢的表名。

diboot 簡單高效的輕代碼開發框架 (求star)code

相關文章
相關標籤/搜索