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.
Related
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';
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;
I am getting all the columns of all tables listed down of my data base with following query in mySQL
SELECT * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position
I want to get all columns whether it's a primary key or not? For primary key columns to appear as y and non primary key columns to appear as n in this query. How can I achieve that with this query?
You can use COLUMN_KEY column and CASE WHEN:
SELECT column_name, CASE WHEN COLUMN_KEY = 'PRI' THEN 'y' ELSE 'n' END AS result
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND `table_name` = 'tab' -- table name
ORDER BY `table_name`, ordinal_position
SqlFiddleDemo
How to find all the relations between all MySQL tables? If for example, I want to know the relation of tables in a database of having around 100 tables.
Is there anyway to know this?
The better way, programmatically speaking, is gathering data from INFORMATION_SCHEMA.KEY_COLUMN_USAGE table as follows:
SELECT
`TABLE_SCHEMA`, -- Foreign key schema
`TABLE_NAME`, -- Foreign key table
`COLUMN_NAME`, -- Foreign key column
`REFERENCED_TABLE_SCHEMA`, -- Origin key schema
`REFERENCED_TABLE_NAME`, -- Origin key table
`REFERENCED_COLUMN_NAME` -- Origin key column
FROM
`INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` -- Will fail if user don't have privilege
WHERE
`TABLE_SCHEMA` = SCHEMA() -- Detect current schema in USE
AND `REFERENCED_TABLE_NAME` IS NOT NULL; -- Only tables with foreign keys
There are more columns info like ORDINAL_POSITION that could be useful depending your purpose.
More info: http://dev.mysql.com/doc/refman/5.1/en/key-column-usage-table.html
Try this:
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;
Try
SELECT
`TABLE_NAME`,
`COLUMN_NAME`,
`REFERENCED_TABLE_NAME`,
`REFERENCED_COLUMN_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE `CONSTRAINT_SCHEMA` = 'YOUR_DATABASE_NAME' AND
`REFERENCED_TABLE_SCHEMA` IS NOT NULL AND
`REFERENCED_TABLE_NAME` IS NOT NULL AND
`REFERENCED_COLUMN_NAME` IS NOT NULL
do not forget to replace YOUR_DATABASE_NAME with your database name!
A quick method of visualizing relationships in MySQL is reverse engineering the database with MySQL Workbench.
This can be done using the reverse engineering too, which will result in an entity-relationship diagram much like the following (though you may have to organize it yourself, once it is generated):
SELECT
count(1) totalrelationships ,
c.table_name tablename,
CONCAT(' ',GROUP_CONCAT(c.column_name ORDER BY ordinal_position SEPARATOR ', ')) columnname,
CONCAT(' ',GROUP_CONCAT(c.column_type ORDER BY ordinal_position SEPARATOR ', ')) columntype
FROM
information_schema.columns c RIGHT JOIN
(SELECT column_name , column_type FROM information_schema.columns WHERE
-- column_key in ('PRI','MUL') AND -- uncomment this line if you want to see relations only with indexes
table_schema = DATABASE() AND table_name = 'YourTableName') AS p
USING (column_name,column_type)
WHERE
c.table_schema = DATABASE()
-- AND c.table_name != 'YourTableName'
GROUP BY tablename
-- HAVING (locate(' YourColumnName',columnname) > 0) -- uncomment this line to search for specific column
ORDER BY totalrelationships desc, columnname
;
1) Go into your database:
use DATABASE;
2) Show all the tables:
show tables;
3) Look at each column of the table to gather what it does and what it's made of:
describe TABLENAME;
4) Describe is nice since you can figure out exactly what your table columns do, but if you would like an even closer look at the data itself:
select * from TABLENAME
If you have big tables, then each row usually has an id, in which case I like to do this to just get a few lines of data and not have the terminal overwhelmed:
select * from TABLENAME where id<5 - You can put any condition here you like.
This method give you more information than just doing select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;, and it also provides you with more bite-sized information each time.
EDIT
As the comments suggested, the above WHERE id < 5 was a bad choice as a conditional placeholder. It is not a good idea to limit by ID number, especially since the id is usually not trustworthy to be sequential. Add LIMIT 5 at the end of the query instead.
Based on xudre's answer, you can execute the following to see all the relations of a schema:
SELECT
`TABLE_SCHEMA`, -- Foreign key schema
`TABLE_NAME`, -- Foreign key table
`COLUMN_NAME`, -- Foreign key column
`REFERENCED_TABLE_SCHEMA`, -- Origin key schema
`REFERENCED_TABLE_NAME`, -- Origin key table
`REFERENCED_COLUMN_NAME` -- Origin key column
FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`
WHERE `TABLE_SCHEMA` = 'YourSchema'
AND `REFERENCED_TABLE_NAME` IS NOT NULL -- Only tables with foreign keys
What I want in most cases is to know all FKs that point to a specific table. In this case I run:
SELECT
`TABLE_SCHEMA`, -- Foreign key schema
`TABLE_NAME`, -- Foreign key table
`COLUMN_NAME` -- Foreign key column
FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`
WHERE `TABLE_SCHEMA` = 'YourSchema'
AND `REFERENCED_TABLE_NAME` = 'YourTableName'
One option is : You can do reverse engineering to understand it in diagrammatic way.
When you install MySQL, you will get MySQLWorkbench. You need to open it and choose the database you want to reverse engineer. Click on Reverse Engineer option somewhere you find under the tools or Database menu. It will ask you to choose the tables. Either you select the tables you want to understand or choose the entire DB. It will generate a diagram with relationships.
you can use:
SHOW CREATE TABLE table_name;
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.