|  | 
|  | 1 | +# MySQL | 
|  | 2 | + | 
|  | 3 | +## Getting started | 
|  | 4 | +- http://www.sqlteaching.com/ | 
|  | 5 | +- https://www.codecademy.com/learn/learn-sql | 
|  | 6 | + | 
|  | 7 | +### Related tutorials | 
|  | 8 | +- [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P) | 
|  | 9 | +- [Analyzing Business Metrics](https://www.codecademy.com/learn/sql-analyzing-business-metrics) | 
|  | 10 | +- [SQL joins infografic](https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins) | 
|  | 11 | + | 
|  | 12 | +## Tools | 
|  | 13 | +- [TablePlus](https://tableplus.io/) | 
|  | 14 | +- [DataGrip](https://www.jetbrains.com/datagrip/) | 
|  | 15 | +- [Sequel Pro](http://www.sequelpro.com/) (abandoned) | 
|  | 16 | + | 
|  | 17 | +## Commands | 
|  | 18 | +Access monitor: `mysql -u [username] -p;` (will prompt for password) | 
|  | 19 | + | 
|  | 20 | +Show all databases: `show databases;` | 
|  | 21 | + | 
|  | 22 | +Access database: `mysql -u [username] -p [database]` (will prompt for password) | 
|  | 23 | + | 
|  | 24 | +Create new database: `create database [database];` | 
|  | 25 | + | 
|  | 26 | +Select database: `use [database];` | 
|  | 27 | + | 
|  | 28 | +Determine what database is in use: `select database();` | 
|  | 29 | + | 
|  | 30 | +Show all tables: `show tables;` | 
|  | 31 | + | 
|  | 32 | +Show table structure: `describe [table];` | 
|  | 33 | + | 
|  | 34 | +List all indexes on a table: `show index from [table];` | 
|  | 35 | + | 
|  | 36 | +Create new table with columns: `CREATE TABLE [table] ([column] VARCHAR(120), [another-column] DATETIME);` | 
|  | 37 | + | 
|  | 38 | +Adding a column: `ALTER TABLE [table] ADD COLUMN [column] VARCHAR(120);` | 
|  | 39 | + | 
|  | 40 | +Adding a column with an unique, auto-incrementing ID: `ALTER TABLE [table] ADD COLUMN [column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;` | 
|  | 41 | + | 
|  | 42 | +Inserting a record: `INSERT INTO [table] ([column], [column]) VALUES ('[value]', [value]');` | 
|  | 43 | + | 
|  | 44 | +MySQL function for datetime input: `NOW()` | 
|  | 45 | + | 
|  | 46 | +Selecting records: `SELECT * FROM [table];` | 
|  | 47 | + | 
|  | 48 | +Explain records: `EXPLAIN SELECT * FROM [table];` | 
|  | 49 | + | 
|  | 50 | +Selecting parts of records: `SELECT [column], [another-column] FROM [table];` | 
|  | 51 | + | 
|  | 52 | +Counting records: `SELECT COUNT([column]) FROM [table];` | 
|  | 53 | + | 
|  | 54 | +Counting and selecting grouped records: `SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];` | 
|  | 55 | + | 
|  | 56 | +Selecting specific records: `SELECT * FROM [table] WHERE [column] = [value];` (Selectors: `<`, `>`, `!=`; combine multiple selectors with `AND`, `OR`) | 
|  | 57 | + | 
|  | 58 | +Select records containing `[value]`: `SELECT * FROM [table] WHERE [column] LIKE '%[value]%';` | 
|  | 59 | + | 
|  | 60 | +Select records starting with `[value]`: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';` | 
|  | 61 | + | 
|  | 62 | +Select records starting with `val` and ending with `ue`: `SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';` | 
|  | 63 | + | 
|  | 64 | +Select a range: `SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];` | 
|  | 65 | + | 
|  | 66 | +Select with custom order and only limit: `SELECT * FROM [table] WHERE [column] ORDER BY [column] ASC LIMIT [value];` (Order: `DESC`, `ASC`) | 
|  | 67 | + | 
|  | 68 | +Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];` | 
|  | 69 | + | 
|  | 70 | +Deleting records: `DELETE FROM [table] WHERE [column] = [value];` | 
|  | 71 | + | 
|  | 72 | +Delete *all records* from a table (without dropping the table itself): `DELETE FROM [table];` | 
|  | 73 | +(This also resets the incrementing counter for auto generated columns like an id column.) | 
|  | 74 | + | 
|  | 75 | +Delete all records in a table: `truncate table [table];` | 
|  | 76 | + | 
|  | 77 | +Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];` | 
|  | 78 | + | 
|  | 79 | +Deleting tables: `DROP TABLE [table];` | 
|  | 80 | + | 
|  | 81 | +Deleting databases: `DROP DATABASE [database];` | 
|  | 82 | + | 
|  | 83 | +Custom column output names: `SELECT [column] AS [custom-column] FROM [table];` | 
|  | 84 | + | 
|  | 85 | +Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` | 
|  | 86 | + | 
|  | 87 | +Use `--lock-tables=false` option for locked tables (more info [here](http://stackoverflow.com/a/104628/1815847)). | 
|  | 88 | + | 
|  | 89 | +Import a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysql -u [username] -p -h localhost [database] < db_backup.sql` | 
|  | 90 | + | 
|  | 91 | +Logout: `exit;` | 
|  | 92 | + | 
|  | 93 | +## Aggregate functions | 
|  | 94 | +Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015年01月01日 00:00:00` | 
|  | 95 | + | 
|  | 96 | +Calculate total number of records: `SELECT SUM([column]) FROM [table];` | 
|  | 97 | + | 
|  | 98 | +Count total number of `[column]` and group by `[category-column]`: `SELECT [category-column], SUM([column]) FROM [table] GROUP BY [category-column];` | 
|  | 99 | + | 
|  | 100 | +Get largest value in `[column]`: `SELECT MAX([column]) FROM [table];` | 
|  | 101 | + | 
|  | 102 | +Get smallest value: `SELECT MIN([column]) FROM [table];` | 
|  | 103 | + | 
|  | 104 | +Get average value: `SELECT AVG([column]) FROM [table];` | 
|  | 105 | + | 
|  | 106 | +Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];` | 
|  | 107 | + | 
|  | 108 | +## Multiple tables | 
|  | 109 | +Select from multiple tables: `SELECT [table1].[column], [table1].[another-column], [table2].[column] FROM [table1], [table2];` | 
|  | 110 | + | 
|  | 111 | +Combine rows from different tables: `SELECT * FROM [table1] INNER JOIN [table2] ON [table1].[column] = [table2].[column];` | 
|  | 112 | + | 
|  | 113 | +Combine rows from different tables but do not require the join condition: `SELECT * FROM [table1] LEFT OUTER JOIN [table2] ON [table1].[column] = [table2].[column];` (The left table is the first table that appears in the statement.) | 
|  | 114 | + | 
|  | 115 | +Rename column or table using an _alias_: `SELECT [table1].[column] AS '[value]', [table2].[column] AS '[value]' FROM [table1], [table2];` | 
|  | 116 | + | 
|  | 117 | +## Users functions | 
|  | 118 | +List all users: `SELECT User,Host FROM mysql.user;` | 
|  | 119 | + | 
|  | 120 | +Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';` | 
|  | 121 | + | 
|  | 122 | +Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';` | 
|  | 123 | + | 
|  | 124 | +## Find out the IP Address of the Mysql Host | 
|  | 125 | +`SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646)) | 
0 commit comments