1、插入数据
插入单行数据(如果发现语句中含有set关键字,则表示插入单行数据)
语法
Insert into table_name set
<字段1>=值1,
<字段2>=值2,
<字段3>=值3;
首先查看表结构
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | |NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
插入数据
mysql> insert into t1 set
-> id=10,
-> name='xxx';
Query OK, 1 row affected (0.09 sec)
查看数据
select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
+------+-------+
5 rows in set (0.00 sec)
插入多行数据
语法
Insert into table_name
[(字段1,字段2,字段N)] 方括号表示可选
Values
(值1,值2,值N),(值1N,值2N,值3N);
查看表结构
mysql> desc t1
-> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | |NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
插入数据
mysql> insert into t1
-> (id,name)
-> values
-> (11,'fox'),
-> (12,'tom');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看数据
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
| 11 | fox |
| 12 | tom |
+------+-------+
7 rows in set (0.00 sec)
如果一张表所有的列都插入数值的话,可以省略列名的内容。
mysql> insert into t1 values(13,'jack'), (14,'han');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看数据
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
| 11 | fox |
| 12 | tom |
| 13 | jack |
| 14 | han |
+------+-------+
9 rows in set (0.00 sec)
插入指定列数据
mysql> insert into t1 (id) values(15),(16);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看数据
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
| 11 | fox |
| 12 | tom |
| 13 | jack |
| 14 | han |
| 15 | NULL |
| 16 | NULL |
+------+-------+
11 rows in set (0.00 sec)
向表插入查询结果
语法
Insert into table_name1
(字段1,字段2,字段N)
Select 字段A,字段B,字段C from
Table_name2
[where condition];
新创建一个T3表
mysql> create table t3 (id int(10), name varchar(20));
Query OK, 0 rows affected (0.06 sec)
查看表结构
mysql> desc t3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | YES | |NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | |NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
插入数据
mysql> insert into t3 select id,namefrom t1;
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
对比两个表的数据
mysql> select * from t3
-> ;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
| 11 | fox |
| 12 | tom |
| 13 | jack |
| 14 | han |
| 15 | NULL |
| 16 | NULL |
+------+-------+
11 rows in set (0.00 sec)
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
| 11 | fox |
| 12 | tom |
| 13 | jack |
| 14 | han |
| 15 | NULL |
| 16 | NULL |
+------+-------+
11 rows in set (0.00 sec)
2、更新数据
语法
Update table_name1 set
字段1=值1,
字段2=值2,
字段N=值N
[where condition];
更新数据
mysql> update t1 set
-> name='lll'
-> where
-> id=15;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看数据
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
| 10 | xxx |
| 11 | fox |
| 12 | tom |
| 13 | jack |
| 14 | han |
| 15 | lll |
| 16 | NULL |
+------+-------+
11 rows in set (0.00 sec)
如果不写where条件更新数据
mysql> update t1 set name='lll';
Query OK, 10 rows affected (0.00 sec)
Rows matched: 11 Changed: 10 Warnings: 0
查看数据 (你会发现所有的行都更改了,所以需要加上where语句)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | lll |
| 1 | lll |
| 2 | lll |
| 3 | lll |
| 10 | lll |
| 11 | lll |
| 12 | lll |
| 13 | lll |
| 14 | lll |
| 15 | lll |
| 16 | lll |
+------+------+
11 rows in set (0.00 sec)
3、删除数据
语法
Delete from table_name
[where <condition>];
删除数据
mysql> delete from t1 where id>13;
Query OK, 3 rows affected (0.01 sec)
查看数据
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | lll |
| 1 | lll |
| 2 | lll |
| 3 | lll |
| 10 | lll |
| 11 | lll |
| 12 | lll |
| 13 | lll |
+------+------+
8 rows in set (0.00 sec)
不加条件删除数据
mysql> delete from t1;
Query OK, 8 rows affected (0.01 sec)
查看数据 (不加条件清空了全部数据)
mysql> select * from t1;
Empty set (0.00 sec)
4、事物操作
使用inoodb数据引擎的表支持事物操作
默认情况下MySQL开启了自动提交
BEGIN开启一个事物
ROLLBACK回滚一个事物
COMMIT提交一个事物
进入事物
mysql> begin
-> ;
Query OK, 0 rows affected (0.00 sec)
在事物中插入数据
mysql> insert into t1 values(1,'liujing');
Query OK, 1 row affected (0.00 sec)
查看数据
mysql> select * from t1;
+------+---------+
| id | name |
+------+---------+
| 1 | liujing |
+------+---------+
1 row in set (0.00 sec)
新开一个终端
查看T1表数据(数据是空的,因为这是另外一个事物,事物之间是隔离的。)
mysql> select * from t1
-> ;
Empty set (0.00 sec)
第一个终端
事物完成,提交修改,这个时候结果才会写到硬盘中,可以被其他事物读取到。
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
新开的终端可以读取到
mysql> select * from t1;
+------+---------+
| id | name |
+------+---------+
| 1 | liujing |
+------+---------+
1 row in set (0.00 sec)
回滚操作
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 values(2,'jingcheng');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+-----------+
| id | name |
+------+-----------+
| 1 | liujing |
| 2 | jingcheng |
+------+-----------+
2 rows in set (0.00 sec)
回滚
mysql> rollback;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from t1;
+------+---------+
| id | name |
+------+---------+
| 1 | liujing |
+------+---------+
1 row in set (0.00 sec)
提交保存
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
查看配置
mysql> show variables like '%commit';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| autocommit | ON |
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
2 rows in set (0.01 sec)
自动保存是默认开启的。
关闭自动提交
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%commit';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| autocommit | OFF |
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
2 rows in set (0.00 sec)
如果关闭自动提交,那么无论你输入任何操作,MySQL都会认为是在一个事物里,当你输入commit之后才会关闭事物,并相当于同时开启了另一个事物。
什么时候需要关闭自动保存?
批量导入数据的时候,很多的单行导入,建议关闭自动保存。导入之后再开启自动保存。
这样可以节省MySQL的资源,提交MySQL的效率,否则一条语句就相当于开启了一个事物。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com