MySQL 如何创建带索引的表

MySQL 如何创建带索引的表

要创建带有索引的MySQL表格,语法如下―

create table 你的表名
(
你的列名1 数据类型,
你的列名2 数据类型
.
.
.
N
);
create index 你的索引名1 on (你的列名1);
create index 你的索引名2 on (你的列名2);
Mysql

让我们先创建一个表格―

mysql> create table DemoTable
 -> (
 -> Id int,
 -> Name varchar(20)
 -> );
Query OK, 0 rows affected (0.63 sec)
Mysql

下面是带有表格索引的查询语句

mysql> create index id_index on DemoTable(Id);
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index Name_index on DemoTable(Name);
Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
Mysql

现在,您可以查看表的结构―

mysql> show create table DemoTable;
Mysql

这将产生以下结果―

+---------------+-----------------------------------------------------------------------------------------+
| Table         | Create Table                                                                            |  
+---------------+-----------------------------------------------------------------------------------------+
| DemoTable     | CREATE TABLE `DemoTable` ( `Id` int(11) DEFAULT NULL, `Name` varchar(20) COLLATE                               utf8_unicode_ci DEFAULT NULL, KEY `id_index` (`Id`), KEY `Name_index` (`Name`) )                            ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci                             |
+---------------+------------------------------------------------------------------------------------------+
1 row in set (0.05 sec)
Mysql

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册