What are the different ways to create SQL indexes?
There are several ways to create SQL indexes:
- Specify index when creating a table: When creating a table, use the INDEX keyword in the CREATE TABLE statement to specify the index to be created. For example:
CREATE TABLE mytable (
id INT,
name VARCHAR(50),
INDEX idx_name (name)
);
- Add index using ALTER TABLE statement: You can add an index using the ALTER TABLE statement. For example:
ALTER TABLE mytable ADD INDEX idx_name (name);
- The CREATE INDEX statement is used to create an index. For example, you can use the CREATE INDEX statement to create an index.
CREATE INDEX idx_name ON mytable (name);
- Create a unique index using the CREATE UNIQUE INDEX statement to ensure that the values in the indexed column are distinct. For example:
CREATE UNIQUE INDEX idx_name ON mytable (name);
It should be noted that the way indexes are created may vary depending on the database management system.