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)
mysql基本操作(二)