Postgresql 实现查询一个表/所有表的所有列名

select ordinal_position as Colorder,column_name as ColumnName,data_type as TypeName,
coalesce(character_maximum_length,numeric_precision,-1) as Length,numeric_scale as Scale,
case is_nullable when 'NO' then 0 else 1 end as CanNull,column_default as DefaultVal,
case when position('nextval' in column_default)>0 then 1 else 0 end as IsIdentity, 
case when b.pk_name is null then 0 else 1 end as IsPK,c.DeText
from information_schema.columns 
left join (
 select pg_attr.attname as colname,pg_constraint.conname as pk_name from pg_constraint 
 inner join pg_class on pg_constraint.conrelid = pg_class.oid 
 inner join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid and pg_attr.attnum = pg_constraint.conkey[1] 
 inner join pg_type on pg_type.oid = pg_attr.atttypid
 where pg_class.relname = '表名' and pg_constraint.contype='p' 
) b on b.colname = information_schema.columns.column_name
left join (
 select attname,description as DeText from pg_class
 left join pg_attribute pg_attr on pg_attr.attrelid= pg_class.oid
 left join pg_description pg_desc on pg_desc.objoid = pg_attr.attrelid and pg_desc.objsubid=pg_attr.attnum
 where pg_attr.attnum>0 and pg_attr.attrelid=pg_class.oid and pg_class.relname='表名'
)c on c.attname = information_schema.columns.column_name
where table_schema='public' and table_name='表名' order by ordinal_position asc
### 回答1: 可以使用INSERT INTO SELECT语句来实现一个的数据插入到另一个。例如: ``` INSERT INTO table2 (col1, col2, col3) SELECT col1, col2, col3 FROM table1; ``` 这条SQL语句将会把table1的col1、col2、col3列的数据插入到table2的对应列。 ### 回答2: 在PostgreSQL,可以使用多种方式来一次性写入两个。 一种方式是使用WITH子句或者临时来将数据从一个复制到另一个。例如,假设有两个table1和table2,它们具有相同的结构,我们可以使用以下的SQL语句将table1的数据一次性写入table2: WITH temp_table AS ( SELECT * FROM table1 ) INSERT INTO table2 SELECT * FROM temp_table; 另一种方式是使用INSERT INTO ... SELECT语句来将数据一次性写入多个。假设有两个table1和table2,我们可以使用以下的SQL语句将table1的数据一次性写入table2和另外一个table3: INSERT INTO table2 (column1, column2, ...) SELECT column1, column2, ... FROM table1; INSERT INTO table3 (column1, column2, ...) SELECT column1, column2, ... FROM table1; 在以上的语句,column1、column2等示要插入的列名称,需要根据的结构进行调整。 总结起来,通过使用WITH子句或者临时,或者使用INSERT INTO ... SELECT语句,我们可以将数据一次性写入到多个。具体的方法取决于具体的需求和的结构。 ### 回答3: 在PostgreSQL,可以使用JOIN语句将两个合并到一条SQL查询。 例如,假设我们有两个A和B,它们之间有一个共同的列(例如,列"ID")。 要在一条SQL语句使用这两个,可以使用INNER JOIN语句来合并它们。下面是一个例子: SELECT * FROM tableA INNER JOIN tableB ON tableA.ID = tableB.ID; 这条SQL语句以A为主,并使用INNER JOIN连接B。连接条件是根据ID列相等来进行匹配。使用"*"代替SELECT语句列名示选择所有列。 当执行此SQL查询时,它将返回两个具有相同ID值的行的组合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值