现在的位置: 首页 > 综合 > 正文

PostgreSQL special sql 1 – list all the columns and primary key

2019年04月17日 ⁄ 综合 ⁄ 共 492字 ⁄ 字号 评论关闭

There are a few special sql commands that I used often recently, mark them here.

1: Show all the columns' name

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='yourtablename';

another solution

\d 'yourtablename'

2: Show the primary key:

SELECT               
  pg_attribute.attname, 
  format_type(pg_attribute.atttypid, pg_attribute.atttypmod) 
FROM pg_index, pg_class, pg_attribute 
WHERE 
  pg_class.oid = 'nygeotwitters'::regclass AND
  indrelid = pg_class.oid AND
  pg_attribute.attrelid = pg_class.oid AND 
  pg_attribute.attnum = any(pg_index.indkey)
  AND indisprimary;

抱歉!评论已关闭.