PostgreSQL教程--数据库查询表是否存在数据库中,表字段信息及主外键
-- 检查表是否存在于数据库中
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'your_table_name'
);
-- 获取表的字段信息
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'your_table_name';
-- 获取表的主键信息
SELECT k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k USING (constraint_name, table_schema, table_name)
WHERE t.constraint_type = 'PRIMARY KEY'
AND t.table_schema = 'public'
AND t.table_name = 'your_table_name';
-- 获取表的外键信息
SELECT t.constraint_name, k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k USING (constraint_name, table_schema, table_name)
WHERE t.constraint_type = 'FOREIGN KEY'
AND t.table_schema = 'public'
AND t.table_name = 'your_table_name';
替换your_table_name
为你需要查询的表名。这些查询会返回表的存在性、字段信息、主键和外键信息。
评论已关闭