oracle简单使用

创建用户 并给连接数据库权限
1)create user CJ identified by DSJCJ;
2)grant create session to CJ;

连接方式
oracle简单使用

Oracle简单使用

Emp数据表

1.sqlplus命令

1)Scott用户登录:Sqlplus scott/tiger
查询emp表的全部内容
Select * from emp;
问题:
(1)表格的显示格式混乱
(2)所有表格都有标题行
设置每行显示的数据长度:set linesize 300;
设置每次显示胡行数:set pagesize 30;

再执行 select * from emp;

命令格式:col job for a8; 列空格设定8

Col job for a8;
select * from emp;

2)调用记事本:
利用“ed”命令启动本机记事本程序(例如notepad,vim)

ed hello;(在打开的hello.sql中编写语句然后保存,一般保存在C:\Users\Administrator\hello.sql)
@hello; (执行@文件名)
利用@指定也可调用磁盘上的文件,如果后缀也是“*.sql”也可以不用写后缀,否则都要写

@d:aa ; (执行d盘中aa.sql文件)
Ed d:aa ; (打开该文件)

Ed a.txt (打开记事本,写好)
@a.txt (执行要加后缀名)

3)显示用户Show user
切换用户:Conn 用户名/密码
切到sys用户 Conn sys/change_on_install as sysdba

     Select  *  from  emp;查询不出  因为该表属于scott用户 为:emp.scott
     用  select  *  from emp.soctt
   Show  user
   Conn sh

4)调用本机程序
不使用用户登录:Sqlplus /nolog
在sqlplus中考虑到用户可能使用系统命令,所以使用到一个host命令,
拷贝文件:copy 原文件 目标位置文件
Copy d:aa.sql c:1.sql; (不在用户中拷贝,在nolog中拷贝)

Sqlplus scott/tiger
Host copy d:aa.sql d:bb.sql ; (在sqlplus中拷贝用host)

2.简单查询

1)[数据的投影操作]---指查询需要的数据列 查询员工编号,姓名,工资,职位
Select empno,ename,sal,job from emp;
除了基本查询列之外,在简单查询中也支持四则运算。
查出编号,姓名,并计算年薪,如下:
Select empno,ename,sal12 from emp;
Select empno,ename,sal
12 income from emp;
Select empno 员工编号,ename 姓名,sal*12 年薪 from emp;

2)连接查询--描述
Select empno || ename from emp;
可以将连接优化下,中间使用文字描述,“编号:XXX”
普通数字:直接写 字符:加单引号
Select ename|| 1 from emp;
Select empno||’hello’ emp;

Select ‘编号:’||empno||’姓名:’||ename from emp;

3)简单查询--消除掉重复内容:
Select job from emp;
Select distinc job from emp;(消除相同的职位)
Select distinc ename,job from emp;(这两列所有数据都存在,在这distinc比较的是姓名和职位没有同时重复的存在)

3 scott 用户表四张

oracle简单使用
1)部门表:dept
Desc dept;
oracle简单使用
Select * from dept;
oracle简单使用

2)雇员表emp(一个部门多个雇员)
oracle简单使用

Select * from emp;oracle简单使用
3)工资等级编号(salgrade)
oracle简单使用

oracle简单使用

4)工资表(bonus)
oracle简单使用
该表是空的没任何数据

4 sql限定查询(and or)
查询工资大于1200:Select * from emp where sal >1200;

工资大于1200总人数:Selelct count(*) from emp where sal >1200;

工资小于3000且不是办事员:Select * from emp where job <>’CLERK’ AND sal <3000;

Betwneen..and 范围
工资在1500到3000雇员
Select from emp where sal >1500 and sal<3000;
Select
from emp where sal between 1500 and 3000;

查询1981年雇佣雇员信息:
Select * from emp where hiredata between ‘01-1月-1981’ end ‘31-12月-1981’;

查询所有领取佣金的雇员信息(comm字段佣金),查询不是空

Select * from emp where comm is not null ;

4 sql限定查询(and or)
查询工资大于1200:Select * from emp where sal >1200;

工资大于1200总人数:Selelct count(*) from emp where sal >1200;

工资小于3000且不是办事员:Select * from emp where job <>’CLERK’ AND sal <3000;

Betwneen..and 范围
工资在1500到3000雇员
Select from emp where sal >1500 and sal<3000;
Select
from emp where sal between 1500 and 3000;

查询1981年雇佣雇员信息:
Select * from emp where hiredata between ‘01-1月-1981’ end ‘31-12月-1981’;

查询所有领取佣金的雇员信息(comm字段佣金),查询不是空

Select * from emp where comm is not null ;

IN操作符
查询雇员编号7369,7566,7788员工信息
Select * from emp where empno IN(7369,7566,7788);

查询雇员编号不是7369,7566,7788的员工信息
Select from emp where empno NOT IN(7369,7566,7788);
Select
from emp where empno not IN(7369,7566,7788,null);错的not in中不能有null

模糊查询like
查询所有雇员姓名中以字母开头为A的员工:
Select * from emp where ename like ‘A%’; 第一个字母为A

Select * from emp where ename like ‘_A%’; 第二个字母为A

Select * from emp where ename like ‘%A%’; 有A的

如果like查询没有设置关键字表示查询全部
Select from emp where ename like ‘%%’ 名字不管是什么全部
Select count(
) from emp where ename like ‘%%’

转载于:https://blog.51cto.com/12346621/2149669

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值