2006-12-05
外键概念
关键字: 数据库weather 和 cities 表。考虑一下下面的问题:你想确保没有人可以在 weather 表里插入一条在 cities 表里没有匹配记录的数据行。 这就叫维护你的表的参考完整性。 在简单的数据库系统里,实现(如果也叫实现)这个特性的方法 通常是先看看 cities 表里是否有匹配的记录, 然后插入或者拒绝新的 weather 记录。 这个方法有许多问题,而且非常不便,因此 PostgreSQL 可以为你做这些。
新的表声明看起来会象下面这样:
CREATE TABLE cities (
city varchar(80) primary key,
location point
);
CREATE TABLE weather (
city varchar(80) references cities(city),
temp_lo int,
temp_hi int,
prcp real,
date date
);
然后我们试图插入一条非法的记录:
INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey" DETAIL: Key (city)=(Berkeley) is not present in table "cities".
发表评论
- 浏览: 14197 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
tomcat默认页诀窍
不错的小技巧!!
-- by xuehongliang -
tomcat默认页诀窍
恩,明白了,如果在webapp下有个index.do,里面又没有内容,则会拼成U ...
-- by hbcui1984 -
tomcat默认页诀窍
Lucas Lee 写道dingyuan 写道Lucas Lee 写道可以直接设 ...
-- by zhangfengsimon -
tomcat默认页诀窍
dingyuan 写道Lucas Lee 写道可以直接设置一个Servlet为默 ...
-- by Lucas Lee -
tomcat默认页诀窍
写一个空文件放到对应的目录下面,起个名叫XXX.do写一个模块专门在特定的时候打 ...
-- by 阳光晒晒






评论排行榜