how to get only name of columns in mysql table? - mysql

i saw this post MySQL query to get column names?
i try to use this code `table name` or DESCRIBE `table name` or SHOW COLUMNS FROM `table name`
but return me also a datatype and more in this mode
id int NO auto_increment
i want only a name id is possible have it ?? thanks
somtime is possible bypass qualitystandard ?? please

use the tables from information_schema to get the meta data of your table:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
For more information see https://docs.oracle.com/cd/E19078-01/mysql/mysql-refman-5.0/information-schema.html#columns-table

If you don't like the default output of the SHOW commands, you can get anything you want from the INFORMATION_SCHEMA tables (which is where the SHOW commands get their data too).
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?;

Related

Get Table Name, Column Name, Data Type & Character Maximum Lenght mySQL

I have a database in MySQL called "Test Database" and I want to create a query to get all database's columns with the following characteristics:
First column - Table name
Second column - Column name
Third column - Type
Forth column - Maximum length
I used the following queries separately to get what i want:
Get Table Name:
Select Table_Name from INFORMATION_SCHEMA.Tables
Get Columns Name:
Select Column_Name from INFORMATION_SCHEMA.Columns
Get Column Type:
Select Data_Type from INFORMATION_SCHEMA.Columns
Get Columns Size:
Select Character_Maximum_Length from INFORMATION_SCHEMA.Columns
But I did not manage to merge this into one query to get the below result:
Any help will be greatly appreciated!
You can put more than one column in a SELECT statement..
Select Table_Name, Column_Name, Data_Type, Character_Maximum_Length
from INFORMATION_SCHEMA.Columns
Use AS if you want to rename the columns you see (SELECT table_name AS "Table Name" .... I'd recommend not putting spaces in them though
If that is the entire table, then you could simplify your query easily as:
SELECT * FROM INFORMATION_SCHEMA.Columns
this will get all columns that you need.
If you want to be more specific you could try this variant:
SELECT Table_Name AS TableName, Column_Name as ColumnName, Data_Type as
DataType, Character_Maximum_Length as Character_Maximum_Length FROM
INFORMATION_SCHEMA.Columns
I hope this will help you!

How to identify a column with name exist in all tables?

i want to query a database which has huge tables for the columns with specific name
ex: fetch all the tables which has column name like 'name'
Thanks.
Try
SELECT table_name -- you might want to add DISTINCT if you use pattern matching with LIKE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND column_name = 'name' -- or column_name LIKE '%name%'
Here is SQLFiddle demo

Can I find out the next auto_increment to be used?

Is it possible to find out what the next auto increment will be for my primary key without executing an INSERT INTO query? Some rows are deleted meaning that it's not as easy as just adding one to a SELECT MAX query on the PK. Many thanks.
If you really want to know next auto_increment value try SHOW TABLE STATUS returns next Auto_increment field, e.g.:
SHOW TABLE STATUS WHERE name = your_table_name;
or
SELECT Auto_increment
FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_name = your_table_name
You can get the value by executing
SHOW TABLE STATUS WHERE Name = nameOfTableHere
and then retrieving the column 'Auto_Increment' from the result
Try the following:
SELECT Auto_increment
FROM information_schema.tables
WHERE table_name= 'tableName'
AND table_schema = DATABASE();
This is also possible:
(SELECT (SELECT your_primary_key FROM Your_Table ORDER BY your_primary_key DESC LIMIT 1)+1);

Show tables status result

If I want to return only the result of one field from the result set of a SHOW TABLE STATUS command, for example "Auto_increment", what would the query look like?
SELECT AUTO_INCREMENT FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'db1' AND TABLE_NAME = 'table1'
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

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