创建用户 并给连接数据库权限 1)create user CJ identified by DSJCJ; 2)grant create session to CJ; oracle11g 用户默认的密码过期时间为 180 天,过期后无法使用,所以 数据创建好后,第一步要去检查一下这个过期时间,如果过期时间有期限,需要修改成 无限期, 查看:Select * FROM dba_profiles s Where s.profile='DEFAULT' AND resource_name='PASSWORD_LIFE_TIME'; 工作常见问题记录(国盛) 修改为无限期:ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

连接方式

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,sal12 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 用户表四张

1)部门表:dept Desc dept; Select * from dept;

2)雇员表emp(一个部门多个雇员)

Select * from emp; 3)工资等级编号(salgrade)

4)工资表(bonus) 该表是空的没任何数据

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 ‘%%’