views:

80

answers:

3
  • Given a column name how can I find which tables in database contain that column ?

    or alternatively

  • How can I find that particular column exists for all tables in Database ?

Note: Kindly explain answers with Examples as that I get most knowledge from the answer.

Edit: I am using MySQL Database.

A: 

Depends on the database you are using. Many database systems expose a set of tables of views that contain details of the schema. For example, you can get schema information from the SYSTABLE and SYSCOLUMN views in Sybase ASA.

mdm
A: 

in SQL Server:

select distinct t.name
from sys.Columns c 
    inner join sys.tables t on c.object_id = t.object_id
where c.name = 'YOUR_COLUMNNAME'
Manu
Database am using is MySQL Database.
Rachel
+4  A: 
SELECT * FROM information_schema.columns WHERE COLUMN_NAME = 'mycolumn'
nickf
@nickf - sorry should have given you a chance to edit that typo in the grace period. +1 anyway.
martin clayton
MySQL stores the data dictionary for the databases in the information_schema database. In here you can see columns, tables, etc. for everything installed in your MySQL database. There may be permission issues around this (I'm not sure). It's also something you shouldn't update manually. Older versions (maybe back to 4.0?) didn't have this, but it's been around for a while now, so most newer MySQL installations will have it.
Milner
... AND TABLE_SCHEMA=DATABASE()
Alex
@martin - thanks :).
nickf