I'm working with fulltext, I executed an command to add the fulltext index to multiple comments, and returned no errors, then I did:
SELECT * FROM products WHERE MATCH(`brand`) AGAINST('Skoda');
Which is in the brand column - but I get following:
Can't find FULLTEXT index matching the column list
Eventho, when my table looks like this:
FULLTEXT KEY `name` (`name`,`breadcrumb`,`description`,`brand`,`price`,`year`,`km`,`usage`,`type`)
Is it because I should use the name instead? to do the search? Or what can be wrong.
Assuming you are using MyISAM engine, Execute:
ALTER TABLE products ADD FULLTEXT(brand);
The fulltext index should contain exactly the same number of columns, in same order as mentioned in MATCH clause.
If you don't feel like having the columns in the same order as in the match clause( or the same number), you can always use 'OR' that is:
ALTER TABLE products ADD FULLTEXT(brand);
ALTER TABLE products ADD FULLTEXT(product_name);
SELECT * FROM products WHERE MATCH(brand) AGAINST('+Skoda*' IN BOOLEAN MODE) OR MATCH(product_name) AGAINST('+productName*' IN BOOLEAN MODE)
When everything was right and still got this error I found that the KEYS were disabled. A simple error that is sometimes overlooked:
Make sure you have enabled the keys on that table.
It didn't work for me when I had disabled the keys. But when I enabled the keys ALTER TABLE table name ENABLE KEYS; it worked fine
I found I also needed to do this on my instance as the index was not visible. It was a checkbox while exploring MySQL Workbench. While invisible the index is not reachable by a query.
ALTER TABLE products ALTER INDEX brand VISIBLE;
Make sure the table engine is set to MyISAM.
Related
Here's my query:
SELECT * from description WHERE (match(description) AGAINST ( '+will +smith' in boolean mode))
I'm aware that will is a stopword that's why i'm getting an empty result.
How would it work that i can still use both words for this query? Do i need to escape it in somekind of way?
There isn't a way to "escape" a stopword for a given search. Think of it this way: when creating the fulltext index, it skips indexing words if they are stopwords. That is, the words are not stored in the fulltext index. So you can't subsequently escape the word in a given search and have a word magically appear in the fulltext index since that wasn't included when the index was created.
Assuming you are using fulltext search with InnoDB, the solution is apparently to define your own table storing stopwords. Then you can put a customized set of words into the table, and use the configuration variable innodb_ft_server_stopword_table to make your instance of MySQL use your custom table before creating your fulltext index. This way, the word you want to be indexed will be included as it builds the fulltext index.
See https://dev.mysql.com/doc/refman/8.0/en/fulltext-stopwords.html
But this is a global variable, so it will affect all fulltext index creation on all tables on that MySQL instance. I suppose you could set the innodb_ft_server_stopword_table to your custom table, build your fulltext index, and then set the option back to its usual value. But that would be tricky, because anytime you rebuild your fulltext index (for instance during an alter table or optimize table), it would revert to the default stopwords.
I created a FULLTEXT index on a table in a MySql database, and left out a column when I did so. I have been trying to figure out how to add this column so that it can be searched against along with the other columns in this table that have already been indexed.
I have searched SO and the web, but am not getting anywhere. I tried going into phpMyAdmin, and when you click on the table and then look at the structure, all the way on the right there is a dropdown that says "More." This has a "Fulltext" option that seems to add fulltext indexing to the column, so I did this, but it is still not allowing me to search the column.
Did I add FULLTEXT to this column? Either way, how do I get this to work?
For SQL Server users, it can be done like this:
ALTER FULLTEXT INDEX ON [table_name] ADD ([column_name])
ALTER TABLE foo DROP FULLTEXT old_ft_index_name, ADD FULLTEXT(this, that);
I use phpmyadmin to manually change the id (auto_increment) of some row data, therefore when I run my program to create a new row data, it returns me an error of "duplicated entry id and thus could not create the row".
This problem seems classical but I don't know the right keywords or phrases to look it up on google. Thank you.
Try
ALTER TABLE tablename AUTO_INCREMENT=X
Where X is some number higher than the highest ID.
I haven't tested this, but it may work:
ALTER TABLE tablename AUTO_INCREMENT=(SELECT MAX(id)+1 FROM tablename)
(It may fail due to selecting from the same table that is being altered)
I'm working with fulltext, I executed an command to add the fulltext index to multiple comments, and returned no errors, then I did:
SELECT * FROM products WHERE MATCH(`brand`) AGAINST('Skoda');
Which is in the brand column - but I get following:
Can't find FULLTEXT index matching the column list
Eventho, when my table looks like this:
FULLTEXT KEY `name` (`name`,`breadcrumb`,`description`,`brand`,`price`,`year`,`km`,`usage`,`type`)
Is it because I should use the name instead? to do the search? Or what can be wrong.
Assuming you are using MyISAM engine, Execute:
ALTER TABLE products ADD FULLTEXT(brand);
The fulltext index should contain exactly the same number of columns, in same order as mentioned in MATCH clause.
If you don't feel like having the columns in the same order as in the match clause( or the same number), you can always use 'OR' that is:
ALTER TABLE products ADD FULLTEXT(brand);
ALTER TABLE products ADD FULLTEXT(product_name);
SELECT * FROM products WHERE MATCH(brand) AGAINST('+Skoda*' IN BOOLEAN MODE) OR MATCH(product_name) AGAINST('+productName*' IN BOOLEAN MODE)
When everything was right and still got this error I found that the KEYS were disabled. A simple error that is sometimes overlooked:
Make sure you have enabled the keys on that table.
It didn't work for me when I had disabled the keys. But when I enabled the keys ALTER TABLE table name ENABLE KEYS; it worked fine
I found I also needed to do this on my instance as the index was not visible. It was a checkbox while exploring MySQL Workbench. While invisible the index is not reachable by a query.
ALTER TABLE products ALTER INDEX brand VISIBLE;
Make sure the table engine is set to MyISAM.
I have a teamnews Table:
And another table called team:
The values in teamnews table are predetermined before a user signs into the site.
Lets say when a user(teamName) signs in I want to update the teamID row where NewsID = 1
And create a relationship so that if I eventually delete the user(teamName) the teamID value in the teamNews table is reset to zero.
Is this possible?
Please bare in mind I am using phpMyAdmin, so I am not entirely familiar with advanced SQL terms.
When I try to do this I get and error:
Here's the error:
You need to specify a FOREIGN KEY. You can add it in by running this command:
ALTER TABLE teamnews
ADD CONSTRAINT fk_teamID
FOREIGN KEY (TeamID)
REFERENCES team (teamID)
ON DELETE SET NULL;
This sets up a formal relationship between the tables using the foreign key. The ON DELETE SET NULL is the part that is important to you. This says that whenever any item in the referenced table (team in this instance) is deleted, then all rows in this table that had that team id should set that field to null — exactly what you're looking for.
Be aware that this will only work if you are using the InnoDB database engine (not the MyISAM engine). You can probably change that through phpmyadmin somewhere (I'm not familiar with phpmyadmin so I can't help you on the details).
Also be aware that for this to work, MySQL must actually be able to "SET NULL" -- the field containing the foreign key with this constraint can't be set to "NOT NULL" or it will fail with an error saying to "check data type".