-
I'm creating my tables and I would like to know if I need to add the index or not to my primary keys. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Schema::table('users', function (Blueprint $table) {
$table->id()->primary();
}); However, the docs repeatedly call this
meaning, that MySQL will implicitly add an index itself under the hood. |
Beta Was this translation helpful? Give feedback.
-
Using 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. |
Beta Was this translation helpful? Give feedback.
-
Thank you @SenaJp for helping the community, God bless you! |
Beta Was this translation helpful? Give feedback.
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: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.