What is the difference between a composite key, primary key and foreign key?
一个composite key, 一个外键和首要的关键是关系数据库标准的复杂和拜占庭性质的很好的例证,该标准使数据库管理成为需要专业技能的高级工作角色。
首要的关键
First, a primary key uniquely identifies each record in a database table. Any individual key that does this can be called a candidate key, but only one can be chosen by database engineers as a primary key.
复合键
接下来,有一个复合键,该键由两个或多个属性组成,这些属性共同唯一地识别每个记录。
一个例子是房地产市场上的房屋清单。在有序的数据库中,应该有一个主键,该键可以唯一地标识每个记录。
How this works may have to do with the sophistication of the database.
In some cases, the homes may only be uniquely identified by a mortgage number — all other data (towns, streets, house numbers) is not unique to each record. The mortgage number would be the primary key. Suppose, however, that an MLS realtor’s listing technology assigns its own unique numbers to the records in the table.
Then, there will be two keys that developers might identify as “candidate keys”:
- The mortgage number.
- MLS编号。
One of them will qualify as the “primary key” in what some would consider an arbitrary way.
那么,一个复合键将是两个键的组合。
例如:鉴于市场列表是本地的,房屋编号和街道的组合可能有资格作为综合钥匙。如果是这样,那么当有人使用房屋号码和街道搜索时,他们只能返回一个记录。
外键
Meanwhile, if there is a key in a linked table, such as a buyer’s table that references the primary key, that will be a foreign key.
虽然主键和复合键可能会执行相同的操作,但主键将由一个列组成,其中复合键将由两个或更多列组成。
The relationship between a primary key and a foreign key is quite different. The key thing to understand here is that the primary key in one database table becomes a foreign key in another database table, and vice versa.
A foreign key in a database table is taken from some other table and applied in order to link database records back to that foreign table.
The foreign key in the database table where it resides is actually the primary key of the other table.
Here's an example to make this clearer: If we have a database table labeled, say, "orders," and we have a foreign key that's labeled "customers," we could perhaps link that foreign key to a primary key, identifying each customer in a separate table.
In sum, all of this interconnection of key categories shows the detailed nature of creating sophisticated patterns and relationships in relational database sy爱游戏网页入口stems.
数据库工程师了解这些类别和属性,并应用它们以帮助确保最终用户触发的SQL查询和其他数据检索方法有效。
Along with database normalization and promoting structured data, these key relationships are core parts of database sy爱游戏网页入口stem integrity and data storage design best practices.
标签
写的贾斯汀·斯托尔兹夫斯(Justin Stoltzfus)|撰稿人,评论者

Justin Stoltzfus is a freelance writer for various Web and print publications. His work has appeared in online magazines including Preservation Online, a project of the National Historic Trust, and many other venues.
我们的专家更多的问答
- 非结构化数据如何使您的企业底线受益?
- What are some of the dangers of using machine learning impulsively without a business plan?
- What is TensorFlow’s role in machine learning?
相关术语
相关文章
技术快速移动!与Techopedia保持领先地位!爱游戏网页登录首页
加入近200,000名从Techopedia获得可行技术见解的订户。爱游戏网页登录首页