Oracle 10g基本語法

qinwen740發表於2009-06-23
1.登陸系統使用者
sqlplus 然後輸入系統使用者名稱和密碼
登陸別的使用者
conn 使用者名稱/密碼;
2.建立表空間
create tablespace 空間名
datafile 'c:\空間名' size 15M  --表空間的存放路徑,初始值為15M
autoExtend on next 10M  --空間的自動增長的值是10M
permanent online;  --永久使用
3.建立使用者
create user shi   --建立使用者名稱為shi
identified by scj  --建立密碼為scj
default tablespace 表空間名 --預設表空間名
temporary tablespace temp --臨時表空間為temp
profile default   --受profile檔案的限制
quota unlimited on 表空間名; --在表空間下面建表不受限制
4.建立角色
create role 角色名 identified by 密碼;
5.給角色授權
grant create session to 角色名;--給角色授予建立會話的許可權
grant 角色名 to 使用者名稱; --把角色授予使用者
6.給使用者授予許可權
grant create session,resource to shi;--給shi使用者授予所有許可權
grant create table to shi; --給shi使用者授予建立表的許可權
7.select table_name from user_tables;   察看當前使用者下的所有表 
8.select tablespace_name from user_tablespaces; 察看當前使用者下的 表空間
9.select username from dba_users;察看所有使用者名稱稱命令 必須用sys as sysdba登陸
Oracle 檢視使用者許可權資料字典檢視分為3大類, 用字首區別,分別為:USER,ALL 和 DBA,許多資料字典檢視包含相似的資訊。
USER_*:有關使用者所擁有的物件資訊,即使用者自己建立的物件資訊
ALL_*:有關使用者可以訪問的物件的資訊,即使用者自己建立的物件的資訊加上其他使用者建立的物件但該使用者有權訪問的資訊
DBA_*:有關整個資料庫中物件的資訊
(這裡的*可以為TABLES, INDEXES, OBJECTS, USERS等。
1.檢視所有使用者:
select * from dba_users;
select * from all_users;
select * from user_users;
2.檢視使用者系統許可權:
select * from dba_sys_privs;
select * from all_sys_privs;
select * from user_sys_privs;
3.檢視使用者物件許可權:
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.檢視當前使用者的預設表空間
select username,default_tablespace from user_users;
7.檢視某個角色的具體許可權,如grant connect,resource,create session,create view to TEST;檢視RESOURCE具有那些許可權,用SELECT * FROM DBA_SYS_PRIVS WHERE GRANTEE='RESOURCE';
 
10.建立表
create table 表名
(
id int not null,
name varchar2(20) not null
)tablespace 表空間名  --所屬的表空間
storage
(
   initial 64K   --表的初始值
   minextents 1   --最小擴充套件值
   maxextents unlimited  --最大擴充套件值
);
11.--為usrs表新增主鍵和索引
alter table users
add constraint pk primary key (ID);
12.為已經建立users表新增外來鍵
alter table users
  add constraint fk_roleid foreign key (roleid)
  references role(role_id) on delete cascad; --下邊寫主表的列
   on delete cascad是建立級聯
13.把兩個列連線起來
select concat(name,id) from 表名;  --把name和id連線起來
14.擷取字串
select column(name,'李') from 表名;  --把name中的‘李’去掉
15.執行事務之前必須寫
set serveroutput on;  --開啟輸入輸出(不寫的話,列印不出資訊)
16.while的應用
declare   --宣告部分
ccc number:=1;  --復職
a number:=0;
begin   --事務的開始
while ccc<=100 loop --迴圈
if((ccc mod 3)=0) then --條件
 dbms_output.put_line(ccc||',');    --列印顯示
 a:=a+ccc;
end if;   --結束if
ccc:=ccc+1;
end loop;  --結束迴圈
dbms_output.put_line(a); 
end;   --結束事務
/   
17.select into  的用法 --只能處理一行結果集
declare
  name varchar(30);
begin
 select username into name
 from users
 where id=2;
dbms_output.put_line('姓名為:'||name);
end;
/
18.利用%rowtype屬性可以在執行時方便的宣告記錄變數和其他結構
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想當於複製一個表
19.遊標的定義和使用
Declare
Cursor ucur is select * from users; --宣告遊標
Us users%rowtype;--定義與遊標想匹配的變數
Begin
Open ucur;--開啟遊標
Fetch ucur into us;
While ucur %found loop --使用迴圈遍歷遊標的查詢結果
Dbms_output.put_line('姓名:'||us.username||'生日'||us.brithday);
Fetch ucur into us;
End loop;
Close ucur; --關閉遊標
End;
=======================================
%found在前一條的fetch語句至少對應資料庫的一行時,%found屬性值為true,否則為false;
% notfound 在前一條fetch語句沒有對應的資料庫行時,%notfound屬性值為true,否則為false;
%isopen 在遊標開啟時%isopen屬性值為true;否則為false;
%rowcount顯示迄今為止從顯示遊標中取出的行數
20.
刪除
drop tablespace 空間名 including contents; --刪除表空間和裡面的內容
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);

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15688952/viewspace-607246/,如需轉載,請註明出處,否則將追究法律責任。