Oracle中的SQL语句(此篇重点介绍DML)

1.DML=Data Manipulation Language 数据操纵,由DBMS提供,实现对数据库中数据的操作。DML分为两种,过程性DML和非过程性DML。
非过程化语言就是它一次处理一个记录,对数据提供自动导航,不对数据结果做过多的处理,使得用户更容易得到结果。
过程化语言就是通过SQL特定的语言对结果集进行特殊的处理,使得结果更符合用户的要求。
insert
insert into student values(3,‘liming’,0);
insert into student(id,name) values(4,‘liming2’);
update
update student set name=‘zhangwei’,sex=1 where id=2;
delete
delete from student where id=1;(删除指定行)
delete from student;(全部删除)
where
where子句用于过滤记录,并提供运算符
在这里插入图片描述
另外where中还常用到and 且 or 或,通常like会配合统配符%和_一起使用,前者匹配多个字符,后者匹配一个字符。
select
含义:用于从数据库中选取数据,结果被存储在一个结果集中,常见写法如下:
select * from student where 1=1;
select id,name from student where 1=1;
select distinct name,sex from student where 1=1;
distinct用于去重
as
给字段或表取别名(可用as,或不写,建议不写)
给字段取别名:
select name n from student where 1=1 或
select name as n from student where 1=1
给表取别名:
select * from dog a
left join student b
on a.sid=b.id
where 1=1;
这里就不能像上面那样也写as,会报错,所以建议都不要写as,直接空格就好了。
运算符
select 1+2-3*4/5 result from dual;
在这里插入图片描述
连接运算符:你可以concat函数,在Oracle里也有concat,但是它只能有两个参数,Oracle里连接可以使用双竖杆||
select concat(name,sex) from student where 1=1
select name||sex from student where 1=1
在这里插入图片描述
order by
order by用于对结果集按照1个列或多个列进行排序,默认按升序ASC,如果要降序使用DESC关键字
select * from student order by id desc;
like
模糊查询,尤其值得注意的是:通配符%替代0个或多个字符;_下划线替代1个字符
select * from student where name like ‘%z%’;
select * from student where name like ‘%s_n’;
in
限定集,允许你在where子句中规定多个值
select * from student where id in(1,3);
select * from student where not id in(1,3);
between
区间集,允许你指定一个条件区间(包左也包右)
select * from student where id between 1 and 3;
select * from student where not id between 1 and 3;
null
null值代表未知数据,它不等同于0,通过is null来判断是否为空
select * from student where name is null;
select * from student where name is not null;
case when then else end
譬如通常性别都用1位number存储0或1,0代表男,1代表女,节省内存,那取数据的时候怎么映射成男或女呢?就用到case函数了。
select name,case sex when 0 then ‘男’ else ‘女’ end sex
from student where 1=1;
在这里插入图片描述
insert into select
从一个表中复制所有的列到另一个表中,前提当然得是这两个表结构相同;
从一个表中赋值某列到另一个表中。
insert into table2 select * from table1;
insert into table2(id,name) select id,name from table1;
join
SQL join用于连接多张表,基于关联字段的多表查询
主要有以下四大类型:
INNER JOIN:内连接
LEFT JOIN:左连接
RIGHT JOIN:右连接
FULL JOIN:全连接
这个不举例了,用的比较多,尤其是涉及多对多的关系时,通常有一个关联表,这时通过三表查询可以得到最全的结果集。有必要提的是,内连接,其实就是自身关联自身,有一个例子,省市二级联动,可以只需要设计一张表,通过pid字段关联自身的id,如果pid为null说明其为省份,如果pid不为null,则其为市。但是三级联动时,就必须有多张表了。
union
union操作符用于合并两个或多个select语句的结果集,union内部的每个select语句必须拥有相同数列的列。列也必须拥有相似的数据类型,同时每个select语句中列的顺序也必须相同。union和union all是有区别的,前者是两张表的去重了的结果集,后者是所有的。
select * from table1
union
select * from table2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值