-1

我想创建一个包含人、房子和家庭的表格,其中只有来自同一家庭的人才能住在同一所房子里。

到目前为止,我所拥有的内容不起作用,因为我只能发布具有独特家庭和房屋的一行。有没有办法做到这一点?

CREATE TABLE familyhouse (
  person VARCHAR(64),
  house VARCHAR(64),
  family VARCHAR(64),
  unique(house,family)
);

正确表格示例:

man,'1','1'
man2,'1','1'
man3,'1','1'
man4,'2','2'
man5,'2','2'
man6,'3','3'

不正确的表格示例:

man,'1','1'
man2,'1','1'
man3,'1','2'
4

3 回答 3

2

我会利用外键的力量,将房子和家庭放在他们自己的桌子(family_house)和居民的单独桌子上。

CREATE TABLE family_house (
  house VARCHAR(128) NOT NULL UNIQUE,
  family VARCHAR(64) NOT NULL,
  PRIMARY KEY (house, family)
);

CREATE TABLE residents (
  person VARCHAR(64),
  house VARCHAR(128),
  family VARCHAR(64),
  UNIQUE (person, house, family),
  FOREIGN KEY (house, family) REFERENCES family_house
);

这样我就可以在同一个家中有多个居民,但一个家只能有一个家庭。

于 2012-11-01T16:35:10.417 回答
1

您可以使用CHECK CONSTRAINT来维护这一点:

CREATE TABLE familyhouse (
  person VARCHAR(64),
  house VARCHAR(64),
  family VARCHAR(64)
);
CREATE FUNCTION CheckFamilyHouse(VARCHAR(64), VARCHAR(64))
RETURNS BOOLEAN AS $$
    SELECT CASE WHEN EXISTS 
                        (   SELECT  1
                            FROM    FamilyHouse
                            WHERE   Family = $1
                            AND     House != $2
                        )
                THEN false
                ELSE true
            END
$$ LANGUAGE SQL;

ALTER TABLE familyHouse 
ADD CONSTRAINT CHK_FamilyHouse
CHECK(CheckFamilyHouse(family, house));

With the above in place the second insert below will fail:

INSERT INTO familyhouse VALUES(1, 1, 1);
INSERT INTO FamilyHouse VALUES(2, 2, 1);

with the message:

ERROR: new row for relation "familyhouse" violates check constraint "chk_familyhouse": INSERT INTO FamilyHouse VALUES(2, 2, 1)

SQL Fiddle Example

于 2012-11-01T16:43:15.780 回答
0
create table house (
    id serial primary key
);

create table family (
    id serial primary key
);

create table house_family (
    house_id integer,
    family_id integer,
    primary key (house_id, family_id),
    foreign key (house_id) references house (id),
    foreign key (family_id) references family (id)
);

create table person (
    id serial primary key,
    family_id integer,
    house_id integer,
    foreign key (house_id, family_id) references house_family (house_id, family_id)
);

insert into house values (1),(2),(3);
insert into family values (1),(2),(3);
insert into house_family values (1,1),(2,2),(3,3);
insert into person (family_id, house_id) values (1,1),(1,1);

select * from house;
 id 
----
  1
  2
  3

select * from family;
 id 
----
  1
  2
  3

select * from house_family;
 house_id | family_id 
----------+-----------
        1 |         1
        2 |         2
        3 |         3

select * from person;
 id | family_id | house_id 
----+-----------+----------
  5 |         1 |        1
  6 |         1 |        1

现在,如果您尝试将来自 family_id 2 的人插入同一个 house_of family_id 1 中:

insert into person (family_id, house_id) values (2,1);
ERROR:  insert or update on table "person" violates foreign key constraint "person_house_id_fkey"
DETAIL:  Key (house_id, family_id)=(1, 2) is not present in table "house_family".
于 2012-11-01T16:39:11.197 回答