| Type | Purpose |
|---|---|
| Primary key | Uniquely identifies each row |
| Unique key | Prevents duplicate values |
| Index | Improves lookup/query performance |
Example:
$table->id(); // Primary key
$table->string('email')->unique(); // Unique + index
$table->index(['status', 'created_at']); // Normal composite index
A primary key is unique and non-null. A unique constraint prevents duplicate values but is not necessarily the table's identity.
Real-project scenario
For users:
id -> PRIMARY KEY
email -> UNIQUE
status -> INDEX
If the application frequently executes:
SELECT * FROM orders
WHERE user_id = 10
ORDER BY created_at DESC;
an index involving user_id and potentially created_at can improve performance.
0 comments:
Post a Comment
Thanks