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

主键主键外键和索引

Primary / Foreign Keys

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/

主键主键外键和索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值