connect:切換使用者,簡寫conn 格式:conn 使用者名/密碼
show user:顯示目前登入的使用者
host:執行(作業系統指令)
host mkdir e:\oraclepractice
host md e:\1
spool:導出記錄到文本
spool e:\oraclepractice\text.txt 建立了text.txt,把内容導入其中
select * from book;
spool off
clear screen:清屏
start:執行檔案中的sql語句
start d:\test.sql
@ d:\test.sql
start和 @ 表示:執行
desc:顯示表結構
show error:顯示錯誤
exit:退出
oracle預設的系統使用者:
sys/system:conn sys/111111 as sysdba
conn system/111111
sysman
scott:有4個表(emp、dept、salgrade、bonus)
注:conn sys/111111 @orcl as sysdba
@orcl:指定了資料庫執行個體(選哪個資料庫),有它,要把監聽服務打開
若沒有@orcl,就連接配接到預設的資料庫
三、表空間及其分類
表空間:永久表空間、臨時表空間、undo表空間
建立表空間格式:create 【temporary】tablespace XX tempfile | datafile 'XX.dbf' size XX
如:create tablespace 1 datafile '1.dbf' size 10m;
create temporary tablespace 2 tempfile '2.dbf' size 10m;
select * from dba_data_files; 檢視表空間的資料檔案
select * from dba_temp_files; 檢視臨時表空間的資料檔案
select * from dba_tablespace; 檢視表空間
修改表空間:修改表空間狀态:alter tablespace a1 online(offline) 設定聯機、脫機狀态
alter tablespace a1 read only(read write) 隻讀、讀寫(聯機)
修改資料檔案:增加資料檔案:alter tablespace a1 add datafile 'XX.dbf' size XX;
删除資料檔案:alter tablespace a1 drop datafile 'XX.dbf';
删除表空間:drop tablespace a1; 隻删表空間
drop tablespace a1 including contents; 表空間連同資料檔案一起删
四、使用者管理
1、建立使用者
create user zhe identify by 111111 default tablespace a1 temporary tablesapce a2;
2、檢視建立的使用者
select username from dba_users;
3、給予權限:grant connect to zhe 給zhe連接配接權限
4、管理使用者
連接配接使用者:conn 使用者名/密碼
更改密碼:alter user zhe identified by 222222;
删除使用者:drop user zhe 【cascade】; cascade表示:使用者連同其建立的東西全删
鎖定使用者:alter user zhe account lock;
解鎖使用者:alter user zhe account unlock;
五、角色、權限、資料字典
1、角色
角色:權限的集合
使用者可以給角色指定權限,并把角色賦予相應使用者
3種标準角色:connect(連接配接角色)、resource(資源角色)、dba(資料庫管理者角色)
對普通使用者:給connect、resource權限
對DBA管理使用者:給dba權限
建立角色:create role 角色名; 如:create role manager;
删除角色:drop role manager;
給角色賦予權限:grant 權限 to 角色;
grant create table,create view to manager;
把角色賦予使用者:grant 角色 to 使用者;
grant manager to user01,user02;
撤回授權:revoke manager from user01;
2、權限
權限:分為系統權限、對象(實體)權限
檢視所有系統權限:select * from system_privilege_map;
常見系統權限:create session、create sequence、create table、create user、
create view
檢視所有對象權限:select * from table_privilege_map;
常見對象權限:select、update、insert、delete、all等(all:包括所有權限)
授予對象權限格式:grant select,insert on scott.emp to manager2;
grant manager2 to user03;
grant all on scott.emp to user04;
回收對象權限:revoke all on scott.emp from user04;
3、資料字典
dba_tablespaces:管理者級别的表空間描述資訊
user_tablespaces:普通級别的...
如:select tablespace_name from dba_tablespaces;
select ... ... ... from user_tablespaces;
dba_users:管理者的使用者資訊
user_users:普通使用者的...
如:select default_tablespace,temporary_tablespace from dba_users where username =' SYSTEM'; 檢視SYSTEM使用者的表空間資訊
select username,defult_tablespace,temporary_tablespace from dba_users;
更改使用者預設的臨時表空間:alter user user01 default tablespace a1 temporary tablespace a2;
六、SQL是什麼
SQL:結構化查詢語言(Structured Query Language),是一種特殊的程式設計語言,用于存儲資料、查詢、更新、管理關系型資料庫系統的語言
oracle的SQL:包括DDL(資料定義語言)、DML(資料操作語言)、DCL(資料控制語言)、TCL(事物控制語言)
DDL語句包括:create table, drop table, create index, drop index, truncate(删除表中所有行)
alter table:更改表結構,增加、修改、删除列
alter table add constraint:在已有表上加限制
DML語句包括:insert:添加資料到資料庫
update:修改資料庫中的資料
delete:删除
select:查詢
DCL包括:grant:把權限/角色,賦予使用者/角色(授予通路權限)
revoke:回收權限
lock:對資料庫的特定部分進行鎖定
TCL包括:commmit:送出事物處理
rollback:事物處理回退
savepoint:設定儲存點
添加列:alter table 表名 add 新增列名 資料類型;
如:alter table student add tel varchar2(11);
修改列:alter table 表名 modify 列名 新的資料類型;
如:alter table student modify tel number(11,0);
删除列:alter table 表名 drop column 列名;
如:alter table student drop column tel;
修改列名:alter table 表名 rename column 列名 to 新列名;
如:alter table student rename column sex to gender;
修改表名:rename 表名 to 新表名;
删除表:truncate table 表名; 隻删除表資料,不删表結構
drop table 表名; 表結構、表資料都删,就是這個表沒有了
2、增删改查格式
1、添加資訊
inset into 表名[(列1,列2...)] values(值1,值2...); 若把表中的列都加資料,可省略[]
如:insert into student(sid,name,sex) values(001,'張三','女');
insert into student values(002,'王五','女',to_date('19880909','YYYYMMDD'));
2、查詢
select * from 表名; select column1,column2... from 表名;
如:select * from student
select sid,name from student;
3、修改
update 表名 set column = values[,column = values] [where 條件];
如:update student set Tel ='13800000000' where sid = 001;
update student set address = '昌平區';
4、删除(資料)
delete from 表名 [where 條件];
如:delete from student;
delete from student where sid = 002;