[PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

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

时间: 2024-10-20 12:04:45

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

How to ensure data integrity during transmission

XMODEM. It's old, it's bad, but it is widely supported both in hardware and in software, with libraries available for literally every language and market niche. HDLC - High-Level Data Link Control. It's the protocol which has fathered lots of reliabl

A MySQL foreign keys drop table, re-create table example

Summary: How to drop MySQL database tables and recreate them when you have foreign keyrelationships between the tables. This is pretty obscure, but I thought I'd post it here so I wouldn't forget how to do this ... if you ever have a situation when u

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

对称/非对称/混合加密的冷知识 数据在互联网上传输,要考虑安全性. 讲到安全,要从三方面考虑: 1.authentication 每一个IP包的认证,确保合法源的数据 2.data integrity 验证数据完整性,保证在传输过程中没有被人为改动 3.confidentiality (私密性)数据包的加密 下面谈谈如何对数据加密. 于是有了非对称加密 对称加密 对称加密算法---使用一把密匙来对信息提供安全的保护.只有一个密匙,即用来加密,也用来解密 特点: - 1.速度快 - 2.密文紧凑

PostgreSQL之Foreign Data Wrappers使用指南

PostgreSQL的fdw实现的功能是各个postgresql数据库及远程数据库之间的跨库操作,功能和oracle的dblink一样. 本文中的环境如下图所示: 1.1.目标端安装软件包 [[email protected] ~]# yum -y install postgresql10-contrib.x86_64 1.2.目标端创建用户 这里的源端不需要创建用户.因为源端已经存在了一个rhnuser用户,直接使用.根据实际情况赋予用户权限,一般就是select.update.delete以

数据完整性(Data Integrity)笔记

因数据库存储数据要持之以恒,数据库中的表需要一些方法验证各种数据类型.不仅仅局限于数据类型,还有唯一值,值的范围,或者某列的值和另外一个表中的列匹配. 当你在定义表的时候其用这些数据验证方法.这叫做声明数据完整性.也就是我们说的表约束. USE tempdb GO CREATE TABLE s ( sid VARCHAR(20) , sname VARCHAR(20) , ssex VARCHAR(2) CHECK ( ssex = '男' OR ssex = '女' ) DEFAULT '男'

Get Foreign Keys

import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Main { public static void main(String[] args) throws Exception { Connection conn = getMySq

MySQL添加外键Foreign Keys出错,报错[HY000][3780]

今天写开发文档的时候需要做一下数据结构相关的内容,于是就想着一个快捷一点儿的操作,直接在DataGrip里面调用视图,然后将视图打印为PDF,这样就可以直接截图获取到图片了.由于开发的过程中也没有怎么注重外键的建立,因为本来就是一个小小的比赛,所以生成视图的时候外键联系的箭头都没有,真的是自闭了,如果这个直接给评委看,评委估计会气哭...所以我打算花点时间加一些外键上去,心里想着也花不了多少时间,谁知道,这下子还遇到一些哲学问题! 首先看下,本来数据库表视图是这样的,我只是想多增加一些外键约束,

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

1. EXEC sp_fkeys 'TableName' 2. SELECT obj.name AS FK_NAME, sch.name AS [schema_name], tab1.name AS [table], col1.name AS [column], tab2.name AS [referenced_table], col2.name AS [referenced_column]FROM sys.foreign_key_columns fkcINNER JOIN sys.object

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,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table&g