MySQL documentations gives this format for creating a FULLTEXT index:
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option]
To implement full-text search with MySQL you can either create index by writing :
CREATE TABLE posts (
id int(4) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
content text,
PRIMARY KEY (id),
FULLTEXT (title, content)
) ENGINE=MyISAM;
OR
CREATE TABLE posts (
id int(4) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
content text,
PRIMARY KEY (id),
FULLTEXT KEY my_index_name (title, content)
) ENGINE=MyISAM;
with my_index_name
a user defined name and not a field name.
I am not able to find any clue in the docs:
FULLTEXT is the index type of full-text index in MySQL. InnoDB or MyISAM tables use Full-text indexes. Full-text indexes can be created only for VARCHAR, CHAR or TEXT columns. A FULLTEXT index definition can be given in the CREATE TABLE statement or can be added later using ALTER TABLE or CREATE INDEX.
A full-text index is a special type of index that provides index access for full-text queries against character or binary column data. A full-text index breaks the column into tokens and these tokens make up the index data.
A fulltext index uses internal tables called full-text index fragments to store the inverted index data. This view can be used to query the metadata about these fragments. This view contains a row for each full-text index fragment in every table that contains a full-text index.
Index: It is a schema object which is used to provide improved performance in the retrieval of rows from a table. Unique Index: Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns).
A full-text index is a special type of index that provides index access for full-text queries against character or binary column data. A full-text index breaks the column into tokens and these tokens make up the index data. Before you can create a full-text index you must create a FULL TEXT CATALOG...
One requirement for creating full-text indexes is that the table has a unique key defined on it. This option associates the unique key with the full-text index . In order to get the best performance this unique key column should be an integer, usually it's the primary key.
However, without indexes defined on the columns, the query engine must perform a full table scan to locate the necessary data. But there is a solution-full-text search. Full-text search refers to the functionality in SQL Server that supports full-text queries against character-based data.
In the ProductDocs table, the two indexed columns are DocSummary (the fifth column defined) and DocContent (the sixth column defined). The following table shows the first 25 terms stored in the ProductDocs full-text index.
There is no difference, if you use the first syntax then mysql will choose the name of the index, and in most cases the column name
, since you have 2 FULLTEXT (title, content)
it most likely to choose the index name as title
And in the 2nd case you are explicitly providing a name.
You can run the command show create table posts
after creating the tables and see what index names are for both cases.
Omitting the index name
If you add an index/key to a table MySQL will store additional information on the specified column (set of columns) to speed up searches.
In your first example MySQL will generate an index and name it
my_index_name
. If you omit the name, MySQL will choose one for you. I could not find documentation on how the name is chosen but in my experience the name of the first column is usually reused as index name.
For this part of the discussion the fulltext
option is irrelevant. It just defines the type of index/key you are creating:
You can create special FULLTEXT indexes, which are used for full-text searches.
Omitting the index/key
keyword
It is sufficient to just give fulltext
:
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ...
but it is probably a good idea to use the fulltext index
form, since the reader is reminded of an index.
index
and key
are synonyms
Note that there is no difference between index
and key
(see create table):
KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.
Example
use test;
create table u (
id int primary key,
a varchar(10),
b varchar(10),
fulltext index (a, b)
);
show index from u;
will print (I have omitted the primary index from this output as well as some additional columns):
table key_name seq_in_index column_name
u a 1 a
u a 2 b
You need the index name for example if you want to delete it:
alter table u
drop index a;
References
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With