主键主键外键和索引

Primary and foreign keys are a way in which to constrain related data together to ensure data in your database remains consistent and to ensure no redundant data is in the database as a result of deleting a table or row in one table that affects data in other tables that may perhaps rely on that information. It can cause both data integrity problems as well as problems with your application that makes use of such database.

主键和外键是一种将相关数据约束在一起的方式,以确保数据库中的数据保持一致,并确保由于删除一个表中的表或行而影响其他表中的数据,从而导致数据库中没有冗余数据也许依赖于这些信息。 它可能导致数据完整性问题,也可能导致使用此类数据库的应用程序出现问题。

什么是主键? (What’s a primary key used for?)

A primary key is used to ensure data in the specific column is unique. You can only set constraints with primary keys, by setting a foreign key to another column which creates a relationship with the column that has the primary key set. A prime use of a primary key is in the case of a users table. The id column is likely to be a primary key because is needs to be unique. You may also set this column to auto increment; but the most important component is the primary key.

主键用于确保特定列中的数据唯一 。 您只可以设定主键约束,通过设置一个外键 ,它创建了一个具有主键设置的关系的另一列。 对于用户表,主键的主要用途。 id列可能是主键,因为它需要唯一。 您也可以将此列设置为自动递增。 但最重要的组成部分是主键。

为什么需要使用主键? (Why do I need to use a primary key?)

Well, even though the column set as a foreign key may have duplicate data in multiple rows, it is still relying on the unique information in the primary key. If there was non-unique rows in the primary key column, there would not be a use for a foreign key because you would be mapping one row in the foreign key to two rows in the primary key. In essence, primary to foreign keys ensure that:

好的,即使设置为外键的列可能在多行中都有重复的数据,它仍然依赖于主键中的唯一信息。 如果主键列中存在不唯一的行,则不会使用外键,因为您会将外键中的一行映射到主键中的两行。 本质上,外键的主键确保:

  • the data that can be inserted in the table that has the foreign key can only be information thatuniquely exists in the primary key column.具有外键的表中可以插入的数据只能是主键列中唯一存在的信息。
  • if a DELETE or UPDATE occurs, the action you specified will be undertaken to ensure data integrity and prevent data redundancy in your database. Two primary actions are CASCADE and RESTRICT. CASCADE means any affected data will be deleted as well (hence, cascading), in the case of an UPDATE execution; in order to keep data consistent and prevent data redundancy.

    如果发生DELETE或UPDATE,则将执行您指定的操作以确保数据完整性并防止数据库中的数据冗余。 两个主要动作CASCADE和RESTRICT。 CASCADE表示在执行UPDATE的情况下,所有受影响的数据也将被删除(因此, 级联 ); 为了保持数据一致并防止数据冗余。

什么是外键? (What’s a foreign key used for?)

How do you actually ensure that data stays integral, consistent and updated? You make use of foreign keys. A primary key ensures that data is unique in one column, but the foreign key is what makes the data stay consistent, as that is where the important data lies that needs to stay consistent and integral. It’s inevitable that in most database-driven applications you create, you’ll need to have multiple tables that are for different areas of your application that makes a reference to an id or some unique data in another table. For example, a profile comments table – your application needs to understand what user id the profile comment is for. You can make use of a foreign key here that maps to the primary key that, as well as keeping data consistent, it also means that you can set the foreign key to CASCADE and any DELETE and UPDATE actions will be cascaded down to any affected row (such as the foreign key).

您实际上如何确保数据保持完整,一致和更新? 您使用外键 。 主键可确保数据在一列中是唯一的,但是外键是使数据保持一致的原因,因为重要数据所在的位置需要保持一致和完整。 不可避免的是,在您创建的大多数数据库驱动的应用程序中,您将需要具有多个表,这些表用于应用程序的不同区域,这些表引用了另一个表中的ID或某些唯一数据。 例如, 配置文件注释表–您的应用程序需要了解配置文件注释的用户ID 。 您可以在此处使用映射到主键的外键,并使数据保持一致,这还意味着您可以将外键设置为CASCADE,并且任何DELETE和UPDATE操作将被级联到任何受影响的行(例如外键)。

翻译自: https://www.eukhost.com/blog/webhosting/whats-the-purpose-use-primary-foreign-keys/

主键主键外键和索引

主键主键外键和索引_主键和外键的目的/用途是什么?相关推荐

  1. mysql主键和唯一索引_主键和唯一索引的有什么区别

    主键和唯一索引的区别 -- 区别 主键是一种约束,唯一索引是一种索引,两者在本质上是不同的. 主键创建后一定包含一个唯一性索引,唯一性索引并不一定就是主键. 唯一性索引列允许空值,而主键列不允许为空值 ...

  2. mysql 主键 聚集索引_主键索引就是聚集索引吗?

    这个问题很刁钻! 答案是错的. 的确在mysql数据库innodb引擎里面,主键的确就是聚集索引. 但是myisam引擎里面主键也不是聚集索引. 另外在sql server中还可以显示的指定聚集索引. ...

  3. mysql中主键约束和唯一约束的区别_主键约束和唯一性约束的区别

    1.主键约束(PRIMARY KEY) 1) 主键用于唯一地标识表中的每一条记录,可以定义一列或多列为主键. 2) 是不可能(或很难)更新. 3) 主键列上没有任何两行具有相同值(即重复值),不允许空 ...

  4. mysql非主键索引_主键索引和非主键索引的区别

    1. 什么是最左前缀原则? 以下回答全部是基于MySQL的InnoDB引擎 例如对于下面这一张表 如果我们按照 name 字段来建立索引的话,采用B+树的结构,大概的索引结构如下 如果我们要进行模糊查 ...

  5. mysql非主键索引_主键索引和非主键索引解析

    1. 什么是最左前缀原则? 以下回答全部是基于MySQL的InnoDB引擎 例如对于下面这一张表 如果我们按照 name 字段来建立索引的话,采用B+树的结构,大概的索引结构如下 如果我们要进行模糊查 ...

  6. jpa 每次启动都要创建外键索引_什么情况下外键建立索引?是必须的吗

    朋友问了一个问题: 首先,外键什么情况下采用呢? 外键是否采用看业务应用场景,以及开发成本的 1. 互联网行业应用不推荐使用外键: 用户量大,并发度高,为此数据库服务器很容易成为性能瓶颈,尤其受IO能 ...

  7. 点击鼠标左键 自动锁定计算机图标,鼠标一按左键桌面图标就消失了怎么办_为什么按鼠标左键时桌面图标都不见了...

    最近有不少用户反馈说碰到这样一个奇怪的现象,就是鼠标只要在桌面一按左键,左面上的图标就会消失了,而且点击鼠标也没有什么用,那么为什么按鼠标左键时桌面图标都不见了呢?原因有很多,可能是系统故障或者中毒等 ...

  8. mysql内连接和外连接的区别_内连接、外连接的区别

    先来看一下,内连接的语法: SELECT  XXX FROM XXX INNER JOIN XXX ON XXX; 这里 INNER 可以省略,在上一篇博客中我们对于笛卡尔积现象的研究中(http:/ ...

  9. java内部类为什么会持有外部类的引用_为什么内部类持有外部类的引?

    对普通类(没有内部类的类)来说,内部类和外部类都与他无关:对有内部类的类来说,它们就是其内部类的外部类,外部类是个相对的说法,其实就是有内部类的类.所以,要回答这个问题,只需要讲解内部类是什么:Jav ...

最新文章

  1. 2021-05-27Series(三):Series和ndarray对比学习 转
  2. GPU profile优化
  3. python之 启动一个子进程并等待其结束
  4. python PIL 单张图像变换大小—— img.resize()
  5. busybox在android开发中的应用
  6. javaweb_JSP 中文字符处理程序
  7. hdu5461(2015沈阳网络赛L题)
  8. file协议 控制面板_如何在Windows File Explorer导航窗格中显示控制面板和回收站
  9. EF Core 6 简化的数据库上下文注册
  10. .Net Core 三大Redis客户端对比和使用心得
  11. Halcon学习笔记:Halcon标定步骤-3d_coordinates.hdev示例
  12. python tkinter控件_python tkinter 控件与布局 项目实战
  13. SSM项目中配置问题
  14. 华为方舟编译器是黑科技?
  15. CSS基础——CSS 三大特性【学习笔记】
  16. vue 事件上加阻止冒泡 阻止默认事件
  17. python pyhook_python使用pyHook.HookManager()返回来的event中,event.Time怎么转换成为datetime形式?...
  18. 强大的网页数据库管理工具Adminer
  19. 各大IT公司经典面试题总结
  20. operator new与placement new

热门文章

  1. 优傲机器人e-Series 3挡位示教器焕新而来
  2. 北京邮电大学计算机视觉博士,北京邮电大学团队蝉联计算机视觉领域COCO挑战赛冠军...
  3. 第二章排错的工具:调试器Windbg(上)
  4. 跟随高博学RGBDSLAM
  5. MPLS BGP 跨域方案A
  6. 顺德农商银行服务器没有收到您的证书,农商行网上银行u盘插入,提示没有证书怎么回......
  7. WiseBrowser 1.0震撼上市 创造移动浏览器巅峰神话
  8. 用于AI开发的6种最佳编程语言
  9. 微信小程序之搜索关键字高亮
  10. 阿里大咖精心烹饪整理85道Java微服务面试题(附答案)