Monday, January 18, 2010

MySql desc [table_name] with limited columns

MySQL has become the world's most popular open source database because of its consistent fast performance, high reliability and ease of use.Not only is MySQL the world's most popular open source database, it's also become the database of choice for a new generation of applications built on the LAMP stack (Linux, Apache, MySQL, PHP / Perl / Python.) MySQL runs on more than 20 platforms including Linux, Windows, OS/X, HP-UX, AIX, Netware, giving you the kind of flexibility that puts you in control.

MySQL DESC/DESCRIBE command:

DESCRIBE provides information about the columns in a table. It is a shortcut for SHOW COLUMNS FROM.

col_name can be a column name, or a string containing the SQL “%” and “_” wildcard characters to obtain output only for the columns with names matching the string. There is no need to enclose the string within quotes unless it contains spaces or other special characters.

mysql> DESCRIBE City;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| Id         | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name       | char(35) | NO   |     |         |                |
| Country    | char(3)  | NO   | UNI |         |                |
| District   | char(20) | YES  | MUL |         |                |
| Population | int(11)  | NO   |     | 0       |                |
+------------+----------+------+-----+---------+----------------+

MySQL desc [table_name] with limited columns as per user need:

There are situation where in people want some of the fields from desc command.
Example: - One of our DBA searching for table details with field and their data 
type other key information are not important at this time. How to get it?
Don't worry MySQL provides information_schema which can be use for such problem.

Consider the city table
mysql> DESCRIBE City;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| Id         | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name       | char(35) | NO   |     |         |                |
| Country    | char(3)  | NO   | UNI |         |                |
| District   | char(20) | YES  | MUL |         |                |
| Population | int(11)  | NO   |     | 0       |                |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
Now to solve the above described problem, we can get that from information_schema by submitting simple command to MySQL server.
example:-
select column_name,column_type from information_schema.columns where table_name ='City';
+------------+----------+
| Field      | Type     |
+------------+----------+
| Id         | int(11)  |
| Name       | char(35) |
| Country    | char(3)  |
| District   | char(20) |
| Population | int(11)  |
+------------+----------+
Now you can explore information_schema to suit your needs.For any queries and doubts please post your comments.

No comments:

Post a Comment