当前位置:文档之家› oracle笔记

oracle笔记

Oracle笔记一、了解oracle二、Oracle的安装三、启动oracle:(1)、使用oracle中自带的sql*plus工具进入oracle。

(2)、利用sqlplus和sqlplusw打开oracle。

(3)、使用sql/pl developer进入oracle.(4)、使用oracle企业管理器四、常用sql*plus命令:(1)、连接数据库: conn (ect) 用户名/密码@网络服务名(数据库名) (as sysdba/sysoper);例如:conn scott/tiger;(2)、退出:exit;(3)、运行sql脚本: start 脚本地址。

例如:start d:\test.sql;(4)、修改sql脚本: edit 脚本地址。

(5)、截取屏幕代码spool,例如:spool d:\demo.txt…………….spool off;(6)、创建用户(以sysoper以上权限创建):create user 用户名identified by 密码;(7)、删除用户(以sysoper以上权限删除):drop user 用户名;(8)、修改用户密码:passw(ord)或alter user 用户名identified by 密码。

(9)、显示当前用户名:show user;(11)、断开数据库连接:disc(onnect);(12)、行距与每页显示次数:set linesize(pagesize) 数目。

(13)、给予权限:grant connect(连接权限)/resource(在system中创建表权限)/dba(最高权限) to 用户名with admin(系统权限下传)/grant(对象权限下传) option ;(14)、收回权限:revoke conn(连接权限)/resource(在system中创建表权限)/dba(最高权限) from 用户名(15)、使用profile文件管理用户:限定用户登录次数:Create profile 文件名limit failed_login_attempts 3 password_lock_time 2;Alter user 用户名profile 文件名用户解锁:Alter user 用户名account unlock;终止口令:Create profile 文件名limit password_life_time 10 password_grace_time 2;Alter user用户名profile 文件名六、简单sql语句:(1)建表(也可利用pl/sql developer进行表创建):Create table 表名(属性1,属性2,属性3….);例如:create table student(stuName varchar2(20),age number(3));(2)、删除表:Delete * from 表名(此操作只是删除表中数据,并且数据库会进行写日志。

因此可以保存节点来进行恢复)Drop table 表名(删除表的数据和结构)Truncate table 表名(删除表中的数据,不写日志)(3)、保存节点及恢复节点信息:Savepoint 节点名;Rollback 节点名;(4)、变量介绍:数值型:number(个数,小数点位置):例如:number(5,2)范围为-999.99~999.99字符型:Char(),固定长度字符串,用户需要频繁搜索。

Varchar2(),可变长度字符串,节省存储空间。

Clob(),超大字符串。

时间日期:Date(),系统默认格式为dd-mm-yy,可以通过set nls_date_format来进行格式设定。

Timestamp(),时间戳,相比date()更精确,多用于银行等高精度的地方。

图片音乐存储:Glob()(5)、insert语句:Insert into 表名(属性1,属性2,属性3….) values (值1,值2,值3,….);例如:Insert into student (stuName,age) values(…zy‟,21);(6)、修改语句:Update 表名set 属性名=属性值where 条件;(7)、删除语句:Delete from 表名where 条件;(8)、查询语句:Select (属性1,属性2,属性3….) from 表名where 条件;(9)、为属性取别名:Select 属性1 别名,属性2 别名from 表名where 条件;(10)、取消重复列distinct:Select distinct 属性1,属性2 from 表名where 条件;(11)、查看表结构:Desc 表名;七、复杂查询(1)、使用算数表达式,例如:Select sal*13 "年薪"from emp;Select sal*13+nvl(comm,0)*13 "年收入" from emp;注:nvl()函数的意思为:若comm为null.则赋值为0,若不为null,则为原数。

(2)、null条件的查询:Select 属性1 from 表名where 属性is null( is not null);(3)、where中的like语句:%,匹配任意多个字符,_匹配任意单个字符例如,查询以s开头的姓名的人的名字;Select ename from emp where ename like 's%';(4)、where中使用in:例如:Select * from emp where sal in (2500,3000,3500);(5)、where中查询date数据;例如:Select * from emp where date>'日-月-年";注意:我们可以通过set nls_date_format来进行date数据的格式化,但是查询时,我们仍然需要用day-month-year的格式来进行查询。

(6)、条件连接符:and、or等八、复杂查询(2)(1)、select table1.ename,table1.sal,table2.dname from emp table1,dept table2 where table1.deptno=table2.deptno ;(2)、select table1.ename,table1.sal,table2.dname from emp table1,dept table2 where table1.deptno=table2.deptno and table1.deptno=10;(3)、select table1.ename,table1.sal,table2.dname from emp table1,dept table2 where table1.deptno=table2.deptno and table1.deptno=10;(4)、select table1.ename,table1.sal,table2.dname from emp table1,dept table2 where table1.deptno=table2.deptno order by table1.deptno;select a1.ename "员工",a2.ename "上级" from emp a1,emp a2 where a1.mgr=a2.empno;select ename from emp where deptno=(select deptno from emp where ename='SMITH');select a1.ename,a1.sal,a1.deptno from emp a1,(select job from emp where deptno=10) a2 where a1.job in a2.job;select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);select ename,sal,deptno from emp where sal > all(select sal from emp where deptno=30);select ename,sal,deptno from emp where sal > any(select sal from emp where deptno=30);select ename,sal,deptno from emp where sal > (select min(sal) from emp where deptno=30);select * from emp table1,(select ename,job,deptno from emp where ename='SMITH') table2 where table1.job=table2.job and table1.deptno=table2.deptno;select * from emp where (job,deptno)=(select job,deptno from emp where ename='SMITH');select table1.ename,table1.sal,table1.deptno from emp table1,(select avg(sal) money,deptno from emp group by deptno) table2 where table1.sal>table2.money and table1.deptno=table2.deptno;select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=10) a2 where a2.rn>=5;create table demo2 (id,empno,job) as (select empno,ename,job from emp);Union:select * from emp where deptno=20 union select * from emp where sal>=2500;Union all:(不去掉重复项)select * from emp where deptno=20 union all select * from emp where sal>=2500;Intersect:select * from emp where deptno=20 intersect select * from emp where sal>=2500;Minus:select * from emp where deptno=20 minus select * from emp where sal>=2500;九、创建数据库:使用DBCA来进行数据库的创建。

相关主题