Mysql sort table description by field name

Categorías: 

Etiquetas: 

Display columns name from a table sort by Field name from shell

  • Step 1: create bash script
  • Step 2: use example

Step 1: create bash script

File name: desc.sh

#!/bin/bash

db_user="database_user"
db_pass="database_pass"
db_name="database_name"
db_host="database_host"
 
# table name from user input
db_table=$1
 
mysql -u${db_user} -p${db_pass} ${db_name} -h ${db_host} -e "SELECT c.COLUMN_NAME as 'Field', c.COLUMN_TYPE as 'Type', c.IS_NULLABLE as 'Null', c.COLUMN_KEY as 'Key', c.COLUMN_DEFAULT as 'Default',  c.EXTRA AS 'Extra', c.COLLATION_NAME as 'Collation', c.COLUMN_COMMENT as 'Comment', t.ENGINE as 'Engine', t.TABLE_ROWS as 'Rows' FROM INFORMATION_SCHEMA.COLUMNS as c, INFORMATION_SCHEMA.TABLES as t WHERE c.TABLE_NAME = '${db_table}' AND c.TABLE_SCHEMA = '${db_name}' AND t.TABLE_NAME= '${db_table}' AND t.TABLE_SCHEMA = '${db_name}' ORDER BY Field ASC"

Step 2: use example

  • Give execution permission
chmod +x desc.sh
  • Try it
./desc.sh table_name