外键使用

PostgreSQL的外键深入使用

て烟熏妆下的殇ゞ 提交于 2020-04-25 16:58:14
有开发同事问及postgresql外键的用法,这里普及一下。外键是一个很基础的概念,使用得当可以对事务的一致性有很好的保障,方法上和Oracle是很接近的,作用很简单地说就是保证子表的数据都能在主表中找到,可保证数据一致性。 建立主表 postgres=# create table t_parent( postgres(# id serial primary key, postgres(# vname varchar(32), postgres(# ctime timestamp without time zone); NOTICE: CREATE TABLE will create implicit sequence "t_parent_id_seq" for serial column "t_parent.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_parent_pkey" for table "t_parent" CREATE TABLE 建立子表 postgres=# create table t_child( postgres(# cid int4, postgres(# vname varchar(32)); CREATE TABLE 查看表外键 postgres=# \d+