mysql: check if table(s) AND column(s) exist in ONE query? - mysql

Working with multiple tables, the task is to check if the table exists and its certain columns exist, too.
The query
SELECT
(TABLE_NAME) AS table_name,
(GROUP_CONCAT(COLUMN_NAME)) AS column_name
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'dbms'
AND
TABLE_NAME IN ('user', 'status')
AND
COLUMN_NAME IN ('id', 'name')
GROUP BY table_name
works fine to detect existing columns, but returns nothing if either table does not exist or none of the columns exist.
The latter should be differentiated.
How to?
How to know whether it's a table or column names that do not exist?
To clarify, the query above will return 2 rows by 2 columns:
user | id, name
status | id, name
means, each table has both of the columns.
Changing one column name to, say, name1, will result in:
user | id
status | id
means, only id exists.
Changing column names to name1, id1 will return nothing.
This is the problem. It is impossible to say whether tables do not exist or columns.
It would be good if the return would look something like:
user | ''
status | ''
and if, say status does not exist, then like:
user | ''
what can be decoded as only user table exists, and none of the columns named in it.

You cannot use WHERE for column_name is you still want rows in the result set even if they don't match. It looks like the only real WHERE clause you want is based on the table name, and the column name is simply a display condition.
SELECT
table_name
GROUP_CONCAT(IF(column_name IN ('id','name'), column_name, NULL))
FROM information_schema.columns
WHERE table_schema = 'dbms'
AND table_name IN ('user','status')
GROUP BY table_name

you just need to change your logic in and condition as mentioned in your query TABLE_SCHEMA = 'dbms'
AND
TABLE_NAME IN ('user', 'status')
AND
COLUMN_NAME IN ('id', 'name')
it will fetch only the resultset if there are tables users or status with column name id or name in dbms,
what you can do is separately check for tables and if table exist check for columns. I hope it would help

Related

mysql want column names which are not null and not blank for given id

Mysql i am trying to get column names for given table which values are not null and not blank for the given id values
I want to pass particular columns names and it needs to check which column has value not null and not blank for the given id record. it should return column names only. i have tried below query but it gives me error
select column_name nonnull_column
from information_schema.columns
where table_schema = 'dbname'
and table_name = 'tablename'
and is_nullable = 'NO'
and tablename.id = "1";
error is Unknown column 'tablename.id' in 'where clause'
see following is my column for example
colA
colB
colC
colD
colE
colF
above are particular columns i want to check that which are not null and not blank for particular given record id need to return as column names.
for ex. if colA,colC,colD is not null for id = 5 then it should return result as
Columns
colA
colC
colD
can you help me to achieve my expected result please. Thanks
You are calling tablename.id but there is no reference to tablename in your WHERE clause anywhere.
Also, you are calling columns from information_schema.columns table where tablename.id apparently does not exist.

How to auto-increment table-name during create-table, if table exists with same name

I'm trying to auto-increment table-name while creating a new table.
Below one, is the generic way to do it.
CREATE TABLE table_name (column_name column_type);
How can we add an auto-increment to table name like: table_name1, table_name2, etc?
There is no "auto incrementing" functionality with table names, you should handle it yourself. You can, for example, count the tables in your database with specific names and in specific schema:
SELECT count(*) FROM information_schema.tables WHERE table_schema = 'YOUR_SCHEMA' AND table_name LIKE '%YOUR_TABLE_NAME%';
Increase the number you got in result and create a new table.
Thanks to #errata, I have solved this problem. If I have tables with names: table_name1, table_name2, table_name_100, table_name_120.
I wanted to add an increment to a new table. So in the first part, I fetched the max count of the alpha-numeric table name.
SELECT MAX(CAST(SUBSTR(TRIM(table_name),12) AS UNSIGNED)) FROM information_schema.tables WHERE table_schema = '${userSchema}' AND table_name LIKE '%table_name%';
It returns, for example, 120 in the above case.
Use this and increment with 1 to new table_name.

How can I have a column called table_name in one table which I can use to refer to other tables in mysql DB?

I have a web application that has multiple tables. These tables and the values in their columns are stores in mysql tables with each mysql table representing one table in the application. Let's say the following is what I in mysql database have:
table1 with column1_1, column1_2, column1_3
table2 with column2_1, column2_2
table3 with column3_1, column3_2, column3_3, column3_4
I have a new requirement wherein I should be able to allow a user to make comments on any of the columns of any of the tables. For this I am creating a mysql table named 'comments' with following fields:
id
table_name (name of table on which comment is being made)
column_name (name of the column on which comment is being made)
Currently, table_name and column_name are varchar. I am wondering if there is a way to restrict table_name column to taking values of the currently existing table names viz. table1, table2 and table3 and also be able to refer to those tables using the entry in table_name. Similarly, for column_name, I want to be able to refer to column present in the other tables. Is there a way to do this?
On MySQL, you can get the names of the tables in a schema by using the following query:
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';
You can also retrieve the names of the columns in a table with the query:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'your_database_name' AND table_name = 'your_table';
According to MySQL's documentation, CHECK constraints cannot contain user-defined functions or subqueries (among other things).
Instead of using a CHECK constraint, what you can do is write a trigger that checks whether or not the inserted table_name is valid, and, if so,
check if the column_name belongs to that table.

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