`

Oracle学习心得,Oracle使用方法,Oracle教程,Oracle入门教程

阅读更多

SYS change_on_install
SYSTEM manager
----清屏----
clear screen;

用户登录数据库
conn 用户名/密码;
eg:conn scott/tiger;

查看当前用户show user;

给自己修改密码:passw或(password)

运行SQL脚本:
eg:start d:\aa.sql

编辑SQL脚本:
eg:edit d:\aa.sql

将当前的SQL脚本输出到bb.sql这个文本文件
spool d:\bb.sql
spool off;

设置行输出为50个字符
set linesize 50;

设置分页,一般在做报表用
set pagesize 5;

----------用户管理---------
创建用户
create user 用户名 identified by 密码;
eg:create user islee identified by abc123;

修改密码
password 用户名;
eg:password islee;

删除用户
drop user 用户名;

系统分配权限(connect、resource、dba)
grant 权限 to 用户名;
eg:grant connect to islee;---为islee分配连接的权限,否则不可以登录


对象分配权限(select、insert、update、delete、all):用户分配自己的表权限给其他用户使用
grant 权限 on 表 to 用户名;
grant select on emp to islee;

islee使用表:
select * from scott.emp;(方案,一定是scott.emp)

回收权限
revoke
eg:revoke all on emp from islee;


权限传递,islee可以给其他用户再分配emp的select权限
eg:grant select on scott.emp to islee with grant option;

系统权限传递
eg:grant connect to islee with admin option;

创建profile管理用户、密码

create profile 文件名 limit failed_login_attempts 输入密码次数 password_lock_time 禁止登录天数;

eg:create profile lock_account limit failed_login_attempts 3 password_lock_time 2;

将上面的规则应用到用户
alter user 用户名 profile 文件名;
eg:alter user islee profile lock_account;

给用户解锁
alter user 用户名 account unlock;
eg:alter user islee account unlock;

删除profile
drop profile 文件名;


---------对数据表的管理-----------
添加一个字段
alter table student add (classId number(2));

修改字段的长度
alter table student modify (stuName varchar2(50));

修改字段的类型
alter table student modify (stuName char(2));

删除一个字段
alter table student drop column stuAddress;

修改表的名字
rename student to stu;

删除表
drop table student;

日期格式
Oracle 默认的日期格式为'DD-MM-YY',且月一定要有月字
eg:'26-9月-1990'或'26-9月-90'代表1990年9月26日;

修改日期默认格式
alter session set nls_date_format='yyyy-MM-dd';


savepoint aa;
rollback to aa;




测试表student
create table student(
   stuId varchar2(10),
   stuName varchar2(30),
   stuSex char(2),
   stuBirthday date,
   stuDescription varchar2(500)
   );

处理Null值
判断字段money是否为Null,如果是用0代替;
nvl(money,0)

如果查询语句中有order by ,group by, having,则他们的顺序必须是group by→having→order by。

如果子查询返回多列的话,就应该如下:
select * from emp where (deptno,job) = (select deptno,job from emp where ename='SMITH');

分页查询:
第一步:select a1.*,rownum rn from (select * from student) a1;

第二步:select a1.*,rownum rn from (select * from student) a1 where rownum<=5;

第三步:select a2.*,rownum from (select a1.*,rownum rn from (select * from student) a1 where rownum<=5) a2 where rn>=3;

利用查询结果创建表,如stu2,创建的表会复制查询的数据
create table stu2(stuId,stuName,stuScore,stuClassId) as select stuId,stuName,score,stuClassId from student;

Oracle日期格式转换
利用to_date函数转换
eg: insert into student values('200807','小陈','男',to_date('1988-10-25','yyyy-MM-dd'),'To_date函数',68,5);

-------处理日期函数--------

返回系统当前时间
select sysdate from dual;

返回系统当前时间>出生时间200个月的信息,用add_months函数
select * from student where sysdate>add_months(stuBirthday,200);

last_day(d):返回指定日期所在月份的最后一天
查找出生日期为每个月倒数第3天的信息
eg:where last_day(stuBirthday)-3=stuBirthday;

时间格式
yyyy-mm-dd hh24:mi:ss或者yyyy-mm-dd hh12:mi:ss


to_char函数
查找出年为1990的信息
where to_char(stuBirthday,'yyyy')=1990;

查找出月为10的信息
where to_char(stuBirthday,'mm')=10;

 

 

-----------------------更多请关注蕃薯耀新浪博客-----------------------

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2012年9月27日 01:06:52 星期四

1
7
分享到:
评论
1 楼 bbbbb1202 2012-09-27  
看看

相关推荐

Global site tag (gtag.js) - Google Analytics