Show tables status result - mysql

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

Related

how to get only name of columns in mysql table?

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 = ?;

To pull count of a specific column of 10k tables in MySQL

SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND TABLE_SCHEMA = 'DatabaseName'
I tried this to pull count from a single table.
How to achieve this with multiple tables which is more than 10k
Remove your filter condition of Table name AS below
SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DatabaseName'

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

How to find specific column names in MySQL table and clear the results?

I am trying to execute a PHP/MySQL query to do the following for a single record:
Find all column names that end in "_abc" in the users table
Clear these fields for a single record (i.e. the user_id)
Leave the data in the remaining columns alone
I have never done anything with database information schema before, so any help would be amazing!
Something like...?
$sth = $dbh->prepare("DESCRIBE users
WHERE column_name LIKE '_abc'
AND id = $user_id");
$sth->execute();
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'db_name'
AND table_name = 'users'
AND column_name like '%_abc';
Then use the returned column names in an update statement
update users
set col1_abc = null, col2_abc = NULL
where id = $user_id

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);