DESCRIBE in a FROM subquery - sql

Is it possible to have a DESCRIBE table clause inserted as a subquery in FROM of a SELECT clause in MySQL?
Further, is there a way to enforce a WHERE like condition on a DESCRIBE output?
EDIT: Basically, I have a table with a large number of columns and I want to pull out and act upon the particulars of only one column.

you can use INFORMATION_SCHEMA instead as following:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'Database Name'
AND TABLE_NAME = 'Table Name' and any condition you want...;

Related

How to get the columns count in a given table in SQL?

I want to get the column count in the given table in SQL. I'm using the DBeaver to do the queries.
Is that connected to a postgres database? Try this script see if it gives you the column count you are a looking for. Remove the aggregate function and it'll give you a lot more system related info on that table
SELECT COUNT(Column_Name) AS ColumnCount
FROM information_schema.columns
WHERE table_schema = 'name of schema'
AND table_name = 'table name';

How to select a column from the output of 'SHOW COLUMNS' sql

I am using the sql command SHOW COLUMNS in this way
SHOW COLUMNS FROM TABLE A;
It outputs multiple columns, specifically one called 'COLUMN_NAME' that I would like to select.
I tried doing
SELECT COLUMN_NAME FROM (SHOW COLUMNS FROM TABLE A);
which gives an error, is there another way I can just show one of the columns of the output? Is this because the output is not a table so I cannot query from it?
You can use the like function:
show columns like 'COLUMN_NAME' in table A;
There are limitations on using the output of the show command, so an alternate approach is to use the INFORMATION_SCHEMA.
select * from INFORMATION_SCHEMA.COLUMNS where
TABLE_SCHEMA = 'MY_SCHEMA'
and TABLE_NAME = 'A'
and COLUMN_NAME = 'COLUMN_NAME';
If you need to query the results of a show command, you can use the result_scan table function:
show columns like '%' in table t1;
select * from table(result_scan(last_query_id())) where "column_name" = 'COLUMN_NAME';
Remember to double quote the column names produced by the show command. It's a metadata result and the column names are lower case.

In Oracle, how do you search a tables' column-names?

I have a large table with over 50 columns. And I would like to search those columns for a particular word. How do I do this in Oracle ?
thanks
You can use the following:
select COLUMN_NAME from ALL_TAB_COLUMNS
where TABLE_NAME='your_table_name' and COLUMN_NAME like '%whatever_word_required%';
Explanation: ALL_TAB_COLUMNS describes the columns of the tables, views, and clusters accessible to the current user. So from all the tables, since you want to search in a particular table so give your table-name in the query and specify the criteria for columns using the LIKE.
Say if your table name is "Table1" and column-name you are trying to search is "Employee", so the query becomes:
select COLUMN_NAME from ALL_TAB_COLUMNS
where TABLE_NAME='Table1' and COLUMN_NAME like '%Employee%';
For reference see the Oracle Docs

Query to get columns from system.catalog table and do a select query

I am trying to build a query to fetch columns from the system.CATALOG table and to continue querying based on the resultset. I looked at a few queries but seem to be unable to find anything that satisfies my requirements. I don't have much to show, that I have tried as I don't know, how to approach this.
I am using Apache Phoenix DB. (Any SQL is also OK, as I am interested in learning.)
I have now written the query below, which will fetch me all the column names, that start with A in schema test for table element.
SELECT
COLUMN_NAME
FROM SYSTEM.CATALOG
WHERE TABLE_SCHEM = 'TEST'
AND TABLE_NAME = 'ELEMENT'
AND COLUMN_NAME LIKE 'A%'";
Now I want to use the list of columns names in an UPSERT query from the resultset of above query, to update these columns in the element table's records. So I am stuck here.
try this, it works perfect.
SELECT column_name
FROM system.catalog
WHERE table_name = 'your_table' AND key_seq IS NOT NULL
Example: To get the salt buckets on the table :
select table_name, salt_buckets
from SYSTEM.CATALOG
where salt_buckets is not null and table_name='TABLE_NAME';

What is Mysql select statement to show a list of tables?

I want to write a program that can show the user a list of tables in the database and also show the descriptions of those tables. So can I do a "select * from system_table" or something like that?
This will give you a list of tables:
show tables;
To describe each table:
describe table_name;
To get both at the same time try:
SELECT * FROM DOMAIN.TABLES WHERE TYPE = 'TABLE'
SELECT * FROM DOMAIN.COLUMNS WHERE TABLETYPE = 'TABLE'
The results are similar to MySql show and describe statements
In addition to show tables, MySQL 5.0+ also supports the INFORMATION_SCHEMA meta-database:
SELECT table_name, table_comment FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';
information_schema.tables also has other information in it, if you're curious.
Note that if you didn't provide a comment when you created the table and use InnoDB, it will fill the table_comment column with unnecessary data, such as the InnoDB space reserved for this table or the foreign key constraints.