当前位置:网站首页>Insert into data table to insert data
Insert into data table to insert data
2022-07-31 15:20:00 【Xiao Liu learns Android】
1、Data table insert data summary:
- 普通插入(全字段):INSERT INTO table_name VALUES (value1, value2, ...)
- 普通插入(限定字段):INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...)
- 多条一次性插入:INSERT INTO table_name (column1, column2, ...) VALUES (value1_1, value1_2, ...), (value2_1, value2_2, ...), ...
- 从另一个表导入:INSERT INTO table_name SELECT * FROM table_name2 [WHERE key=value]
2、自增id的处理
The existing data sheet is as follows
| Filed | Type | Null | Key | Extra | Default | Comment |
| id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
| uid | int(11) | NO | (NULL) | 用户ID | ||
| exam_id | int(11) | NO | (NULL) | 试卷ID | ||
| start_time | datetime | NO | (NULL) | 开始时间 | ||
| submit_time | datetime | YES | (NULL) | 提交时间 | ||
| score | tinyint(4) | YES | (NULL) | 得分 |
插入两条数据:
- 用户1001在2021年9月1日晚上10点11分12秒开始作答试卷9001,并在50分钟后提交,得了90分;
- 用户1002在2021年9月4日上午7点1分2秒开始作答试卷9002,并在10分钟后退出了平台.
这里的id是自增主键(PRIMARY KEY),这就意味着不需要你自己手动填入,它会跟随表格行数进行自己增加(比如这样增加id值1,2,3...n).
所以我们在插入数据的时候,
方法之一: 可以指定插入的列名, 这样就不用填写id这一列的数据,让他自增.具体插入代码如下:
INSERT INTO exam_record (uid, exam_id, start_time, submit_time, score) VALUES
(1001, 9001, '2021-09-01 22:11:12', '2021-09-01 23:01:12', 90),
(1002, 9002, '2021-09-04 07:01:02', NULL, NULL);方法之二:把id的值设置为NULL或0,这样MySQL会自己处理这个自增的id列. 具体代码如下:
INSERT INTO exam_record VALUES(NULL, 1001, 9001, '2021-09-01 22:11:12', '2021-09-01 23:01:12', 90),(NULL, 1002, 9002, '2021-09-04 07:01:02', NULL, NULL);方法三:直接填入id值,However, it is only used when there is not much data inserted,大家了解一下:
INSERT INTO exam_record VALUES
(1, 1001, 9001, '2021-09-01 22:11:12', '2021-09-01 23:01:12', 90),
(2, 1002, 9002, '2021-09-04 07:01:02', NULL, NULL);3、Handling of date and time
- interval 时间间隔关键字,常和date_add() 或 date_sub()搭配使用.
The following expressions are correct
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
The following expression is wrong
A.T_DATE = B.T_DATE interval '1' hour
错.单独的intervalIt needs to follow the addition and subtraction operators,如果使用date_add()或date_sub()则可以省略B.T_DATE和interval之间的运算符.
4、replace into插入
replace into 跟 insert into功能类似,不同点在于:replace into 首先尝试插入数据到表中,
- 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据;
- 否则,直接插入新数据.
要注意的是:插入数据的表必须有主键或者是唯一索引!否则的话,replace into 会直接插入数据,这将导致表中出现重复的数据.
REPLACE INTO examination_info
VALUES(NULL,9003,'SQL','hard',90,'2021-01-01 00:00:00');
5、Delete existing data first,再插入新数据
DELETE FROM examination_info WHERE exam_id=9003;
INSERT INTO examination_info (exam_id, tag, difficulty, duration, release_time) VALUES
(9003, "SQL", "hard", 90, "2021-01-01 00:00:00")
边栏推荐
- Efficient use of RecyclerView Section 3
- Gorm—Go language database framework
- 浏览器自带的拾色器
- Linux查看redis版本(查看mongodb版本)
- Female service community product design
- Public Key Retrieval is not allowed error solution when DBeaver connects to MySQL 8.x
- org.apache.jasperException(could not initialize class org)
- R language moves time series data forward or backward (custom lag or lead period): use the lag function in the dplyr package to move the time series data forward by one day (set the parameter n to a p
- Codeforces Round #796 (Div. 2) (A-D)
- 01 邂逅typescript,环境搭建
猜你喜欢
随机推荐
leetcode303 Weekly Match Replay
vb中如何连接mysql_vb怎么连接数据库「建议收藏」
Getting started with UnityShader (3) - Unity's Shader
Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
力扣:56. 合并区间
网银被盗?这篇文章告诉你如何安全使用网银
Gorm—Go language database framework
乡村基冲刺港交所:5个月期内亏2224万 SIG与红杉中国是股东
模板与泛型编程值typelist实现
Unity中实现点选RenderTexture中的3D模型
ASP.NET Core 产生连续 Guid
11 pinia使用
修改SQL语言实现Mysql 多表关联查询优化
大健云仓冲刺美股:增营收反减利润 京东与DCM是股东
Codeforces Round #796 (Div. 2) (A-D)
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
SQL、HQL、JPQL 到底有什么区别
org.apache.jasperException(could not initialize class org)
数据库的范式(第一范式,第二范式,第三范式,BCNF范式)「建议收藏」
Introductory UnityShader learning (2) - the rendering pipeline









