Create index of three columns in MySQL?


For index, you can use KEY(). Let us first create a −

mysql> create table DemoTable1437
   -> (
   -> StudentId int,
   -> StudentName varchar(20),
   -> StudentMarks int,
   -> StudentAge int
   -> ,
   -> KEY(StudentId,StudentMarks,StudentAge)
   -> );
Query OK, 0 rows affected (0.97 sec)

Following is the query to check the description of −

mysql> desc DemoTable1437;

This will produce the following output −

+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| StudentId    | int(11)     | YES  | MUL | NULL    |       |
| StudentName  | varchar(20) | YES  |     | NULL    |       |
| StudentMarks | int(11)     | YES  |     | NULL    |       |
| StudentAge   | int(11)     | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1437 values(101,'Chris',78,21);
Query OK, 1 row affected (0.44 sec)
mysql> insert into DemoTable1437 values(102,'David',89,22);
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select −

mysql> select * from DemoTable1437;

This will produce the following output −

+-----------+-------------+--------------+------------+
| StudentId | StudentName | StudentMarks | StudentAge |
+-----------+-------------+--------------+------------+
|       101 | Chris       |           78 |         21 |
|       102 | David       |           89 |         22 |
+-----------+-------------+--------------+------------+
2 rows in set (0.00 sec)

Updated on: 12-Nov-2019

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements