grant语句:专门用来设置数据库用户的访问权限。当指定的用户名不存在时,grant语句将会创建新的用户;当指定的用户名存在时,grant语句用于修改用户信息。
grant 权限列表 on 数据库名.数据表名 to '用户名'@'来源地址' [identified by '密码'];
权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如"select,insert,update"。使用"all"表示所有权限,可授权执行任何操作。
数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符"*"。例如,使用"*.*"b表示授权操作的对象为所有数据库中的所有表。
'用户名@来源地址':用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里链接。来源地址可以是域名、IP地址,还可以使用"%"通配符,表示某个区域或网段内的所有地址,如"%.test.com"、"192.168.122.%"等。
identified by:用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略"identified by"部分,则用户的密码将为空。
mysql> grant select on test.* to 'zhangsan'@'localhost' identified by '123456'; #允许用户zhangsan在本地查询test数据库中所有表的数据记录,但禁止查询数据库中的表的记录。 Query OK, 0 rows affected, 2 warnings (0.00 sec mysql> grant all on *.* to 'lisi'@'%' identified by '123456'; #允许用户lisi在所有终端远程连接mysql,并拥有所有权限。 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; #刷新权限 Query OK, 0 rows affected (0.00 sec)
方法一:
show grants for '用户名'@'来源地址';
mysql> show grants for zhangsan@localhost; +----------------------------------------------------+ | Grants for zhangsan@localhost | +----------------------------------------------------+ | GRANT USAGE ON *.* TO 'zhangsan'@'localhost' | | GRANT SELECT ON "test".* TO 'zhangsan'@'localhost' | +----------------------------------------------------+ 2 rows in set (0.00 sec) mysql> show grants for lisi@'%'; +-------------------------------------------+ | Grants for lisi@% | +-------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'lisi'@'%' | +-------------------------------------------+ 1 row in set (0.00 sec)
方法二:
select * from mysql.user where user='用户名' and host='来源地址'/G;
mysql> select * from mysql.user where user='zhangsan' and host='localhost'\G; *************************** 1. row *************************** Host: localhost User: zhangsan Select_priv: N Insert_priv: N Update_priv: N Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N Trigger_priv: N Create_tablespace_priv: N ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 password_expired: N password_last_changed: 2021-08-27 10:56:48 password_lifetime: NULL account_locked: N 1 row in set (0.00 sec) ERROR: No query specified mysql> select * from mysql.user where user='lisi' and host='%'\G; *************************** 1. row *************************** Host: % User: lisi Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Reload_priv: Y Shutdown_priv: Y Process_priv: Y File_priv: Y Grant_priv: N References_priv: Y Index_priv: Y Alter_priv: Y Show_db_priv: Y Super_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Execute_priv: Y Repl_slave_priv: Y Repl_client_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Create_user_priv: Y Event_priv: Y Trigger_priv: Y Create_tablespace_priv: Y ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 password_expired: N password_last_changed: 2021-08-27 10:59:19 password_lifetime: NULL account_locked: N 1 row in set (0.00 sec) ERROR: No query specified
revoke 权限 on 数据库名.数据表名 from '用户名'@'来源地址';
mysql> revoke all on test.* from zhangsan@localhost; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show grants for zhangsan@localhost; +----------------------------------------------+ | Grants for zhangsan@localhost | +----------------------------------------------+ | GRANT USAGE ON *.* TO 'zhangsan'@'localhost' | +----------------------------------------------+ 1 row in set (0.00 sec) #权限删除后,仍会有允许用户登录的权限存在 mysql> revoke update on *.* from lisi@'%'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for lisi@'%'; +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Grants for lisi@% | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | GRANT SELECT, INSERT, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'lisi'@'%' | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
all privileges所含权限 | 功能 |
---|---|
select | 查询数据 |
insert | 插入数据 |
update | 更新数据 |
delete | 删除数据 |
create | 创建库/表 |
drop | 删除库/表 |
reload | 重载,可使用flush语句进行刷新操作 |
shutdown | 关闭MySQL服务 |
process | 显示或杀死属于其他用户的服务线程 |
file | 在MySQL服务器上读写文件 |
references | 建立外键约束 |
index | 建立索引 |
alter | 更改表属性 |
show databases | 查看全局数据库 |
super | 允许用户终止任何查询;修改全局变量的SET语句;使用CHANGE MASTER,PURGE MASTER LOGS |
create temporary tables | 创建临时表 |
lock tables | 锁表 |
execute | 执行存在的函数和程序 |
replication slave | 查看从服务器,从主服务器读取二进制日志 |
replication client | 查询主服务器、从服务器状态 |
create view | 创建视图 |
show view | 显示视图 |
create routine | 创建存储过程 |
create user | 创建用户 |
event | 时间 |
trigger | 创建触发器 |
create tablespace | 创建表空间 |
注:
[root@localhost ~]# mysql --version mysql Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using EditLine wrapper
不同版本的权限列表不同,以上仅以5.7.20为例。
● 索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址(类似于C语言的链表通过指针指向数据记录的内存地址)
● 使用索引后可以不用扫描全表来定位某行的数据,而是先通过索引表找到该行数据对应的物理地址然后访问相应的数据,因此能加快数据库的查询速度
● 索引就好比是一本书的目录,可以根据目录中的页码快速找到所需的内容
● 索引是表中一列或者若干列值排序的方法
● 建立索引的目的是加快对表中记录的查找或排序
● 设置了合适的索引之后,数据库利用各种快速定位技术,能够大大加快查询速度,这是创建索引的最主要的原因
● 当表很大或查询设计到多个表时,使用索引可以成千上万倍地提高查询速度
● 可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本
● 通过创建唯一性索引,可以保证数据表中每一行数据的唯一性
● 可以加快表与表之间的连接
● 在使用分组和排序时,可大大减少分组和排序的时间
● 建立索引在抖索和恢复数据库中的数据时能显著提高性能
● 索引需要占用额外的磁盘空间
对于MyISAM引擎而言,索引文件和数据文件是分离的,索引文件用于保存数据记录的地址
而InnoDB引擎的表数据文件本身就是索引文件
● 在插入和修改数据时要花费更多的额时间,因为索引也要随之变动
索引虽可以提升数据库查询的速度,但并不是任何情况下都适合创建索引。因为索引本身会消耗系统资源,在有索引的情况下,数据库会先进行索引查询,然后定位到具体的数据行,如果索引使用不当,反而会增加数据库的负担。
● 表的主键、外键必须有索引。因为主键具有唯一性,外键关联的是主表的主键,查询时可以快速定位
● 记录数超过300行的表应该有索引。如果没有索引,每次查询都需要把表遍历一遍,会严重影响数据库的性能
● 经常与其他表进行连接的表,在连接字段上应该建立索引
● 唯一性太差的字段不适合建立索引
● 更新太频繁的字段不适合创建索引
● 经常出现在where字句中的字段,特别是大表的字段,应该建立索引
● 在经常进行group by、order by的字段上建立索引
● 索引应该建在选择性高的字段上
● 索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引
新建实验表
mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> create table member (id int(10),name varchar(10),cardid int(18),phone int(11),address varchar(50),remark text); Query OK, 0 rows affected (0.00 sec) mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | | cardid | int(18) | YES | | NULL | | | phone | int(11) | YES | | NULL | | | address | varchar(50) | YES | | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
普通索引:最基本的所有类型,没有唯一性之类的限制
create index 索引名 on 表名 (列名[(length)]);
● (列名[(length)]):length是可选项,下同。如果省略length的值,则使用整个列的值作为索引。如果指定,使用列的前length个字符来创建索引,这样有利于减小索引文件的大小。在不损失精确性的情况下,长度越短越好。
● 索引名建议以"_index"结尾。
mysql> create index name_index on member (name); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | | NULL | | | phone | int(11) | YES | | NULL | | | address | varchar(50) | YES | | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
alter table 表名 add index 索引名 (列名);
mysql> alter table member add index cardid_index (cardid); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | | NULL | | | address | varchar(50) | YES | | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
create table 表名 (字段1 数据类型,字段2 数据类型[,...],index (列名));
mysql> create table test (id int,name varchar(10),index name_index (name)); Query OK, 0 rows affected (0.01 sec) mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
唯一索引:与普通索引类似,但区别是唯一索引列的每个值都唯一。唯一索引允许有空值(注意和主键不同)。如果是用组合索引创建,则列值的组合必须唯一。添加唯一键将自动创建唯一索引。
create unique index 索引名 on 表名(列名);
mysql> create unique index phone_index on member(phone); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
alter table 表名 add unique 索引名 (列名);
mysql> alter table member add unique add_index (address); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
create table 表名 (字段1 数据类型,字段2 数据类型[,...],unique 索引名 (列名));
mysql> create table test (id int,name varchar(10),cardid bigint(18),unique cardid_index (cardid)); Query OK, 0 rows affected (0.00 sec) mysql> desc test; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | | cardid | bigint(18) | YES | UNI | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
主键索引:是一种特殊的唯一索引,必须指定为"primary key"。一个表只能有一个主键索引,不允许有空值。添加主键将自动创建主键索引。
create table 表名 ([...],primary key (列名));
mysql> create table test (id int,name varchar(10),primary key(id)); Query OK, 0 rows affected (0.01 sec) mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
或
create table 表名 ([...],主键字段 数据类型 primary key[;...]);
mysql> create table test (id int primary key,name varchar(10)); Query OK, 0 rows affected (0.01 sec) mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
alter table 表名 add primary key (列名);
mysql> alter table member add primary key (id); Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.01 sec)
组合索引(单列索引与多列索引):可以是单列上创建的索引,也可以是在多列上创建的所有。需要满足最左原则,因为select语句的where条件是依次从左往右执行的,所以在使用select语句查询时where条件使用的字段顺序必须和组合索引中的排序一直,否则索引将不会生效。
create table 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,index 索引名 (列名1,列名2,列名3));
mysql> create table menu (id int,foodname varchar(20),price int,index foodprice_index (id,foodname,price)); Query OK, 0 rows affected (0.00 sec) mysql> desc menu; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | | foodname | varchar(20) | YES | | NULL | | | price | int(11) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> show create table menu; +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | menu | CREATE TABLE "menu" ( "id" int(11) DEFAULT NULL, "foodname" varchar(20) DEFAULT NULL, "price" int(11) DEFAULT NULL, KEY "foodprice_index" ("id","foodname","price") ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
select * from 表名 where 列名1='...' and 列名2='...' and 列名3='...';
mysql> insert into menu values(1,'鱼香肉丝',28); Query OK, 1 row affected (0.00 sec) mysql> insert into menu values(2,'麻婆豆腐',18); Query OK, 1 row affected (0.00 sec) mysql> insert into menu values(3,'水煮肉片',38); Query OK, 1 row affected (0.00 sec) mysql> insert into menu values(4,'辣子鸡',38); Query OK, 1 row affected (0.00 sec) mysql> select * from menu; +------+--------------+-------+ | id | foodname | price | +------+--------------+-------+ | 1 | 鱼香肉丝 | 28 | | 2 | 麻婆豆腐 | 18 | | 3 | 水煮肉片 | 38 | | 4 | 辣子鸡 | 38 | +------+--------------+-------+ 4 rows in set (0.00 sec) mysql> select * from menu where price=38 and foodname='辣子鸡'; +------+-----------+-------+ | id | foodname | price | +------+-----------+-------+ | 4 | 辣子鸡 | 38 | +------+-----------+-------+ 1 row in set (0.00 sec) mysql> select * from menu where price=38 and id=3; +------+--------------+-------+ | id | foodname | price | +------+--------------+-------+ | 3 | 水煮肉片 | 38 | +------+--------------+-------+ 1 row in set (0.00 sec)
全文索引(fulltext):适合在进行模糊查询的时候使用,可用于在一篇文章中检索文本信息。在MySQL5.6版本以前,fulltext索引仅可用于MyISAM引擎,在5.6版本之后innodb引擎也支持fulltext索引。全文索引可以在char、varchar或者text类型的列上创建。每个表只允许有一个全文索引。
create fulltext index 索引名 on 表名 (列名);
mysql> create fulltext index remark_index on member (remark); Query OK, 0 rows affected, 1 warning (0.02 sec) Records: 0 Duplicates: 0 Warnings: 1 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> show create table member; +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | member | CREATE TABLE "member" ( "id" int(10) NOT NULL, "name" varchar(10) DEFAULT NULL, "cardid" int(18) DEFAULT NULL, "phone" int(11) DEFAULT NULL, "address" varchar(50) DEFAULT NULL, "remark" text, PRIMARY KEY ("id"), UNIQUE KEY "phone_index" ("phone"), UNIQUE KEY "add_index" ("address"), KEY "name_index" ("name"), KEY "cardid_index" ("cardid"), FULLTEXT KEY "remark_index" ("remark") ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
alter table 表名 add fulltext 索引名 (列名);
mysql> drop index remark_index on member; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> alter table member add fulltext remark_index (remark); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
create table 表名 (字段1 数据类型[,...],fulltext 索引名 (列名));
数据类型只可为char、varchar、text。
mysql> create table staff_info (id int(4),name char(10),cardid bigint(18),age int(3),phone bigint(11),remark text, fulltext remark_index (remark)); Query OK, 0 rows affected (0.15 sec) mysql> desc staff_info; +--------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | char(10) | YES | | NULL | | | cardid | bigint(18) | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | remark | text | YES | MUL | NULL | | +--------+------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> show create table staff_info; +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | staff_info | CREATE TABLE "staff_info" ( "id" int(4) DEFAULT NULL, "name" char(10) DEFAULT NULL, "cardid" bigint(18) DEFAULT NULL, "age" int(3) DEFAULT NULL, "phone" bigint(11) DEFAULT NULL, "remark" text, FULLTEXT KEY "remark_index" ("remark") ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
select * from 表名 where match(列名) against('查询内容');
mysql> insert into staff_info values (1,'zhangsan',112233445566778899,23,13111111111,'this is chairman'); Query OK, 1 row affected (0.00 sec) mysql> insert into staff_info values (2,'lisi',212233445566778899,33,13222222222,'this is ceo'); Query OK, 1 row affected (0.00 sec) mysql> insert into staff_info values (3,'wangwu',312233445566778899,43,13333333333,'this is cfo'); Query OK, 1 row affected (0.00 sec) mysql> insert into staff_info values (4,'zhaoliu',412233445566778899,44,13444444444,'this is hr'); Query OK, 1 row affected (0.00 sec) mysql> select * from staff_info; +------+----------+--------------------+------+-------------+------------------+ | id | name | cardid | age | phone | remark | +------+----------+--------------------+------+-------------+------------------+ | 1 | zhangsan | 112233445566778899 | 23 | 13111111111 | this is chairman | | 2 | lisi | 212233445566778899 | 33 | 13222222222 | this is ceo | | 3 | wangwu | 312233445566778899 | 43 | 13333333333 | this is cfo | | 4 | zhaoliu | 412233445566778899 | 44 | 13444444444 | this is hr | +------+----------+--------------------+------+-------------+------------------+ 4 rows in set (0.00 sec) mysql> mysql> select * from staff_info whereremark) against('ceo'); +------+------+--------------------+------+-------------+-------------+ | id | name | cardid | age | phone | remark | +------+------+--------------------+------+-------------+-------------+ | 2 | lisi | 212233445566778899 | 33 | 13222222222 | this is ceo | +------+------+--------------------+------+-------------+-------------+ 1 row in set (0.00 sec)
show index from 表名;
show keys from 表名;
mysql> show index from member; +--------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +--------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | member | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | | member | 0 | phone_index | 1 | phone | A | 0 | NULL | NULL | YES | BTREE | | | | member | 0 | add_index | 1 | address | A | 0 | NULL | NULL | YES | BTREE | | | | member | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | | | member | 1 | cardid_index | 1 | cardid | A | 0 | NULL | NULL | YES | BTREE | | | | member | 1 | remark_index | 1 | remark | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | +--------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 6 rows in set (0.00 sec) mysql> show keys from member; +--------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +--------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | member | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | | member | 0 | phone_index | 1 | phone | A | 0 | NULL | NULL | YES | BTREE | | | | member | 0 | add_index | 1 | address | A | 0 | NULL | NULL | YES | BTREE | | | | member | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | | | member | 1 | cardid_index | 1 | cardid | A | 0 | NULL | NULL | YES | BTREE | | | | member | 1 | remark_index | 1 | remark | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | +--------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 6 rows in set (0.00 sec)
一般建议使用\G纵向查看
show index from 表名\G;
show keys from 表名\G;
mysql> show keys from member\G; *************************** 1. row *************************** Table: member Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: member Non_unique: 0 Key_name: phone_index Seq_in_index: 1 Column_name: phone Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: member Non_unique: 0 Key_name: add_index Seq_in_index: 1 Column_name: address Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 4. row *************************** Table: member Non_unique: 1 Key_name: name_index Seq_in_index: 1 Column_name: name Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 5. row *************************** Table: member Non_unique: 1 Key_name: cardid_index Seq_in_index: 1 Column_name: cardid Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 6. row *************************** Table: member Non_unique: 1 Key_name: remark_index Seq_in_index: 1 Column_name: remark Collation: NULL Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: FULLTEXT Comment: Index_comment: 6 rows in set (0.00 sec) ERROR: No query specified
drop index 索引名 on 表名;
mysql> drop index name_index on member; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | | NULL | | | cardid | int(18) | YES | MUL | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
alter table 表名 drop index 索引名;
mysql> alter table member drop index cardid_index; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | | NULL | | | cardid | int(18) | YES | | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
alter table 表名 drop primary key;
mysql> alter table member drop primary key; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc member; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | | NULL | | | name | varchar(10) | YES | | NULL | | | cardid | int(18) | YES | | NULL | | | phone | int(11) | YES | UNI | NULL | | | address | varchar(50) | YES | UNI | NULL | | | remark | text | YES | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
比如为某商场做一个会员卡系统。这个系统有一个会员表,有下列字段:
● 会员编号 int(10)
作为主键,使用primary key
● 会员姓名 varchar(10)
建立普通索引
● 会员身份证号码 varchar(18)
建立唯一索引
● 会员电话 bigint(11)
● 会员住址 varchar(50)
● 会员备注信息 text
建立fulltext,全文索引。不过fulltext用于搜索很长一篇文章的时候,效果最好。用在比较短的文本,如果就一两行字的,普通的index也可以
mysql> create table vip (id int(10),name varchar(10),cardid varchar(18),phone bigint(11),address varchar(50),remark text); Query OK, 0 rows affected (0.00 sec) mysql> alter table vip add primary key(id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> create index name_index on vip (name); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> create unique index cardid_index on vip(cardid); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table vip add fulltext remark_index (remark); Query OK, 0 rows affected, 1 warning (0.04 sec) Records: 0 Duplicates: 0 Warnings: 1 mysql> desc vip; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | varchar(10) | YES | MUL | NULL | | | cardid | varchar(18) | YES | UNI | NULL | | | phone | bigint(11) | YES | | NULL | | | address | varchar(50) | YES | | NULL | | | remark | text | YES | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> show index from vip\G; *************************** 1. row *************************** Table: vip Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: vip Non_unique: 0 Key_name: cardid_index Seq_in_index: 1 Column_name: cardid Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: vip Non_unique: 1 Key_name: name_index Seq_in_index: 1 Column_name: name Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 4. row *************************** Table: vip Non_unique: 1 Key_name: remark_index Seq_in_index: 1 Column_name: remark Collation: NULL Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: FULLTEXT Comment: Index_comment: 4 rows in set (0.00 sec) ERROR: No query specified