9.9.7.8 往表中插入数据
(1)命令语法
Insert into<表名>[(<字段名1>[…<字段名n>])]values(值1)[,(值n)]
(2)新建一个简单的测试表test1
Create Table: CREATE TABLE `test1` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
(3)往表中插入数据的不同的例子
a.按规矩指定所有列名,并且每列都插入值
Insert into test(id,name) values(1,’zbf’);
[email protected] 12:0600-> insert into test(id,name) values(1,‘zbf‘);
Query OK, 1 row affected (0.00 sec)
[email protected] 12:1313->select * from test;
+----+------+
| id | name |
+----+------+
| 1 | zbf |
+----+------+
1 row in set (0.00 sec)
b.由于id列为自增的,所以只在name列插入值
[email protected] 12:1329-> insert into test(name) values(‘wwnwan‘);
Query OK, 1 row affected (0.00 sec)
[email protected] 12:1738->select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | zbf|
| 2 | wwnwan |
+----+--------+
2 rows in set (0.00 sec)
c.如果不指定列,就要按规矩为每列都插入适当的值
[email protected] 12:1742->insert into test values(3,‘lisi‘);
Query OK, 1 row affected (0.00 sec)
[email protected] 12:2349->select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | zbf|
| 2 | wwnwan |
| 3 | lisi |
+----+--------+
d.批量插入数据方法提升效率。
[email protected] 12:2630->insert into test values(4,‘woshishei‘),(5,‘nimei‘);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
[email protected] 12:2708->select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | zbf |
| 2 | wwnwan|
| 3 | lisi |
| 4 | woshishei |
| 5 | nimei |
+----+-----------+
e.删除数据(慎用)
[email protected] 12:2723->delete from test;
Query OK, 5 rows affected (0.00 sec)
[email protected] 12:4005->select * from test;
Empty set (0.00 sec)
f.批量插入演示
[email protected] 12:4502->select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wwnwan|
| 2 | zbf |
| 3 | lisi |
| 4 | woshishei |
| 5 | nimei |
g.测试完毕,退出数据库,然后备份上述数据,留着备用。备份后检查备份的sql数据内容;过滤无用信息。
[[email protected] ~]# mysqldump -usystem -pzbf666 -B wwn >/opt/zbf_bak.sql
[[email protected] ~]# grep -E -v "#|\/|^$|--" /opt/zbf_bak.sql
h.一个错误的备份案列,如果不检查备份数据可能导致数据不是想要的
[[email protected] ~]# mysqldump -usystem -pzbf666 -A -B wwn >/opt/zbf_bak1.sql
[[email protected] ~]# grep -E -v "#|\/|^$|--" /opt/zbf_bak1.sql
Usage: mysqldump [OPTIONS] database [tables]
提示:
- -A备份所有的库,-B备份指定的库
- 5.1.68版本[[email protected] ~]# mysqldump -usystem -pzbf666 –A -B --events >/opt/zbf_bak.sql
补充强调:我们平时登录网站发帖子,发博文,实质上就是调用网站的程序连接mysql数据库,通过上述insert语句把帖子博文数据存入数据库的。
原文地址:http://blog.51cto.com/10642812/2065797
时间: 2024-10-03 19:08:38