Ok, so I've full text index on one table for col1 and col2 for example. I want to change this to only have col1 included in the full text index. How to do this?
You can not ALTER INDEX in MySQL, you need to DROP INDEX and CREATE INDEX a new one
Firstly, identify the name of your Full Text Index using the following MySQL query;
SHOW CREATE TABLE your_table_name;
Then run the following MySQL query to drop the table;
ALTER TABLE your_table_name DROP INDEX name_of_index_identified_by_previous_query_above;
Then create your Full Text Index again using the following code;
ALTER TABLE your_table_name ADD FULLTEXT INDEX `FullText` ('col1', 'col2', 'col3', 'etc');
Related
I'm using SugarCRM and a few weeks ago I executed a a query on MySQL which created an index to prevent duplicate rows. Where can I see that or find it and edit or delete this ? I'm not able to remember the exact query but it's needed to add more columns. Using MySQL only just a few weeks.
MySQL error 1062: Duplicate entry 'example-dyplicate' for key
'idx_name'
To see the structure of a table, including all the indexes, use:
SHOW CREATE TABLE tablename;
You can delete an index with:
DROP INDEX indexname ON tablename;
There's no way to edit an index. If you want to change an index, you drop it and then add a new index with the new columns you want. However, you can do both in a single query using ALTER TABLE:
ALTER TABLE tablename DROP INDEX indexname ADD INDEX indexname (col1, col2, ...);
The question is:
Write the SQL statement to create an index on the city table for the attribute CountryCode. Name the index CountryCode_idx.
So my statement is:
CREATE INDEX city_name_countrycode_idx ON city (name, countrycode);
but the problem is, when I try to drop the index in the next question using statement
DROP INDEX city_name_countrycode_idx;
I get a syntax error expecting ON
and then I try making a statement called:
DROP INDEX city_name_countrycode_idx ON city;
but my statement affected 0 rows apparently. Can someone tell me if my create index statement is correct and why my drop index statement isn't working?
DROP INDEX city_name_countrycode_idx;
I get a syntax error expecting ON ...
As per documentation on DROP INDEX Syntax:
Name: 'DROP INDEX'
Description:
Syntax:
DROP INDEX index_name ON tbl_name
[algorithm_option | lock_option] ...
algorithm_option:
ALGORITHM [=] {DEFAULT|INPLACE|COPY}
lock_option:
LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}
DROP INDEX drops the index named index_name from the table tbl_name.
Conclusion:
Hence to drop an index, you must use table name on which the index was created.
And for your index city_name_countrycode_idx to be dropped you have to use the table name city on which the said index is created.
DROP INDEX city_name_countrycode_idx ON city;
0 rows affected means no change in data . Although the index is dropped.
you can cross check by desc the table before and after dropping the index
Thanks
I have a table where two columns are used in a where condition.
This is a MyIsam table and both columns hold text and use FULLTEXT as index.
The values in both columns are not unique.
The select statement works pretty slow.
Question is: can I simply remove the FULLTEXT index and use another index instead?
The query that is used is just as simple as possbile:
SELECT * FROM tbl WHERE col1=X AND col2=y and col3=z
Thanks!
ALTER TABLE `tableName` DROP INDEX `indexName` ,
ADD INDEX `indexName` ( `ColName` )
This shuld remove the old "FULLTEXT" index and add a "NOT FULTEXT" index.
I am using MySQL v5.1.
I would like to create index on a table by executing the following SQL statement:
CREATE INDEX index_name
ON table_name (column_name)
But, I wan to firstly check if the index on that column has already been created or not, if not, create it (otherwise do not create). What is the SQL syntax for this?
Mysql doesn't have IF NOT EXISTS for CREATE INDEX. You can work it out by querying information_schema.statistics table. Take a look here, there is an example of stored procedure that does what you are looking for (search for "CREATE INDEX IF NOT EXISTS" on the page)
You want SHOW INDEX.
To get all the indexes on a table:
SHOW INDEX FROM table_name
MySQL allows you to add a WHERE clause to limit the results as well.
Lock the table while you're checking to see if the index exists (and if it doesn't exist, creating the index) so that another process doesn't create the index right after you've checked for it but before you've created it yourself.
I am creating a temp table with a query like this:
CREATE TEMPORARY TABLE temp_table
SELECT * FROM regular_table
WHERE 1
But regular_table has FULLTEXT index on some of the fields. I try to do a FULLTEXT search on the new temporary table and I get an error telling me "Can't find FULLTEXT index matching the column list". So obviusly the index is not copying over to the new table. Is there a way to force this?
Thanks.
You could use CREATE TEMPORARY TABLE temp_table LIKE regular_table, but that will create all the indexes, so when you do INSERT INTO temp_table SELECT * FROM regular_table, the indexes will be rebuilt - which could be lengthy.
Or, you can create the table and add the index afterwards:
CREATE TEMPORARY TABLE temp_table
ALTER TABLE temp_table ADD FULLTEXT INDEX (foo,bar,baz)
INSERT INTO temp_table SELECT * FROM regular_table
but the index will be, again, updated on every insert.
Probably the most efficient way would be to create the temp table, insert all, build index afterwards:
CREATE TEMPORARY TABLE temp_table
ALTER TABLE temp_table ADD FULLTEXT INDEX (foo,bar,baz)
ALTER TABLE temp_table DISABLE KEYS
INSERT INTO temp_table SELECT * FROM regular_table
ALTER TABLE temp_table ENABLE KEYS
Again, you will have to wait for the index to build, except it will happen in one chunk, with the last ALTER statement.
A temporary table is exactly the same as any other table except that it will be dropped at the end of the session. The only way to have the same indexes (from within the database) is to create them on the table as you would any other table.
Now there is a bit of a hack. You can copy the physical files on disk to a new name and have a clone of the table which includes indexes but I'm assuming you're doing this within an app so that might not be very practical.