3.3 外键

回到第二章中使用的weather和cities表。考虑如下问题:你需要确认,在cities表中没有的城市,不能在weather表中有天气信息。这叫做维护你数据的引用完整性。在一些不太专业的数据库中,这种限制是通过以下方式实现的:首先查询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".

外键的行为可以根据您的应用程序进行微调。 在此,我们所述不会超出本教程中的这个简单示例,但您可以参考第5章以获取更多的相关信息。正确使用外键可显著提高您数据库应用程序的质量,所以强烈建议您进一步学习它们。

3.3. Foreign Keys

Recall the weather and cities tables from Chapter 2. Consider the following problem: You want to make sure that no one can insert rows in the weather table that do not have a matching entry in the cities table. This is called maintaining the referential integrity of your data. In simplistic database systems this would be implemented (if at all) by first looking at the cities table to check if a matching record exists, and then inserting or rejecting the new weather records. This approach has a number of problems and is very inconvenient, so PostgreSQL can do this for you.

The new declaration of the tables would look like this:

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

);

Now try inserting an invalid record:

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".

The behavior of foreign keys can be finely tuned to your application. We will not go beyond this simple example in this tutorial, but just refer you to Chapter 5 for more information. Making correct use of foreign keys will definitely improve the quality of your database applications, so you are strongly encouraged to learn about them.

3.3. Foreign Keys相关推荐

  1. 使用navicat for mysql 创建外键foreign keys时,总会自动创建索引indexs

    使用navicat for mysql 创建外键foreign keys时,总会自动创建索引indexs.如果删除这个索引就会提示错误error 1553:cann't drop index-:nee ...

  2. 3780mysql_MySQL添加外键Foreign Keys出错,报错[HY000][3780]

    今天写开发文档的时候需要做一下数据结构相关的内容,于是就想着一个快捷一点儿的操作,直接在DataGrip里面调用视图,然后将视图打印为PDF,这样就可以直接截图获取到图片了.由于开发的过程中也没有怎么 ...

  3. 5.4.4. Primary Keys

    5.4.4. Primary Keys 5.4.4.主键约束 A primary key constraint indicates that a column, or group of columns ...

  4. android sqlite foreign key,SQLite外键(Foreign Key) 的使用例子

    从SQLite 3.6.19 开始支持 外键约束. sqlite> PRAGMA foreign_keys; 0 sqlite> PRAGMA foreign_keys = ON; sql ...

  5. powerdesigner 同步mysql 报错_PowerDesigner实用技巧小结 及 导出word,想字段顺序跟模型中一致,如何设置...

    powerdesigner导出word,想字段顺序跟模型中一致,如何设置 右键List of columns of the table %PARENT% ,selection,define  sort ...

  6. 设置mysql默认字符集_MySQL之修改默认引擎和字符集

    一.数据库引擎 1.1 查看数据库引擎 mysql> show engines; +--------------------+---------+------------------------ ...

  7. python 之路,Day11 (下)- sqlalchemy ORM

    python 之路,Day11 - sqlalchemy ORM 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM介绍 ...

  8. MySQL中定义fk语句_MySQL基础篇/第3篇:MySQL基本操作语句.md · qwqoo/MySQL-Review - Gitee.com...

    ### 第3篇:MySQL基本操作语句 - MySQL基础操作 #### 排序检索数据 - 之前的数据没有进行排序,其是按照默认在数据表中的数据返回的 - SELECT语句的ORDER BY 子句进行 ...

  9. mysql 分表原理_MYSQL 分表原理(转)

    简介: 引用MySQL官方文档中的一段话:MERGE存储引擎,也被认识为MRG_MyISAM引擎,是一个相同的可以被当作一个来用的MyISAM表的集合."相同"意味着所有表同样的列 ...

最新文章

  1. 数据分析软件python 对电脑的要求-数据分析5大软件PK,你最爱哪个?
  2. 没有form的表单验证_PHP动态生成表单,内置17种常用组件并且支持表单验证!
  3. Python基础教程(十一):多线程、XML解析
  4. Python教程:对 a = [lambda : x for x in range(3)] 的理解
  5. php的json_encode实例,php json_encode()函数返回json数据实例代码
  6. 【数据结构与算法】之深入解析“水壶问题”的求解思路与算法示例
  7. 高德地图AmapSDKDemo运行
  8. 安装高版本的java_运行“需要Java 11或更高版本”的Visual Studio代码。请下载并安装最新的JDK”...
  9. R语言_安装包时联网失败
  10. 文件传阅系统无纸化文件流转系统
  11. Altium Designer安装教程,适合小白的一键安装,简单明了,傻瓜式安装
  12. 芯片可靠性测试要求及标准解析
  13. iOS开发之音视频边下边播缓存方案
  14. 北京外国语大学pip镜像站
  15. pregnant用法
  16. 为什么自建 UI 框架?
  17. zigbee以太网网关方案
  18. 杨震霆(carboy) -传奇人物
  19. Unity3D: 做自己的卡牌游戏 (一)
  20. Ebox 的OS定制

热门文章

  1. SURF SIFT ORB三种特征检测算法比较
  2. cocos2d spine 与 tmx相遇崩溃问题
  3. 超声波明渠流量计在自然界灌溉的应用
  4. intel Realsense R200在Ubuntu 14.04上驱动的安装
  5. ims 注册鉴权认证过程
  6. xx年xx月xx日xx时xx分xx秒——if版本
  7. 怎么用迅雷下载python_迅雷违规资源高速下载解决办法 python 源码
  8. i9-13900K华硕 Z-790A wifi D5吹雪 编译Qt 源码实测
  9. 华强北众多三代版本如何选购?
  10. 计算机网络-学习总结篇