I use this query to select fields in a given table. Is it possible to select only the fieldname and not the whole structure of the table?
SHOW COLUMNS FROM student
You're trying to determine the table structure? You can query MySQL's information_schema database directly for the fieldnames:
select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='student';
The solution mentioned here earlier is not the correct one. Example:
CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE TABLE db1.t ( id_1 INT);
CREATE TABLE db2.t ( id_2 INT);
SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME ='t';
This will display:
+-------------+
| COLUMN_NAME |
+-------------+
| id_1 |
| id_2 |
+-------------+
suggesting that the table t has two column which is obviously not true. This query lists all the columns of the tables called t in all of your databases.
Instead, you should specify which database contains the table t you want to select the column names from:
SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE
TABLE_NAME = 't' AND
TABLE_SCHEMA = 'db1';
select COLUMN_NAME FROM TABLE_NAME
FOR EXAMPLE: ROLLNO is a column_Name of table Student....
select ROLLNO from Student
Related
I want to create a table / view from variable multiple table names that I get from a SELECT query.
It's possible to create a table from multiple known tables like so:
CREATE TABLE new_table AS
SELECT column_1, column_2
FROM clients_1, clients_2, ... clients_n;
To get list of tables I can use something like:
SELECT DISTINCT table_name FROM information_schema.columns WHERE table_name like '%clients_%';
Which returns:
table_name
1 clients_1
2 clients_2
How can I use the table names result as a list in CREATE TABLE FROM clause?
I tried something like this with a WITH:
WITH mytable AS
(SELECT DISTINCT table_name FROM information_schema.columns WHERE table_name like '%clients_%')
CREATE TABLE new_table AS
SELECT column_1, column_2
FROM mytable;
But it's mostly not working.
And even if it does, for example by not using WITH and selecting * columns -
CREATE TABLE new_table AS
SELECT *
FROM (SELECT DISTINCT table_name FROM information_schema.columns WHERE
table_name like '%clients_%');
new_table is just a copy of mytable/nested query.
Ideas?
Thanks!
I ended up going with a VIEW like so:
CREATE OR REPLACE VIEW clients_all AS
SELECT column_1, column_2 FROM client1
UNION ALL
SELECT column_1, column_2 FROM client2
The downside is I'll have to update the view whenever adding new client_n table,
And explicitly specify the name of the tables rather than inferring them from a query,
But I probably had to break it into two queries anyway even if creating a table as originally intended.
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
I was wondering if you could create columns based on the result from a Select statement.
Table 1:
availableColumns
-----------------
column1 -> record 1
column2 -> record 2
column3 -> record 3
So if I did a Select availableColumns From Table1, how could I create a table that has then the following structure, the results must be used to create columns:
column1 | column2 | column3
If I try:
CREATE TABLE test SELECT availableColumns FROM table1
I get the following:
Column
------
Column1
Column2
Column3
So instead of columns, I get my result as rows which I don't want.
Thanks in advance :)
You can have a list of columns for a specific table by selecting them from Information Schema table
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='databasename'
AND `TABLE_NAME`='tablename';
To create the table just use it
CREATE TABLE test SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='databasename' AND `TABLE_NAME`='tablename';
Just change 'databasename' and 'tablename' to fit your actual database and table
If I have a database some_database with multiples tables like:
table_one
table_two
table_three
where
table_one has CPV_one
table_two has CPV_two
table_three has CPV_three
I need to find all tables that have a column like '%CPV%'
This can be done by sql query? I want to avoid check all tables one by one.
You can query information_schema to get a list of each table in your database with a matching column name:
SELECT
TABLE_NAME, COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
EDIT (selecting only the TABLE_NAME column)
As pointed out in a comment, if you want to select only the name of the table (without the list of columns that match), you should also use the DISTINCT keyword - otherwise a duplicate row will be returned for the same table for each column that matches:
SELECT
DISTINCT TABLE_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%CPV%'
AND TABLE_SCHEMA='YourDatabase';
How about querying the MySQL information_schema?
SELECT TABLE_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_NAME LIKE '%CPV%'
Here you have the code to get the table names with the needed condition:
SELECT DISTINCT TABLE_NAME
FROM information_schema.Columns
WHERE COLUMN_NAME LIKE '%CPV%';
For more details on the INFORMATION SCHEMA take a look here
And for COLUMNS table here
How do I create a query that prints all records from all tables using SQL*Plus?
I can retrieve all tables using this:
select object_name from all_objects
where owner='me' and object_type='TABLE'
SELECT column_name
FROM user_tab_cols
WHERE table_name in (SELECT table_name FROM dba_tables)