创建表和索引
Posted aaallenn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建表和索引相关的知识,希望对你有一定的参考价值。
1、创建表,这里需要注意,5.5以后默认引擎是InnoDB,5.1及之前是MyIsam
mysql> create table student( -> id int(4) not null, -> name char(20) not null, -> age tinyint(2) not null default ‘0‘, -> dept varchar(16) default null -> ); Query OK, 0 rows affected (0.03 sec)
mysql> mysql> show create table student\G *************************** 1. row *************************** Table: student Create Table: CREATE TABLE `student` ( `id` int(4) NOT NULL, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT ‘0‘, `dept` varchar(16) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) mysql>
2、创建带有主键和普通索引的表
mysql> create table student1( -> id int(4) not null auto_increment, -> name char(20) not null, -> age tinyint(2) not null default ‘0‘, -> dept varchar(16) default null, -> primary key (id), -> key index_name(name)); Query OK, 0 rows affected (0.11 sec) mysql> desc student1 -> ; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
3、增加主键
mysql> alter table student change id id int primary key auto_increment; Query OK, 0 rows affected (0.08 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student -> ; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql>
4、增加普通索引
mysql> alter table student add index ind_name(name); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql>
5、增加具有长的普通索引
mysql> create index ind_dept on student(dept(8)); Query OK, 0 rows affected (0.29 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student -> ; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql>
6、查看表上有哪些索引
mysql> show index from student -> ; +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_dept | 1 | dept | A | 0 | 8 | NULL | YES | BTREE | | | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec) mysql>
7、创建组合索引
mysql> create index ind_name_age on student(name,age); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from student; +---------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_dept | 1 | dept | A | 0 | 8 | NULL | YES | BTREE | | | | student | 1 | ind_name_age | 1 | name | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_name_age | 2 | age | A | 0 | NULL | NULL | | BTREE | | | +---------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 5 rows in set (0.00 sec) mysql>
8、创建唯一索引
mysql> create unique index uni_ind_age on student(age); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | UNI | 0 | | | dept | varchar(16) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> mysql> mysql> show index from student; +---------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | | student | 0 | uni_ind_age | 1 | age | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_dept | 1 | dept | A | 0 | 8 | NULL | YES | BTREE | | | | student | 1 | ind_name_age | 1 | name | A | 0 | NULL | NULL | | BTREE | | | | student | 1 | ind_name_age | 2 | age | A | 0 | NULL | NULL | | BTREE | | | +---------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 6 rows in set (0.00 sec) mysql>
以上是关于创建表和索引的主要内容,如果未能解决你的问题,请参考以下文章