码迷,mamicode.com
首页 > 其他好文 > 详细

创建表和索引

时间:2018-05-19 18:49:58      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:mys   0.11   mysq   hang   auto   tree   sha   alt   har   

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> 

  

创建表和索引

标签:mys   0.11   mysq   hang   auto   tree   sha   alt   har   

原文地址:https://www.cnblogs.com/aaallenn/p/9060979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!