建立数据库用户
新建数据库用户 (在sysdba用户下创建)
create user scott identified by tiger,scott是人名 tiger是密码
给新建用户权限
grant connect,resource to scott;
进入新建用户
conn scott/tiger
执行脚本,Developer里就能使用了
@?/sqlplus/demo/demobld.sql 执行脚本 @是运行的意思 ?是一个路径
查看用户信息
看表在什么位置 比如hr表在什么位置
desc dba_users; select username from dba_users;
select username,ACCOUNT_STATUS from dba_users where username= ‘HR’ 查看状态 是open的话就是解锁了,能进入
解锁语句 alter user XDB account unlock XDB是用户
限定查询
用where实现
查找薪水大于1500的员工
select * from emp where sal>1500;
关系运算符应用
< > <= >= != <> 一般用在where语句后面<>也是不等于的意思
查找薪水不高于2000的员工
select * from emp where sal <= 2000;
查找SMITH这名员工的信息
select * from emp where ENAME=‘SMITH’;
查找薪水在1000到2000之间(包含1000、2000)的员工
select * from emp where sal <= 2000 and sal >=1000;
查找薪水在1000到2000之间(包含1000、2000)的员工 另一种表达方式
select * from emp where sal between 1000 and 2000;
查找工作不是CLERK且薪水超过两千的员工
select * from emp where job!=‘CLERK’ and sal>2000;
查找工作不是CLERK或者SALEMAN 的员工
select * from emp where not (job=‘CLERK’ or job=‘SALEMAN’);
查找10号部门的MANAGER和20号部门的CLERK
select * from emp where (deptno=10 and job=‘MANAGER’) or (deptno=20 and job=‘CLERK’);
筛选年龄是1981年的人
oracle里有专用表达日期的格式 上表的表达方式
筛选日期时字符集需要注意
select * from emp where hiredate between date’1981-01-01’ and date’1981-12-31’;
默认字符集里面存储的是格式,比如日期和中文等
模糊检索
like 模糊查询 %代表任意个数字符 代表单一一个字符
select * from emp where ename like '_____%’; 查找名字中多余五个字符的名字(中间有六个下划线)
查找10号部门的manager和20号部门的clerk,还有工作不是clerk和manager且薪水超过2000的员工,这些员工的名字中有S和K
select * from emp where ((deptno=10 and job=‘MANAGER’) or (deptno=20 and job=‘CLERK’) or (job not in (‘CLERK’,‘MANAGER’) and sal>2000)) and (ename like ‘%S%’ or ename like ‘%K%’);
注意模糊查找时的大小写,不然会报错
排序语法
order by语法 永远 放在后面
薪水从低到高排序
select * from emp order by sal;正序
薪水从高到低排序
select * from emp order by sal DESC;倒序
薪水从高到低排序,如果薪水相同的,按照录用日期从早到晚排序
select * from emp order by sal desc,hiredate;