日期:2014-05-16  浏览次数:20398 次

oracle学习笔记(1)


1、sql*plus常用命令
1)disc[onnect]? 用来断开与当前数据库的连接
2)passw[ord]? 用来修改用户的密码,如果想修改其它用户的密码,需要用sys/system登陆
3)show urser? 显示当前用户名
4)exit 断开连接,同时退出
5)&? 可以替代变量,而该变量在执行时,需要用户输入。
?? select * from emp where job="&job"
6) eidt? 可以编辑指定的sql脚本
?? edit d:\a.sql
7) spool 将slq*plus 屏幕上的内容输出到指定的文件中去。
?? spool d:\b.sql 并输入 spool off????
8) 设置行的大小默认80:set linesize
9) 连接命令:conn system/netjava
10)运行脚本start和&:start d:\a.sql或者& d:\a.sql
11) set timing on 显示操作所耗时间
2、用户管理
1) 创建用户(只有sys和System具有权限) create user champion identified by netjava;
2)修改密码? alter user champion? identified by "champion"

3) 删除用户 drop user champion
4) 赋予用户权限grant(角色:connect、dab、resource等,对象权限:select、update、delete、insert等)
?? 系统权限:grant connect to champion
?? 对象权限:grant select on emp to champion
???????????? grant all on emp to champion
5)收回权限 revoke select on emp form champion
6) 权限传递
?? 对象权限:grant select on emp to champion with grant option
?? 系统权限:grant connect to champion with admin option?
7) 账户锁定: profile 管理用户口令
?? 创建profile 文件:create profile lock_account(可以改) limit failed_login_attempts 3(可以改) password_lock_time 2(可以改);
?? 给用户配置文件:alert user champion profile lock_account;
8)账户解锁:alert user champion account unlock;
9) 终止口令:定期要用户修改密码
?? 创建profile文件:create profile myprofile limit password_life_time 10 password_grance_time 2
?? 给用户配置文件:alert user champion profile myporfile
10)口令历史:密码在定期不可以重复使用
?? 创建profile文件:create profile myprofile limit password_life_time 10 password_grance_time 2 password_reuse 10
?? 给用户配置文件:alert user champion profile myporfile???
11)删除profile
?? drop profile myprofile [cascade]
3、数据类型
1)字符型
?? (1)char 定长 最大2000字符 如:char(10),若存放“冠军”,则前四个字符放“冠军”,
??????? 后添6个空格,查询时间快,因为它先比较长度是否相等
?? (2)varchar2(20)? 变长 最大4000字符,可以节省空间
?? (3)clob 字符型大对象,最大4G
2) 数字类型
?? number 范围-10的-38次方至10的38次方,可以表示小数,也可以表示整数
?? number(5,2)表示一个小数有5位有效数字,2位小数,范围 -999.99-999.99
?? number(5) 表示一个五位整数,范围-99999-99999
3)日期类型
?? (1)date 包含年月日时分秒? 默认格式:‘DD-MON-YY’ ‘16-8月-89’
??????? 修改默认的格式 alter session set nls_date_format='yyyy-mm-dd'
?? (2)timestamp 对date的拓展,精度高一点,微秒级
4)图片类型
?? blob 二进制数据 可以存放图/声音 4G
4、表的操作
?? 1)写日志与数据恢复
????? 设置回滚点:savepoint a
????? 回滚到a点: rollback to a;
?? 2) truncate table student;删除表中的所有记录,表结构还在,不写日志,
????? 无法找回删除的数据,删除速度很快。
?? 3)简单查询
???? (1)select count(*) from student 查看表中的记录数
???? (2)select distinct name from student 查询表中不重复的名字
???? (3)select sal*12+nvl(comm,0)*12 from emp 如果comm为null,则用0来替代
????? (4) select ename? from emp where ename like 'S%' 查找名字以S开头的员工
?????????? % :表示任意0到多个字符,_:表示单个字符
???? (5)select empno , ename from emp where empno in(200,300,400) 查找职工号为200,300,400的员工
???? (6)select ename from emp where sal is null;
????? (7) select * from emp order by sal; 按工资从低到高排序
????? (8) select * from emp order by sal desc ;从高到低排序
????? (9) select * from emp order by deptno,sal desc;
????? (10)select ename ,