創建表和索引
阿新 • • 發佈:2018-05-19
rds ati name 5.5 myisam 組合 seq pan primary
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.00sec) 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>
創建表和索引