如果一開始學的Mysql,再學Oracle,會有一些疑惑的地方(我當初就很淩亂。。。),比如在Mysql中,我們都會說建立一個資料庫,在Oracle沒有這個概念,下面簡單說一下我對這方面的了解。
執行個體
跟Mysql不同,Oracle沒有建立一個資料庫的概念,Oracle是執行個體的概念,一個安裝好的Oracle資料庫可以運作多個執行個體。
使用者
使用者是在執行個體下建立的,不同執行個體可以建立相同名字的使用者。
表空間
表空間是一個用來管理資料存儲的邏輯概念,表空間隻是和資料檔案(ORA或者DBF檔案)發生關系,資料檔案是實體的,一個表空間可以包含多個資料檔案,而一個資料檔案隻能隸屬一個表空間。
對應關系

一般在使用Mysql的時候,如果系統上運作的Mysql需要承載多應用的資料,一般我們會為每個應用分别建立對應的資料庫。而在Oracle中不這樣,通常是在同一表空間下建立不同的使用者,每個不同的使用者對應不同的應用,也擁有不同的資料檔案。
Oracle資料庫可以建立多個執行個體,每個執行個體可以建立多個表空間,每個表空間下可以建立多個使用者(同時使用者也屬于表空間對應的執行個體)和資料庫檔案,使用者可以建立多個表(每個表随機存儲在一個或多個資料庫檔案中)
關于表空間和使用者的操作
--建立表空間create tablespace MySpace --建立表空間--組成表空間的一個或多個資料檔案datafile 'D:softOracleUZDataFileMySpacedDtaFileest.dbf' size 1M --資料檔案的大小autoextend on next 1M maxsize unlimited; --到檔案大小達到時,每一次增長的大小,并在最大無限制drop tablespace MySpace --删除表空間--删除表空間及資料檔案drop tablespace MySpace including contents and datafiles;--檢視表空間select * from user_tablespaces;--更改表空間狀态alter tablespace MySpace read only; --把狀态改為隻讀--更改使用者的表空間alter user Test1 default tablespace MySpace;--建立使用者create user Test1identified by 123456default tablespace MySpace;--給使用者授予***權限 --文法: grant 系統權限,對象權限,角色 to 使用者;grant create session,create tablespace,create user to Test1;--通過目前使用者,把techer的select的權限賦予給Test1grant select on teacher to Test1;-- 授權給目前使用者連接配接及資源權限grant create session,resource to interview0710;--回收權限revoke select on teacher from Test1;--删除使用者drop user Test1;
對資料表的操作
select * from user_tablespaces; --檢視表空間--建立教師表create table teacher ( tNo number(8) not null, tname varchar2(30) not null, tcardId char(18), birthday date, tsalary number(10,2));--建立部門表create table dept ( deptno number(2) not null, dname varchar2(14), loc varchar2(13), tno number(4));--給表添加描述comment on table teacherinfo is '老師表';--給表中的列添加描述comment on column teacherinfo.tno is '編号';--向某一個字段設定預設值:alter table GDBASEDATA.PORT_ENT_PROP modify IS_DANGEROUS default(0);--新增列alter table teacher add (deptno number(2)); --新增一列deptnoalter table teacher add (tsex char(3)); --修改列alter table dept modify (dname varchar2(20)); --修改列dname--添加主鍵限制:要求主鍵列資料唯一,并且不允許為空alter table teacher add constraints pk_tNo primary key (tNo); --為教師表的編号添加主鍵限制alter table dept add constraints pk_deptno primary key (deptno); --為部門表的編号添加主鍵限制--添加唯一限制alter table teacher add constraints uq_tcardid unique (tcardid);--添加檢查限制alter table teacher add constraints ck_tsex check (tsex in('男','女')); --添加外鍵限制--關于主表和從表:一對多的關系,一為主表,多為從表alter table teacher add constraints fk_deptno foreign key (deptno) references dept (deptno);--檢視teacher表select * from teacher;--檢視表資訊select * from user_tables;--檢視添加的限制select * from user_constraints;select * from user_cons_columns aa where aa.constraint_name = 'UQ_TCARDID';--序列:自動增長,産生唯一的值--建立預設的序列create sequence my_seq;--檢視sequence的目前值select my_seq.currval from dual;--檢視sequence的下一個值select my_seq.nextval from dual;--删除序列drop sequence my_seq;--建立指定參數的序列create sequence my_seqstart with 100 --開始值maxvalue 9999 --最大值increment by 10 --每次的遞增量cache 20 --緩存值--dml:insert --文法:insert into 表名 values(列值1,列值2...); --插入一條資料:每個字段都必須有值insert into teacher values(my_seq.nextval,'王老師','12345678888', to_date('2001-1-2','yyyy-mm-dd hh:mi:ss'),12000,null,'男');insert into teacher values(my_seq.nextval,'李白','12345678889', to_date('1996-8-11','yyyy-mm-dd hh:mi:ss'),15600,null,'男'); --插入一條資料,隻出入編号,姓名insert into teacher(tno,tname) values(my_seq.nextval,'哈哈');--複制表(隻會複制表結構和資料記錄,不會複制限制)create table teacher_bak as select * from teacher;select * from teacher_bak;--複制表,隻複制表結構,不複制記錄create table teacher_bak1 as select * from teacher where 1=2;select * from teacher_bak1;--把查詢結果插入到另一個表中insert into teacher_bak as select * from teacher where tname='李白' or tname = '哈哈';--查詢目前資料庫執行個體名select name from v$database;--檢視資料庫使用者select * from dba_users;