MySQL:增删查改


#MySQL 笔记


TODO: 待补充。

插入数据

创建表:

CREATE TABLE test_table (
    c1 varchar(100) not null,
    c2 varchar(100)
) ENGINE = InnoDB CHARACTER SET = utf8mb4;

插入一行数据,但只插入一列:

insert into test_table(c1) values('你好');

查看表中数据:

mysql> select * from test_table;
+------+--------+
| c1   | c2     |
+------+--------+
| 你好 | <null> |
+------+--------+

插入一行数据,插入两列:

insert into test_table(c1, c2) values('你好', '世界');

查看表中数据:

mysql> select * from test_table;
+------+--------+
| c1   | c2     |
+------+--------+
| 你好 | <null> |
| 你好 | 世界   |
+------+--------+

一次插入多行数据:

insert into test_table(c1, c2) values('你好', '中国'), ('你好', '北京');
mysql> select * from test_table;
+------+--------+
| c1   | c2     |
+------+--------+
| 你好 | <null> |
| 你好 | 世界   |
| 你好 | 中国   |
| 你好 | 北京   |
+------+--------+

注意,这里有一个坑:

insert into test_table(c1, c2) values(null, '中国'); -- 报错 (1048, u"Column 'c1' cannot be null")
insert into test_table(c1, c2) values(null, '中国'), ('你好', '上海'); -- 成功,第一条数据的c1列的值变成了默认的空字符串


( 本文完 )