MySQL basic operation (ii)

Source: Internet
Author: User

1. Insert the data into the table MSG, create the table first, and then insert the data

Mysql> CREATE TABLE MSG (
ID int,
Title varchar (60),
Name varchar (10),
Content varchar (1000)
);
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT into MSG (id,title,name,content) VALUES (1, "newcomer", "Zhang San", "I am the eldest");
ERROR 1366 (HY000): Incorrect string value: ' \xe5\x88\x9d\xe6\x9d\xa5 ... ' for column ' title ' at row 1

The error is inserted because the database does not have a character set reason, set as follows:

Modifying the character set of a database

mysql> ALTER DATABASE test character set UTF8;
Query OK, 1 row affected (0.10 sec)

Modifying the database table character set

mysql> ALTER TABLE MSG character set UTF8;
Query OK, 0 rows affected (0.10 sec)
records:0 duplicates:0 warnings:0

Modify the character set for each table field

Mysql> ALTER TABLE MSG change title title varchar (character) set UTF8;
Query OK, 0 rows affected (0.33 sec)
records:0 duplicates:0 warnings:0

Mysql> ALTER TABLE MSG change name name varchar (character) set UTF8;

Query OK, 0 rows affected (0.14 sec)

Mysql> ALTER TABLE MSG change content content varchar (character) set UTF8;
Query OK, 0 rows affected (0.16 sec)
records:0 duplicates:0 warnings:0

or perform

Set names UTF8;

2. Update data

mysql> Update msg set name= "Harry", content= "I just want to be a dick" where msg.id=2;
Query OK, 1 row affected (0.02 sec)
Rows matched:1 changed:1 warnings:0

Mysql> select * from MSG;
+------+--------------+--------+--------------------+
| ID | Title | name | Content |
+------+--------------+--------+--------------------+
| 1 | Newcomers | Zhang San | I'm the Boss |
| 2 | Just arrived soon | Harry | I just want to be a dick |
+------+--------------+--------+--------------------+
2 rows in Set (0.00 sec)

Mysql>

3. Delete table data

Mysql> Delete from msg where id=3;
Query OK, 1 row affected (0.02 sec)

5. Query table Data

Mysql> select Id,name from msg;
+------+--------+
| ID | name |
+------+--------+
| 1 | Zhang San |
| 1 | Liu Bei |
| 2 | About |
+------+--------+
3 Rows in Set (0.00 sec)

MySQL basic operation (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.