-- the difference
The primary key is a constraint, the unique index is an index, and the two are essentially different.
After the primary key is created, it must contain a unique index, and the unique index is not necessarily the primary key.
Unique index columns allow null values, while primary key columns do not.
When the primary key column is created, the default value is null + unique index.
Primary keys can be referenced as foreign keys by other tables, while unique indexes cannot.
A table can only create at most one primary key, but can create multiple unique indexes.
Primary keys are more suitable for unique identifiers that cannot be easily changed, such as auto-incrementing columns, ID numbers, etc.
In RBO mode, the execution plan priority of the primary key is higher than that of the unique index. Both can improve the speed of the query.
-- Create a table with only primary key and unique index
CREATE TABLE test
(PrimaryKey VARCHAR2(20),
UniqueKey VARCHAR2(20)
);
-- Create primary key and unique index separately, with different syntax
ALTER TABLE test ADD CONSTRAINT test_PrimaryKey PRIMARY KEY (PrimaryKey);
CREATE UNIQUE INDEX test_UniqueKey ON test (UniqueKey);
-- Two index names can be seen in USER_INDEXES
SELECT table_name,table_type,index_name,index_type,uniqueness
FROM USER_INDEXES
WHERE TABLE_NAME='TEST';
-- Two index field names can be seen in USER_IND_COLUMNS
SELECT table_name,index_name,column_name,column_position
FROM USER_IND_COLUMNS
WHERE TABLE_NAME='TEST';
-- Only primary key constraint name can be seen in USER_CONSTRAINTS
SELECT table_name,constraint_name,constraint_type
FROM USER_CONSTRAINTS
WHERE TABLE_NAME='TEST';
-- Only the primary key constraint field name can be seen in USER_CONS_COLUMNS
SELECT table_name,constraint_name,column_name,position
FROM USER_CONS_COLUMNS
WHERE CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME='TEST');
-- Add a not-null constraint to the unique index
ALTER TABLE test MODIFY UniqueKey NOT NULL;
-- Only primary key constraint names and non-null constraint names can be seen in USER_CONSTRAINTS
SELECT table_name,constraint_name,constraint_type
FROM USER_CONSTRAINTS
WHERE TABLE_NAME='TEST'
-- Only primary key constraint field names and non-null constraint field names can be seen in USER_CONS_COLUMNS
SELECT table_name,constraint_name,column_name,position
FROM USER_CONS_COLUMNS
WHERE CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME='TEST')
http://www.cnblogs.com/-619569179/p/6528896.html