摘要:Oracle数据库的安装完成后:Oracle自动会生成sys和system两种用户。
1.1 Oracle的概述
Oracle数据库的安装完成后:Oracle自动会生成sys和system两种用户。
Sys:是超级用户,拥有至高无上的权利,具有sysdb的角色,可以createdatabase,该用户的默认密码是manager;
System:是管理员操作,具有sysoperate角色,不可以createdb,该用户默认密码是change_on_install.
连接命令:conn system/manager
断开命令: disc
修改密码:password
显示用户:show user;
退出命令:exit;
文件操作:
运行脚本:Start d:\\liu.txt;
编辑脚本: edit d:\\liu.txt;
将屏幕内容输出到指定文件中:
Spool d:\\liu.txt;
1.2 Oracle的常用命令
1. 查询表的结构:
Desc dempt;
2. 去掉重复的列:
Select distinct name,age from dempt;
3 null值如何处理:
使用nvl函数来处理;
3. 查询年龄大于1982-09-08的姓名:
Select name from emp where bir>’8-9月-1982’;
4.查询首字母为s的姓名:
Select name from emp where name like ‘s%’;
5 查询第三个字母为o的姓名:
Selectname from emp where name like ‘_ _o%’;
6 查询name为空的语句:
Select* from emp where name is null; Select* from emp where name =null;错 Select * from emp where name=””l; 错 Select * from emp where name is not null; 不为空:is notnull;
7 order by 中的别名需要加“”
Select salary*12 as “年薪” from emp order by “年薪”;
8 group by 对查询的结果进行分组,having 用于限制分组结果;
Group by xxx,xxx必须在select xxx中出现;
9 多表查询: 表名必须以字母开头;
1.3 表的管理
1.基本数据类型:
字符类型:varchar2(),最大4000字符型;char,最大2000字符;clob 最大4g
数据类型:number(5,2)5位有效数字,2位小数;
日期类型:date timestamp;
图片类型:blob
2.修改表的结构:
Alter table liu add (classid number(2)); Alter table liu modify(classname varchar2(34)); Aleter table liu drop column classid; Drop table liu; Rename liu to ljf;
修改日期的格式
Alter session set nls_data_format=’yyyy-mm-dd’;
3.几种删除的区别:
Delete from liu;数据删除,结构存在,日志存在,可以恢复 Drop table liu;结构数据均删除 Truncate table liu;数据删除,结构存在,不可恢复; Save point aa; Delect * from liiu; Rollback to aa;
1.4 常用查询语句
1.insertinto tb_start_order values(null,'100171','100282','100289',to_date('2014-06-23 08:02:06','yyyy-mm-dd HH24:MI:SS'),'213','0.01%');
2.查询某一个数据库中多少个表:
select * fromuser_tables;
3.oracle的分页:
select * from (select a.*,rownum rn from (select * fromtemp) as a where rn<=3)where rn>=1
4.修改成自己习惯的日期格式
To_date(‘1989-05-03’,’yyyy-mm-dd’)
5.从一个表查询一些字段创建新表:
Create table liu (id,name,age)as select name ,age fromtemp;
6.查询出工资比部门30的所有员工的工资都高的员工姓名,工资和部门号:
Selectsal,name,dpo from temp where sal>all (Select sal from temp where dpo=30)
7.子查询:单行查询:
select * from temp where pno=(select pno from temp where name=’smit’);
多行查询:
select * from temp where job in (select dictinetjob from temp where pno=10);
8.高于自己部门的平均的工资:
Select a2.pno,a2.sal,a1.mysal from temp a2,(Selectpno,avg(sal)as mysal from temp group by pno) a1 where a2.pno=a.pno anda2.sal>mysal;
9.分页查询:
select*from(select a.*,rownumas rn from(select*from CVMS_START_WORK_ORDER) a whererownum<=2)where rn>=1; select*from (select d.*,rownumas rn from cvms_start_work_order d where start_time>=to_date('2014-06-29','yyyy-mm-dd')andrownum<10)where rn>=1;
10.向另外一个表中插入多条数据:(一条insert 可以插入大量的数据)
insertinto tb_jf (age)select age from tb_ljf;(注意没有values)
11.向带有日期的表中插入数据:
Insert into temp values(‘liu’,23,45,to_date(‘1989-05-03’,’yyyy-mm-dd’))
12.问题:希望员工SCOTT 的岗位、工资、补助与SMITH 员工一样。
update emp set(job, sal, comm)=(select job,sal, comm from emp where ename='SMITH') whereename='SCOTT';
1.5 Oracle的事务管理
事务管理:
事物:保证数据一致性,由一组相关的dml语句组成;
锁:作用于被操作的表中,防止其他用户操作此表;
1.6 通过java api 修改程序
16. // 2.得到连接
17.
ct = DriverManager.getConnection(
18.
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "s cott", "tiger");
20. // 加入事务处理
21. ct.setAutoCommit(false);// 设置不能默认提交
23. Statement sm = ct.createStatement();
25. // 从scott 的sal 中减去100
26. sm.executeUpdate("update emp set sal=sal-100 where
ename='SCOTT'");
28. int i = 7 / 0;
30. // 给smith 的sal 加上100
31. sm.executeUpdate("update emp set sal=sal+100 where
ename='SMITH'");
33. // 提交事务
34. ct.commit();
sm.close();
38.ct.close();
39. } catch (Exception e) {
40.// 如果发生异常,就回滚
41.try {
42.ct.rollback();
43. } catch (SQLException e1) {
44.e1.printStackTrace();
45. }
46. e.printStackTrace();
47. }
学习Oracle数据库记录笔记由职坐标整理并发布。了解更多详情,请关注职坐标Oracle数据库频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号