This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
fuss:mysql [2016/05/28 18:58] – [Import Multiple SQL Files] office | fuss:mysql [2022/04/19 08:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== List Users ====== | ||
+ | <code sql> | ||
+ | SELECT user FROM mysql.user; | ||
+ | </ | ||
+ | |||
+ | ====== Reset Passwords ====== | ||
+ | |||
+ | Sets the password '' | ||
+ | |||
+ | <code sql> | ||
+ | UPDATE mysql.user SET password=PASSWORD(' | ||
+ | </ | ||
+ | |||
+ | ====== List the Size of Databases ====== | ||
+ | |||
+ | <code sql> | ||
+ | SELECT table_schema " | ||
+ | </ | ||
+ | |||
+ | ====== Select Random Records ====== | ||
+ | |||
+ | The following query, selects one random entry from the table '' | ||
+ | |||
+ | <code sql> | ||
+ | SELECT * FROM table ORDER BY RAND() | ||
+ | </ | ||
+ | |||
+ | ====== Select Using RegExp ====== | ||
+ | |||
+ | This query will select '' | ||
+ | |||
+ | <code sql> | ||
+ | SELECT lastname, firstname FROM employees WHERE lastname REGEXP | ||
+ | </ | ||
+ | |||
+ | ====== Comma-separated Values ====== | ||
+ | |||
+ | A structurally simple format is the '' | ||
+ | |||
+ | ===== Export ===== | ||
+ | |||
+ | This line exports data from the table '' | ||
+ | |||
+ | <code sql> | ||
+ | SELECT * FROM account INTO OUTFILE '/ | ||
+ | </ | ||
+ | |||
+ | ===== Import ===== | ||
+ | |||
+ | Imports are preformed symmetrically, | ||
+ | <code sql> | ||
+ | LOAD DATA INFILE '/ | ||
+ | </ | ||
+ | |||
+ | ====== Dump and Restore a Database ====== | ||
+ | |||
+ | To dump a database to a file, issue: | ||
+ | <code bash> | ||
+ | mysqldump -u root -p database > database.sql | ||
+ | </ | ||
+ | |||
+ | to restore a database from a file, issue: | ||
+ | <code bash> | ||
+ | mysqldump -u root -p database < database.sql | ||
+ | </ | ||
+ | |||
+ | ====== Sum-Up Columns in MySQL Table ====== | ||
+ | |||
+ | Given the following table: | ||
+ | |||
+ | ^ '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | we want to find out the total score for '' | ||
+ | |||
+ | <code sql> | ||
+ | SELECT Name, | ||
+ | </ | ||
+ | |||
+ | which will return the following table: | ||
+ | |||
+ | ^ '' | ||
+ | | '' | ||
+ | |||
+ | The same can be done if the scores are distributed on multiple columns: | ||
+ | |||
+ | ^ '' | ||
+ | | '' | ||
+ | |||
+ | To find out the total, summative score for '' | ||
+ | |||
+ | <code sql> | ||
+ | SELECT Name, | ||
+ | </ | ||
+ | |||
+ | which will return the table: | ||
+ | |||
+ | ^ '' | ||
+ | | '' | ||
+ | |||
+ | ====== Empty a Database Without Dropping ====== | ||
+ | |||
+ | To empty the database '' | ||
+ | |||
+ | <code sql> | ||
+ | SELECT CONCAT(' | ||
+ | </ | ||
+ | |||
+ | ====== Repair MySQL Tables ====== | ||
+ | |||
+ | <code bash> | ||
+ | mysql -Be " | ||
+ | </ | ||
+ | |||
+ | or | ||
+ | |||
+ | <code bash> | ||
+ | mysqlcheck --auto-repair -A | ||
+ | </ | ||
+ | |||
+ | ====== Reset Root Password ====== | ||
+ | |||
+ | First, stop MySQL using: | ||
+ | <code bash> | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | Now we run MySQL by omitting to read the grant tables: | ||
+ | <code bash> | ||
+ | mysqld --skip-grant-tables | ||
+ | </ | ||
+ | |||
+ | And we log-in as '' | ||
+ | <code bash> | ||
+ | mysql -u root | ||
+ | </ | ||
+ | |||
+ | Then we run a query to replace the root password: | ||
+ | <code sql> | ||
+ | FLUSH PRIVILEGES; | ||
+ | UPDATE mysql.user SET Password=PASSWORD(' | ||
+ | FLUSH PRIVILEGES; | ||
+ | exit | ||
+ | </ | ||
+ | |||
+ | And after that we restart MySQL: | ||
+ | <code bash> | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | ====== Import Multiple SQL Files ====== | ||
+ | |||
+ | In a directory containing '' | ||
+ | <code bash> | ||
+ | ls -1 *.sql | awk '{ print " | ||
+ | </ | ||
+ | |||
+ | where: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ====== Drop all Tables in Database ====== | ||
+ | |||
+ | This can be accomplished with: | ||
+ | |||
+ | <code bash> | ||
+ | mysql -u USER --password=PASSWORD -Nse 'SHOW TABLES' | ||
+ | </ | ||
+ | |||
+ | where: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ====== Truncate all Tables in Database ====== | ||
+ | |||
+ | This can be done with: | ||
+ | <code bash> | ||
+ | mysql -u USER --password=PASSWORD -Nse 'show tables' | ||
+ | </ | ||
+ | |||
+ | where: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' |