Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to create separate index for primary key of relational database table

If I create table with primary key is index automatically created for the table or does that need doing separately.

i.e if this is the table ddl

 CREATE TABLE release(guid varchar(36) NOT NULL PRIMARY KEY, name varchar(255),xmldata  CLOB(512 K)) 

do I also need to do

CREATE INDEX release_idx ON release(guid) 

or not

(I'm using Derby a database that comes with Java)

like image 372
Paul Taylor Avatar asked Feb 01 '13 12:02

Paul Taylor


People also ask

Do we need to create separate index on a primary key?

In MySQL, a primary index is automatically created, and we have already described above how MySQL chooses the primary index. But in the database world, it's actually not necessary to create an index on the primary key column — the primary index can be created on any non primary key column as well.

Should primary key be indexed?

Yes, primary key is automatically indexed in MySQL because primary key, index, etc gets stored into B-trees. All engines including InnoDB as well as MyISAM automatically supports the primary key to be indexed. The primary key is implicitly indexed in InnoDB, MyISAM, and other engines.

Is index automatically created on primary key?

A primary index is automatically created for the primary key and ensures that the primary key is unique. You can use the primary index to retrieve and access objects from the database. The unique index is a column, or an ordered collection of columns, for which each value identifies a unique row.

Can we index a primary key column?

When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index. The primary key column cannot allow NULL values.


1 Answers

You don't need to. The primary key is already an index.

like image 161
Jon Avatar answered Oct 07 '22 16:10

Jon