PostgreSQL的知识学习


1.PostgreSQL中的serial

等效于 MySQL 里头的 int auto_increment ,比如:

create table (
  id int auto_increment
);

等效于 PostgreSQL 里头的:

create table (
  id serial
);
CREATE TABLE foo (
  id SERIAL,
  bar varchar
);

INSERT INTO "foo" (bar) values ('blah');
INSERT INTO "foo" (bar) values ('blah');

SELECT * FROM foo;
1,blah
2,blah
参考链接:
2.PostgreSQL的建表、插入操作
create table company(
   id int primary key     not null,
   name           text    not null,
   age            int     not null,
   address        char(50),
   salary         real,
   join_date	  date
);

insert into company (id,name,age,address,salary,join_date) values (1, 'paul', 32, 'california', 20000.00 ,'2001-07-13');
insert into company (id,name,age,address,join_date) values (2, 'allen', 25, 'texas', '2007-12-13');
insert into company (id,name,age,address,salary,join_date) values (3, 'teddy', 23, 'norway', 20000.00, default );
insert into company (id,name,age,address,salary,join_date) values (4, 'mark', 25, 'rich-mond ', 65000.00, '2007-12-13' ), (5, 'david', 27, 'texas', 85000.00 , '2007-12-13');
参考链接:
3.如何快速获取PostgreSQL数据库中行的个数?
cur.execute("select reltuples from pg_class where oid = 'public.%s'::regclass;" % (table_name,))
cur.execute("select n_live_tup from pg_stat_user_tables where relname=%s;" % (table_name,))
cur.execute("select count(*) from %s;" % (table_name,))
4.PostgreSQL常见/有用的操作命令
1.查找最大的表
# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC;
# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC limit 1;

relname – name of the relation/table.
relpages - relation pages ( number of pages, by default a page is 8kb )
pg_class – system table, which maintains the details of relations
limit 1 – limits the output to display only one row.

2.计算PostgreSQL数据库在磁盘中所占大小
# SELECT pg_database_size('geekdb');
# SELECT pg_size_pretty(pg_database_size('geekdb'));

3.计算某个表在磁盘中所占大小
# SELECT pg_size_pretty(pg_total_relation_size('big_table'));
# SELECT pg_size_pretty(pg_relation_size('big_table'));	#不包括索引

4.查看某一表的所有索引信息
# \d table_name

5.快速产生大量连续数字并插入表中
# INSERT INTO numbers (num) VALUES ( generate_series(1,1000));

6.计算某个表中行的个数
# select count(*) from table;
# select count(col_name) from table;	#列col_name中非空行的个数
# select count(distinct col_name) from table;

7.计算某个表某一列的最值
# select max(col_name) from table;
# SELECT MAX(num) from number_table where num < ( select MAX(num) from number_table );	#次大值
# select min(col_name) from table;
# SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table );	#次小值

8.查看PostgreSQL中有哪些可用的基础数据类型
# SELECT typname,typlen from pg_type where typtype='b';

typname – name of the datatype
typlen – length of the datatype

9.将查询的结果导出至文件
# \o output_file
# SELECT * FROM pg_class;
# \o

10.查看当前所在数据库
# SELECT current_database();
# \conninfo
5.相关调试信息
6.PostgreSQL中的show databases命令:“postgresql show databases of select”
mysql: show databases;
postgresql: \l
postgresql: SELECT datname FROM pg_database;
postgresql: SELECT datname FROM pg_database WHERE datistemplate = false;
7.PostgreSQL中的show tables命令:“postgresql show tables of select”
mysql: show tables;
postgresql: \d
postgresql: SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
postgresql: SELECT table_schema || '.' || table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema');
postgresql: SELECT table_schema,table_name FROM information_schema.tables where table_schema = 'public' ORDER BY table_schema,table_name;
8.PostgreSQL中的describe table命令:“postgresql show tables of select”、“postgresql show columns”
mysql: show columns;
postgresql: \d table
postgresql: SELECT column_name FROM information_schema.columns WHERE table_name ='table';
postgresql: select column_name from information_schema.columns;
postgresql: select table_name, column_name from information_schema.columns;
postgresql: select column_name from information_schema.columns where table_name='pg_proc';
9.PostgreSQL中的select top N命令:“postgresql show tables of select”
10.如何在PostgreSQL中查询另外一个数据库的表内容?

比如在MySQL中是:

Current_DB_1 #> Select * From DB_2.TABLE_2;
参考信息:

不能直接查,需要安装dblink,或者postgres_fdw

# With PostgreSQL 9.1 or later, installation of additional modules has been simplified.

安装 dblink 扩展
postgres=# create extension dblink;
如果提示“CREATE EXTENSION”,说明安装成功。

安装 postgres_fdw 扩展
postgres=# create extension postgres_fdw;
如果提示“CREATE EXTENSION”,说明安装成功。

因为PostgreSQL更推荐一个db,下面建多个schema;和Oracle类似,MySQL没有schema的概念。

参考链接:

《“PostgreSQL的知识学习”》 有 1 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注