English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to escape the apostrophe (') in MySQL?

We can escape the apostrophe (' in MySQL in the following two ways-

  • We can use the backslash.

  • We can use single quotes twice (double quotes)

Using backslash

Let's first create a table.

mysql> create table SingleQuotesDemo
  - > (
  - > id int,
  - > name varchar(100)
  - );

The effect of the name 'John's' used directly is not ideal.

mysql> insert into SingleQuotesDemo values(1,'John's');
    '>

Now let's use the backslash.

mysql> insert into SingleQuotesDemo values(1,'John\'s');

Now, we will display the records.

mysql> select *from SingleQuotesDemo;

This is the output, indicating that we have correctly implemented the backslash.

+------+--------+
| id | name |
+------+--------+
|    1 | John's |
+------+--------+
1 row in set (0.00 sec)

Using double quotes

The following is the syntax of using double quotes to implement the backslash. We are inserting another record into the same table we are using above.

mysql> insert into SingleQuotesDemo values(2,'John''s');

Now, we will display the records.

mysql> select *from SingleQuotesDemo;

The following is the output.

+------+--------+
| id | name |
+------+--------+
|    1 | John's |
|    2 | John's |
+------+--------+
2 rows in set (0.00 sec)

In the above possible ways, we can escape the apostrophe (').