
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL CREATE statement with KEY keyword
As stated in the official docs −
KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(50), Age int ); Query OK, 0 rows affected (0.69 sec)
Following is the query for INDEX, which is a synonym to KEY −
mysql> create index Name_Age_Index on DemoTable(Name,Age); Query OK, 0 rows affected (0.65 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of the table once again −
mysql> desc DemoTable;
This will produce the following output −
+-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(50) | YES | MUL | NULL | | | Age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Age) values('Robert',21); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name,Age) values('Bob',23); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name,Age) values('David',22); Query OK, 1 row affected (0.39 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 2 | Bob | 23 | | 3 | David | 22 | | 1 | Robert | 21 | +----+--------+------+ 3 rows in set (0.00 sec)
- Related Articles
- Create MySQL column with Key=MUL?
- Create variables in MySQL stored procedure with DECLARE keyword
- What does the KEY keyword mean in MySQL?
- While creating a MySQL table use the reserved keyword ‘Key’
- MySQL Syntax to create Foreign Key?
- Fetch info with MySQL EXPLAIN KEYWORD?
- Using CREATE TABLE AS statement with UNION of two tables in MySQL
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How can we modify a MySQL view with CREATE OR REPLACE VIEW statement?
- Why BINARY keyword used with MySQL REGEXP operator?
- How we have multiple stored GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- How to create Tab Delimited Select statement in MySQL?
- MySQL SELECT IF statement with OR?
- MySQL If statement with multiple conditions?
- Using variables with MySQL prepare statement

Advertisements