给定一个表名,如何从plpgsql函数中提取主键列及其数据类型列表?
答案 0 :(得分:28)
上面的查询非常糟糕,因为它非常慢。
我会推荐这个官方版本:
http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
如果需要架构,则查询如下
SELECT
pg_attribute.attname,
format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
FROM pg_index, pg_class, pg_attribute, pg_namespace
WHERE
pg_class.oid = 'foo'::regclass AND
indrelid = pg_class.oid AND
nspname = 'public' AND
pg_class.relnamespace = pg_namespace.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary
答案 1 :(得分:19)
要提供直接的SQL,您可以使用以下命令列出主键列及其类型:
SELECT c.column_name, c.data_type
FROM information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
WHERE constraint_type = 'PRIMARY KEY' and tc.table_name = 'mytable';
答案 2 :(得分:6)
以下SQL
声明对我有用:
SELECT a.attname
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = 'tablename'::regclass
AND i.indisprimary;
直接取自here。
答案 3 :(得分:5)
查看pg_constraint
系统表。或者information_schema.table_constraints
查看您是否愿意接近SQL标准。
有关完整示例使用带有“-E”选项的psql
连接到数据库并键入\d <some_table>
- 您将看到用于描述表格的实际查询。
答案 4 :(得分:1)
请注意列顺序与表的列顺序不同的索引。 (即,如果主键使用了第3列,第2列和第1列)
以下查询要复杂得多,但会以正确的顺序返回列。 (删除&#39; indisprimary&#39;子句以获取表中所有索引的相同信息)
WITH ndx_list AS
(
SELECT pg_index.indexrelid
FROM pg_index, pg_class
WHERE pg_class.relname = 'test_indices_table'
AND pg_class.oid = pg_index.indrelid
AND pg_index.indisprimary
), ndx_cols AS
(
SELECT pg_class.relname AS index_name, UNNEST(i.indkey) AS col_ndx, i.indisunique, i.indisprimary
FROM pg_class, pg_index i
WHERE pg_class.oid = i.indexrelid
AND pg_class.oid IN (SELECT indexrelid FROM ndx_list)
)
SELECT ndx_cols.index_name, ndx_cols.indisunique, ndx_cols.indisprimary,
a.attname, format_type(a.atttypid, a.atttypmod), a.attnum
FROM pg_class c, pg_attribute a
JOIN ndx_cols ON (a.attnum = ndx_cols.col_ndx)
WHERE c.oid = 'test_indices_table'::regclass
AND a.attrelid = c.oid
答案 5 :(得分:1)
SELECT a.attname AS name, format_type(a.atttypid, a.atttypmod) AS type
FROM
pg_class AS c
JOIN pg_index AS i ON c.oid = i.indrelid AND i.indisprimary
JOIN pg_attribute AS a ON c.oid = a.attrelid AND a.attnum = ANY(i.indkey)
WHERE c.oid = 'example'::regclass
输出:
name | type
------+--------
id | bigint
答案 6 :(得分:0)
使用generate_subscripts
保留列顺序:
SELECT
a.attname,
format_type(a.atttypid, a.atttypmod)
FROM
pg_attribute a
JOIN (SELECT *, GENERATE_SUBSCRIPTS(indkey, 1) AS indkey_subscript FROM pg_index) AS i
ON
i.indisprimary
AND i.indrelid = a.attrelid
AND a.attnum = i.indkey[i.indkey_subscript]
WHERE
a.attrelid = 'your_table'::regclass
ORDER BY
i.indkey_subscript
答案 7 :(得分:0)
SELECT
conrelid::regclass AS table_from,
conname,
pg_get_constraintdef ( c.oid )
FROM
pg_constraint c
JOIN pg_namespace n ON n.oid = c.connamespace
WHERE
contype IN ( 'f', 'p ' )
AND conrelid::regclass::TEXT IN ( 'foo' )
ORDER BY
conrelid::regclass::TEXT,
contype DESC
答案 8 :(得分:0)
您实际上只需要2个系统表:
注意:系统表可能会在PostgreSQL版本之间进行更改,但是这种情况并不经常发生(实际上很少发生)。与使用information_schema.table_constraints不同,您不需要任何特殊权限,只需在表上进行选择即可。 (这已在Postgres 10.6中进行了测试)
SELECT string_agg(a.attname, ', ') AS pk
FROM
pg_constraint AS c
CROSS JOIN LATERAL UNNEST(c.conkey) AS cols(colnum) -- conkey is a list of the columns of the constraint; so we split it into rows so that we can join all column numbers onto their names in pg_attribute
INNER JOIN pg_attribute AS a ON a.attrelid = c.conrelid AND cols.colnum = a.attnum
WHERE
c.contype = 'p' -- p = primary key constraint
AND c.conrelid = '<schemaname>.<tablename>'::REGCLASS; -- regclass will type the name of the object to its internal oid
答案 9 :(得分:0)
\d tablename
将为您提供主键信息以及其他与表相关的信息,例如所有列,它们的类型,关联的索引,约束,规则,触发器等。 您可能不需要所有这些信息,但这是一目了然地获取所有详细信息的最快方法,请参阅更多详细信息here。
它返回如下内容:
Table "public.tablename"
Column | Type | Modifiers
--------+---------+-----------
col1 | text | not null
col2 | numeric |
col3 | text |
col4 | text |
col5 | numeric |
Indexes:
"tablename_pkey" PRIMARY KEY, btree (col1)