MySQL: get all references to another scheme - mysql

I have: Two schemes scheme1 and scheme2.
I want To delete something from scheme1.
Problem: scheme2 have dependencies on scheme1 so I can't delete what I want.
Question: Is there a way to print all dependencies in scheme2 to scheme1? How to do that?
Question2: It would be perfect if you know how to get ALL dependencies on some_table from all schemes. Do you know?

Answer for second question, if it's tables that are dependent on "some_table":
select TABLE_NAME
from information_schema.REFERENTIAL_CONSTRAINTS
where REFERENCED_TABLE_NAME = <some_table>
of the other way around:
select REFERENCED_TABLE_NAME
from information_schema.REFERENTIAL_CONSTRAINTS
where TABLE_NAME = <some_table>

If you by dependency mean foreign key you can check that in:
select * from information_schema.REFERENTIAL_CONSTRAINTS;

Your first question already had a good answer. If you are in MySQL 5.0 you can use information_schema.table_constraints else if you are in 5.1 or higher you can use information_schema.referential_constraints.
See this thread here from dbo.stackexchange.com
https://dba.stackexchange.com/questions/5441/how-to-find-dependencies-on-a-table-in-mysql-5-0
By your second query if you mean find dependencies of a table, I think you can just use
SHOW CREATE TABLE
which will list out all foreign key constraint defined for a particular table.

Related

Table missing... or not (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

how to select all columns without PRIMARY key in mysql?

I going to list all columns of a table except PRIMARY key in mysql.
Note : operation is automatically so I don't know the names of fields.
Can you help me for making this query?
This is something worth doing some research on, if you are going to be working with databases in any length.
All DBMS I have worked with thus far have a means of looking at the constraints, columns, and table information. The ones for MySQL that will help you do what you want are likely in the INFORMATION_SCHEMA:
TABLE_CONSTRAINTS The MySQL Reference for this is here.
SELECT table_name, constraint_name, constraint_type FROM INFORMATION_SCHEMA.table_constraints;
COLUMNS The MySQL reference for this is here.
SELECT column_name FROM INFORMATION_SCHEMA.columns;
KEY_COLUMN_USAGE
You should be able to do something like this to get what you want:
SELECT INFORMATION_SCHEMA.key_column_usage.column_name
FROM INFORMATION_SCHEMA.key_column_usage
JOIN INFORMATION_SCHEMA.table_constraints
ON INFORMATION_SCHEMA.key_column_usage.column_name = INFORMATION_SCHEMA.table_constraints.column_name
WHERE INFORMATION_SCHEMA.table_constraints.constraint_type <> 'PRIMARY KEY'
The should be essentially what you need. Views/tables like these can be your best friend when needing to get information about your schema.
I hope that this information helps.

What is the MySQL command to check if a field of table A is already referenced to a field of table B as the foreign key?

As the question says, what is the MySQL command to check if a field of table A is already referenced to a field of table B as the foreign key? Are there any commands to let the user know it?
Are you looking for something like this:-
USE information_schema;
SELECT *
FROM
KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_NAME = 'A'
AND REFERENCED_COLUMN_NAME = 'A_id';
You can use the systems table for this purpose, however from your previous question I think this might be the wrong way to achieve whatever you are trying to achieve.
http://dev.mysql.com/doc/refman/5.6/en/innodb-sys-foreign-table.html

Constraint detail from information_schema (on update cascade, on delete restrict)

Almost all the information I had needed about a database, I could find in information_schema
This time I needed to read details of all foreign keys in a database through single query I found every thing in information_schema.key_Column_usage but could not find the constraints like on delete, on update
I could do show create table for all individual tables. But is there any way to get these details through some select query like this?
SELECT CONSTRAINT_NAME, TABLE_NAME,COLUMN_NAME, REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME FROM information_schema.`KEY_COLUMN_USAGE` WHERE
table_schema = 'mydbname' AND referenced_column_name IS NOT NULL
It is doing the job well but just missing constraints like on delete, on update How can I get those values as well so that I can get all info about foreign keys in a single query?
UPDATE_RULE and DELETE_RULE is the thing you asked for
it's a little bit too late but it could help someone else, here the solution :
SELECT tb1.CONSTRAINT_NAME, tb1.TABLE_NAME, tb1.COLUMN_NAME,
tb1.REFERENCED_TABLE_NAME, tb1.REFERENCED_COLUMN_NAME, tb2.MATCH_OPTION,
tb2.UPDATE_RULE, tb2.DELETE_RULE
FROM information_schema.`KEY_COLUMN_USAGE` AS tb1
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS tb2 ON
tb1.CONSTRAINT_NAME = tb2.CONSTRAINT_NAME
WHERE table_schema = 'sfa' AND referenced_column_name IS NOT NULL
Update From information_schema.REFERENTIAL_CONSTRAINTS table add in mysql 5.1 mysql-5.1 we can get information about all constraints**. Accepted answer gives the solution as query.
Earlier
Before mysql 5.1 like mysql-5.0, we could not get this information, we could only use show create table for individual table.
If you are looking for (primary|foreign|unique) keys :
http://dev.mysql.com/doc/refman/5.5/en/table-constraints-table.html
Now, you can find foreign key constraint details in table INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
http://dev.mysql.com/doc/refman/5.5/en/referential-constraints-table.html

How to find out if a certain value exists as a primary key in mySql?

What is the best way to find out if a primary key with a certain value already exists in a table?
I can think of:
SELECT key FROM table WHERE key = 'value';
and count the results, or:
SELECT SQL_CALC_FOUND_ROWS key FROM table WHERE key = 'value' LIMIT 1;
SELECT FOUND_ROWS();
I think either of your suggestions in the question are suitable.
Depending on how you are using this though, you can potentially save time by doing an INSERT IGNORE, which allows you to insert a new row if the primary key doesn't exist. If it does exist, the error is ignored so you can continue as normal.
Other similar options depending on your usage include using the REPLACE or the INSERT ON DUPLICATE KEY UPDATE types of inserts. This allows you to update the existing entry if the primary key already exists, otherwise it just inserts your new entry.
Do the first and count the results (always 0 or 1). Easy and fast.
I'd do:
SELECT 1 FROM table WHERE id key = 'value'
Anything else is likely to interfere with query optimisation a little, so I'd stick with that.
Edit: Although I just realised I don't think I've ever done that in MySQL, although I can't see why it wouldn't work.
Example
Select count(key) into :result from table where key = :theValue
If your trying to decide between an insert or update, use a MERGE statement in Oracle. I believe MS-SQL is something like an UPSERT statement.
SELECT 1 FROM mytable WHERE mykey = 'value' LIMIT 1
I think it would be more intuitive and simplier to use IF EXISTS.
IF EXISTS (SELECT key FROM table WHERE key = 'value')
PRINT 'Found it!'
ELSE
PRINT 'Cannot find it!'