MySQL

MySQL之插入数据

作者头像 刘宇帅
2779 0

插入 students 表一条数据包含所有字段的值

insert into students values (1,'0001','洛基',2,25,'2019-12-20 20:00:00','2019-12-20 20:00:00');

执行

mysql> insert into students values (1,'0001','洛基',2,25,'2019-12-20 20:00:00','2019-12-20 20:00:00');
Query OK, 1 row affected (0.00 sec)

一次插入多条数据包含所有字段的值到 mysql

insert into students values (2,'0002','冷锋',2,25,'2019-12-20 20:00:00','2019-12-20 20:00:00'),(3,'0003','六一',2,25,'2019-12-20 20:00:00','2019-12-20 20:00:00');

执行

mysql> insert into students values (2,'0002','冷锋',2,25,'2019-12-20 20:00:00','2019-12-20 20:00:00'),(3,'0003','六一',2,25,'2019-12-20 20:00:00','2019-12-20 20:00:00');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

students 表中 id 是自增的,created_at、updated_at 也是可取默认值的,所以在实际使用中我们经常需要插入指定字段的数据到数据库

insert into students (`no`,`name`,`sex`,`age`) values ('0004','杨过',2,25);

执行

mysql> insert into students (`no`,`name`,`sex`,`age`) values ('0004','杨过',2,25);
Query OK, 1 row affected (0.00 sec)

查看数据库最后结果

mysql> select * from students;
+----+------+--------+-----+------+---------------------+---------------------+
| id | no   | name   | sex | age  | created_at          | updated_at          |
+----+------+--------+-----+------+---------------------+---------------------+
|  1 | 0001 | 洛基   |   2 |   25 | 2019-12-20 20:00:00 | 2019-12-20 20:00:00 |
|  2 | 0002 | 冷锋   |   2 |   25 | 2019-12-20 20:00:00 | 2019-12-20 20:00:00 |
|  3 | 0003 | 六一   |   2 |   25 | 2019-12-20 20:00:00 | 2019-12-20 20:00:00 |
|  4 | 0004 | 杨过   |   2 |   25 | 2019-12-20 00:26:14 | NULL                |
+----+------+--------+-----+------+---------------------+---------------------+
4 rows in set (0.00 sec)

其他插入语句

不存在时插入,存在时先删除再添加

replace into students values (1,'0001','洛基',2,24,'2019-12-20 20:00:00','2019-12-20 20:00:00');

我们执行会发现返回的 affect rows 是 2

mysql> replace into students values (1,'0001','洛基',2,24,'2019-12-20 20:00:00','2019-12-20 20:00:00');
Query OK, 2 rows affected (0.07 sec)

不存在时插入,存在则忽略

insert ignore into students values (1,'0001','洛基',1,23,'2019-12-20 20:00:00','2019-12-20 20:00:00');

我们执行可看到 affected rows 是 0

mysql> insert ignore into students values (1,'0001','洛基',1,23,'2019-12-20 20:00:00','2019-12-20 20:00:00');
Query OK, 0 rows affected, 1 warning (0.00 sec)

不存在添加,已存在更新

insert into students values (1,'0001','洛基',1,23,'2019-12-20 20:00:00','2019-12-20 20:00:00') on duplicate key update age=79;

执行如下

mysql> insert into students values (1,'0001','洛基',1,23,'2019-12-20 20:00:00','2019-12-20 20:00:00') on duplicate key update age=79;
Query OK, 2 rows affected (0.01 sec)

如上执行返回的 affected rows 也是 2,原因目前不可知,待深入学习。。

作者头像

刘宇帅

非著名程序员,全栈开发工程师,长期专注系统开发与架构设计。

提示

功能待开通!


暂无评论~

相关文章

MySQL之创建库

MySQL 创建库语法 CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name 创建一个默认的数据库 create database school; 执行结果 mysql> create database school; Query OK, 1 row affecte

MySQL之创建表

创建表 创建学生表 SQL create table if not exists`students` ( `id` int not null auto_increment comment '学生id', `no` char(5) not null comment '学生学号', `name` varchar(128) not null default '' comment '学生姓名', `sex` tinyint not null default 0 comment '0 无 1 女 2 男', `age` tinyint null default

MySQL之删除数据

单表删除语法 DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias] [PARTITION (partition_name [, partition_name] ...)] [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] 多表删除 DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name[.*] [, tbl_name[.*]] ... FROM table_r

MySQL 技术内幕阅读笔记(0)--- InnoDB 体系结构和存储引擎

MySQL 体系结构 从图我们可以看出,MySQL 有如下几部分组成: 连接池组件 管理服务和工具组件 SQL 接口组件 查询优化组件 优化器组件 缓冲组件 插件式存储引擎 物理文件 需要注意存储引擎是基于表的而不是库,比如我们可以在建表的时候为表指明存储引擎。 MySQL 存储引擎 常见存储引擎 InnoDB MySQL 5.5.8 版本之后的默认存储引擎,事务性,行锁,支持外键。 MyISAM 存储引擎不支持事务、表锁,支持全文索引,主要面对一些 OLAP 数据库应用。MySQL 5.5.6 版本之前的默认存储引擎。 NDB 存储引擎是一个集群存储引擎。 Memory 存储引擎将表

查看 MySQL 表的大小

information_schema MySQL 自带一个数据库 information_schema ,这个数据库用于记录 MySQL 数据库的基本信息,比如数据库名,数据库的表,表栏的数据类型与访问权限等。 mysql> use information_schema; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql>