Mysql show create constraint? - mysql

Is there a easy way to query a table for its constraints(foreignkeys specificaly)
like show create table, but for the constraints only?
thanks,
pvgoddijn

To show only the foreign key constraints you can check the constraint_type in information_schema.table_constraints and get the affected columns in information_schema.key_column_usage via a join
SELECT b.table_name, b.column_name, b.constraint_name,
b.referenced_table_name, b.referenced_column_name
FROM information_schema.table_constraints a
JOIN information_schema.key_column_usage b
ON a.table_schema = b.table_schema AND a.constraint_name = b.constraint_name
WHERE a.table_schema=database() AND a.constraint_type='FOREIGN KEY'
ORDER BY b.table_name, b.constraint_name;

select * from
information_schema.KEY_COLUMN_USAGE
where table_schema = <db_name>
and table_name = <table_name>;

SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';
The foreign key constraints are listed in the Comment column of the output.

MySQL 5.1 Manual
SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';

Related

How do I check if table exists before alter table

I need query to update all tables and set ENGINE = INNODB To many databases. But some of the databases don't have all the same tables; some databases have more tables than others.
So the problem is that
ALTER TABLE `ads` ENGINE = INNODB;
ALTER TABLE `modules` ENGINE = INNODB;
ALTER TABLE `ad_extras` ENGINE = INNODB;
Throws an error when the table modules doe snot exist. I see that I cannot make a direct IF statement' I tried:
IF EXISTS (SHOW TABLES LIKE 'modules') BEGIN
ALTER TABLE `modules` ENGINE = INNODB;
END IF
But it throws
Unrecognized statement type (near IF EXISTS)
Any ideas?
If this is just an ad-hoc task,
select concat('ALTER ', TABLE_NAME, " ENGINE = INNODB;")
from information_schema.TABLES
where TABLE_SCHEMA = '<your schema>';
Execute the output again.
If Exists works only in a stored procedure.
a fixed edition of Jacob's answer:
SET SESSION group_concat_max_len = 1000000;
select group_concat(concat('ALTER Table ', TABLE_NAME, ' ENGINE = INNODB;') SEPARATOR '\n')
from information_schema.TABLES
where TABLE_SCHEMA = 'your_schema_name' and TABLE_TYPE<>'VIEW';
This will give you a script to copy and run separately.
What about testing it simple as this query :
DROP TABLE IF EXISTS `yourtablename`;

MySQL: If after select(count(*) for Dropping column if exists

I already read multiples post about drop a column if exists on the forum as
if exists
(select * from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename' AND COLUMN_NAME='columname' )
ALTER TABLE table_name DROP COLUMN column_name
But, unfortunately, none give a results on MySQL 5.7. It's because i try this:
if (select count(*) from information_schema.columns
where table_name = 'XXXX'
and column_name = 'XXXX')=1
then
ALTER TABLE `XXXX`.`XXXX` DROP COLUMN `XXXX`;
end if;
Please can someone explain me how to solve this issues?
Thanks in advance!

View the constraints enforced on a table- SQL

Since I am not much of a DB guy, I have a query.I use Mysql.
I am given a table which has around 12 columns and that table has a PRIMARY key,UNIQUE & a FOREIGN key defined.
Is there a way to find on which columns the constraints are defined?
I came across one query:
SHOW INDEX FROM tablt_name;
But It does not give a clear idea, only the primary key column is displayed by the above query.
If there is any other way to get the info, pls help
You can try like thisL
USE information_schema;
SELECT table_name,
column_name,
constraint_name,
referenced_table_name,
referenced_column_name
FROM key_column_usage
WHERE table_schema = ""
AND table_name = ""
AND referenced_column_name IS NOT NULL;
or
DESCRIBE table_name

Delete MySQL column index without knowing its name

I have an application which uses Hibernate to support Oracle and MySQL databases. After an update I have to manually delete some columns with indexes/constraints on it. These indexes have Hibernate generated random names.
In Oracle I can do this:
ALTER TABLE table_name DROP (column_name) CASCADE CONSTRAINTS;
Unfortunately this isn't possible for MySQL. Is there a possibility to do something like this
DROP INDEX (SELECT Key_name FROM (SHOW INDEX FROM table_name WHERE Column_name = 'column_name')) ON table_name;
before I drop the column?
EDIT: This should work without user interaction in a SQL script.
You can select indexes for a table form information_schema:
SELECT DISTINCT INDEX_NAME, TABLE_NAME, TABLE_SCHEMA FROM information_schema.STATISTICS;
There is no need to manually delete the indexes, MySQL 5.7 Reference Manual says:
If columns are dropped from a table, the columns are also removed from
any index of which they are a part. If all columns that make up an
index are dropped, the index is dropped as well. If you use CHANGE or
MODIFY to shorten a column for which an index exists on the column,
and the resulting column length is less than the index length, MySQL
shortens the index automatically.
To get all indexes for a particular database (replace <Database_Name> with your database name), use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>';
To get all indexes for a table (replace <Table_Name> with your table name) of a particular database, use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE '<Table_Name>';
To get all indexes of a specific column (replace <Column_Name> with your column name) of a table in a particular database, use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE '<Table_Name>' AND
COLUMN_NAME LIKE '<Column_Name>';
In addition to that, you may also use any Wildcard character in the LIKE operator to get specific records, like:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE 'tbl_prefix_%';

Query to find foreign keys

I have a database where I need to drop some foreign keys, but I don't know beforehand whether the foreign keys still exist.
I've found some stored procedures (http://forums.mysql.com/read.php?97,218825,247526) that does the trick, but I don't want to create a stored procedure for this.
I've tried to use the query inside the stored procedure, but I get an error using "IF EXISTS (SELECT NULL FROM etc.. etc...
Can I only use IF EXISTS in stored procedures?
right now, the only thing I can run is
SELECT * FROM information_schema.TABLE_CONSTRAINTS
WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';
and I've tried this too
IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = parm_key_name) THEN
(...) do something (...)
END IF;
but I get a You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF' at line 1
I've looked for examples in forums with simple queries and I can't make sense of why this isn't working.
NOTE: Edit to correct broken link
You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table
SELECT * FROM information_schema.TABLE_CONSTRAINTS T;
you need to be a ROOT user to access the information_schema.
USING this table you can find the table, db and whether it has foreign key.
Hope this helps if you dont wanna use IF EXIST and Stored Procedure. But I am Sure you can use IF EXIST can be used for non stored procedure queries....
Why don't You use the table "INFORMATION_SCHEMA" to this?
SELECT *
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table
select
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
information_schema.key_column_usage
where
referenced_table_name is not null;
HELP: see this link list-foreign-keys-in-mysql