照顾好你的数据,数据库也会照顾你。保持数据库的整洁,查询起来也会更快,应用也会少些错误。半夜被叫醒解决数据问题并不酷。接下来,就和章郎虫博主一起来了解postgresql的表和数据吧。
一、选择一个好的数据库对象名(choosing good names for database objects)让其他人可以快速了解数据库的最简单方法就是给数据库各对象取一个有意义的名字。具体注意事项可以参考《postgresql-9-admin-cookbook》的96页。
在postgresql中,标准的索引表的格式是:{tablename}_{columnname(s)}_{suffix} ,即{表名}_{列名}_{后缀}。后缀有pkey、key、excl、idx和seq几种,分别对应主键约束、唯一约束、排他性约束、其它类型的索引和序列。
postgresql中的表可以同时包含多个触发器。触发器名中可以包含一些动作,比如update、delete等。触发器一个有用的命名规范格式为:{tablename}_{actionname}_{after|before}__trig 。
二、处理包含引用名的对象(handling objects with quoted names)博主第一次看到这个标题,实在不明白是什么意思(英语不好),不过看了以下例子大家应该就会明白。
首先创建包含引用的对象,create table “mycust” as select * from cust;
然后用下面几个语句查询,可以发现都出现了相同的错误。
postgres=# select count(*) from mycust;
error: relation “mycust” does not exist
line 1: select * from mycust;
postgres=# select count(*) from mycust;
error: relation “mycust” does not exist
line 1: select * from mycust;
而这个是对的。
postgres=# select count(*) from “mycust”;
count
——-
5
(1 row)
从上面这个例子可以发现,如果创建对象的时候名字中用了引号,那么查询时也一定要包含引号。而且postgresql中对象名对大小写不敏感,也就是说“select * from mycust;”、“select * from mycust;”和“select * from mycust;”是一样的。
三、执行相同的名称,相同的列定义(enforcing same name, same column definition)两个比较复杂的sql。
columns?:
we can identify columns that are defined in different ways in different tables using a query
against the catalog.
select table_schema,table_name,column_name,data_type ||coalesce(‘ ‘ || text(character_maximum_length), ”) ||coalesce(‘ ‘ || text(numeric_precision), ”) ||coalesce(‘,’ || text(numeric_scale), ”) as data_type from information_schema.columns
where column_name in(
select column_name from
(select column_name,data_type,character_maximum_length,numeric_precision,numeric_scale from information_schema.columns
where table_schema = ‘public’
group by column_name,data_type,character_maximum_length,numeric_precision,numeric_scale
) derived
group by column_name
having count(*) > 1
) and table_schema not in (‘information_schema’, ‘pg_catalog’)
order by column_name ;
tables:
the following query looks for all tables of the same name (and
hence in different schemas) that have different definitions.
select table_schema,table_name,column_name,data_type from information_schema.columns
where table_name in
(select table_name from
(select table_schema,table_name,string_agg(‘ ‘||column_name||’ ‘||data_type) from information_schema.columns
group by table_schema,table_name
) def
group by table_name
having count(*) > 1
) order by table_name,table_schema,column_name;
四、识别和去除重复定义(identifying and removing duplicates)关系型数据库中可以标识唯一的数据项,但是可能不知道什么原因,数据中会出现重复。
比如这个例子,在customerid就有重复的数据。
postgres=# select * from cust;
customerid | firstname | lastname | age
———— ———– ———- —–
1 | philip | marlowe | 38
2 | richard | hannay | 42
3 | holly | martins | 25
4 | harry | palmer | 36
4 | mark | hall | 47
(5 rows)
这里可以用下面这个语句找出重复的数据。
select * from cust where customerid in (select customerid from cust group by customerid having count(*) > 1);
找出重复数据后可以对这些数据进行更新或者删除。
五、防止出现重复行(preventing duplicate rows)从四中我们可以知道,数据库可能会出现重复的数据项。如果我们不想让某列出现重复,那么我们可以在定义数据库表的时候进行唯一限制。具体有下面几种方法。
1.创建主键
alter table newcust add primary key(customerid);
运行后创建新索引newcust_pkey 。
2.创建唯一约束
alter table newcust add unique(customerid);
运行后创建新索引newcust_customerid_key 。
3.创建唯一索引
create unique index on newcust (customerid);
运行后创建新索引newcust_customerid_idx 。
六、在一组数据中找出唯一键(finding a unique key for a set of data)没有工具,我们找唯一键可能也会很快,比如看列的名字、外键就可以了。这里我们使用postgresql提供的optimizer statistics。
postgresql=# analyze article ;
analyze
postgresql=# select attname,n_distinct fro
浅析影响网站自然搜索流量的因素网站加载速度很慢呐帮忙看下什么原因app需要的云服务器配置如何保护电脑安全设置摄像头禁止使用新基建时代需要怎样的数据中心?小兔买菜获千万美元A轮融资,线上市场发展强势虚拟主机跟vps有哪些区别?虚拟主机跟vps有何不同?为什么过了一段时间就远程桌面连接不上了