Oracle学习 sql根本语法(一)
oracle sql语句
-----------sql*plus命令---------------------
该变用户一般有两种方法:
1.退出当前用户 exit
2.切换用户 connect 用户名/密码
connect / as sysdba 切换成超级用户
通常简写:conn /as sysdba
查看当前用户
show user
设置行宽 set linesize 150 默认为80
查看行宽 show linesize
/ 执行上一条sql语句
设置页面大小 set pagesize 100
查看页面大小 show pagesize
显示参数的值:
show 参数值
select * from tab; 查看当前有多少表
注释:
单行--
多行/* */
desc 表名 显示表的结构
--------------
scoot用户的表
dept表:
emp表
清屏 windos中: host cls
linux: host clear
设置某一列的大小
column job format a15 字符形式 / col job for a15
column sal format 9999 数字形式 / col sal for 9999
对于字符串: a20 ,20表示一个数据,有20个字符的宽度
对于数字: 9表示一位,有几位数就是最多显示几位
--------------
空值的处理:
不是一个有效的值,不是0 ,也不是空字符串
所以不能这样写: 某列 !=null
正确写法: ename is not null
含有null的表达式结果为空
edit 修改上一条sql语句;ed[it] 注意: 修改里面的sql语句时不用使用分号
滤空函数:
nvl(表达式,当表达式为空值时使用的值)
select empno ,ename,sal,sal*12 as 年薪 ,nvl(comm,0) 奖金,(sal*12+nvl(comm,0) )总收入 from emp
=====================
对字符串的处理:
是区分大小写的,在使用时要加引号
在制定别名时,引号可以加,也可以不加,
当含有空格,特殊字符时,一定要加引号。
不加引号时,显示都为大写。加上一号后,按自己写的显示;
在使用字符串时,使用单引号,使用别名的时候用双引号;
distinct 去掉重复值
作用于一个列: select distinct job from emp;
作用于多个列 ,所有列的值重复才算重复的记录
: select distinct job,ename from emp;
如果只查询一个表达式,没用到任何表的数据,这时也必须写from子句;
可以写成from dual
dual 是oracle提供的一个虚表,本身存在,可以直接使用;
如: select 3+2 from dual;
select 'hello' || 'world' from dual;
也可以使用concat函数;
select concat('hello','world') from dual;
-----------------------
like
在使用like时,可以使用%与_,分别表示任意数量的任意字符或任意一个字符,
要想表达%或_本身,需要使用转义符,例:
select * from emp where ename like "KI\%%" escape '\';
between and :
select *from emp where sal between 3000 and 4000;(前面是小值,后面是大值,否则没有结果)
or 和in:
select *from emp where empno=7369 or empno=7654 or empno=7934
或
select *from emp where empno in(7369,7654,7934);
in
where ..in (...,.....,....) 如果含有null,没有影响。
例如: 查询所有事经理的员工
select *from emp where empno in (select mgr from emp);
where ...not in (....,...,...) 如果含有null,则不返回任何结果
例如查询所有的不是经理员工
select *from emp where empno not in (select mgr from emp where mgr is not null);