MySQl DML语句基础(随堂博客)
本博客为课程随堂博客作业,个人技术有限,表达略显抽象,望请见谅
DML是什么
DML代指对表中的数据进行操作的SQL语言
四种基础语句的关键词
插入数据:insert
查询数据:select
更新数据:update
删除数据:delete
SQL语句
查询数据
##查看表所有数据
select * from tb_user
插入数据
#语法:
insert into 表名(字段1,字段2,字段3……) values(字段1值,字段2值,字段3值……)
#注意:
#(1)前后字段 个数、类型必须一样
#(2)数值(整数,小数,)、布尔类型 值正常写;其他类型需要加上单引号“”
#(3)如果插入的 值 是空的 null
一般用写法
##插入数据:1、zsf 18岁、男,1772222,90
insert into tb_user(id,name,age,sex,tel,score) values(1,'zsf',18,'1','17722228888','90')
前字段可以省略
##插入数据前面的字段可以不写
insert into tb_user values(2,'zsf',18,'1','17722228888','90')
一次性插入多条
##一次性插入多条
insert into tb_user values(3,'zsf',18,'1','17722228888','90'),(4,'zsf',18,'1','17722228888','90')
字段缺失写法(空缺字段显示为空(null))
##字段缺失
insert into tb_user(id,tel)values(5,10086)
insert into tb_user(id,name,tel)values(5,null,10086)
更新数据
基础语法
update 表名 set 字段1名字 = 字段1的值,字段2的名字 = 字段二的值 where 条件
#(1)将所有人的成绩设置为100
update tb_user set score=100
条件语法
#(2)将学号是6的学生,成绩设为59;
update tb_user set score=59 where id=6
#(3)将名字为adg的学生,成绩设置为60
update tb_user set score=60 where name='adg'
#(4)将学号是5的学生,成绩设置为99,
update tb_user set score = 99,name = 'xl' where id = 5
变量语法
#(5)将所有人成绩 减少10分
update tb_user set score = score - 10
删除数据
基础语法
#语法
delete from 表名 where 条件
删除表中所有数据
#删除所有数据
delete from tb_user;
*DDl语句*truncate table tb_user;
条件删除
#(2)删除学号为1的数据
delete from tb_user where id=1
#(3)删除名字为ww学生数据
delete from tb_user where name = 'ww'
有关于delete 和truncate的区别
首先delete属于DML(对数据操作)语句,
truncate属于DDL(对数据库、表的操作)语句。
所以delete可以与DML的相关语句进行进阶操作。
同时,如果说delete是将表上的数据用橡皮擦擦去一样
truncate就是用一张新的纸写上未删除的数据。
区别在于delete的删除是有痕迹,有空间占用,可恢复的。
而truncate是清楚占用空间,不可恢复的。