日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

hql中获取前一天的数据_PostgreSql 怎么获取数据库中关键系统信息(一)

發(fā)布時間:2023/12/2 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hql中获取前一天的数据_PostgreSql 怎么获取数据库中关键系统信息(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如何通過SQL 的方式獲得數(shù)據(jù)庫中的一些關(guān)鍵信息,是一個DB最正常的工作,如何通過一些SQL來獲得PG的一些關(guān)鍵的參數(shù)和信息或者是數(shù)據(jù)庫中的一些信息是需要知道的一件事情。以下是部分 1

一般來說每種數(shù)據(jù)庫中都有一個或幾個系統(tǒng)的數(shù)據(jù)庫,在PG中schemaname 以 pg_catalog開頭都是系統(tǒng)表,通過系統(tǒng)表我們就可以了解的大部分?jǐn)?shù)據(jù)庫系統(tǒng)所做的事情

1? 查看當(dāng)前所有的表(用戶表)

SELECT relname

? FROM pg_class

?WHERE relname !~ '^(pg_|sql_)'

? ?AND relkind = 'r';

或者

也可以

SELECT table_name

? FROM information_schema.tables

?WHERE table_type = 'BASE TABLE'

? ?AND table_schema NOT IN

? ? ? ?('pg_catalog', 'information_schema');

2? 查看用戶建立的VIEW

SELECT table_name

? FROM information_schema.views

?WHERE table_schema NOT IN ('pg_catalog', 'information_schema')

? ?AND table_name !~ '^pg_';

3 當(dāng)前數(shù)據(jù)庫的用戶

SELECT usename? FROM pg_user;

4 列出某個表的字段

SELECT a.attname

? FROM pg_class c, pg_attribute a, pg_type t

?WHERE c.relname = '表名'

? ?AND a.attnum > 0

? ?AND a.attrelid = c.oid

? ?AND a.atttypid = t.oid

5 查詢表的索引

SELECT relname, indkey

? FROM pg_class, pg_index

?WHERE pg_class.oid = pg_index.indexrelid

? ?AND pg_class.oid IN (

? ? SELECT indexrelid

? ? ? FROM pg_index, pg_class

? ? ?WHERE pg_class.relname='表名'

? ? ? ?AND pg_class.oid=pg_index.indrelid

? ? ? ?AND indisunique != 't'

? ? ? ?AND indisprimary != 't'

);

查詢這個表的那些字段,被建立了索引

SELECT t.relname, a.attname, a.attnum

? ? ?FROM pg_index c

LEFT JOIN pg_class t

? ? ? ?ON c.indrelid? = t.oid

LEFT JOIN pg_attribute a

? ? ? ?ON a.attrelid = t.oid

? ? ? AND a.attnum = ANY(indkey)

? ? WHERE t.relname = '表名'

? ? ?;

獲得當(dāng)前數(shù)據(jù)庫表的建立索引的語句

SELECT

? ? tablename,

? ? indexname,

? ? indexdef

FROM

? ? pg_indexes

WHERE

? ? schemaname = 'public'

ORDER BY

? ? tablename,

? ? indexname;

6 系統(tǒng)中指定表建立的約束

SELECT constraint_name, constraint_type

? FROM information_schema.table_constraints

?WHERE table_name = '表名';

7 對某個約束的詳細(xì)信息的展示

SELECT c.conname AS constraint_name,

??????????CASE c.contype

????????????WHEN 'c' THEN 'CHECK'

????????????WHEN 'f' THEN 'FOREIGN KEY'

????????????WHEN 'p' THEN 'PRIMARY KEY'

????????????WHEN 'u' THEN 'UNIQUE'

??????????END AS "constraint_type",

??????????CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS is_deferrable,

??????????CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS is_deferred,

??????????t.relname AS table_name,

??????????array_to_string(c.conkey, ' ') AS constraint_key,

??????????CASE confupdtype

????????????WHEN 'a' THEN 'NO ACTION'

????????????WHEN 'r' THEN 'RESTRICT'

????????????WHEN 'c' THEN 'CASCADE'

????????????WHEN 'n' THEN 'SET NULL'

????????????WHEN 'd' THEN 'SET DEFAULT'

??????????END AS on_update,

??????????CASE confdeltype

????????????WHEN 'a' THEN 'NO ACTION'

????????????WHEN 'r' THEN 'RESTRICT'

????????????WHEN 'c' THEN 'CASCADE'

????????????WHEN 'n' THEN 'SET NULL'

????????????WHEN 'd' THEN 'SET DEFAULT'

??????????END AS on_delete,

??????????CASE confmatchtype

????????????WHEN 'u' THEN 'UNSPECIFIED'

????????????WHEN 'f' THEN 'FULL'

????????????WHEN 'p' THEN 'PARTIAL'

??????????END AS match_type,

??????????t2.relname AS references_table,

??????????array_to_string(c.confkey, ' ') AS fk_constraint_key

?????FROM pg_constraint c

LEFT JOIN pg_class t? ON c.conrelid? = t.oid

LEFT JOIN pg_class t2 ON c.confrelid = t2.oid

????WHERE t.relname = '表名'

?????AND c.conname = '約束名';

8 列出相關(guān)的自增序列

SELECT relname

? FROM pg_class

?WHERE relkind = 'S'

? ?AND relnamespace IN (

? ? ? ? SELECT oid

? ? ? ? ? FROM pg_namespace

? ? ? ? ?WHERE nspname NOT LIKE 'pg_%'

? ? ? ? ? ?AND nspname != 'information_schema'

);

9 篩選相關(guān)數(shù)據(jù)庫中建立的trigger

SELECT DISTINCT trigger_name

??FROM information_schema.triggers

?WHERE trigger_schema NOT IN

???????('pg_catalog', 'information_schema');

以及關(guān)于 trigger 詳細(xì)的信息

SELECT *

? FROM information_schema.triggers

?WHERE trigger_schema NOT IN

? ? ? ?('pg_catalog', 'information_schema');

10 查看系統(tǒng)中創(chuàng)建的函數(shù)

SELECT routine_name

? FROM information_schema.routines

?WHERE specific_schema NOT IN

? ? ? ?('pg_catalog', 'information_schema')

? ?AND type_udt_name != 'trigger';

11 查看當(dāng)前數(shù)據(jù)庫中表的主鍵

?SELECT tc.constraint_name,

? ? ? ? ? tc.constraint_type,

? ? ? ? ? tc.table_name,

? ? ? ? ? kcu.column_name,

? ? ? tc.is_deferrable,

? ? ? ? ? tc.initially_deferred,

? ? ? ? ? rc.match_option AS match_type,

? ? ? ? ? rc.update_rule AS on_update,

? ? ? ? ? rc.delete_rule AS on_delete,

? ? ? ? ? ccu.table_name AS references_table,

? ? ? ? ? ccu.column_name AS references_field

? ? ?FROM information_schema.table_constraints tc

LEFT JOIN information_schema.key_column_usage kcu

? ? ? ?ON tc.constraint_catalog = kcu.constraint_catalog

? ? ? AND tc.constraint_schema = kcu.constraint_schema

? ? ? AND tc.constraint_name = kcu.constraint_name

LEFT JOIN information_schema.referential_constraints rc

? ? ? ?ON tc.constraint_catalog = rc.constraint_catalog

? ? ? AND tc.constraint_schema = rc.constraint_schema

? ? ? AND tc.constraint_name = rc.constraint_name

LEFT JOIN information_schema.constraint_column_usage ccu

? ? ? ?ON rc.unique_constraint_catalog = ccu.constraint_catalog

? ? ? AND rc.unique_constraint_schema = ccu.constraint_schema

? ? ? AND rc.unique_constraint_name = ccu.constraint_name

? ? WHERE tc.table_name !~ '^(pg_|sql_)'

? ? ? AND tc.constraint_type = 'PRIMARY KEY';

12? 獲得索引與表之間的關(guān)系

SELECT a.index_name, b.attname?

? FROM (?

? ? SELECT a.indrelid,?

? ? ? ? ? ?c.relname index_name,?

? ? ? ? ? ?unnest(a.indkey) index_num?

? ? ? FROM pg_index a,?

? ? ? ? ? ?pg_class b,?

? ? ? ? ? ?pg_class c?

? ? ?WHERE?

? ? ? ? b.oid=a.indrelid?

? ? ? ?AND a.indisprimary != 't'?

? ? ? ?AND a.indexrelid=c.oid?

? ? ? ?) a,?

? ? ? ?pg_attribute b?

?WHERE a.indrelid = b.attrelid?

? ?AND a.index_num = b.attnum and a.index_name !~ '^(pg_|sql_)'

?ORDER BY a.index_name, a.index_num

13? 顯示VIEW 之間的依賴關(guān)系

?SELECT v.relname AS "dependent_view",?

? ? ? ? ? t.relname AS "referenced_relation"

? ? ?FROM pg_depend dv?

LEFT JOIN pg_class v ON v.oid = dv.refobjid?

LEFT JOIN pg_namespace nv ON v.relnamespace = nv.oid

LEFT JOIN pg_depend dt?

? ? ? ?ON dv.classid = dt.classid

? ? ? AND dv.objid = dt.objid

? ? ? AND dv.refobjid <> dt.refobjid?

? ? ? AND dv.refclassid = dt.refclassid

? ? ? AND dv.classid = 'pg_catalog.pg_rewrite'::regclass?

? ? ? AND dv.refclassid = 'pg_catalog.pg_class'::regclass?

LEFT JOIN pg_class t ON t.oid = dt.refobjid?

LEFT JOIN pg_namespace nt?

? ? ? ?ON t.relnamespace = nt.oid

? ? ? AND nv.nspname = 'public'

? ? ? AND nt.nspname = 'public'?

? ? WHERE dv.deptype = 'i'?

? ? ? AND v.relkind = 'v'?

? ? ? AND t.relkind IN ('r', 'v')?

? ? ? AND v.relname = 'testttt' -- VIEW NAME

?GROUP BY v.relname, t.relname;?

共享電子書,需要可以加群自取

總結(jié)

以上是生活随笔為你收集整理的hql中获取前一天的数据_PostgreSql 怎么获取数据库中关键系统信息(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。