Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the table comment from a mysql table?

Tags:

comments

mysql

How can I get just the table comment from a mysql table? I tried the following, but they didn't work for various reasons. I want to figure out how to get just the string 'my comment' (ideally via perl =)

Any help?

-- Abbreviated output for convenience. SHOW TABLE STATUS WHERE Name="foo" +------+--------+---------+------------+------+----------------+---------------+ | Name | Engine | Version | Row_format | Rows | Create_options | Comment       | +------+--------+---------+------------+------+----------------+---------------+ | foo  | MyISAM |      10 | Fixed      |    0 |                | my comment    |  +------+--------+---------+------------+------+----------------+---------------+ 

and

SHOW CREATE TABLE foo; +-------+------------------------------------------------------------------------------+ | Table | Create Table                                                                 | +-------+------------------------------------------------------------------------------+ | fooo  | CREATE TABLE `fooo` (`id` int(11) NOT NULL PRIMARY KEY) COMMENT='my comment' |  +-------+------------------------------------------------------------------------------+ 
like image 920
Sir Robert Avatar asked Oct 15 '10 02:10

Sir Robert


People also ask

How do I view comments in a table?

You can view the comments on a particular table or column by querying the data dictionary views USER_TAB_COMMENTS , DBA_TAB_COMMENTS , or ALL_TAB_COMMENTS or USER_COL_COMMENTS , DBA_COL_COMMENTS , or ALL_COL_COMMENTS . Specify the name of the operator to be commented.

How do I show comments in MySQL?

Syntax Using # symbol In MySQL, a comment started with # symbol must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line.

How do I view the contents of a table in MySQL workbench?

To open, right-click a table in the object browser of the Navigator pane and choose Table Inspector from the context menu. The Table Inspector shows information related to the table.

How can I get column details of a table in MySQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.


2 Answers

Based on the answer by OMG Ponies, but using INFORMATION_SCHEMA.TABLES instead of INFORMATION_SCHEMA.COLUMNS. When looking around on the web, all I could find was info on the columns' comments, but never on the table's. This is how to get a table's comment.

SELECT table_comment      FROM INFORMATION_SCHEMA.TABLES      WHERE table_schema='my_cool_database'          AND table_name='user_skill'; 
+--------------------------+ | table_comment            | +--------------------------+ | my awesome comment       |  +--------------------------+ 
like image 128
Sir Robert Avatar answered Sep 30 '22 03:09

Sir Robert


If you don't want to have both database name and table name in the query, you can use :

SHOW TABLE STATUS WHERE Name='table_name'; 

and then pick up the "Comment" key of the result (you have to use an associative function like mysqli_fetch_assoc() in php).

like image 25
Jerry Avatar answered Sep 30 '22 04:09

Jerry