我目前有一个父表:
CREATE TABLE members (
member_id SERIAL NOT NULL, UNIQUE, PRIMARY KEY
first_name varchar(20)
last_name varchar(20)
address address (composite type)
contact_numbers varchar(11)[3]
date_joined date
type varchar(5)
);
和两个相关的表格:
CREATE TABLE basic_member (
activities varchar[3]) // can only have 3 max activites
INHERITS (members)
);
CREATE TABLE full_member (
activities varchar[]) // can 0 to many activities
INHERITS (members)
);
我还有另一张桌子:
CREATE TABLE planner (
day varchar(9) FOREIGN KEY REFERENCES days(day)
time varchar(5) FOREIGN KEY REFERENCES times(time)
activity varchar(20) FOREIGN KEY REFERENCES activities(activity)
member bigint FOREIGN KEY REFERENCES members(member_id)
);
ALTER TABLE planner ADD CONSTRAINT pk_planner PRIMARKY KEY (day,time,activity,member);
我目前正在尝试添加
INSERT INTO planner VALUES ('monday','09:00','Weights',2);
我已使用
将一个集添加到full_members中INSERT INTO full_members
VALUES (Default, 'Hayley', 'Sargent', (12,'Forest Road','Mansfield','Nottinghamshire','NG219DX'),'{01623485764,07789485763,01645586754}',20120418,'Full');
我对Planner的插入目前无效 - 你能解释一下原因吗?
答案 0 :(得分:2)
我管理不回答我自己的问题,因为目前posgreSQL与继承和外键不能很好地工作,所以我没有创建规则
CREATE RULE member_ref
AS ON INSERT TO planner
WHERE new.member NOT IN (SELECT member_id FROM members)
DO INSTEAD NOTHING;
这与外键基本相同
答案 1 :(得分:1)
不确定这是否是更好的解决方案,但在这里......
原则很简单:
table_with_pkeys
,它将复制继承表child1
,child2
,child3
... table_with_pkeys
新创建的PK,更新后如果更改则更新它,删除后删除table_with_pkeys中的相同PK。希望它有所帮助。
答案 2 :(得分:0)
您在地址中的12之前缺少公开引用:
INSERT INTO full_members
VALUES (Default, 'Hayley', 'Sargent', (12 Forest Road', 'Mansfield', 'Nottinghamshire', 'NG219DX'),
'{01623485764,07789485763,01645586754}',20120418,'Full');
应该是:
INSERT INTO full_members
VALUES (Default, 'Hayley', 'Sargent', ('12 Forest Road', 'Mansfield', 'Nottinghamshire', 'NG219DX'),
'{01623485764,07789485763,01645586754}',20120418,'Full');
答案 3 :(得分:0)
如果物化视图方法对您不起作用,请创建约束触发器以检查参照完整性。遗憾的是,声明性引用完整性目前在继承方面效果不佳。