How do I detect if a table exist? MySql - mysql

I was suggested to do this
SELECT table_schema, table_name FROM information_schema.tables
WHERE table_schema = 'mydb' AND table_name='ApprovePost';
However it is not reliable and cause me errors on several versions of mysql on windows and linux.
Maybe there is another way. Does anyone know?
This issue is I can do create table if not exists but I do a second pass to add the FK constraint. In my SQL dump I see > 130 contains on a single table. The table only has 6 columns, only two of these need constrains. The constrains keep building and building every time I restart the Apache server or whenever mono feels the need to call my global init method in my webapp.

Looks like you need to use the FLUSH TABLES command for the INFORMATION_SCHEMA.TABLES to reflect existing tables.
Reference:
TABLE CACHE

If your only actual problem now is recreating the foreign key constantly (aside from a possibly broken MySQL install considering your other troubles), why not:
1) give it a constraint symbol (should be unique in database) and let the adding fail silently / catch 'em?
ALTER TABLE tbl ADD CONSTRAINT only_one_please FOREIGN KEY (columnname) ...
2) or better yet, add the foreign key clause in your create table in the first place?
As for the original question: I know no reason why this should happen, and I cannot recreate it. Selecting from information_schema is afaik quite the preferred way of checking this, and hasn't failed me yet. Aside from a brute force check like SELECT * FROM tablename LIMIT 0; and checking for errors, you first might want to check for any other caching mechanisms besides MySQL's query cache, and if they're not there / not the problem, perhaps try a SELECT SQL_NO_CACHE table_schema, table_name FROM information_schema.tables.

Related

How Can I Drop ALL Constraints From a Database?

The constraints from the database design i'm using are limiting my updates and I want to get rid of all of them. Is there a way to DROP ALL CONSTRAINTS from ALL tables at once? I tried the query below without success as I do not know what`s information_schema... I'm not using any schema per se... Everything is created in phpmyAdmin...
SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE="FOREIGN KEY"

MySql Finding a tree structure using Reference Keys

We have a parent table which stores the user details. Since now we were doing soft delete. However due to some legal commitments, we are forced to do a hard delete for the user details.
So the problem is the main table is referenced many places.
We could able to find all the referenced tables with the following query in MySQL
USE information_schema;
SELECT TABLE_NAME, Column_Name,Constraint_Name
FROM
KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_NAME = 'ProjectUser'
AND REFERENCED_COLUMN_NAME = 'userid'
AND TABLE_SCHEMA = 'testproduct';
It was a success as we got all tables around 45. However the real problem is, it is possible that the child table of "ProjectUser" also may referenced somewhere else.
For example,
one of the child table UserAddress is used as foriegn Key for some other table. How can I query to bring all the tables, which reference ProjectUser, and their child tables and grand child tables?
There is no query that's going to get you out of this mess. You can write a program to successively run queries to build this structure, but you will not be able to do it all in one query.
1) Use mysqldump, write something to parse the dump and build the tree
2) Use a tool to visualize the schema such as (schemaspy)[http://schemaspy.sourceforge.net/] or (mysql workbench)[http://www.mysql.com/products/workbench/]
3) Take #AlmaDo's suggestion seriously and add ON DELETE CASCADE. You can drop an existing foreign key and readd it using the later table function. If you have problems re adding the foreign key due to constraint violation, disable keys while you re add the foreign key.

How to disable the non-unique indexes globally in MySQL?

I am using a lots of insert on a MySQL database. However, the table that we are inserting into has many indexed. Many of them are not unique but some of them are unique.
So I need to disable only the indexes that are not unique. If that possible to do? I want to set that globally and not locally to a session. The server is test so I know it will slow down all select statements.
If that is not possible then How can I disable all the indexes globally??
Thanks
As far as I know you can disable all indexes, but not individual ones. You may need to remove the indexes you don't want, then add them later.
Another approach is to create an alternate table, hack around with it, and then swap it for the master:
CREATE TABLE stunt_table LIKE master_table
INSERT INTO stunt_table SELECT * FROM master_table
-- (...Any modifications or manipulations...)
RENAME TABLE master_table TO old_master_table, stunt_table TO master_table

MySQL: How to find where a specific primary key is used as a foreign key in other tables?

I'm working on implementing a function to prevent removal of certain elements in a database (through the front end) if they have other items associated with them in other tables. Otherwise those other tables are looking for keys that aren't there.
If you understood that my hat is off to you.
I have many sets of tables to look through and need either a SQL query or a MySQL Workbench feature that can tell me, on entry of the primary key (column name, not actual value), if that key is used as a foreign key somewhere else.
Otherwise if anyone knows an offhand workaround, that would be great too!
SELECT
table_name, column_name
FROM
information_schema.key_column_usage
WHERE
referenced_table_name = '<table>'
and referenced_column_name = '<primary key column>'
A solution is described in this post to retrieve this information from information_Schema table.
1) If you want to work on these tables from your code, then fetch them as a container, for example ArrayList in your code and perform your logic.
2) If you want to work on these tables from your Stored Procedure, then use temporary tables to achive the same work you'd do in your java code through containers.

Determine InnoDB FK Constraints without information_schema

I'm writing some code to inspect a MySQL database structure, and need information about Foreign Key constraints (on InnoDB tables).
There are two ways I know of to do this:
Parse the results of SHOW CREATE TABLE X
Use INFORMATION_SCEMA.REFERENTIAL_CONSTRAINTS
Unfortunately option two requires MySQL 5.1.16 or later, so I can't use it unless/until I can convince our server guy to update, And while I can probably get away with option 1, it feels messy and without writing a full SQL parser I wouldn't feel sure my code would always work with any table.
Is there another way of getting at this information?
Thanks
From the MySQL 5.0 manual online:
You can also display the foreign key constraints for a table like
this:
SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';
The foreign key constraints are listed in the Comment column of the
output.
Poster indicates that this doesn't provide ON UPDATE and ON DELETE information which is an important part of foreign key behavior.
Another option:
Since you control the code involved, is it possible to set up another MySQL instance in the same environment which is version 5.1+? If so, let's call that instance dummy. Run the SHOW CREATE TABLE on the live database. Then, on dummy run a DROP TABLE IF EXIST followed by the output from the SHOW CREATE TABLE query.
Now you can use INFORMATION_SCHEMA on the dummy database to get the information.