Every movie needs a director and every rented movie needs to exist in the store. How do we make sure something in another table exists before inserting new data? This lesson will teach us about foreign keys and references.

CREATE TABLE directors (
id SERIAL PRIMARY KEY,
name VARCHAR(100) UNIQUE NOT NULL
); CREATE TABLE movies (
id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
release_date DATE,
count_stars INTEGER,
director_id INTEGER REFERENCES directors(id)
);

Now, if we try to insert to movies table some new data which contains director_id is not inside directors table, it will report error

[PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres的更多相关文章

  1. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  2. [svc]对称加密/非对称加密细枝末节-如何做到数据传输的authentication/data integrity/confidentiality(私密)

    对称/非对称/混合加密的冷知识 数据在互联网上传输,要考虑安全性. 讲到安全,要从三方面考虑: 1.authentication 每一个IP包的认证,确保合法源的数据 2.data integrity ...

  3. How can I list all foreign keys referencing a given table in SQL Server?

    How can I list all foreign keys referencing a given table in SQL Server?  how to check if columns in ...

  4. ORA-02273: this unique/primary key is referenced by some foreign keys

    关于ORA-02273错误,以前还真没有仔细留意过.昨天遇到了这个问题,遂顺便总结一番,以后遇到这类问题就可以直接用下面方案解决.如下所示,我们首先准备一下测试环境. CREATE TABLE TES ...

  5. 云原生 PostgreSQL 集群 - PGO:来自 Crunchy Data 的 Postgres Operator

    使用 PGO 在 Kubernetes 上运行 Cloud Native PostgreSQL:来自 Crunchy Data 的 Postgres Operator! Cloud Native Po ...

  6. 数据完整性(Data Integrity)笔记

    因数据库存储数据要持之以恒,数据库中的表需要一些方法验证各种数据类型.不仅仅局限于数据类型,还有唯一值,值的范围,或者某列的值和另外一个表中的列匹配. 当你在定义表的时候其用这些数据验证方法.这叫做声 ...

  7. 外键 Foreign keys

    https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...

  8. [转]How can I list all foreign keys referencing a given table in SQL Server?

    本文转自:https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given- ...

  9. How do I see all foreign keys to a table or column?

    down voteaccepted For a Table: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME, ...

随机推荐

  1. BZOJ5332: [Sdoi2018]旧试题(莫比乌斯反演)

    时光匆匆,转眼间又是一年寒暑…… 这是小 Q 同学第二次参加省队选拔赛. 今年,小 Q 痛定思痛,不再冒险偷取试题,而是通过练习旧 试题提升个人实力.可是旧试题太多了,小 Q 没日没夜地做题,却看不到 ...

  2. 【Python学习】爬虫报错处理bs4.FeatureNotFound

    [BUG回顾] 在学习Python爬虫时,运Pycharm中的文件出现了这样的报错: bs4.FeatureNotFound: Couldn’t find a tree builder with th ...

  3. Codeforces #258 Div.2 E Devu and Flowers

    大致题意: 从n个盒子里面取出s多花.每一个盒子里面的花都同样,而且每一个盒子里面花的多数为f[i],求取法总数. 解题思路: 我们知道假设n个盒子里面花的数量无限,那么取法总数为:C(s+n-1, ...

  4. XMPP开发之从零開始

    对于server的搭建和设置.我在这里就不再多说了.有好多前辈已经帮大家攻克了.能够參考下这篇博客 XMPPserver配置 我依照这个博客配置好了,server后,然后在网上參照代码写了一个小的de ...

  5. worktools-mmx 添加编译模块

    1,添加模块到mmx.py文件 1>vim /home/zhangshuli/git/vanzo_team/xulei/mmx.py 2>按照格式"Gallery2": ...

  6. Sql延时

    IF EXISTS(SELECT * FROM sys.procedures WHERE name='usp_wait30s')BEGIN DROP PROC usp_wait30sENDgocrea ...

  7. natapp解决Invalid Host header的问题

    最近在做一个微信公众号项目,用微信开发工具调试本地项目,需要做一下内网穿透,代理都配置好了,页面出现这个Invalid Host header错误,内网穿透工具我是用的frps做的,最后通过googl ...

  8. Spider_reg

    # 解析 数据的分类 结构化数据 有固定的格式,如 :HTML.XML.JSON 非结构化数据 图片.音频.视频,这类数据一般都存储为二进制 # 正则表达式 re 使用流程 创建编译对象:p = re ...

  9. 洛谷——P1021 邮票面值设计

    https://www.luogu.org/problem/show?pid=1021 题目描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15)种邮票的情况下(假定所有的邮票数量都 ...

  10. swift Reflection(字典转模型)变量继承本类类名解决办法

    class IWStatus: Reflect { var source: NSString! var created_at: NSString! var idstr: NSString! var u ...