mysql基本操作(二)

1、向表msg中插入数据,先创建表,再插入数据

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,"初来乍到","张三","我是老大");
ERROR 1366 (HY000): Incorrect string value: '\xE5\x88\x9D\xE6\x9D\xA5...' for column 'title' at row 1

插入报错,是因为数据库没有设置字符集原因,设置如下:

修改数据库的字符集

mysql> alter database test character set utf8;
Query OK, 1 row affected (0.10 sec)

修改数据库表字符集

mysql> alter table msg character set utf8;
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

修改每个表字段的字符集

mysql> alter table msg change title title varchar(60) 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(60) character set utf8;

Query OK, 0 rows affected (0.14 sec)

mysql> alter table msg change content content varchar(1000) character set utf8;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

 

或者执行

set names utf8;

 

2、更新数据

mysql> update msg set name="王五",content="我只想当老二" 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 | 初来乍到 | 张三 | 我是老大 |
| 2 | 刚来不久 | 王五 | 我只想当老二 |
+------+--------------+--------+--------------------+
2 rows in set (0.00 sec)

mysql>

 

3、删除表数据

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

5、查询表数据

mysql> select id,name from msg;
+------+--------+
| id | name |
+------+--------+
| 1 | 张三 |
| 1 | 刘备 |
| 2 | 关于 |
+------+--------+
3 rows in set (0.00 sec)

posted @ 2018-01-23 14:44  凉生墨客  阅读(158)  评论(0编辑  收藏  举报