1. 程式人生 > >mysql11---主鍵普通全文索引

mysql11---主鍵普通全文索引

自己 security primary bold part values 處理 incr like

1.1主鍵索引添加
當一張表,把某個列設為主鍵的時候,則該列就是主鍵索引
create table aaa
(id int unsigned primary key auto_increment ,
name varchar(32) not null defaul ‘’);
這是id 列就自動是主鍵索引.
如果你創建表時,沒有指定主鍵索引,也可以在創建表後,在添加, 指令:
alter table 表名 add primary key (列名);
舉例: 
create table bbb (id int , name varchar(32) not null default ‘’);
alter
table bbb add primary key (id);
1.2普通索引
一般來說,普通索引的創建,是先創建表,然後在創建普通索引
比如:
create table ccc(
id int unsigned,
name varchar(32)
)
create index 索引名 on 表 (列1,列名2);
1.3創建全文索引
全文索引,主要是針對對文件,文本的檢索, 比如文章, 全文索引針對MyISAM有用.
創建 :
CREATE TABLE articles (
       id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY
KEY, title VARCHAR(200), body TEXT, FULLTEXT (title,body) #title和body的復合全文索引 )engine=myisam charset utf8; INSERT INTO articles (title,body) VALUES (MySQL Tutorial,DBMS stands for DataBase ...), (How To Use MySQL Well,After you went through a ...
), (Optimizing MySQL,In this tutorial we will show ...), (1001 MySQL Tricks,1. Never run mysqld as root. 2. ...), (MySQL vs. YourSQL,In the following database comparison ...), (MySQL Security,When configured properly, MySQL ...); 如何使用全文索引:
錯誤用法:
select * from articles where body like%mysql%’; 【不會使用到全文索引】
證明:
explain  select * from articles where body like%mysql%+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | articles | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |    16.67 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
正確的用法是:
select * from articles where match(title,body) against(‘database’); 【可以,possible_keys,key+----+-------------+----------+------------+----------+---------------+-------+---------+-------+------+----------+-------------+
| id | select_type | table    | partitions | type     | possible_keys | key   | key_len | ref   | rows | filtered | Extra       |
+----+-------------+----------+------------+----------+---------------+-------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | articles | NULL       | fulltext | title         | title | 0       | const |    1 |      100 | Using where |
+----+-------------+----------+------------+----------+---------------+-------+---------+-------+------+----------+-------------+
mysql> select * from articles where match(title,body) against(database);
+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |
+----+-------------------+------------------------------------------+
? 說明:
1.在mysql中fulltext 索引只針對 myisam生效
2.mysql自己提供的fulltext針對英文生效----->sphinx (coreseek) 技術專門處理中文全文索引
3.使用方法是 match(字段名..) against(關鍵字)

4.全文索引一個 叫 停止詞,因為在一個文本中,創建索引是一個無窮大的數,因此,對一些常用詞和字符,就不會創建(常用詞你我他太多了,即使查詢出來也沒有多大意思和匹配度),這些詞,稱為停止詞.全文裏面建索引是很困難的,如果把每一個字都建成索引難度很高,因為字符太多了,所以只會針對生僻詞(關鍵詞)建索引,否則每個詞都建索引,量就很大了,
mysql> select  match(title,body) against(database) from articles ;
+--------------------------------------------------------+
| match(title,body) against(database) |
+--------------------------------------------------------+
|                    0.6554583311080933 |  (第一條記錄裏面可能匹配到database的概率是0.65545833)
|                                     0 |
|                                     0 |
|                                     0 |
|                    0.6626645922660828 |
|                                     0 |
+--------------------------------------------------------+
mysql> select  match(title,body) against(da) from articles ;
+---------------------------------+
| match(title,body) against(da) |
+---------------------------------+
|                               0 |(da就沒有建索引)
|                               0 |
|                               0 |
|                               0 |
|                               0 |
|                               0 |
+---------------------------------+

mysql11---主鍵普通全文索引