mysql

MySQL / MariaDB

Formatting output

Truncate long fields for easier display

use SUBSTRING(fieldname,position,length)

gotcha: first character is position "1" NOT "0", as you would expect

example, to view just the first 15 characters of 'myfield':

mysql> SELECT SUBSTRING(myfield,0,15) FROM tablename

To display from character 10 forward:

mysql> SELECT SUBSTRING(myfield,10) FROM tablename

Show epochtime in human readable format

mysql> SELECT from_unixtime(fieldname) FROM tablename

Format output in 1 field per line

just add <space> \G to the end of your query

mysql>  $query  \G;

Use a pager with multi-screen results

You can run your results through an external pager program, rather than trying to scroll back within your terminal window.

To turn it off

Common actions

See warnings when an update/delete/insert reports warnings

NOTE: You must run this IMMEDIATELY after a command that reports "warnings > 1", or you won't be able to see them

Run commands in a flatfile (ex: output of mysqldump)

Run single command and output results (without formatting)

See what processes and queries are running

Creating a user

You must be logged in, as root, to the mysql db and use "-D mysql" from the cmd line (or "use mysql" from within the cli)

Examples:

format:

Empty out a table

Foreign Keys

NOTE: This is for MySQL v4.x

Requirements

1) table type must be InnoDB (requires flag set when mysql is compiled)

2) all fields that are to be foreign keys must be indexed

Adding foreign keys

to update an existing table:

OR if creating new table:

ORDER BY/GROUP BY/HAVING

Example

output:

Last updated