当前位置:网站首页>[华为云在线课程][SQL语法分类][数据操作][学习笔记]
[华为云在线课程][SQL语法分类][数据操作][学习笔记]
2022-08-09 10:36:00 【华为云】
1.数据操作
1.1.数据插入
- 功能描述
- 在表中插入新的数据。
- 注意事项
- 只有拥有表insert权限的用户,才可以向表中插入数据。
- 如果使用returning子句,用户必须要有该表的select权限。
- 如果使用query子句插入来自查询里的数据行,用户还需要拥有在查询里使用的表的select权限。
- insert事务提交是默认开启的。
- 语法格式
- insert语句有三种形式。
- 值插入,构造一行记录并插入到表中。
insert [ignore] [into] table_name [partition (partition_name [,partition_nam] ...)] [(col_name [,col_name] ...)] [values|value] (expression [,...])
- 查询插入,通过select子句返回的结果集构造一行或多行记录插入到表中。
insert [ignore] [into] table_name [partition (partition_name [,partition_nam] ...)] [(col_name [,col_name] ...)] [as row_alias[(col_alias [,col_alias] ...)]] select_clause
- 先插入记录,如果报主键冲突错误则执行update操作,更新指定字段值。
insert [ignore] [into] table_name [partition (partition_name [,partition_name] ...)] [as row_alias[(col_alias [,col_alias] ...)]] [on duplicate key update] set assignment_list
- 示例:向表training1中插入数据。
- 创建表training1。
create table `training1`( `staff_id` int not null , `course_name` char(50), `exam_date` datetime, `score` int);
- 值插入:向表training1中插入一条记录。
insert into training1(staff_id, course_name, exam_date, score) values(1,'information safety','2022-08-09 06:13:00',95);
- 查询插入:通过子查询向表training1中插入training表中的所有数据。
insert into training1 select * from training;
- 主键冲突错误,执行update操作。
-- 创建主键alter table training1 add primary key (staff_id);-- 插入记录insert into training1 values (1,'information1','2022-08-09 06:16:00',97) on duplicate key update course_name='information1',exam_date= '2022-08-09 06:16:00',score=97;
- 创建表training1。
1.2.数据修改
- 功能描述
- 更新表中行的值。
- 注意事项
- update事务提交是默认开启的。
- 执行该语句的用户需要有表的update权限。
- 语法格式
update table_reference set { [col_name=expression][,...]|(col_name[,...])=(select expression[,...]) } [where condition]
- table_reference子句
{ table_name | join_table }
- join_table子句。
table_reference [left [outer] | right [outer] | inner] join table_reference on condition_expr
- 其中,只有在使用join_table子句时支持使用(col_name[,…])=(expression[,…])。
- 注意:在安全模式下,不使用where条件子句或不使用limit限制行数,无法进行update和delete操作。
- table_reference子句
- 示例:更新表training中id和表education中staff_id相同的记录,修改first_name为其他值。
- 删除表education、training。
drop table if exists education,training;
- 创建表education、training。
create table education(staff_id int primary key ,first_name varchar(20));create table training(staff_id int primary key ,first_name varchar(20));
- 插入记录。
insert into education values (1,'alice'),(2,'brown');insert into training values(1,'alice'),(1,'alice'),(1,'alice'),(3,'bob');
- 更新表training中staff_id和表education中staff_id相同的记录的first_name字段。
update training inner join education e on training.staff_id = e.staff_id set training.first_name='alan';
- 删除表education、training。
1.3.数据删除
- 功能描述
- 从表中删除行。
- 注意事项
- 执行该语句的用户需要有表的delete权限。
- delete事务提交是默认开启的。
- 语法格式
delete from table_name[where condition][order by {column_name [asc|desc][nulls first|nulls last]}[,...]][ limit [start,] count | limit count offset start | offset start[ limit count]]
- 删除表中与另一个表相匹配的行记录
delete table_ref_list from join_table
- 或者
delete from table_ref_list using join_table
- 删除表中与另一个表相匹配的行记录
- 示例:删除表中training中staff_id为10且用户名为"information safety"的培训记录。
- 删除表training。
drop table if exists training;
- 创建表training。
create table training( staff_id int not null , course_name char(50), exam_date datetime, score int);
- 向表training中插入记录。
insert into training values (10,'sql1','2022-08-09 06:53:00',90), (10,'information1','2022-08-09 06:53:00',95), (10,'master1','2022-08-09 06:53:00',97);
- 删除表training中同时匹配course_name='information safety’和staff_id=10的记录。
delete from training where course_name='sql1' and staff_id=10;
- 删除表training。
1.4.思考题
- 使用sql命令将staffs表中员工的年龄age字段的值增加5岁,应该使用命令:update age set age=age+5
- 下面四组sql命令,全部属于数据操作语言的命令是:insert,update,delete
- 删除表student中班级(cid)为6的全部学生信息:delete from student where cid=6;
边栏推荐
猜你喜欢
MySQL执行过程及执行顺序
非科班毕业生,五面阿里:四轮技术面+HR一面已拿offer
[Halcon&定位] 解决Roi区域外的模板匹配成功
Redis + NodeJS 实现一个能处理海量数据的异步任务队列系统
批量转换经纬度的网页实现方法
ESIM(Enhanced Sequential Inference Model)- 模型详解
Probably 95% of the people are still making PyTorch mistakes
shap库源码和代码实现
3D printed this DuPont cable management artifact, and the desktop is no longer messy
今天做了手机播放器的均衡器
随机推荐
用Word写代码
numpy的ndarray取数操作
unix环境编程 第十五章 15.9 共享存储
Master-slave postition changes cannot be locked_Slave_IO_Running shows No_Slave_Sql_Running shows No---Mysql master-slave replication synchronization 002
numpy库中的函数 bincount() where() diag() all()
使用.NET简单实现一个Redis的高性能克隆版(四、五)
cesium加载地图
Cpolar内网穿透的面板功能介绍
[贴装专题] 贴装流程中涉及到的位置关系计算
unix系统编程 第十五章 15.2管道
15.8 the semaphore Unix environment programming chapter 15
[相机配置] 海康相机丢包配置环境
[项目配置] 配置Qt函数库和ui界面库的封装并调用的项目
10000以内素数表(代码块)
机器学习--线性回归(Linear Regression)
单元测试2之实际结果检查的引用
上传张最近做的E2用的xmms的界面的截图
unix环境编程 第十五章 15.10 POSIX信号量
shap库源码和代码实现
MySQL执行过程及执行顺序