mysql: Update formatting

This commit is contained in:
Rico Sta. Cruz 2018-12-25 20:25:48 +08:00 committed by GitHub
parent 0e64b13d77
commit 28fac2ae2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 248 additions and 145 deletions

137
mysql.md
View File

@ -1,31 +1,52 @@
--- ---
title: MySql title: MySQL
updated: 2018-12-25
layout: 2017/sheet
category: Databases category: Databases
--- ---
### Create / Delete Database ### Create / Delete Database
```sql
CREATE DATABASE dbNameYouWant CREATE DATABASE dbNameYouWant
CREATE DATABASE dbNameYouWant CHARACTER SET utf8 CREATE DATABASE dbNameYouWant CHARACTER SET utf8
DROP DATABASE dbNameYouWant DROP DATABASE dbNameYouWant
ALTER DATABASE dbNameYouWant CHARACTER SET utf8 ALTER DATABASE dbNameYouWant CHARACTER SET utf8
```
### Backup Database to SQL File ### Backup Database to SQL File
```bash
mysqldump -u Username -p dbNameYouWant > databasename_backup.sql mysqldump -u Username -p dbNameYouWant > databasename_backup.sql
```
### Restore from backup SQL File ### Restore from backup SQL File
```bash
mysql - u Username -p dbNameYouWant < databasename_backup.sql mysql - u Username -p dbNameYouWant < databasename_backup.sql
```
### Repair Tables After Unclean Shutdown ### Repair Tables After Unclean Shutdown
```bash
mysqlcheck --all-databases mysqlcheck --all-databases
mysqlcheck --all-databases --fast mysqlcheck --all-databases --fast
```
### Browsing ### Browsing
```sql
SHOW DATABASES SHOW DATABASES
SHOW TABLES SHOW TABLES
SHOW FIELDS FROM table / DESCRIBE table SHOW FIELDS FROM table / DESCRIBE table
SHOW CREATE TABLE table SHOW CREATE TABLE table
SHOW PROCESSLIST SHOW PROCESSLIST
KILL process_number KILL process_number
```
### Select ### Select
```sql
SELECT * FROM table SELECT * FROM table
SELECT * FROM table1, table2, ... SELECT * FROM table1, table2, ...
SELECT field1, field2, ... FROM table1, table2, ... SELECT field1, field2, ... FROM table1, table2, ...
@ -37,13 +58,19 @@ category: Databases
SELECT ... FROM ... WHERE condition LIMIT 10 SELECT ... FROM ... WHERE condition LIMIT 10
SELECT DISTINCT field1 FROM ... SELECT DISTINCT field1 FROM ...
SELECT DISTINCT field1, field2 FROM ... SELECT DISTINCT field1, field2 FROM ...
```
### Select - Join ### Select - Join
```sql
SELECT ... FROM t1 JOIN t2 ON t1.id1 = t2.id2 WHERE condition SELECT ... FROM t1 JOIN t2 ON t1.id1 = t2.id2 WHERE condition
SELECT ... FROM t1 LEFT JOIN t2 ON t1.id1 = t2.id2 WHERE condition SELECT ... FROM t1 LEFT JOIN t2 ON t1.id1 = t2.id2 WHERE condition
SELECT ... FROM t1 JOIN (t2 JOIN t3 ON ...) ON ... SELECT ... FROM t1 JOIN (t2 JOIN t3 ON ...) ON ...
```
### Conditions ### Conditions
```sql
field1 = value1 field1 = value1
field1 <> value1 field1 <> value1
field1 LIKE 'value _ %' field1 LIKE 'value _ %'
@ -53,48 +80,73 @@ category: Databases
field1 IS NOT IN (value1, value2) field1 IS NOT IN (value1, value2)
condition1 AND condition2 condition1 AND condition2
condition1 OR condition2 condition1 OR condition2
```
### Insert ### Insert
```sql
INSERT INTO table1 (field1, field2, ...) VALUES (value1, value2, ...) INSERT INTO table1 (field1, field2, ...) VALUES (value1, value2, ...)
```
### Delete ### Delete
```sql
DELETE FROM table1 / TRUNCATE table1 DELETE FROM table1 / TRUNCATE table1
DELETE FROM table1 WHERE condition DELETE FROM table1 WHERE condition
DELETE FROM table1, table2 FROM table1, table2 WHERE table1.id1 = DELETE FROM table1, table2 FROM table1, table2 WHERE table1.id1 =
table2.id2 AND condition table2.id2 AND condition
```
### Update ### Update
```sql
UPDATE table1 SET field1=new_value1 WHERE condition UPDATE table1 SET field1=new_value1 WHERE condition
UPDATE table1, table2 SET field1=new_value1, field2=new_value2, ... WHERE UPDATE table1, table2 SET field1=new_value1, field2=new_value2, ... WHERE
table1.id1 = table2.id2 AND condition table1.id1 = table2.id2 AND condition
```
### Create / Delete / Modify Table ### Create / Delete / Modify Table
*Create* #### Create
```sql
CREATE TABLE table (field1 type1, field2 type2, ...) CREATE TABLE table (field1 type1, field2 type2, ...)
CREATE TABLE table (field1 type1, field2 type2, ..., INDEX (field)) CREATE TABLE table (field1 type1, field2 type2, ..., INDEX (field))
CREATE TABLE table (field1 type1, field2 type2, ..., PRIMARY KEY (field1)) CREATE TABLE table (field1 type1, field2 type2, ..., PRIMARY KEY (field1))
CREATE TABLE table (field1 type1, field2 type2, ..., PRIMARY KEY (field1, CREATE TABLE table (field1 type1, field2 type2, ..., PRIMARY KEY (field1,
field2)) field2))
```
```sql
CREATE TABLE table1 (fk_field1 type1, field2 type2, ..., CREATE TABLE table1 (fk_field1 type1, field2 type2, ...,
FOREIGN KEY (fk_field1) REFERENCES table2 (t2_fieldA)) FOREIGN KEY (fk_field1) REFERENCES table2 (t2_fieldA))
[ON UPDATE|ON DELETE] [CASCADE|SET NULL] [ON UPDATE|ON DELETE] [CASCADE|SET NULL]
```
```sql
CREATE TABLE table1 (fk_field1 type1, fk_field2 type2, ..., CREATE TABLE table1 (fk_field1 type1, fk_field2 type2, ...,
FOREIGN KEY (fk_field1, fk_field2) REFERENCES table2 (t2_fieldA, t2_fieldB)) FOREIGN KEY (fk_field1, fk_field2) REFERENCES table2 (t2_fieldA, t2_fieldB))
```
```sql
CREATE TABLE table IF NOT EXISTS (...) CREATE TABLE table IF NOT EXISTS (...)
```
```sql
CREATE TEMPORARY TABLE table (...) CREATE TEMPORARY TABLE table (...)
```
*Drop* #### Drop
```sql
DROP TABLE table DROP TABLE table
DROP TABLE IF EXISTS table DROP TABLE IF EXISTS table
DROP TABLE table1, table2, ... DROP TABLE table1, table2, ...
```
*Alter* #### Alter
```sql
ALTER TABLE table MODIFY field1 type1 ALTER TABLE table MODIFY field1 type1
ALTER TABLE table MODIFY field1 type1 NOT NULL ... ALTER TABLE table MODIFY field1 type1 NOT NULL ...
ALTER TABLE table CHANGE old_name_field1 new_name_field1 type1 ALTER TABLE table CHANGE old_name_field1 new_name_field1 type1
@ -106,58 +158,109 @@ category: Databases
ALTER TABLE table ADD new_name_field1 type1 AFTER another_field ALTER TABLE table ADD new_name_field1 type1 AFTER another_field
ALTER TABLE table DROP field1 ALTER TABLE table DROP field1
ALTER TABLE table ADD INDEX (field); ALTER TABLE table ADD INDEX (field);
```
*Change field order* #### Change field order
```sql
ALTER TABLE table MODIFY field1 type1 FIRST ALTER TABLE table MODIFY field1 type1 FIRST
ALTER TABLE table MODIFY field1 type1 AFTER another_field ALTER TABLE table MODIFY field1 type1 AFTER another_field
ALTER TABLE table CHANGE old_name_field1 new_name_field1 type1 FIRST ALTER TABLE table CHANGE old_name_field1 new_name_field1 type1 FIRST
ALTER TABLE table CHANGE old_name_field1 new_name_field1 type1 AFTER ALTER TABLE table CHANGE old_name_field1 new_name_field1 type1 AFTER
another_field another_field
```
### Keys ### Keys
```sql
CREATE TABLE table (..., PRIMARY KEY (field1, field2)) CREATE TABLE table (..., PRIMARY KEY (field1, field2))
CREATE TABLE table (..., FOREIGN KEY (field1, field2) REFERENCES table2 CREATE TABLE table (..., FOREIGN KEY (field1, field2) REFERENCES table2
(t2_field1, t2_field2)) (t2_field1, t2_field2))
```
### Users and Privileges ### Users and Privileges
```sql
GRANT ALL PRIVILEGES ON base.* TO 'user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON base.* TO 'user'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, DELETE ON base.* TO 'user'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT, DELETE ON base.* TO 'user'@'localhost' IDENTIFIED BY 'password';
REVOKE ALL PRIVILEGES ON base.* FROM 'user'@'host'; -- one permission only REVOKE ALL PRIVILEGES ON base.* FROM 'user'@'host'; -- one permission only
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user'@'host'; -- all permissions REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user'@'host'; -- all permissions
```
```sql
SET PASSWORD = PASSWORD('new_pass') SET PASSWORD = PASSWORD('new_pass')
SET PASSWORD FOR 'user'@'host' = PASSWORD('new_pass') SET PASSWORD FOR 'user'@'host' = PASSWORD('new_pass')
SET PASSWORD = OLD_PASSWORD('new_pass') SET PASSWORD = OLD_PASSWORD('new_pass')
```
```sql
DROP USER 'user'@'host' DROP USER 'user'@'host'
host % indicates any host. ```
Host % indicates any host.
### Main Data Types ### Main Data Types
TINYINT (1o: -217+128) SMALLINT (2o: +-65 000)
MEDIUMINT (3o: +-16 000 000) INT (4o: +- 2 000 000 000) ```sql
TINYINT (1o: -217+128)
SMALLINT (2o: +-65 000)
MEDIUMINT (3o: +-16 000 000)
INT (4o: +- 2 000 000 000)
BIGINT (8o: +-9.10^18) BIGINT (8o: +-9.10^18)
```
```sql
Precise interval: -(2^(8*N-1)) -> (2^8*N)-1 Precise interval: -(2^(8*N-1)) -> (2^8*N)-1
/!\ INT(2) = "2 digits displayed" -- NOT "number with 2 digits max" ```
FLOAT(M,D) DOUBLE(M,D) FLOAT(D=0->53) ⚠ INT(2) = "2 digits displayed" -- NOT "number with 2 digits max"
/!\ 8,3 -> 12345,678 -- NOT 12345678,123!
TIME (HH:MM) YEAR (AAAA) DATE (AAAA-MM-JJ) DATETIME (AAAA-MM-JJ HH:MM; années 1000->9999) ```sql
FLOAT(M,D)
DOUBLE(M,D)
FLOAT(D=0->53)
```
⚠ 8,3 -> 12345,678 -- NOT 12345678,123!
```sql
TIME (HH:MM)
YEAR (AAAA)
DATE (AAAA-MM-JJ)
DATETIME (AAAA-MM-JJ HH:MM; années 1000->9999)
TIMESTAMP (like DATETIME, but 1970->2038, compatible with Unix) TIMESTAMP (like DATETIME, but 1970->2038, compatible with Unix)
```
VARCHAR (single-line; explicit size) TEXT (multi-lines; max size=65535) BLOB (binary; max size=65535) ```sql
Variants for TEXT&BLOB: TINY (max=255) MEDIUM (max=~16000) LONG (max=4Go) VARCHAR (single-line; explicit size)
Ex: VARCHAR(32), TINYTEXT, LONGBLOB, MEDIUMTEXT TEXT (multi-lines; max size=65535)
BLOB (binary; max size=65535)
```
Variants for TEXT&BLOB: `TINY` (max=255), `MEDIUM` (max=~16000), and `LONG` (max=4Go). Ex: `VARCHAR(32)`, `TINYTEXT`, `LONGBLOB`, `MEDIUMTEXT`
```sql
ENUM ('value1', 'value2', ...) -- (default NULL, or '' if NOT NULL) ENUM ('value1', 'value2', ...) -- (default NULL, or '' if NOT NULL)
```
### Reset Root Password ### Reset Root Password
```bash
$ /etc/init.d/mysql stop $ /etc/init.d/mysql stop
```
```bash
$ mysqld_safe --skip-grant-tables $ mysqld_safe --skip-grant-tables
```
```bash
$ mysql # on another terminal $ mysql # on another terminal
mysql> UPDATE mysql.user SET password=PASSWORD('new_pass') WHERE user='root'; mysql> UPDATE mysql.user SET password=PASSWORD('new_pass') WHERE user='root';
```
```bash
## Switch back to the mysqld_safe terminal and kill the process using Control + \ ## Switch back to the mysqld_safe terminal and kill the process using Control + \
$ /etc/init.d/mysql start</code> $ /etc/init.d/mysql start
```
Your commands may vary depending on your OS.