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. angular入门(基础篇)

    一.什么是AngularJs? AngularJs是一个JavaScript框架,通过指令扩展了HTML,并且通过表达式绑定数据到HTML. AngularJs使得开发现代的单页面应用程序(SPA:S ...

  2. Oracle11gR2 Windows 7 64bit and PL/SQL Developer排错

    1.Environment variable: "PATH" - This test checks whether the length of the environment va ...

  3. Android经常使用自己定义控件(二)

           经常使用的Android自己定义控件分享 http://www.see-source.com//androidwidget/list.html?type=&p=1

  4. OpenStack_Swift源代码分析——ObjectReplicator源代码分析(1)

    1.ObjectorReplicator的启动 首先执行启动脚本 swift-init object-replicator start 此执行脚本的执行过程和ring执行脚本执行过程差点儿相同.找到s ...

  5. pdnsd 解析原理

    apt install dnsmasq dnsmasq-fullvim /etc/dnsmasq.conf vim /etc/pdnsd.conf killall pdnsdpdnsd -c /etc ...

  6. Swagger文档转Word

    Swagger文档转Word 文档   GitHub 地址:https://github.com/JMCuixy/SwaggerToWord/tree/developer 原创作品,转载请注明出处:h ...

  7. ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)

    原文  http://blog.csdn.net/gubenpeiyuan/article/details/25618177   概要: 目前ONVIF协议家族设备已占据数字监控行业半壁江山以上,亲, ...

  8. Objective-C基础笔记(7)Foundation中的常用结构体

    一.NSRange NSRange的定义: typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; N ...

  9. C# winform利用反射和自定义特性加载功能模块(插件式开发)

    由于在实际的工作中, 碰见这样的一个问题: 一个软件, 销售给A客户 他需要所有功能, 但是销售给B客户, 他只需要其中的一部分, 1.如果我们在实际的开发过程中, 没有把一些功能模块区分开来的话, ...

  10. method initializationerror not found:JUnit4单元測试报错问题

           今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下:            网上说版本 ...