sqlplus 然后輸入系統(tǒng)用戶名和密碼
登陸別的用戶
conn 用戶名/密碼;
2.創(chuàng)建表空間
create tablespace 空間名
datafile 'c:\空間名' size 15M??--表空間的存放路徑,初始值為15M
autoExtend on next 10M??--空間的自動(dòng)增長(zhǎng)的值是10M
permanent online;??--永久使用
3.創(chuàng)建用戶
create user shi???--創(chuàng)建用戶名為shi
identified by scj??--創(chuàng)建密碼為scj
default tablespace 表空間名?--默認(rèn)表空間名
temporary tablespace temp?--臨時(shí)表空間為temp
profile default???--受profile文件的限制
quota unlimited on 表空間名;?--在表空間下面建表不受限制
4.創(chuàng)建角色
create role 角色名 identified by 密碼;
5.給角色授權(quán)
grant create session to 角色名;--給角色授予創(chuàng)建會(huì)話的權(quán)限
grant 角色名 to 用戶名;?--把角色授予用戶
6.給用戶授予權(quán)限
grant create session,resource to shi;--給shi用戶授予所有權(quán)限
grant create table to shi;?--給shi用戶授予創(chuàng)建表的權(quán)限
7.select table_name from user_tables;?? 察看當(dāng)前用戶下的所有表
8.select tablespace_name from user_tablespaces; 察看當(dāng)前用戶下的 表空間
9.select username from dba_users;察看所有用戶名稱命令 必須用sys as sysdba登陸
Oracle 查看用戶權(quán)限數(shù)據(jù)字典視圖分為3大類, 用前綴區(qū)別,分別為:USER,ALL 和 DBA,許多數(shù)據(jù)字典視圖包含相似的信息。
USER_*:有關(guān)用戶所擁有的對(duì)象信息,即用戶自己創(chuàng)建的對(duì)象信息
ALL_*:有關(guān)用戶可以訪問(wèn)的對(duì)象的信息,即用戶自己創(chuàng)建的對(duì)象的信息加上其他用戶創(chuàng)建的對(duì)象但該用戶有權(quán)訪問(wèn)的信息
DBA_*:有關(guān)整個(gè)數(shù)據(jù)庫(kù)中對(duì)象的信息
(這里的*可以為TABLES, INDEXES, OBJECTS, USERS等。
1.查看所有用戶:
select * from dba_users;
select * from all_users;
select * from user_users;
2.查看用戶系統(tǒng)權(quán)限:
select * from dba_sys_privs;
select * from all_sys_privs;
select * from user_sys_privs;
3.查看用戶對(duì)象權(quán)限:
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;
4.查看所有角色:
select * from dba_roles;
5.查看用戶所擁有的角色:
select * from dba_role_privs;
select * from user_role_privs;
6.查看當(dāng)前用戶的缺省表空間
select username,default_tablespace from user_users;
7.查看某個(gè)角色的具體權(quán)限,如grant connect,resource,create session,create view to TEST;查看RESOURCE具有那些權(quán)限,用SELECT * FROM DBA_SYS_PRIVS WHERE GRANTEE='RESOURCE';
?
10.創(chuàng)建表
create table 表名
(
id int not null,
name varchar2(20) not null
)tablespace 表空間名??--所屬的表空間
storage
(
?? initial 64K???--表的初始值
?? minextents 1???--最小擴(kuò)展值
?? maxextents unlimited??--最大擴(kuò)展值
);
11.--為usrs表添加主鍵和索引
alter table users
add constraint pk primary key (ID);
12.為已經(jīng)創(chuàng)建users表添加外鍵
alter table users
? add constraint fk_roleid foreign key (roleid)
? references role(role_id) on delete cascad;?--下邊寫(xiě)主表的列
???on delete cascad是創(chuàng)建級(jí)聯(lián)
13.把兩個(gè)列連接起來(lái)
select concat(name,id) from 表名;??--把name和id連接起來(lái)
14.截取字符串
select column(name,'李') from 表名;??--把name中的‘李’去掉
15.運(yùn)行事務(wù)之前必須寫(xiě)
set serveroutput on;??--打開(kāi)輸入輸出(不寫(xiě)的話,打印不出信息)
16.while的應(yīng)用
declare???--聲明部分
ccc number:=1;??--復(fù)職
a number:=0;
begin???--事務(wù)的開(kāi)始
while ccc<=100 loop?--循環(huán)
if((ccc mod 3)=0) then?--條件
?dbms_output.put_line(ccc||',');??? --打印顯示
?a:=a+ccc;
end if;???--結(jié)束if
ccc:=ccc+1;
end loop;??--結(jié)束循環(huán)
dbms_output.put_line(a);?
end;???--結(jié)束事務(wù)
/???
17.select into ?的用法?--只能處理一行結(jié)果集
declare
? name varchar(30);
begin
?select username into name
?from users
?where id=2;
dbms_output.put_line('姓名為:'||name);
end;
/
18.利用%rowtype屬性可以在運(yùn)行時(shí)方便的聲明記錄變量和其他結(jié)構(gòu)
Set serveroutput on;
Declare
?? utype hr.employees%rowtype;
Begin
Select * into utype from hr.employees where employee_id=194;
Dbms_output.put_line('姓名'|| utype.first_name);
Dbms_output.put_line('生日'|| utype.last_name);
end;
/???--%rowtype想當(dāng)于復(fù)制一個(gè)表
19.游標(biāo)的定義和使用
Declare
Cursor ucur is select * from users; --聲明游標(biāo)
Us users%rowtype;--定義與游標(biāo)想匹配的變量
Begin
Open ucur;--打開(kāi)游標(biāo)
Fetch ucur into us;
While ucur %found loop --使用循環(huán)遍歷游標(biāo)的查詢結(jié)果
Dbms_output.put_line('姓名:'||us.username||'生日'||us.brithday);
Fetch ucur into us;
End loop;
Close ucur; --關(guān)閉游標(biāo)
End;
=======================================
%found在前一條的fetch語(yǔ)句至少對(duì)應(yīng)數(shù)據(jù)庫(kù)的一行時(shí),%found屬性值為true,否則為false;
% notfound 在前一條fetch語(yǔ)句沒(méi)有對(duì)應(yīng)的數(shù)據(jù)庫(kù)行時(shí),%notfound屬性值為true,否則為false;
%isopen 在游標(biāo)打開(kāi)時(shí)%isopen屬性值為true;否則為false;
%rowcount顯示迄今為止從顯示游標(biāo)中取出的行數(shù)
20.
刪除
drop tablespace 空間名 including contents;?--刪除表空間和里面的內(nèi)容
drop table 表名???--刪除表
drop user 用戶名??--刪除用戶
insert into student(stuid,stuname,age,birthday)values(stu_sn.nextval,'lisi',21,to_date('1989-09-08','yyyy-mm-dd'));
insert into student(stuid,stuname,age,birthday)values(stu_sn.nextval,'wangwu',21,to_date('1989-09-08','yyyy-mm-dd'));
insert into student(stuid,stuname,age,birthday)values(stu_sn.nextval,'zhaoliu',20,to_date('1990-09-08','yyyy-mm-dd'));
insert into exam(stuid,subid,grade)values(1,2,70);
insert into exam(stuid,subid,grade)values(2,1,45);
insert into exam(stuid,subid,grade)values(2,2,70);