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>