Junior — Middle
Which fields are automatically indexed when creating a database without explicitly specifying index fields?
sobes.tech AI
Answer from AI
When creating a database without explicitly specifying indexes, the primary key of the table is usually automatically indexed. This is because the primary key must ensure uniqueness and quick access to records.
For example, if a primary key is defined in a table, the DBMS will automatically create an index on that field or set of fields.
Other fields without an explicitly specified index are usually not indexed to avoid unnecessary costs for storing and updating indexes.
Thus, the following are automatically indexed:
- Fields declared as PRIMARY KEY.
- Sometimes fields with a UNIQUE constraint (depending on the DBMS).
Example in SQL:
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100)
);
In this case, an index will be automatically created on the id field.