Oracle Database Study Notes (2)

1. SqlPlus common commands

● Connection command: [conn]

Usage: conn username (as sysdba / as sysoper) and then enter the password

     or conn username/password (as sysdba / as sysoper)

When connecting as a privileged user, you must bring as sysdba or as sysoper 

● Disconnect command: [disc]

This command is used to disconnect from the current database

● Modify password command: [passw]

Usage: passw first enter the old password and then enter the new password

     or passw username

     or alter user username identified by new password

Description: This command is used to modify the user's password. If you want to modify the password of other users, you need to log in with sys/system.

● Display the current user command: [show user]

Description: Displays the current user name

● Exit command: 【exit】

Description: This command will disconnect from the database and exit sql*plus

● Run sql script command: [start or @]

Usage: start script path

     or   @ script path

● Edit sql script command: [edit]

Usage: edit script path

Description: This command can edit the specified sql script 

● Interactive command: 【&】

用法:select * from scott.emp where sal = '&sal';

Description: You can substitute a variable that requires user input when it is executed.

● Output to file command: [spool]

Usage: spool file path (run the command after the output is complete) spool off

Description: This command can output the contents of the sql*plus screen to the specified file.

● Display and set environment variable command: [show set]

Usage: Display and set display line width

show linesize;

set linesize 90;

Usage: Display and set the number of pages displayed per page

show pagesize;

set pagesize 10;

说明: 可以用来控制输出的各种格式。

 

 

2. Oracle用户管理

● 创建用户命令:【create user indentifide by】

用法:create user 用户名 identifide by 密码

说明: 在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用。

● 删除用户命令:【drop user cascade】

用法:drop user 用户名 cascade

说明: 一般以dba的身份去删除某个用户,如果用其它用户去删除用户则需要具有drop user的权限。如果要删除的用户,已经创建了表,那么就需要在删除的时候带一个参数cascade; 

● 实例:用户管理的综合案例

概述:创建的新用户是没有任何权限的,甚至连登陆的数据库的权限都没有,需要为其指定相应的权限。给一个用户赋权限使用命令grant,回收权限使用命令revoke。

SQL> conn xiaoming/m12; 

ERROR: 

ORA-01045: user XIAOMING lacks CREATE SESSION privilege; logon denied 

警告: 您不再连接到 ORACLE。 

SQL> show user; 

USER 为 "" 

SQL> conn system/p; 

已连接。 

SQL> grant connect to xiaoming; 

授权成功。 

SQL> conn xiaoming/m12; 

已连接。 

SQL> 

 

注意:grant connect to xiaoming;在这里,准确的讲,connect不是权限,而是角色。 看图: 



 

* 希望xiaoming用户可以去查询scott的emp表 

  grant select on emp to xiaoming 

* 希望xiaoming用户可以去修改scott的emp表 

  grant update on emp to xiaoming 

* 希望xiaoming用户可以去修改/删除,查询,添加scott的emp表 

  grant all on emp to xiaoming 

* scott希望收回xiaoming对emp表的查询权限 

  revoke select on emp from xiaoming 

//对权限的维护。 

* 希望xiaoming用户可以去查询scott的emp表/还希望xiaoming可以把这个权限继续给别人。 

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

  grant select on emp to xiaoming with grant option 

操作过程: 

SQL> conn scott/tiger; 

已连接。 

SQL> grant select on scott.emp to xiaoming with grant option; 

授权成功。 

SQL> conn system/p; 

已连接。 

SQL> create user xiaohong identified by m123; 

用户已创建。 

SQL> grant connect to xiaohong; 

授权成功。 

SQL> conn xiaoming/m12; 

已连接。 

SQL> grant select on scott.emp to xiaohong; 

授权成功。 

 

--如果是系统权限。 

system给xiaoming权限时: 

grant connect to xiaoming with admin option 

 

问题:如果scott把xiaoming对emp表的查询权限回收,那么xiaohong会怎样? 

答案:被回收。 

下面是我的操作过程: 

SQL> conn scott/tiger; 

已连接。 

SQL> revoke select on emp from xiaoming; 

撤销成功。 

SQL> conn xiaohong/m123; 

已连接。 

SQL> select * from scott.emp; 

select * from scott.emp 

第 1 行出现错误: 

ORA-00942: 表或视图不存在 

 

结果显示:株连九族

 

3. 使用Profile管理用户口令

概述:profile是口令限制,资源限制的命令集合,当建立数据库的,oracle会自动建立名称为default的profile。当建立用户没有指定profile选项,那么oracle就会将default分配给用户。 

● 账户锁定

概述:指定该账户(用户)登陆时最多可以输入密码的次数,也可以指定用户锁定的时间(天)一般用dba的身份去执行该命令。 

例子:指定scott这个用户最多只能尝试3次登陆,锁定时间为2天,让我们看看怎么实现。 

创建profile文件 

SQL> create profile lock_account limit failed_login_attempts 3 password_lock_time 2; 

 

SQL> alter user scott profile lock_account; 

● 账户解锁

alter user scott account unlock;

● 终止口令

为了让用户定期修改密码可以使用终止口令的指令来完成,同样这个命令也需要dba的身份来操作。 

例子:给前面创建的用户tea创建一个profile文件,要求该用户每隔10天要修改自己的登陆密码,宽限期为2天。

SQL> create profile myprofile limit password_life_time 10 password_grace_time 2; 

 

SQL> alter user tea profile myprofile; 

● 口令历史

概述:如果希望用户在修改密码时,不能使用以前使用过的密码,可使用口令历史,这样oracle就会将口令修改的信息存放到数据字典中,这样当用户修改密码时,oracle就会对新旧密码进行比较,当发现新旧密码一样时,就提示用户重新输入密码。 

例子: 

1)建立profile 

SQL>create profile password_history limit password_life_time 10 password_grace_time 2 password_reuse_time 10 

password_reuse_time //指定口令可重用时间即10天后就可以重用 

 

2)分配给某个用户

● 删除profile

概述:当不需要某个profile文件时,可以删除该文件。 

SQL> drop profile password_history 【casade】 

注意:文件删除后,用这个文件去约束的那些用户通通也都被释放了。

 

加了casade,就会把级联的相关东西也给删除掉

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326892814&siteId=291194637