JBay Solutions Development Blog on Java, Android, Play2 and others
RSS RSS RSS RSS

SQL queries to get table and columns names for MySQL, PosgreSQL and SQL Server

Something that now and then are always needed, the SQL queries to get the list of table names from a database, or the column names for a table.
Below are the queries for MySQL, PostgreSQL and Microsoft SQL server.

MySQL

To get the table list:

show tables;

To get the column list for a table:

show columns from <tableName>;



PostgreSQL

To get the table list:

SELECT table_name FROM information_schema.tables WHERE table_catalog ='<dbName>' and table_schema = 'public';

To get the column list for a table:

SELECT column_name FROM information_schema.columns where table_catalog = '<dbName>' and table_name = '<tableName>';



SQL Server

To get the table list:

SELECT TABLE_NAME FROM <dbName>.INFORMATION_SCHEMA.Tables;

To get the column list for a table:

SELECT COLUMN_NAME FROM <dbName>.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<tableName>';





comments powered by Disqus