How do I insert a special character such as ' (single quote) into MySQL?


To insert a special character such as “ ‘ “ (single quote) into MySQL, you need to use \’ escape character. The syntax is as follows −

insert into yourTableName(yourColumnName) values(' yourValue\’s ');

To understand the above syntax, let us create two tables. The query to create first table is as follows −

mysql> create table AvoidInsertErrorDemo
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Sentence text
-> );
Query OK, 0 rows affected (0.53 sec)

Now you can insert special character such as ‘ in the table using insert command. The query is as follows −

mysql> insert into AvoidInsertErrorDemo(Sentence) values('a woman\'s hat');
Query OK, 1 row affected (0.16 sec)
mysql> insert into AvoidInsertErrorDemo(Sentence) values('Mrs. Chang\'s house');
Query OK, 1 row affected (0.31 sec)

Display all records from the table using select statement:

mysql> select *from AvoidInsertErrorDemo;

The following is the output −

+----+--------------------+
| Id | Sentence           |
+----+--------------------+
|  1 | a woman's hat      |
|  2 | Mrs. Chang's house |
+----+--------------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements