您的位置:首页 > 数据库 > MySQL

mysql 之 sql管理数据 - 索引和表操作

2017-06-21 00:00 761 查看
索引

索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度。

1、单列索引:即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。

组合索引:即一个索包含多个列。

查看 某个表的 存储引擎情况:

show table status where name = 'user'; user(表名);

2、普通索引

普通索引的使用方式,一般在对筛选条件比较多的where后的字段建立索引,但是索引会影响数据的插入更新速度;

> 创建索引:

create table if not exists temp_test(
id int primary key auto_increment,
title varchar(30) not null,
describ varchar(20) not null,
index(title,describ)
) engine=innodb;

mysql> create index title on temp_test(title);
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table temp_test add index idx_title(title);
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

> 删除索引

mysql> drop index idx_title on temp_test;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

> 查看某个表 索引情况

mysql> show index from temp_test;
+-----------+------------+-----------+--------------+-------------+-----------+-
------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name  | Seq_in_index | Column_name | Collation |
Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+-----------+--------------+-------------+-----------+-
------------+----------+--------+------+------------+---------+---------------+
| temp_test |          0 | PRIMARY   |            1 | id          | A         |
0 |     NULL | NULL   |      | BTREE      |         |               |
| temp_test |          1 | idx_title |            1 | title       | A         |
0 |     NULL | NULL   |      | BTREE      |         |               |
+-----------+------------+-----------+--------------+-------------+-----------+-
------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

3、唯一索引(UNIQUE)

> 创建唯一索引

mysql> create unique index idx_title on temp_test(title);
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

> 删除唯一索引

mysql> drop index title on temp_test;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

4、全文索引(FULLTEXT)

> 创建 全文索引

mysql> create table if not exists article(
-> id int primary key auto_increment,
-> title varchar(200) default '',
-> content text,
-> fulltext key idx_content (content)
-> )engine=myisam character set utf8;
Query OK, 0 rows affected (0.06 sec)

mysql> alter table article add fulltext index idx_content(content);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

> 查询操作:

强烈注意:MySql自带的全文索引只能用于数据库引擎为MYISAM的数据表,如果是其他数据引擎,则全文索引不会生效,特殊查询操作如下:

mysql> SELECT * FROM article WHERE MATCH(content) AGAINST ('查询');
Empty set (0.01 sec)

5、查看下 当前表的 定义情况

mysql> show create table article;
+---------+---------------------------------------------------------------
--------------------------------------------------------------------------
----------------------------------------------------------------------+
| Table   | Create Table

|
+---------+---------------------------------------------------------------
--------------------------------------------------------------------------
----------------------------------------------------------------------+
| article | CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT '',
`content` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `idx_content` (`content`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+---------+---------------------------------------------------------------
--------------------------------------------------------------------------
----------------------------------------------------------------------+

6、修改某个表中的 列属性

mysql> alter table article modify title varchar(210);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

7、修改某个表中的 列名称

mysql> alter table article change title head varchar(210);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table article;
+---------+-------------------------------------------------------------------
------------------------------------------------------------------------------
-----------------------------------------------------------------------+
| Table   | Create Table

|
+---------+-------------------------------------------------------------------
------------------------------------------------------------------------------
-----------------------------------------------------------------------+
| article | CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`head` varchar(210) DEFAULT NULL,
`content` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `idx_content` (`content`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------
------------------------------------------------------------------------------
-----------------------------------------------------------------------+
1 row in set (0.00 sec)

8、表的 重命名

mysql> alter table article rename to t_article;
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+-------------------------+
| Tables_in_my_mysql_test |
+-------------------------+
| hh                      |
| log_2016                |
| log_2017                |
| log_merge               |
| log_partition           |
| t_article               |
+-------------------------+
6 rows in set (0.00 sec)

9、查看 指定数据库 的 新建情况

mysql> show create database my_mysql_test;
+---------------+---------------------------------------------------------------
---------+
| Database      | Create Database
|
+---------------+---------------------------------------------------------------
---------+
| my_mysql_test | CREATE DATABASE `my_mysql_test` /*!40100 DEFAULT CHARACTER SET
utf8 */ |
+---------------+---------------------------------------------------------------
---------+
1 row in set (0.00 sec)

10、查看某个表中 以 c 开头的 列

mysql> show columns from t_article like 'c%';
+---------+------+------+-----+---------+-------+
| Field   | Type | Null | Key | Default | Extra |
+---------+------+------+-----+---------+-------+
| content | text | YES  | MUL | NULL    |       |
+---------+------+------+-----+---------+-------+
1 row in set (0.02 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql
相关文章推荐