database2018년 7월 29일4 min read

Q&A: MySQL Questions Collection

A collection of MySQL questions and answers I gathered while learning.

FFrank Advenoh
#Q&A#faq#mysql

This is a personal note where I jot down the things I don't know and briefly summarize what I learn along the way. If you happen to know the answer to any of the unanswered questions, please leave a comment. Thank you.

Full Q&A List

[Answered]

1. Why do you need to set InnoDB when creating a table?

MySQL has several storage engines. The most commonly used ones are MyISAM and InnoDB. You can choose which engine to use when creating a table.

Reference

2. Why does auto_increment jump by 4 instead of increasing by 1?

By default it increases by 1, but if you set the auto_increment_increment value differently, it will increase by the specified amount.

mysql> show variables like 'auto_inc%;

Reference

3. I sometimes find '@variable := …’ in SQL statements. What does it mean?

It's used to store a user-defined variable. In this case, the media_no value obtained by the SELECT is stored in the mediaNo variable.

Reference

4. What is the difference between COUNT(*) vs COUNT(1) vs COUNT(pk)?

  • COUNT(*)
    • Counts the number of rows
    • Counts NULLs as well
  • COUNT(1)
    • Counts the number of rows, but only queries against a single table and cannot query a JOINed table
    • Some argue against using it
  • COUNT(pk)
    • Counts only non-NULL values

Reference

5. The IFNULL() function?

In the form IFNULL(expression, alt_value), it returns alt_value if expression is NULL.

Reference

6. What does 'order by 2,1’ mean for sorting?

It means sort by the second column, and when there are duplicate values, sort by the first column.

Reference

6. When MySQL Error 1093 : You can’t specify target table ..for update in FROM clause occurs, how do you handle it?

The following SQL raised an error when executed.

UPDATE ` tmon_media ` . ` media_external_trans `
SET use_yn = 'Y'
WHERE trans_seqno IN (SELECT trans_seqno FROM media_external_trans as t where t.trans_seqno > 3162);

The cause is that, unlike Oracle, MySQL has an issue where it can't directly use a table's own data during an UPDATE or DELETE, so you can solve it by creating one more sub-query to build a temporary table.

Solution

Reference

8. How do you log all queries in MySQL?

You can do it by enabling general_log in the MySQL configuration.

mysql> set global general_log=ON;
mysql> show variables like ‘general%;

Reference

8. Is there a way to check queries in MySQL when general_log is not enabled?

You can check them if MySQL saves all statements to the bin log when it runs. For details on bin log analysis, please refer to the link below.

Reference

9. What is a Slow Query?

A slow query, as the name suggests, refers to a query that takes a long time to execute.

Reference

10. How are offset and limit used in paging?

Since you can't fetch a large amount of data all at once, you use LIMIT and OFFSET to fetch data partially through paging.

  • LIMIT : the number of rows to fetch
  • OFFSET : the starting number that determines from which position among the retrieved rows to begin fetching

Reference

11. What's the solution when the error message "Server time zone value ‘KST’ is unrecognized…" appears?

The solution likely differs depending on the situation. In our case, we solved it by changing the version of mysql-connector-java in pom.xml (e.g. 8.0.13 —> 5.1.47).

Reference

12. What is the difference between ON and WHERE in a JOIN?

For an inner join, the ON clause produces the same result as using the WHERE clause, so you only need to use ON for an outer join.

Reference

13. How do you print the value assigned to a variable?

You can use SELECT @variable.

SET @col = "c1";
SELECT @col;

Reference


[Unanswered Questions]

- When you want to copy the values of a row but change only a few column values?

Reference

- How do you migrate data? For example, how do you split data that exists in one table?

- How do you copy a table?

- What is group_concat?

Reference

관련 글