This is an old revision of the document!


Database

These are all for MySQL


Cleaner Terminal Output

MySQL can have quite messy output when running queries from the mysql cli. You can terminate a statement with \G instead of ; to format as a list instead of table.

SELECT * FROM users\G

Alternatively, you can set the pager to less which retains the table format and can be scrolled with arrowkeys.

pager less -SFX
SELECT * FROM users;
nopager; -- to turn off

Grant permissions

replace *.* with schema.table if needed

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';

Basic syntax

update

UPDATE employees SET email = 'user@domain.com' WHERE id = 123;

Many to Many Relationship Joins

SELECT 
  a.*
FROM 
  map_ads_categories m
  JOIN ads a ON (m.ad_id = a.id)
WHERE
  m.category_id = 28;
SELECT 
  *
FROM 
  map_ads_categories m
  JOIN ads a ON (m.ad_id = a.id)
  JOIN categories c ON (m.category_id = c.id)
WHERE
  c.id = 28;