0

I have SQL for example

show tables from mydb;

It shows the list of table

|table1|
|table2|
|table3|

Then,I use sql sentence for each table. such as "show full columns from table1 ;"

+----------+--------+-----------+------+-----+---------+----------------+---------------------------------+---------+
| Field    | Type   | Collation | Null | Key | Default | Extra          | Privileges                      | Comment |
+----------+--------+-----------+------+-----+---------+----------------+---------------------------------+---------+
| id       | bigint | NULL      | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| user_id  | bigint | NULL      | NO   | MUL | NULL    |                | select,insert,update,references |         |
| group_id | int    | NULL      | NO   | MUL | NULL    |                | select,insert,update,references |         |
+----------+--------+-----------+------+-----+---------+----------------+---------------------------------+---------+

So in this case I can use programming language such as .(this is not correct code just showing the flow)

tables = "show tables from mydb;"
for t in tables:
    cmd.execute("show full columns from {t} ;")

However is it possible to do this in sql only?

1
  • 2
    Please specify the DB server - MS SQL Server, MySQL, PostgreSQL, ... Probably the INFORMATION_SCHEMA.COLUMNS approach, suggested by @gotqn will work in most cases. Commented Jan 30, 2023 at 6:42

1 Answer 1

1

If you are using MySQL you can use the system view - INFORMATION_SCHEMA.

It contains table name and column name (and other details). No loop is require and you can easily filter by other information, too.

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS

If you are using Microsoft SQL Server, you can use the above command

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.