在RDBMS中,用于为数据库建立索引的键是否总是主键? [英] In RDBMS, is the key used for building the index for a database always the primary key?

查看:99
本文介绍了在RDBMS中,用于为数据库建立索引的键是否总是主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 如何在SQL中指定用于构建
    数据库索引的键?

  2. 在大多数情况下(如果没有)所有RDBMS,是用于为数据库建立索引的搜索键始终
    是主键吗?

  1. How do we specify which key is used for building the index for a database in SQL?
  2. In most if not all RDBMS, is the search key used for building the index for a database always the primary key?

来自数据库管理系统,第3版,由Raghu
Ramakrishnan和Johannes Gehrke

From Database Management Systems, 3rd Edition, by Raghu Ramakrishnan, and Johannes Gehrke


原则上,我们可以使用任何键,而不仅仅是主键
键,以引用一个元组。但是,最好使用
,因为这是DBMS期望的-这是将特定候选键指定为
主键并对其进行优化的
的意义。例如,DBMS 可以创建一个以主键字段作为搜索键的索引
,以使元组的
检索具有主键值的效率很高。 p>

In principle, we can use any key, not just the primary key, to refer to a tuple. However, using the primary key is preferable because it is what the DBMS expects - this is the significance of designating a particular candidate key as a primary key and optimizes for. For example, the DBMS may create an index with the primary key fields as the search key, to make the retrieval of a tuple given its primary key value efficient.


谢谢。

推荐答案


  1. 这取决于您所使用的RDBMS。就像
    CREATE INDEX index_name ON table_name(key_name)

  2. 是和否。

  1. That depends on which RDBMS you are using. It will be something like CREATE INDEX index_name ON table_name(key_name).
  2. YES and NO.

a)如果要创建表,通常RDBMS将使用在 CREATE中指定的主键为该表创建索引。 TABLE 语句。如果您不指定主键,RDBMS将帮助您选择一个唯一且非空的键,或者为该表创建一个内部键(可能是int类型)作为主键。

a) If you are creating a table, and generally the RDBMS will create the index for this table using the primary key you specify in your CREATE TABLE statement. If you don't specify a primary key, RDBMS will help you choose an unique and non-null key, OR create an internal key (probably an int type) as primary key for this table.

b)有时,根据查询模式,您可能会发现除主键之外的其他一些键也经常使用(在其中子句),那么最好使用这些键来建立新索引。

b) Sometimes, according to the query pattern, you may find some keys other than primary key are used frequently (in where clause for example), then it is good to build new indexes using these keys.

这篇关于在RDBMS中,用于为数据库建立索引的键是否总是主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆