SQL Query To Get Column Names in MySQL

When you install a new MySQL Database Server, some schemas are created by default. These databases or schemas are used to store meta data about other databases and the database server users. The meta data describes:
  1. Which database users can connect with database server
  2. Which database users can access, update or delete which schemas
  3. Which user can connect as an admin
  4. Who has the read access to databases
  5. What is the structure of different databases created on the database server, etc. 
Even you can configure table and row level security for different users.

So to query list of columns of a particular database's table, you need to query information_schema database which already exist in MySQL, the query is following:

SELECT column_name  FROM information_schema.columns WHERE table_schema = 'university_db' AND table_name  = 'students';

Here UNIVERSITY_DB is the database name and STUDENTS is the table whose columns we want to fetch. 

Make sure you are connected with a user, who has privilege to execute the meta data related queries.

Comments