To avoid repair with keycache in MySQL, you need to follow the below syntax −
create table yourNewTableName as (select *from yourOldTableName); alter table yourNewTableName add index(yourCoumnName);
Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20), StudentLastName varchar(20) ); Query OK, 0 rows affected (0.24 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('John','Doe'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('David','Miller'); Query OK, 1 row affected (0.05 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+-----------------+ | StudentId | StudentFirstName | StudentLastName | +-----------+------------------+-----------------+ | 1 | John | Doe | | 2 | David | Miller | +-----------+------------------+-----------------+ 2 rows in set (0.00 sec)
Step 1 − Following is the query to avoid “repair with keycache” in MySQL.
mysql> create table DemoTable as (select *from DemoTable); Query OK, 2 rows affected (0.34 sec) Records: 2 Duplicates: 0 Warnings: 0
Step 2 − Now, use alter table
mysql> alter table DemoTable add index(StudentFirstName); Query OK, 2 rows affected (0.57 sec) Records: 2 Duplicates: 0 Warnings: 0