Postgresql流水帐(第四天): DDL 限制约束

CREATE TABLE products (

product_no integer,

name text,

price numeric CONSTRAINT positive_price CHECK (price > 0)

);

CHECK 返回bool 值。

外键参照完整性、引用完整性

A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the?referential integrity?between two related tables.

CREATE TABLE orders (

order_id integer PRIMARY KEY,

product_no integer REFERENCES products (product_no),

quantity integer

);

下面是一个装逼的例子:

CREATE TABLE t1 (

a integer PRIMARY KEY,

b integer,

c integer,

FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)

);

A table can have more than one foreign key constraint. This is used to implement many-to-many relationships between tables. Say you have tables about products and orders, but now you want to allow one order to contain possibly many products (which the structure above did not allow). You could use this table structure:

一个表里可以包含多个外键,这通常用来实现表之间的多对多关系。例如:一个order 可以包含多个products, 一类product 可以属于多个order:

CREATE TABLE products (

product_no integer PRIMARY KEY,

name text,

price numeric

);

?

CREATE TABLE orders (

order_id integer PRIMARY KEY,

shipping_address text,

...

);

?

CREATE TABLE order_items (

product_no integer REFERENCES products,

order_id integer REFERENCES orders,

quantity integer,

PRIMARY KEY (product_no, order_id)

);

?

当两个表发生了关系后,这两个表就会受到一些限制,具体要看他们是如何谈妥的, 例如:

CREATE TABLE products (
    product_no integer PRIMARY KEY,
    name text,
    price numeric
);

?

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    shipping_address text,
    ...
);

?

CREATE TABLE order_items (
    product_no integer REFERENCES products ON DELETE RESTRICT,
    order_id integer REFERENCES orders ON DELETE CASCADE,
    quantity integer,
    PRIMARY KEY (product_no, order_id)
);

当 items 表有 某product 时,products 表里的这个product 是不允许被删除的。

当 orders 表的某个 order 被删除时, items 表里含该 order_id 的记录会被自动删除。

Restricting and cascading deletes are the two most common options.?RESTRICT?prevents deletion of a referenced row.?NO ACTION?means that if any referencing rows still exist when the constraint is checked, an error is raised; this is the default behavior if you do not specify anything. (The essential difference between these two choices is that?NO ACTION?allows the check to be deferred until later in the transaction, whereas?RESTRICT?does not.)?CASCADE?specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well. There are two other options:?SET NULL?and?SET DEFAULT. These cause the referencing column(s) in the referencing row(s) to be set to nulls or their default values, respectively, when the referenced row is deleted. Note that these do not excuse you from observing any constraints. For example, if an action specifies?SET DEFAULT?but the default value would not satisfy the foreign key constraint, the operation will fail.

Analogous to?ON DELETE?there is also?ON UPDATE?which is invoked when a referenced column is changed (updated). The possible actions are the same. In this case,?CASCADE?means that the updated values of the referenced column(s) should be copied into the referencing row(s).

Normally, a referencing row need not satisfy the foreign key constraint if any of its referencing columns are null. If?MATCH FULL?is added to the foreign key declaration, a referencing row escapes satisfying the constraint only if all its referencing columns are null (so a mix of null and non-null values is guaranteed to fail a?MATCH FULL?constraint). If you don‘t want referencing rows to be able to avoid satisfying the foreign key constraint, declare the referencing column(s) as?NOT NULL.

A foreign key must reference columns that either are a primary key or form a unique constraint. This means that the referenced columns always have an index (the one underlying the primary key or unique constraint); so checks on whether a referencing row has a match will be efficient. Since a?DELETE?of a row from the referenced table or an?UPDATE?of a referenced column will require a scan of the referencing table for rows matching the old value, it is often a good idea to index the referencing columns too. Because this is not always needed, and there are many choices available on how to index, declaration of a foreign key constraint does not automatically create an index on the referencing columns.

More information about updating and deleting data is in?Chapter 6. Also see the description of foreign key constraint syntax in the reference documentation for?CREATE TABLE.

?

时间: 2024-08-27 04:11:54

Postgresql流水帐(第四天): DDL 限制约束的相关文章

Postgresql流水帐(第六天):view

CREATE OR REPLACE view_name AS query DROP VIEW [ IF EXISTS ] view_name; 一个复杂的 query: SELECT cu.customer_id AS id, ????(((cu.first_name)::text || ' '::text) || (cu.last_name)::text) AS name, ????a.address, ????a.postal_code AS "zip code", ????a.p

Postgresql流水帐(第五天):增删查改

增:insert INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99), (2, 'Bread', 1.99), (3, 'Milk', 2.99); 可以一次插入多行数据. INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT); INSERT INTO products DEFAULT VALUES;

Postgresql流水帐(第七天):Trigger

创建一个函数 创建一个trigger, 将表和函数绑定 ? ALTER TRIGGER trigger_name ON table_name RENAME TO new_name; ? ALTER TRIGGER last_name_changes ON employees RENAME TO log_last_name_changes; ? ALTER TABLE table_name DISABLE TRIGGER trigger_name | ALL You specify the tri

PostgreSQL Replication之第四章 设置异步复制(1)

执行完您的第一个即时恢复(PITR,Point-In-Time-Recovery),我们准备在一个真正的复制设置上工作.在本章,您将学会如何设置异步复制和流.我们的目标是确保您可以实现更高的高可用和更高的数据安全性. 在本章,我们将讨论以下主题: • 配置异步复制 • 理解流 • 合并流和归档 • 管理时间线 在本章的最后,您将很容易地在几分钟内设置流复制. 4.1 设置流复制 在前面章节中,我们已经从简单的16MB XLOG文件做了恢复.从逻辑上讲,重放进程一次只能重放16MB.这在您的复制设

PostgreSQL Replication之第四章 设置异步复制(4)

4.4 基于流和基于文件的恢复 生活并不总只是黑色或白色:有时也会有一些灰色色调.对于某些情况下,流复制可能恰到好处.在另一些情况下,基于文件复制和PITR是您所需要的.但是也有许多情况下,您既需要流复制,也需要基于文件的复制.一个例子是:当您较长一段时间中断复制,您可能想再次使用归档来重新同步(resync)slave,而不是再次执行完全的基础备份.这也可能是有用的---保留一个归档一段时间以后调查或重放操作.好消息是PostgreSQL允许您混合基于文件和基于流的复制.您没有必要决定基于流的

PostgreSQL Replication之第四章 设置异步复制(7)

4.7 冲突管理 在PostgreSQL中,流复制数据仅在一个方向流动.XLOG由master提供给几个slave,这些slave消耗事务日志并为您提供一个较好的数据备份.您可能想知道这怎么会导致冲突,这会发生冲突. 考虑一下情形:如您所知,数据复制有很小的延迟.因此,XLOG在由master产生之后结束于slave.这微小的延迟会引起如下图所示的情景: 我们假设一个slave开始读取一个表.它是一个长读操作.与此同时,master收到一个请求,实际地删除那个表.这有一点问题,因为slave仍然

postgresql学习笔记(四)角色

1.在PostgreSQL安装过程中的数据初始化阶段,系统会默认创建一个名为postgres的角色(同时会创建一个名为postgres的同名database). 2.可以通过ident身份验证机制来将操作系统的的root用户映射到数据的Postgresql角色,这样可以实现root用户无密码直接登录 3.创建具备登录权限的角色 postgres=# create role leo login password 'king' createdb valid until 'infinity'; val

PostgreSQL Replication之第四章 设置异步复制(2)

4.2 配置级联复制 正如您在本章已经看到的,设置流复制真的很容易.只需要设置几个参数,做一个基础备份,并享受您的复制设置. 在许多情况下,这种情况更有一点点微妙.在这个例子中我们假设:我们要使用一个master传送数据到几十台服务器.复制的开销其实很小(通常的说法是一个slave的开销是3%左右),但是您做小的事情是足够了,它仍然可能是一个问题.对100个 slave来说这绝对没有任何益处. 另一个用例是一个地方的master和在 另一个地方的多个slave.一遍又一遍地长距离发送大量的数据是

PostgreSQL Replication之第四章 设置异步复制(3)

4.3 slave到master的切换 如果您想扩展读或您想做一个数据备份,一个 slave是件美好的事情.但是,slave可能不会一直是slave.在有些时候,您可能需要把slave转换为master.PostgreSQL提供了一些简单的方法来做到这一点.第一个也是最有可能的最便捷的方法把一个slave转换为一个master是使用pg_ctl: iMac:slavehs$ pg_ctl -D . promote server promoting iMac:slavehs$ psql test