Table missing... or not (MYSQL)? - mysql

Suddenly I've a strange problem with Mysql:
In the navigator I see the "company" table (even after refresh), but if I do SELECT * FROM company; says that the table does not exist.
With the command SHOW TABLES FROM smartex_develop; the table "company" is present, but if I use the command SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'smartex_develop'; the table is missing. It's very strange also considering that there are a lot of table with a foreign key of that table.
Someone know how to resolve it?
[SELECT * FROM company] 1
[SHOW TABLES FROM smartex_develop] 2
[SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'smartex_develop'] 3

We had a similar problem - a table suddenly went missing. In the logs we had:
Load table <name-of-missing-table> failed, the table has missing foreign key indexes. Turn off 'foreign_key_checks' and try again.
InnoDB: Foreign Key referenced table <name-of-missing-table> not found for foreign table <some-other-table>
This happened upon mysqld startup, so the root cause might have been much older than that. In our case, the root cause was charset conversions. We converted a few tables, and ended up with foreign keys where the column in one table and the column in the referenced table had different character sets.
How we solved:
Disable foreign key checks
Restart mysql -- the missing table will now reappear
Convert all the columns that reference each other to have the same character set. You can use this query: select table_name,column_name,CHARACTER_SET_NAME from INFORMATION_SCHEMA.COLUMNS where table_schema = 'myschema' and data_type='varchar';
Re-enable foreign key checks
Restart mysql -- all should be fine now

Related

MySQL table listed twice with different case name

I have a MySQL database table which has been listed twice with case sensitive name.
Both table names are pointing to same table, for example Admin and admin
When I checked information_schema it is listed as below:
mysql> SELECT TABLE_CATALOG, TABLE_NAME , TABLE_TYPE, ENGINE, CREATE_TIME
FROM information_schema.tables
where table_schema='school';
How do I clean up this mess?
Usually MySQL does not allow you to create the table with case-sensitive. It will show the error as :
ERROR 1050 (42S01): Table 'admin' already exists
But MySQL allows you to create a temp table with a existing name because they don't have the same "scope". A temporary table is visible in the session only, and it is dropped at session ending. If you have the same name, MySQL "hide" the original table until you drop your temp table.
I would suggest you to take a backup of the existing data and update the MySql version to 5.7 .

drop primary key if exists

I need a query that could drop primary key only if it exists.
ALTER TABLE tablename DROP PRIMARY KEY;
This will return error if it does not exists, but my requirement is to run a query in different databases.
In MariaDB 10.2.16 i was able to solve this problem with:
ALTER TABLE tablename DROP INDEX IF EXISTS `PRIMARY`;
This should work with any table since the primary keys in MySQL are always called PRIMARY as stated in MySQL doc:
The name of a PRIMARY KEY is always PRIMARY, which thus cannot be
used as the name for any other kind of index.
I would recommend using this:
SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.',TABLE_NAME,
' DROP PRIMARY KEY; ANALYZE TABLE ', TABLE_SCHEMA, '.',TABLE_NAME, ';')
FROM information_schema.COLUMNS
WHERE CONCAT(TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME) IN
(SELECT CONCAT(TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE INDEX_NAME = 'PRIMARY' -- *Required* to get only the primary keys from the statistics table.
-- *Optional*
AND TABLE_SCHEMA = 'clients_database');
Run this to generate your required SQL.
Copy your results, then run them as working queries.
ANALYZE TABLE is optional as well as the WHERE clause.
You can remove ANALYZE TABLE ', TABLE_SCHEMA, '.',TABLE_NAME, ';' if desired from the query below.
I exploit the information_schema when researching and utilizing standardization techniques.
Just about everything you would ever need or want to know about your tables and columns lives in some System table in either (if applicable)
Database / Table_schema:
information_schema
performance_schema
mysql
Note: From doc's
Internal schemas, such as "performance_schema", "information"schema", "sys", and "mysql", are hidden by default. Toggle the Show Metadata and Internal Schemas preference to list them in the object browser. Schemas beginning with a "." are also controlled by this setting.
NOTE: Here's something similar that has been created.
Hope this helps!
Cheers,
Jay
I think the easy option might be this:
first go to :
'YourDatabase'>tables>your table name>keys>copy the constraints like 'PK__TableName__0001'
then run this:
Query:alter Table 'TableName' drop constraint PK__TableName__0001

How to remove MySQL tables prefix in InnoDB database with constraints

I have MySQL database about 5GB size and about 200 tables in it. All tables have prefix which I'd like to remove and I found some ideas for that.
The problem is that this database has referential integrity checking by using CONSTRAINT...FOREIGN KEY.
How to remove prefix from tables, including change in constraints, without manual modification or removing constraints?
Unfortunately you have to drop and recreate the foreign keys according to the mysql documentation on rename table:
Foreign keys that point to the renamed table are not automatically
updated. In such cases, you must drop and re-create the foreign keys
in order for them to function properly.
Use the tables in information_schema (mostly TABLES and COLUMNS) to construct the code you need. Perhaps 3 scripts would be wise:
SELECT CONCAT('ALTER TABLE ', table_name, ' DROP FOREIGN KEY ' ... ) FROM ...
SELECT CONCAT('RENAME TABLE ', ...
SELECT CONCAT('ALTER TABLE ', table_name, ' ADD FOREIGN KEY ' ... ) FROM ...
Then manually copy and paste the 3 scripts into the mysql commandline tool.
As for removing the prefix, look at the string functions MID, LOCATE, SUBSTRING_INDEX, etc., to see what would be useful.

mysql: alter tablename only if table exists

I tried writing query using exists, but no success so far. Searching hasn't helped so far.
If you attempt to alter a table that does not exist, the query will fail with an error: Table 'database.table' doesn't exist
MySQL does support ALTER IGNORE TABLE, but that only turns errors into warnings if you're attempting to create a unique index while there are values in the table that violate that index.
If you would like to make sure that you do not produce any database queries, I would suggest ensuring the table's existence using SHOW TABLES LIKE 'tablename' before running your ALTER TABLE query.

mysql full text search error

I try to add full text search to an existing table. When I tried:
alter table tweets add fulltext index(tags);
I got the error:
ERROR 1214 (HY000): The used table type doesn't support FULLTEXT indexes
what is the problem? How can I know what table type it is?
If you want to use full text indexing you need to make sure your table's underlying engine is MyISAM. You can change this using ALTER TABLE tweets ENGINE = MYISAM;
This is how you check the table type:
SELECT table_schema,engine FROM information_schema.tables WHERE table_name='tweet';
Only MyISAM supports FULLTEXT Indexes.
You may also want to preempt the stopword list.
Click Here for the Stop Words that FullText Indexing Would Normally Ignore.
You can override this as Follows:
1) Create a text file in /var/lib/mysql like this
echo "a" > /var/lib/mysql/stopwords.txt<BR>
echo "an" >> /var/lib/mysql/stopwords.txt<BR>
echo "the" >> /var/lib/mysql/stopwords.txt<BR>
2) Add this to /etc/my.cnf
ft_stopword_file=/var/lib/mysql/stopwords.txt<BR>
ft_min_word_len=2
3) service mysql restart
Here is something else to consider:
You may not want to convert the table 'tweets' to MyISAM.
1) If the InnoDB table 'tweets' contains CONSTRAINT(s).
2) If the InnoDB table 'tweets' is the parent of other InnoDB tables with Foreign Key Constraints back to 'tweets'.
3) You cannot afford to have table-level locking of the 'tweets' table.
Remember, each INSERT into the 'tweets' table will trigger a table-level lock if it were a MyISAM table. Since it currently an InnoDB table (which does row-level locking), the 'tweets' table can be INSERTed into very quickly.
You many want to create a separate MyISAM table, called tweets_tags, with the same Primary Key of the 'tweets' table along with a TEXT column called 'tags' the same as in the 'tweets' table.
Next, do an initial load of tweets_tags like this:
INSERT INTO tweets_tags (id,tags) SELECT id,tags FROM tweets;
Then, periodically (every night or every 6 hours), load new tweets into tweets_tags like this :
INSERT INTO tweets_tags (id,tags) SELECT id,tags FROM tweets WHERE id > (SELECT max(id) FROM tweets_tags);