Αποτελέσματα Αναζήτησης
Luckily, MySQL provides another kind of index called UNIQUE index that allows you to enforce the uniqueness of values in one or more columns. Unlike the PRIMARY KEY index, you can have more than one UNIQUE index per table. To create a UNIQUE index, you use the CREATE UNIQUE INDEX statement as follows:
CREATE UNIQUE INDEX index_name. ON table_name (column1, column2, ...); MySQL CREATE INDEX Example. The SQL statement below creates an index named "idx_lastname" on the "LastName" column in the "Persons" table: CREATE INDEX idx_lastname. ON Persons (LastName);
CREATE INDEX enables you to add indexes to existing tables. CREATE INDEX is mapped to an ALTER TABLE statement to create indexes. See Section 15.1.9, “ALTER TABLE Statement”. CREATE INDEX cannot be used to create a PRIMARY KEY; use ALTER TABLE instead.
5 Ιαν 2024 · The syntax for creating a UNIQUE INDEX involves the CREATE INDEX statement. Creating a UNIQUE INDEX on a Single Column: CREATE UNIQUE INDEX index_unique_column ON tbl_name (col_name); Creating a UNIQUE INDEX on Multiple Columns: CREATE UNIQUE INDEX index_unique_columns ON tbl_name (co1, col2, ...); Exmaples of MySQL Unique Index.
Creating a Simple Unique Index. Let's start with a basic example. Suppose we have a table of students, and we want to ensure that each student has a unique student ID. CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, student_id VARCHAR (10), name VARCHAR (50), email VARCHAR (50) );
To create a unique index, use the CREATE UNIQUE INDEX statement like this: CREATE UNIQUE INDEX index_name [USING {BTREE | HASH}] ON table_name (column_list) [algorithm_option | lock_option]; Explanation: The UNIQUE keyword indicates that this index is a unique index. index_name is the name of the index.
Learn how to optimize MySQL database performance by creating indexes using the CREATE INDEX command. Explore various indexing strategies, including unique indexes, multi-column indexes, and functional key parts, to speed up your queries and enhance user experience.