oracle

第一章 用户管理

//2014417

oracle40多种权限。

connect是权限问题,如省长,

 

权限

系统权限:描述的用户对数据库的访问的权限

对象权限(数据对象(用户的表,视图,存储过程,触发器)权限):用户对其他用户的数据对象操作的权限。

create ression

角色:事先定义一些角色,如connect 是一种角色,具有一些特定的权限(系统权限,)。140多种系统权限,用户有40权限,

自定义角色:自己写的角色,是给角色需要的权限

预定义角色:这个角色在安装数据库的时候已经定好,connect角色。

登陆系统:

第一步是直接输入用户名和密码登陆

第二步,在system用户下创建一个用户:create user wly identified by 123;

第三步:换用户:conn wly/123;

第四步:第三步无法实现的时候,这里主要原因是权限问题:因此要重新登陆:conn system/123;

第五步:赋予权限:grant connect to wly;

第六步:conn wly;

 

 

建立table操作步骤:

conn wly;

select * from exp;//会显示表或视图不存在

create table test(userId varchar(20),userName varchar(30));

//会显示权限不够。

conn system/123;

grant resource to wly;

conn wly/123;

create table test(userId varchar(20),userName varchar(30));

select * from test;

desc test;//可以看到所有的表的参数和类型,属性

 

 

当前用户访问其他用户(wlytest)的数据的操作

 

对数据库的操作有:select insert,update,delete,all,create index...

对用户的权限设定:grant select on emp to wly;

conn wly/123;

select * from wlytest.emp;

desc wlytest.emp;//显示wlytest用户的emp信息。

 

举一反三:其他权限设定 grant update/delete/select/insert/... on emp to wly;

gratn all on emp to wly;

 

收回权限:revoke select/update/delete/insert/... on emp from wly;

  revoke all on emp from wly;

 

总结:给用户访问另外用户的表格,表格的主人或者system用户都可以

给用户赋予访问,修改表格的权限。但是谁赋予的权限,撤销或收回时

需要谁去弄。

 

对权限的维护:

需求:wly用户既要有访问,修改等权限来访问wlytest的表emp,同时更有

wly拥有的权限传递给其他用户。

-->如果是对象权限,就加入with grant option

grant select on emp to wly with grant option

 

wlytest>grant select on student to wly with grant option;

wlytest>conn wly/123;

wly>grant select on student to wlytest2;

wly>conn wlytest2;

wlytest2>desc wly.student;//此时无法访问。

wlytest2>desc wlytest.student;

 

注意:如果wlytest把权限给(with grant option->wly->wlytest2

就像中央给招聘省长,省长在位时招聘县长,某天省长的权利失去了,

县长的也会丢掉职位吗?不会。

 

wlytest>grant select on emp to wly with grant option

wlytest>conn wly/123;

wly>grant select on emp to wlytest2;

wly>conn wlytest2/123;

wlytest2>desc wlytest.emp;

wlytest2>conn wlytest/123;

wlytest>revoke select on emp to wly;

wlytest>conn wlytest2/123;

wlytest2>desc wlytest.emp;//显示emp表中的信息依然存在。

 

profile管理用户口令

sql>create profile (lock_account) limit failed_login_attempts (3) password_lock_time (2);

用户密码连续输入3次错误就会被锁掉,在这里锁定2天。

sql>alter user tea profile lock_account;

 

所有的规则放在lock.account文件里面,

oracle是连接服务器的,远程连接的。改了系统时间也不行????

 

如何解锁?

sql>alter user tea account unlock;

如何让用户定期修改密码:

sql>create profile myprofile) limit password_life_time 10) password_grace_time 2;

其中102,均表示的是天数。

sql>alter user tea) profile myprofile;

这个功能主要是对系统管理员而言的,主要为了安全起见。对客户无价值。

 

oracle5视频有详解:口令历史;

 

讲解:

连接命令:disc[onnect]断开与当前数据库的连接

passw[ord]:修改用户的密码,如果要想修改其他用户的密码,需要用sys/system登陆

show user:显示当前用户

exit:断开数据库连接,同时退出。

 

//2014418

 

oracle汉化的界面工具:PLSQL Developer

 

文件操作命令:

start@:运行sql脚本

sql>@ d:\a.sql

sql>start d:\a.sql

 

edit:该命令可以编辑制定的sql脚本

sql>edit d:\a.sql

 

spool:sql*plus屏幕上的内容输出到制定文件中去

sql>spool d:\b.sql

sql>spool off;

 

显示和设置环境变量

linesize:设置显示行的宽度,默认是80个字符

sql>show linesize;

sql>set linesize 90;

 

pagesize:设置每页显示的行数目,默认是14.用法和linesize一样。

 

创建用户的用户必须是DBA(数据库管理员)

 

第二章:oracle表的管理:

必须以字母开头,长度不能超过30字符

不能使用oracle的保留字

只能使用如下字符#,$,a-z,A-Z

 

数据类型:

char  定长 最大2000字符

比如:userName varchar(20)//会自动增加,优势是节省空间,分配的空间是跟字段的值相同的。是一个一个的比较字段的内容。是oracle推荐的方式。

 

varchar2()

 

userName char(20);//优势是,字段的长度已知,常被查询

最好是用char,这样速度能高很多。

 

数字型:

number 表示的-1038)到1038次方,可以表示整数或小数,

number(3,3):3位的整数加上3位的小数位

number(5):表示一个5位的整数。

 

日期类型

 

date  包含年月日和时分秒

 

timestamp (银行用户)这是oracle9idate数据类型的扩展,精度更高

 

图片类型

blob:二进制数据 可以存放图片/声音/视频等。

 

真实项目中,不会把图片,声音等放数据库中,而是放在磁盘的文件夹中,数据库中只是将其目录,但是为了保密的话,就将文件放在数据库里。

 

 

sql>create table student(birthday date,userId number(4),sex char(2),sal number(4,2));

 

增删改一个字段

sql>alter table student add(classId number(2));

sql>alter table student modify(classId number(4));

sql>alter table student drop column sal;

 

sql>rename student to stu;

sql>drop table student;

 

 

sql>insert into student values(‘11-02-1988‘,2,‘‘,333.34,‘a32‘);

//注意生日的格式,mm-dd-yyyy;这是oracle

默认的日期格式。

sql>alter session set nls_date_format=‘yyyy-mm-dd‘;

 

sql>insert into student values(‘1988-11-02‘,2,‘‘,333.34,‘a32‘);

//插入部分字段

 

sql>insert into student(userId,sex,sal) values(‘a003‘,‘‘,3232.43);

 

增删改查

sql>insert into student(userId,sex,sal) values(‘a003‘,‘‘,null);

sql>select * from student where sex=‘‘;

sql>update student set sal=sal/2 where sex=‘‘;//更新某一行的内容

sql>select * from student;

sql>delete from student where userId=2;//删除数据,表存在

sql>roll back;

sql>drop table student;

sql>truncate table student;//很快的删除表。

 

 

创建数据库

DBCA(数据库配置助手):用来创建和删除数据库,点击下一步,可以创建数据库,删除,管理等。

 

有四种方法创建数据库:

一般是new database 取全局名

专用服务器:只有一个客户端,分配内存,缓冲区,高速缓冲区。

空间越大,数据库性能越高

 

公用服务器

 

打开操作时间的命令:set time on;

 

 

要想疯狂的赋值

命令:

insert into users{userid,username,userpass) select * from users;

 

 

 

 

第三章:java中使用oracle

1.Jdbc_Odbc桥接方式

演示:使用jdbc_odbc桥连接方式:

加载驱动

Class.forName(“sun.jdbc.odbc.jdbcodbcDriver”);

Connection ct=DriverManager.getConnection(“jdbc:odbc:???”,”scott”,”s123”)

//???配置数据源的名称,此种方式不允许远程连接。

Statement sm=ct.createStatement();

ResultSet rs=sm.executeQuery(“select * from emp”);

While(rs.next()){

System.out.println(“用户名:”+rs.getString(2));//默认从1下标开始。。

}

Rs.close();

Sm.close();

Ct.close();

 

 

 

 

2.JDBC连接oracle

演示:

Class.forName(“oracle.jdbc.driver.OracleDriver”);

url=”jdbc:oracle:thin:@127.0.0.1:1521:myora1”;

Connection ct=DriverManger.getConnection(url,”scott”,”s123”);

Statement sm=ct.createStatement();

ResulteSet rs=sm.executeQuery(“select * from emp”);

While(rs.next()){

System.out.println(“用户名”+rs.getString(2));

}

Rs.close();

Sm.close();

Ct.close();

 

第四章:事务

使用子查询

Select * from emp

需求:将emp中一部分的数据导入新表

Create table kkk (myId number(4),myname varchar(50),myDept char(20));

只导入原来表中的部门号为10

Insert into kkk (myid,myname,mydept) as select empno,ename,edept from emp where edept=10;

 

Updata emp set (job,sal,comm)=(select job,sal,comm from emp where ename=’Smith’) where ename=”scott”;

事务作用:保证数据的一致性

,一组相关的dml语句。数据操作语言,增删改,无查询。

Connection ct=null;  

Try{

Class forName(“oracle.jdbc.driver.OracleDriver”);

ct=DirverManager.getConnection(“jdbc:oracle:thin:@127.0.0.1:1257:myora1”,”scott”,”s123”);

//加入事务

ct.setAutoCommit(false);

Statement st=ct.createStatement();

st.executeUpdate(“update emp set sal=sal-100 where ename=

SMITH’”);

 

st.executeUpdate(“update emp set sal=sal+100 where ename=

SMITH’”);

//提交事务

Ct.commit();

St.close()

Ct.close():

}catch(Exception e){

//出错时回滚

Ct.rollback();

e.printStack();

 

}

只读事务

Set transaction read only

如:每天销售情况读取

Set linesize 120;

Select * from emp;

 

 

字符函数

Lower(char);

Upper(char);

Length(char);

Substr(char,m,n);

 

 

需求:将所有员工的名字按小写字母排序。

 

第五章:数据库管理

管理员职责

每一个oracle数据库都要与一个管理员:

安装和升级oracle数据库

建库,表空间,表,视图,索引...

备份和恢复计划

权限管理,调优,故障排除

参与项目,编写sql语句,存储过程,触发器,规则,约束,包

 

备份和恢复数据库

逻辑备份

使用工具export/import导出(备份)/入(恢复)(从数据库到磁盘)。

导出三种:

导出表,方案,数据库

使用命令exp

导出自已用户的表

1)导出表的结构和数据:exp/imp userid=scott/tiger@myoral tables=(emp,student) file=d:\e1.dmp

>conn scott/s123,//导入和导出要到oracle.bin目录下

T>d:

Teminal>cd ..oracle/bin

.../bin>exp userid=scott/s123@myoral talbes=(emp,student)  file=d:\emp.dmp

2)只导出结构

exp userid=scott/tiger@myoral tables=(emp,student) file=d:\e1.dmp rows=n

若表很大,可以再命令后面加上direct=y 即可快速导出。

导出/入自己的方案

exp scott/tiger@myoral owner=scott file=d:\scott.dmp

imp scott/tiger file=d:\scott.dmp 

 

exp System/manager@myoral owner=(system,scott ) file=d:\scott.dmp //导出2个方案

exp System/manager file=d:\scott.dmp fromuser=system touser=scott

导出/入数据库

exp userid=system/manager@myoral full=y inctype=complete file=d:\x.dmp

Imp userid=system/manager  full=y  file=d:\x.dmp

 

 

 

oracle,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。