oracle基本的查询语句,Oracle中的基本查询语句总结

1.基本查询语句:

select [distinct] column_name1,.. from table_name where [conditions];

注:distinct关键字是为了去掉重复的记录

2.在sql/plus中设置格式

column column_name Heading new_name;

column column_name format dataformat;

column column clear;

例:col id heading 编号;

3.给字段设置别名

select colum_name as new_name,... from table_name ;

4.逻辑运算符的优先级:按照not、and、or的顺序依次递减

比较运算符的优先级高于逻辑运算符

5.模糊查询:like

通配符的使用:(,%)一个“”代表一个字符,%可以代表0到任意个字符。

例:select u.id,u.username from users u where username like "%b%";

6.范围查询

between..and..

例:select from users where salary between 800 and 2000;

in/not in

例:select from users where username in("aaa","bbb");

7.对查询结果排序:order by

select .. from table_name [where conditions] order by column_name desc/asc,...;

desc:降序 asc:升序

例:select id,username,salary from users where username not in("aaa","bbb") order by salary desc;

8.case...when语句的使用

Case column_name when values then result..[else result ]end;

例1:select username,case username when "aaa" then "计算机部门"

2 when "bbb" then "市场部门" else "其他部门" end as 部门

3 from users;

例2:select username,case when salary<=800 then "工资低"

2 when salary>5000 then "工资高" end as 工资水平

3 from users;

9.distinct可以去除多余的行,

如,查询员工信息表中的部门编号,要求去除重复数据:select distinct deptno from employee

10.使用as关键字设置字段别名的语法:select column_name as new_name,... from table_name;可以同时为多个字段设置别名。

使用column命令设置字段别名的语法:column column_name heading new_name,只能为一个字段设置别名。

11.decode函数的使用

decode(column_name,value1,result1,

...,defaultvalue)

二、操作表中的数据

1.插入语句;

insert into table_name (column1,column2,..) values(value1,value2,..);

例:insert into userinfo values(1,"xxx",123,..);

系统函数:sysdate//获取当前日期

注意:如皋添加的值为字符串类型,要加单引号

2.为表设置默认值

alter table userinfo modify email default "无";

3.复制表

在创建时复制

create table table_new as select column1...from table_old;

例子:create table userinfo_new as select from userinfo;

在插入数据时复制

insert into table_new [column1,...] as select * from table_old;

4.update语句

update table_name set column1=value1,... [where conditions];

例:update userinfo set userpwd="111",email="111@1126.com" where id=3;

5.删除表中的数据(记录)

delete from table_name [where conditions];

delete from userinfo whre id=2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值