MYSQL - Determine tables primary key in order - mysql

I am looking for a query which gives me the primary key of all tables from a database, at the same order that it was created, for example:
CREATE TABLE a_antennaport
...
PRIMARY KEY (dateday,neid,cn,srn,sn,pn)
I need as output the fields in the same order:
dateday,neid,cn,srn,sn,pn
I already tried:
SELECT GROUP_CONCAT(COLUMN_NAME), TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = schema()
AND CONSTRAINT_NAME='PRIMARY'
GROUP BY TABLE_NAME;

Your query should work already (in fact, it does. Tested, to be sure). Only thing you can improve to make sure, that the columns are in the right order, is to order by the ordinal position in the group_concat().
SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION), TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = schema()
AND CONSTRAINT_NAME='PRIMARY'
GROUP BY TABLE_NAME;

Related

How to see column constraints in h2-database [duplicate]

I'm trying to find out how to get the following constraint information from a table in MySQL 5.0:
primary key
foreign keys and table references
unique columns
What is the syntax of the query or queries to do so? I have a feeling I'm close with this, but there is no example.
For MySQL:
1) get Table/Fields metadata
SELECT table_schema, table_name, column_name, ordinal_position, data_type,
numeric_precision, column_type, column_default, is_nullable, column_comment
FROM information_schema.columns
WHERE (table_schema='schema_name' and table_name = 'table_name')
order by ordinal_position;
OR
show fields from 'table_name'
2) get Foregn Keys referenced table
SELECT `REFERENCED_TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = 'table_name' AND
`COLUMN_NAME` = 'Column_Name'
3) get indexes (primary and foreign) for a table
show keys from `table_name`
5) get All indexes and referreced table
SELECT *
FROM `KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = 'table_name' AND
`TABLE_SCHEMA` = 'schema_name'
OR
SELECT *
FROM `REFERENTIAL_CONSTRAINTS`
WHERE
`TABLE_NAME` = 'table_name' AND
`CONSTRAINT_SCHEMA` = 'schema_name'
6) get STORED PROCEDURES
SELECT *
FROM `ROUTINES`
WHERE
`ROUTINE_SCHEMA` = 'schema_name'
7) get TRIGGERS
SELECT *
FROM `TRIGGERS`
WHERE
`TRIGGER_SCHEMA` = 'schema_name'
8) get EVENTS
SELECT *
FROM `EVENTS`
WHERE
`EVENT_SCHEMA` = 'schema_name'
9) get VIEWS
SELECT *
FROM `VIEWS`
WHERE
`TABLE_NAME` = 'table_name' AND
`TABLE_SCHEMA` = 'schema_name'
The SHOW COLUMNS command will show you the primary key and unique columns for a table.
As for foreign keys, you could use something like the SHOW CREATE TABLE command which will output the DDL statements needed to replicate the table.
Use
show fields from table_name
show keys from table_name
to get primary keys, foreign keys, unique, etc.
to get the table referenced by a foreign key use:
SELECT `REFERENCED_TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = '[table_containing_foreign_key]' AND
`COLUMN_NAME` = '[foreign_key]'
substituting [table_containing_foreign_key] and [foreign_key] with your values
use the following to get the same using Select Query:
SELECT table_schema, table_name, column_name, ordinal_position, data_type, numeric_precision, column_type FROM information_schema.columns WHERE table_name = '[TABLE_NAME]';
You should try it and see. INFORMATION_SCHEMA is part of some standard and is supported in a (mostly) similar way in other databases; this standard should be documented - you can look for that doc.
But mainly the way would be to create a bunch of test tables, and then have a look at INFORMATION_SCHEMA to see what's there.

Cannot Drop unique constraint [duplicate]

I created them, but I forgot which ones they are.
I just want to
show them.
remove all the constraints on a table.
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where table_name = 'table_name' and constraint_type = 'UNIQUE';
This doesn't produce elegant output but is easy to remember:
SHOW CREATE TABLE table_name;
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where CONSTRAINT_SCHEMA = 'mysql'
This query returns primay keys, unique keys and foreign ones :
show indexes from table_name;
The OP asked for a single table, which this will do.
In addition, removing the last where clause will show all columns for a database which are protected by unique constraints:
SELECT
CONSTRAINT_NAME,
TABLE_NAME,
COLUMN_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE
CONSTRAINT_NAME LIKE 'UNIQ%'
AND TABLE_SCHEMA = 'your_database_name'
AND TABLE_NAME = 'your_table_name';
Unfortunately mysql doesn't facilitate the removal of indexes based on a query result. You could execute the output of the following query to drop all unique columns in 2 queries:
SELECT CONCAT(
'ALTER TABLE ',
TABLE_NAME,
' DROP INDEX ',
CONSTRAINT_NAME,
'; -- drops ',
COLUMN_NAME,
' constraint'
)
FROM information_schema.KEY_COLUMN_USAGE
WHERE
CONSTRAINT_NAME LIKE 'UNIQ%'
AND TABLE_SCHEMA = 'your_database_name';

Desc primary key of all tables in MYSQL

I want to get the result of Desc for only specific columns of each table in the information schema, let's say Primary Key.
Can this be done?
I tried things like
SELECT * FROM (DESC TABLENAME) WHERE ....;
but it did not work.
Also I want this to work for all tables.
DESC aka DESCRIBE aka SHOW COLUMNS will not work in a subquery.
But SELECT will.
You can for example replace
SHOW COLUMNS FROM t IN test LIKE '%2';
with
SELECT column_name AS `Field`, column_type AS `Type`,
is_nullable AS `Null`,
column_key as 'Key',
column_default AS `Default`, extra AS `Extra`
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 't'
AND column_name like '%2';
and you'll get the same thing.

How to see indexes for a database or table in MySQL?

How do I see if my database has any indexes on it?
How about for a specific table?
To see the index for a specific table use SHOW INDEX:
SHOW INDEX FROM yourtable;
To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA:
SELECT DISTINCT
TABLE_NAME,
INDEX_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'your_schema';
Removing the where clause will show you all indexes in all schemas.
If you want to see all indexes across all databases all at once:
use information_schema;
SELECT * FROM statistics;
SHOW INDEX FROM mytable FROM mydb;
SHOW INDEX FROM mydb.mytable;
See documentation.
You could use this query to get the no of indexes as well as the index names of each table in specified database.
SELECT TABLE_NAME,
COUNT(1) index_count,
GROUP_CONCAT(DISTINCT(index_name) SEPARATOR ',\n ') indexes
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'mydb'
AND INDEX_NAME != 'primary'
GROUP BY TABLE_NAME
ORDER BY COUNT(1) DESC;
to see indexes you have created use
SHOW INDEX from your_table_name;
to see all indexes on a table ( created by DB and you)
SHOW EXTENDED INDEX from your_table_name;
To get all indexed columns per index in one column in the sequence order.
SELECT table_name AS `Table`,
index_name AS `Index`,
GROUP_CONCAT(column_name ORDER BY seq_in_index) AS `Columns`
FROM information_schema.statistics
WHERE table_schema = 'sakila'
GROUP BY 1,2;
Ref: http://blog.9minutesnooze.com/mysql-information-schema-indexes/
Why not show create table myTable ?
Someone told me this but I didn't see anyone mention here, anything bad?
It's neat if you just want to take a glance at the indexes along with column infomations.
I propose this query:
SELECT DISTINCT s.*
FROM INFORMATION_SCHEMA.STATISTICS s
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t
ON t.TABLE_SCHEMA = s.TABLE_SCHEMA
AND t.TABLE_NAME = s.TABLE_NAME
AND s.INDEX_NAME = t.CONSTRAINT_NAME
WHERE 0 = 0
AND t.CONSTRAINT_NAME IS NULL
AND s.TABLE_SCHEMA = 'YOUR_SCHEMA_SAMPLE';
You found all Index only index.
Regard.
This works in my case for getting table name and column name in the corresponding table for indexed fields.
SELECT TABLE_NAME , COLUMN_NAME, COMMENT
FROM information_schema.statistics
WHERE table_schema = 'database_name';
To check all disabled indexes on db
SELECT INDEX_SCHEMA, COLUMN_NAME, COMMENT
FROM information_schema.statistics
WHERE table_schema = 'mydb'
AND COMMENT = 'disabled'
You can check your indexes in MySQL workbench.under the performance reports tabs you can see all used indexes and unused indexes on the system. or you can fire the query.
select * from sys.schema_index_statistics;
To query the index information of a table, you use the SHOW INDEXES statement as follows:
SHOW INDEXES FROM table_name;
You can specify the database name if you are not connected to any database or you want to get the index information of a table in a different database:
SHOW INDEXES FROM table_name
IN database_name;
The following query is similar to the one above:
SHOW INDEXES FROM database_name.table_name;
Note that INDEX and KEYS are the synonyms of the INDEXES, IN is the synonym of the FROM, therefore, you can use these synonyms in the SHOW INDEXES column instead. For example:
SHOW INDEX IN table_name
FROM database_name;
Or
SHOW KEYS FROM tablename
IN databasename;
we can directly see the indexes on to the table if we know the index name with below :
select * from all_indexes where index_name= 'your index'
select
table_name,
index_name,
seq_in_index,
column_name,
non_unique,
index_type,
comment
from
information_schema.statistics
where 1=1
and table_schema = 'my_schema'
and table_name = 'my_table'
order by 1,2,3,4,5,6

Getting table metadata in MySQL

I'm trying to find out how to get the following constraint information from a table in MySQL 5.0:
primary key
foreign keys and table references
unique columns
What is the syntax of the query or queries to do so? I have a feeling I'm close with this, but there is no example.
For MySQL:
1) get Table/Fields metadata
SELECT table_schema, table_name, column_name, ordinal_position, data_type,
numeric_precision, column_type, column_default, is_nullable, column_comment
FROM information_schema.columns
WHERE (table_schema='schema_name' and table_name = 'table_name')
order by ordinal_position;
OR
show fields from 'table_name'
2) get Foregn Keys referenced table
SELECT `REFERENCED_TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = 'table_name' AND
`COLUMN_NAME` = 'Column_Name'
3) get indexes (primary and foreign) for a table
show keys from `table_name`
5) get All indexes and referreced table
SELECT *
FROM `KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = 'table_name' AND
`TABLE_SCHEMA` = 'schema_name'
OR
SELECT *
FROM `REFERENTIAL_CONSTRAINTS`
WHERE
`TABLE_NAME` = 'table_name' AND
`CONSTRAINT_SCHEMA` = 'schema_name'
6) get STORED PROCEDURES
SELECT *
FROM `ROUTINES`
WHERE
`ROUTINE_SCHEMA` = 'schema_name'
7) get TRIGGERS
SELECT *
FROM `TRIGGERS`
WHERE
`TRIGGER_SCHEMA` = 'schema_name'
8) get EVENTS
SELECT *
FROM `EVENTS`
WHERE
`EVENT_SCHEMA` = 'schema_name'
9) get VIEWS
SELECT *
FROM `VIEWS`
WHERE
`TABLE_NAME` = 'table_name' AND
`TABLE_SCHEMA` = 'schema_name'
The SHOW COLUMNS command will show you the primary key and unique columns for a table.
As for foreign keys, you could use something like the SHOW CREATE TABLE command which will output the DDL statements needed to replicate the table.
Use
show fields from table_name
show keys from table_name
to get primary keys, foreign keys, unique, etc.
to get the table referenced by a foreign key use:
SELECT `REFERENCED_TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = '[table_containing_foreign_key]' AND
`COLUMN_NAME` = '[foreign_key]'
substituting [table_containing_foreign_key] and [foreign_key] with your values
use the following to get the same using Select Query:
SELECT table_schema, table_name, column_name, ordinal_position, data_type, numeric_precision, column_type FROM information_schema.columns WHERE table_name = '[TABLE_NAME]';
You should try it and see. INFORMATION_SCHEMA is part of some standard and is supported in a (mostly) similar way in other databases; this standard should be documented - you can look for that doc.
But mainly the way would be to create a bunch of test tables, and then have a look at INFORMATION_SCHEMA to see what's there.