您好!欢迎来天下评测网-推荐优质主机-揭露骗子垃圾主机免费收录IDC商家
首页 >> 编程语言

PostgreSQL导出数据库,成SQL语句

   c.conname,
   pg_catalog.pg_get_constraintdef(c.oid, true) AS consrc,
   ns1.nspname as p_schema,
   r1.relname as p_table,
   ns2.nspname as f_schema,
   r2.relname as f_table,
   f1.attname as p_field,
   f1.attnum AS p_attnum,
   f2.attname as f_field,
   f2.attnum AS f_attnum,
   pg_catalog.obj_description(c.oid, 'pg_constraint') AS constcomment,
   c.conrelid,
   c.confrelid
   FROM
   pg_catalog.pg_constraint AS c JOIN pg_catalog.pg_class AS r1 ON (c.conrelid=r1.oid)
   JOIN
   pg_catalog.pg_attribute AS f1 ON (f1.attrelid=r1.oid AND (f1.attnum=c.conkey[1]))
   JOIN
   pg_catalog.pg_namespace AS ns1 ON r1.relnamespace=ns1.oid
   LEFT JOIN
   ( pg_catalog.pg_class AS r2 JOIN pg_catalog.pg_namespace AS ns2 ON (r2.relnamespace=ns2.oid) ) ON (c.confrelid=r2.oid)
   LEFT
   JOIN pg_catalog.pg_attribute AS f2 ON (f2.attrelid=r2.oid AND ((c.confkey[1]=f2.attnum AND c.conkey[1]=f1.attnum))) WHERE r1.relname = 'products(表名)' AND ns1.nspname='public' ORDER BY 1";

/*==========获取主键=====*/



/*==========导出所有表=====*/
$query="select * from pg_tables where tableowner='gongkai'";

/*==========导出所有表=====*/



/*==========导出所有表字段=====*/
$sql="select column_name,column_default,data_type,character_maximum_length from information_schema.columns where table_name = 'users(表名)'";
/*==========导出所有表字段=====*/

/*==========导出所有表索引=====*/
$sql="select * from pg_indexes where tablename='users(表名)'";
/*==========导出所有表索引=====*/

/*==========导出表序列=====*/
$sql="select nextval('good_id_seq') as keys";
$result=pg_query($sql);
$row = pg_fetch_array($result);

$sql="select setval('good_id_seq',".$row['keys'].",false)";
$result=pg_query($sql);
print_R($row);
/*==========导出表序列=====*/