mysql

Add auto_increment after table created

Snippet
ALTER TABLE purchase_links ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT first, add primary key (id);

Importing MySQL data into Access

Snippet

Sometimes, a linked database needs to be exported into Access for someone to use with Excel. Don't ask me, it happens. How to do that, even with linked database tables.

20.1.5.4.2. Importing MySQL Data to Access
 
To import a table or tables from MySQL to Access, follow these instructions:
 
1. Open a database, or switch to the Database window for the open database.
 
2. To import tables, on the File menu, point to Get External Data, and then 
click Import.
 
3. In the Import dialog box, in the Files Of Type box, select ODBC Databases (). 
The Select Data Source dialog box lists the defined data sources The Select Data 
Source dialog box is displayed; it lists the defined data source names.
 
4. If the ODBC data source that you selected requires you to log on, enter your
login ID and password (additional information might also be required), and then 
click OK.
 
5. Microsoft Access connects to the MySQL server through ODBC data source and 
displays the list of tables that you can import.
 
6. Click each table that you want to import, and then click OK.

Pause a MySQL query with sleep

Snippet

For MySQL > 5.0.12, execution of a series of queries can be delayed / paused by using SELECT SLEEP(), which is useful when a previous query needs to be paused, say, when a slave is reconnecting to a master.

-- delay a query by one second
SELECT SLEEP(1);
 
-- delay a series of queries by a 30 seconds
SELECT SLEEP(30);
 
-- stop the slave, skip the error SQL, start the slave, pause a second, then check the slave status
STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; START SLAVE; SELECT SLEEP(1); SHOW SLAVE STATUS;

Skip duplicate entries for replication queries

Snippet

If a conflicting query occurs on a slaved database, and it's okay to skip that query, use the SQL_SLAVE_SKIP_COUNTER setting.

STOP SLAVE;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = N;
START SLAVE;

Turn email addresses into fake addresses for testing databases

Snippet

When you have live data in a testing database, you don't want to trigger emails that go to your users. In the case where you need the rest of the data, and need valid email addresses, append .example.com to turn current valid email addresses into syntactically valid email addresses, but not emailable addresses.

UPDATE `orders` SET email = CONCAT(email, '.example.com') WHERE email NOT LIKE '%example.com';
 
-- alternately, to undo this change, use replace
 
UPDATE `orders` SET email = REPLACE(email, '.example.com', '');

Show MySQL warnings after query

Snippet

When a query has run, a number of warnings may have occurred that didn't stop the query from running, but may be of concern. Use "SHOW WARNINGS [LIMIT n]" to display the warnings and see what might have gone wrong in the query.

mysql> SELECT name, addr1, addr2, city, state, FROM_UNIXTIME(created_on) AS created FROM `orders` WHERE ...;
Empty set, 20180 warnings (0.02 sec)
 
mysql> show warnings limit 10;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-21 00:01:01' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-26 00:01:01' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-21 00:01:01' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-26 00:01:01' | 

Pages