当前位置:网站首页>数据表插入数据insert into
数据表插入数据insert into
2022-07-31 15:12:00 【小刘学安卓】
1、数据表插入数据方式汇总:
- 普通插入(全字段):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的处理
现有数据表如下
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值,不过仅针对插入数据不多的时候使用,大家了解一下:
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、关于日期时间的处理
- interval 时间间隔关键字,常和date_add() 或 date_sub()搭配使用。
以下表达正确
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
以下表达错误
A.T_DATE = B.T_DATE interval '1' hour
错。单独的interval需要跟在加减运算符后,如果使用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 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")
边栏推荐
- svn安装及使用(身体功能手册)
- PDF 拆分/合并
- The R language ggstatsplot package ggbarstats function visualizes bar charts, and adds hypothesis test results (including sample number, statistics, effect size and its confidence interval, significan
- Architecture actual combat battalion module 8 message queue table structure design
- 使用 PyTorch 检测眼部疾病
- R语言向前或者向后移动时间序列数据(自定义滞后或者超前的期数):使用dplyr包中的lag函数将时间序列数据向前移动一天(设置参数n为正值)
- Essential Learning for Getting Started with Unity Shader - Transparency Effect
- ASP.NET Core 产生连续 Guid
- R language ggplot2 visualization: use the ggmapplot function of the ggpubr package to visualize the MA plot (MA-plot), the font.legend parameter and the font.main parameter to set the title and legend
- Linux查看redis版本(查看mongodb版本)
猜你喜欢
随机推荐
国内市场上的BI软件,到底有啥区别
The R language ggstatsplot package ggbarstats function visualizes bar charts, and adds hypothesis test results (including sample number, statistics, effect size and its confidence interval, significan
NPM淘宝镜像(最新版本)于2021-11-21 16:53:52发布新版本npm镜像[通俗易懂]
Excel快速对齐表格的中姓名(两个字姓名和三个字姓名对齐)
NPM Taobao mirror (latest version) released a new version of npm mirror at 2021-11-21 16:53:52 [easy to understand]
Gorm—Go语言数据库框架
Word table to Excel
abaqus find contact pairs报错:surface name is already in use
力扣:738.单调递增的数字
基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
深入浅出边缘云 | 4. 生命周期管理
学习笔记12--路径-速度分解法之局部路径搜索
R language test whether the sample conforms to normality (test whether the sample comes from a normally distributed population): shapiro.test function tests whether the sample conforms to the normal d
【MySQL】Mysql范式及外键作用
RecyclerView的高效使用第一节
MANIFEST.MF文件(PDB文件)
[CUDA study notes] First acquaintance with CUDA
为什么毕业季不要表白?
Nuget打包并上传教程
大健云仓冲刺美股:增营收反减利润 京东与DCM是股东