Oracle约束官方文档--主键约束

Primary Key Constraints


In a primary key constraint, the values in the group of one or more columns subject to the constraint uniquely identify the row. 

Each table can have one primary key, which in effect names the row and ensures that no duplicate rows exist.

("键",之前说过是代表列)

在主键约束中,受约束的一列或多列组中的值唯一标识该行。每张表都只能有一个主键,该主键实际上命名了数据行并且确保不存在重复的行。(获取一行数据时,可以根据主键准确的获取一行)

A primary key can be natural or a surrogate. A natural key is a meaningful identifier made of existing attributes in a table.

For example, a natural key could be a postal code in a lookup table. In contrast, a surrogate key is a system-generated incrementing identifier that ensures uniqueness within a table. Typically, surrogate keys are generated by a sequence.

主键可以是自然键或是代理键(说的是虚拟列吧)。

自然键:有意义的标识符,有表中现有的数据构成。比如,查询表中的邮政代码字段,就可以是自然键。

对比来说,代理键:由系统生成的递增的标识符,确保表中的的唯一性。最经典的场景,由数据库序列生成代理键。

The Oracle Database implementation of the primary key constraint guarantees that the
following statements are true:

■ No two rows have duplicate values in the specified column or set of columns.
■ The primary key columns do not allow nulls.

Oracle数据库在主键约束的实现上,确保了如下的声明是正确的:

  • 在明确的单列或多列中,任何两行都没有相同的值(在组成主键的列中,任何两行都没有重复的值)
  • 主键列,不允许空值

A typical situation calling for a primary key is the numeric identifier for an employee. Each employee must have a unique ID. A employee must be described by one and only one row in the employees table.

使用主键的经典场景是,员工的员工数字编号。每个员工只能拥有一个唯一的ID。在员工表中,一个员工必须由一行数据也只能由一行数据来描述。(ID是主键,任何两行都不能出现重复值。一个ID代表一个员工,通过主键获取一行数据)

Example 5-1 indicates that an existing employee has the employee ID of 202, where the employee ID is the primary key. The following example shows an attempt to add an employee with the same employee ID and an employee with no ID:

SQL> INSERT INTO employees (employee_id, last_name, email, hire_date, job_id)    
  1  VALUES (202,'Chan','ICHAN',SYSDATE,'ST_CLERK');
.
ERROR at line 1:
ORA-00001: unique constraint (HR.EMP_EMP_ID_PK) violated

SQL> INSERT INTO employees (last_name) VALUES ('Chan');
.
ERROR at line 1:
ORA-01400: cannot insert NULL into ("HR"."EMPLOYEES"."EMPLOYEE_ID")

前提如下:员工表employee有一个ID为202的员工,并且ID列为主键。下面的例子做了一个尝试:向employee表添加一个ID为202的员工,再新增一个没有ID/编号的员工

报错:违反唯一性约束、主键不接收NULL

The database enforces primary key constraints with an index. Usually, a primary key constraint created for a column implicitly creates a unique index and a  NOT NULL constraint. Note the following exceptions to this rule:

  • In some cases, as when you create a primary key with a deferrable constraint, the generated index is not unique.

Note:

You can explicitly create a unique index with the CREATE UNIQUE INDEX statement.

  • If a usable index exists when a primary key constraint is created, then the constraint reuses this index and does not implicitly create a new one.

数据库使用索引时强制要求满足主键约束。通常,创建一个主键约束时,隐式的创建了一个唯一索引和一个非空约束。存在一些

例外:你可以明确的创建一个唯一索引,通过SQL:CREATE UNIQUE INDEX。(只创建一个唯一索引,而不是主键)

如果已经存在一个可用的索引,在创建一个主键约束时会重新使用该索引。并不会隐式的再创一个新的索引。(

自己尝试:

create index pk_a on a(id);   --在a表新建了一个索引:pk_a

alter table a add constraint pk_a primary key (id) ;  --在a表添加主键约束。约束名与上面的索引名相同,作用列相同。

第二个sql会执行成功。

By default the name of the implicitly created index is the name of the primary key constraint. You can also specify a user-defined name for an index. You can specify storage options for the index by including the ENABLE clause in the CREATE TABLE or ALTER TABLE statement used to create the constraint.

默认的,隐式创建的索引名和主键约束名相同。你也可以为索引指定一个用户定义的名字。

在创建约束的CREATE TABLE或ALTER TABLE语句中,添加ENABLE子句,可以指定存储选项。(这里说的是存储选项。约束的状态,从另一方面来说也是存储选项。要注意理解。)(百度出来的一篇:还有很多很多篇介绍约束状态的文章

作者:haiross

来源:CSDN

原文:https://blog.csdn.net/haiross/article/details/41977717

版权声明:本文为博主原创文章,转载请附上博文链接!)

See Also:

Oracle Database 2 Day Developer's Guide and Oracle Database Advanced Application Developer's Guide to learn how to add primary key constraints to a table

发布了57 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/shafatutu/article/details/96726368