数据库------DML操作

DML的操作

数据操纵语言,指的是对表中的数据的增、删和改操作

添加数据 insert into

【不推荐】方式一:

insert into <tableName> values ( val... )

val值的顺序必须和建表的时候字段的顺序保持一致、且个数和字段个数必须相同
缺点: 1.值顺序和表字段顺序必须一致,2.表中所有字段必须插入值

【推荐】方式二:

insert into <tableName>(column...) values(val...)

column设置插入的字段列表,多个值用逗号分割, val插入值的顺序和 column定义的顺序保持一致即可,个数一致

方式三(批量插入):

insert into <tableName>(column...) values 
(val1...) , 
(val2...) ,
(val3...) ,
...
(valn...) ;

优点:可以同时插入多条记录、缺点: SQL语句可能过长

删除数据 delete

delete from <tableName> [where 条件] ;

不推荐使用 delete from 表 删除所有数据, 如果确实需要删除表中所有数据,推荐使用 truncate table <tableName>

truncate table 和 delete 删除表中所有数据的时候区别

  • 会删除表中所有的数据和 数据占用的空间
  • delete 只删除数据

条件

条件 使用 where 关键字

  • 关系条件查询
    > , >= , < , <= , != (不等), <> (不等) , = (等于)

  • 逻辑条件查询
    and (逻辑与) , or (逻辑或)

delete from animal where name = '雪橇犬5' and color = '白色' ;
  • 区间查询 between … and
-- 删除 成绩 在 80 ~ 90 之间的内容 

delete from student where score between 80 and 90 ;

delete from student where score >= 80 and score <= 90 ;

  • 枚举查询 in
delete from animal where color in ('黑色',  '白色');
  • 模糊查询 like

% : 用来匹配 0 ~ N 个字符
_ : 用来匹配 1个字符

- 删除 名字 姓张 的同学 

delete from student where name like '张%' ; 

-- 删除 名字 姓张、且 两个字的同学 
delete from student where name like '张_' ; 

-- 删除名字中包含 三的学生 

delete from student where name like '%三%' ;

- 删除名字 以  三 结尾的学生 

delete from student where name like '%三' ;
  • 非/空值查询 is [not] null

修改数据 update

update <tableName> set <columnName> = <val> , ... <columnName> = <val> [where 条件] ;

查看表中所有数据

select * from <tableName>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值