Skip to content

Does laravel automatically add the index to the primary keys? #48491

Answered by iazaran
SenaJp asked this question in General
Discussion options

You must be logged in to vote

Using $table->id(); is enough to make ID, auto increment and primary key. Take a look at the DB DDL for a simple table result:

CREATE TABLE `tests` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

But for indexing, in MySQL, a primary index is automatically created, and finally, it's actually not necessary to create an index on the primary key column.

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@henzeb
Comment options

@shaedrich
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by SenaJp
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants