1. 用戶
1.1 常用用戶
| 用戶 | 描述 | 默認密碼 |
|---|---|---|
| sys | 系統(tǒng)管理員,擁有最高權限 | change_on_install |
| system | 本地管理員,次高權限 | manager |
| scott | 普通用戶,默認未解鎖 | tiger |
解鎖scott賬戶命令:
alter user scott account unlock;
1.2 顯示當前登錄的用戶
Show user
1.3 創(chuàng)建用戶
create user 用戶名 identifiled by 密碼;
1.4 刪除用戶
drop user 用戶名
1.5 查看所有用戶
select * from dba_users/all_users;
或者
select * from all_users;
1.6 查看用戶角色
(1)當前用戶被激活的角色:
select * from session_roles;
(2)當前用戶被授予的角色:
select * from user_role_privs;
(3)全部用戶被授予的角色:
select * from dba_role_privs;
(4)查看某個用戶擁有的角色:
select * from dba_role_privs where grantee='用戶名'
2. 登錄
管理員身份登錄:sqlplus/nolog--->conn/as sysdba
普通用戶登錄:sqlplus/nolog---->conn 用戶名/密碼
管理員切換到普通用戶:conn 用戶名/密碼
普通用戶切換到管理人員:conn sys as sysdba,然后輸入密碼回車
3. 權限
Oracle11g具有二百多種權限,可以通過下面語句查看:
SELECT * FROM SYSTEM_PRIVILEGE_MAP;
3.1 系統(tǒng)權限分類
- DBA:擁有全部特權,是系統(tǒng)最高權限,只有DBA才可以創(chuàng)建數(shù)據庫結構。
- RESOURCE:擁有Resource權限的用戶只可以創(chuàng)建實體,不可以創(chuàng)建數(shù)據庫結構。
- CONNECT:擁有Connect權限的用戶只可以登錄Oracle,不可以創(chuàng)建實體,不可以創(chuàng)建數(shù)據庫結構。
對于普通用戶:授予connect, resource權限。
對于DBA管理用戶:授予connect,resource,dba權限。
3.2 常用權限
CREATE SESSION 創(chuàng)建會話
CREATE SEQUENCE 創(chuàng)建序列
CREATE SYNONYM 創(chuàng)建同名對象
CREATE TABLE 在用戶模式中創(chuàng)建表
CREATE ANY TABLE 在任何模式中創(chuàng)建表
DROP TABLE 在用戶模式中刪除表
DROP ANY TABLE 在任何模式中刪除表
CREATE PROCEDURE 創(chuàng)建存儲過程
EXECUTE ANY PROCEDURE 執(zhí)行任何模式的存儲過程
CREATE USER 創(chuàng)建用戶
DROP USER 刪除用戶
CREATE VIEW 創(chuàng)建視圖
3.3 分配權限
grant 權限名稱1,權限名稱2 to 用戶名 with admin option
3.4 回收系統(tǒng)權限
revoke create session from 用戶名
4. 角色
4.1 顯示角色信息
查詢所有的角色:
select * from dba_roles;
查詢用戶的角色:
select GRANTED_ROLE from dba_role_privs where grantee = '用戶名';
查詢角色具有的權限:
select privilege from role_sys_privs where role='角色名';
查詢用戶具有的權限:
select * from session_privs;
4.2 創(chuàng)建角色
創(chuàng)建公用角色:
create role 角色名 not identified
創(chuàng)建公用角色:
create role 角色名 identified by 密碼
4.3 為角色授權
創(chuàng)建用戶沒有分配任何權限的時候,sqlplus上登錄該用戶會報錯
角色授予命令:
grant 權限名稱 to 角色名
4.4 為用戶分配角色
grant 角色名 to 用戶名 with admin option;
加上with admin option是為了用戶可以將system分配給它的角色分配給別的其他用戶
4.5 刪除角色
drop role 角色名稱;
5. oracle基本操作
建表空間:
create tablespace 表間名 datafile '數(shù)據文件名' size 表空間大小;
【路徑可以沿用系統(tǒng)自帶數(shù)據文件,但是記得修改文件名】
知識點普及:
Oracle數(shù)據庫的物理結構是由數(shù)據庫的操作系統(tǒng)文件所決定,每一個Oracle數(shù)據庫是由三種類型的文件組成:
-
數(shù)據文件
select name from v$datafile; -
日志文件
select * from v$logfile; -
控制文件
select name from v$controlfile;
數(shù)據庫的文件為數(shù)據庫信息提供真正的物理存儲(就是數(shù)據文件)
一個oracle數(shù)據庫有一個或多個數(shù)據文件
一個數(shù)據文件只與一個數(shù)據庫連接
一旦建立,數(shù)據文件只增不減
一個表空間由一個或多個數(shù)據文件組成
??:
創(chuàng)建表空間:
create tablespace data_test datafile 'D:\APP\YITOP\ORADATA\ORCL\TEST.DBF' size 2000M;
創(chuàng)建用戶:
create user lee identified by 123456 default tablespace data_test;
授權:
grant connect,resource to lee;
從管理員切換到普通用戶:
conn lee/123456
建表:
create table t_user(
id varchar2(10),
name varchar2(30),
age number(3)
);
為表添加約束:
alter table t_user add constraint ck_info_age check(age >=0 and age<=100);
6. 增刪改查
6.1 增
insert into 表名(列1,列2...) values('數(shù)據1','數(shù)據2'...);
6.2 刪
① 刪除表字段
alter table 表名 drop column 字段名;
② 刪除表數(shù)據
刪除某條數(shù)據:
delete from 表名 where 條件;
刪除整張表的數(shù)據:
truncate table 表名;
或者
drop table 表名;
刪除有外鍵約束的表:
drop table student cascade constraints;
6.3 改
重命名表:
rename 表名 to 新表名;
向表中添加注釋:
comment on table 表名 is '注釋文字';
向列中添加注釋:
comment on column 表名.列名 is '注釋文字';
更新表數(shù)據:
update 表名 set 列名='新值' where 條件;
修改列名:
alter table 表名 rename column 列名 to 新列名;
修改列的屬性:
alter table 表名
modify 列名 varchar2(字符長度)/number(精度)/char(數(shù)據類型/default '默認值');
6.4 查
語法:
SELECT
[DISTINCT|ALL]
select_list
FROM table_list
[WHERE condition]
[group_by_clause]
[HAVING condition]
[order_by_clause]
7. 約束
語法:
alter table 表名 add constraint 約束名 約束(字段名);
??:
添加檢查約束:
alter table t_user add constraint ck_user_check check(age>=0 and age<=100)
添加外鍵:
alter table scores
add constraint fk_scores_infos_stuid foreign key(stuid)
references infos(stuid) on delete cascade;
主鍵約束:
alter table 表名 add constraint 約束名稱 primary key(列名);
不為空:
alter table 表名 add constraint 約束名稱 not null(列名)
唯一約束:
alter table 表名 add constraint 約束名稱 unique(列名);
默認值約束:
alter table 表名 add constraint 約束名稱 default '默認值';
8. 分頁
查詢第一條記錄:
select *
from (select * from emp order by sal desc)
where rownum = 1;
查詢前3條:類似Sqlserver中的TOP 3
select *
from (select * from emp order by sal desc)
where rownum <= 3;
查詢第2至第3條記錄:
select *
from (select t.*, rownum as no
from (select * from emp order by sal desc) t)
where no between 2 and 3;