oracle資料庫基礎知識

才智咖 人氣:6.66K

20世紀約70年代 一間名為Ampex的軟體公司,正為中央情報局設計一套名叫Oracle的資料庫,埃裡森是程式設計師之一。下面是小編整理的關於oracle資料庫基礎知識,歡迎大家參考!

oracle資料庫基礎知識

【1】oracle資料庫基礎知識

第一篇 基本操作

--解鎖使用者 alter user 使用者 account unlock;

--鎖定使用者 alter user 使用者 account lock;

alter user scott account unlock;

--建立一個使用者yc 密碼為a create user 使用者名稱 identified by 密碼;

create user yc identified by a;

--登入不成功,會缺少create session 許可權,賦予許可權的語法 grant 許可權名 to 使用者;

grant create session to yc;

--修改密碼 alter user 使用者名稱 identified by 新密碼;

alter user yc identified by b;

--刪除使用者

drop user yc ;

--查詢表空間

select *from dba_tablespaces;

--查詢使用者資訊

select *from dba_users;

--建立表空間

create tablespace ycspace

datafile 'E:oracleappproduct11.2.0dbhome_1oradata'

size 2m

autoextend on next 2m maxsize 5m

offline ;

--建立臨時表空間

create temporary yctempspace

tempfile 'E:oracleappproduct11.2.0dbhome_1oradata'

size 2m

autoextend on next 2m maxsize 5m

offline ;

--查詢資料檔案

select *from dba_data_files;

--修改表空間

--1、修改表空間的狀態

--預設情況下是online,只有在非離線情況下才可以進行修改

alter tablespace ycspace offline ; --離線狀態,不允許任何物件對該表空間的使用,使用情況:應用需要更新或維護的時候;資料庫備份的時候

alter tablespace ycspace read write;--讀寫狀態

alter tablespace ycspace online;

alter tablespace ycspace read only; --只讀,可以查詢資訊,可以刪除表空間的物件,但是不能建立物件和修改物件 。使用情況:資料存檔的時候

--2、修改表空間的大小

--增加檔案的大小

alter database datafile 'E:oracleappproduct11.2.0dbhome_1oradata' resize 10m;

--增加資料檔案

alter tablespace ycspace add datafile 'E:oracleappproduct11.2.0dbhome_1oradata' size 2m;

--刪除表空間的資料檔案

alter tablespace 表空間的名字 drop datafile 資料檔名;

--刪除表空間

drop tablespace ycspace;

--刪除表空間且表空間中的內容和資料檔案

drop tablespace ycspace including contents and datafiles;

--指定表空間 的 建立使用者的語法

create user yc1 identified by a default tablespace ycspace temporary tablespace temp;

--刪除使用者

drop user yc1;

--許可權

--賦予建立會話的許可權

grant create session to yc1;

--建立一個表

create table studentInfo(

sid int,

sname varchar2(10)

);

--賦予yc1使用者建立表的許可權

grant create table to yc1;

--賦予yc1使用表空間的許可權

grant unlimited tablespace to yc1;

--系統許可權

--物件許可權

--插入

insert into studentInfo values (2,'abcd');

--查詢

select *from studentInfo;

--修改

update studentInfo set sid=1;

--刪除

delete studentInfo ;

drop table studentInfo; --系統許可權刪除表

--賦權的語法

--系統許可權

grant 許可權名(系統許可權或物件許可權,角色,all) to 使用者(角色,public) with admin option;

--物件許可權

grant 許可權名(系統許可權或物件許可權,角色,all) on 使用者(角色,public) with grant option;

--收權語法

--系統許可權

revoke 許可權名(系統許可權或物件許可權,角色,all) from 使用者(角色,public) with admin option;

--物件許可權

revoke 許可權名(系統許可權或物件許可權,角色,all) from 使用者(角色,public) with grant option;

--賦予建立使用者的許可權並且把這個許可權傳遞下去,即yc1可以給別人賦權

grant create user to yc1 with admin option;

--收回許可權,只能收回scottd ,不能收回由scott賦權的yc1的許可權

revoke create user from scott;

--檢視使用者所具有的許可權

select *from user_sys_privs;

--物件許可權詳解

select * from emp;

--使用yc1來查詢scott裡面的emp表

select * from ;

--賦予yc1查詢emp表和插入的許可權

grant select on emp to yc1;

grant insert on emp to yc1;

grant update(empno,ename) on emp to yc1;

grant delete on emp to yc1;

--對scott的emp表新增資料

insert into (empno,ename) value(111,'acv');

update set ename='yc'where empno=111;

--賦予查詢、賦予刪除、新增、修改

grant select on 表名 to 使用者

--grant select,delete,update,insert on 表名 to 使用者

grant select,delete,update,insert on emp to yc1;

grant all on dept to yc1; --all代表所有的物件許可權

select *from ;

select *from ;

insert into values(50,'企事業文化部','bumen');

--檢視角色

--dba:資料庫管理員,系統最高許可權,可以建立資料結構(表空間等)

--resource:可以建立實體(表、檢視),不可以建立資料庫的結構

--connect:連線的許可權,可以登入資料庫,但是不可以建立實體和不可以建立資料庫結構

select *from role_sys_privs;

grant connect to yc1;

--將可以連線的角色賦予給yc1,則yc1就是應該可以連線資料庫的人,類似於 create session 。

create table StuInfos(sid int);

select *from StuInfos;

create table stuInfo(

sid int primary key , --主鍵 primary key 非空且唯一 (主鍵約束)

sname varchar2(10) not null, --姓名不能為空,(非空約束)

sex char(2) check(sex in('男','女')), --(檢查約束),check,

age number(3,1) constraint ck_stuInfo_age check(age>10 and age<100) , --也可以用varchar ;age between 10 and 100 ,在10和100之間,是一個閉區間

tel number(15) unique not null, --唯一約束,

address varchar2(200) default '什麼鬼'

)

insert into stuInfo values(3,'大大','男',18,4321543,default);

insert into stuInfo values(1,'張三','男',10);

select *from stuInfo;

drop table stuInfo;

create table classInfo(

cid int primary key, --班級id

cname varchar2(20) not null unique --班級名

)

create table stuInfo(

sid int primary key,

sname varchar2(20),

cid int constraint fofk_stuInfo_cid references classInfo(cid) on delete cascade

)

insert into classInfo values(1,'1班');

insert into classInfo values(2,'2班');

insert into classInfo values(3,'3班');

insert into classInfo values(4,'4班');

select *from classInfo;

select *from stuInfo;

insert into stuInfo values(1001,'張三',2);

insert into stuInfo values(1002,'張四',4);

update classInfo set cid=1 where cid=8;

drop table stuInfo;--要先刪除這個

drop table classInfo; --再刪除這個

delete classInfo where cid=4 ;--同時刪除這兩個表中的4

--刪除使用者的時候

drop user yc1 [cascade] --刪除使用者的同時把它建立的物件都一起刪除

--修改表

--1、新增表中欄位

--alter table 表名 add 欄位名 型別

alter table classInfo add status varchar2(10) default '未畢業'

--2、修改已有欄位的資料型別

--alter table 表名 modify 欄位名 型別

alter table classInfo modify status number(1)

--3、修改欄位名

--alter table 表名 rename column 舊欄位名 to 新的欄位名

alter table classInfo rename column cname to 班級名;

--4、刪除欄位

--alter table 表名 drop column 欄位名

alter table classInfo drop column status ;

--5、修改表名

--rename 舊錶名 to 新表名

rename classInfo to 班級資訊;

--刪除表

--1、截斷表效率高,每刪除一次會產生一次日誌 2、截斷會釋放空間,而delete不會釋放空間

--刪除表結構和資料

drop table 表名;

--刪除表中所有資料

truncate table classInfo;

delete classInfo;

create table classInfo(

cid int primary key, --班級id

cname varchar2(20) not null unique , --班級名

stasuts varchar2(100)

);

select *from classInfo;

--資料的`操作

--增加資料語法

--insert into 表名[(列名,....)] values (對應的資料的值);

insert into classInfo values(1,'一班','未畢業');--需要按照表結構的順序插入

insert into classInfo values(4,'六班','未畢業');

insert into classInfo(cname,cid) values('二班',2); --需要按照括號中的順序插入,但是 not null primary key 必須插入的。

insert into classInfo(cname,cid) values('三班',3);

--刪除的語法

--delete 表名 [where 條件]

delete classInfo where cid>=2;

--修改記錄的語法

--update 表名 set [欄位='值' ] [where 條件]

update classInfo set cname='三班'; --會修改所有該欄位

update classInfo set cname='四班' where cid=1;

update classInfo set cname='五班', stasuts ='未畢業' where cid=3;

--alter table classInfo drop constraint SYS_C0011213;

--新增多個時可以使用序列

--用序列來做自動增長

create sequence seq_classInfo_cid start with 1001 increment by 1;

insert into classInfo values(seq_classInfo_val,'七班','未畢業');

insert into classInfo values(seq_classInfo_val,'八班','未畢業');

insert into classInfo values(seq_classInfo_val,'九班','未畢業');

insert into classInfo values(seq_classInfo_val,'十班','未畢業');

create table classInfo2(

cid int primary key, --班級id

cname varchar2(20) not null unique , --班級名

stasuts varchar2(100)

);

select *from classInfo2;

drop table classInfo2;

insert into classInfo2 select *from classInfo;

insert into classInfo(cname,cid) select cname,cid from classInfo;

alter table classInfo2 drop constraint SYS_C0011213;

select seq_classInfo_val from dual;

select seq_classInfo_val from dual;

--直接建立一個新表,並拿到另一個表其中的資料

create table newTable as select cname,cid from classInfo;

create table newTable1 as select *from classInfo;

select *from newTable;

select *from newTable1;

insert into newTable1 values(1008,'dg','');

第二篇:高階操作

直接在使用scott登陸,進行查詢操作

----------------------------------------------------------------------------------

--簡單查詢

select *from emp;

select empno as id,ename as name from emp;

select empno 編號,ename 姓名 from emp;

--去除重複

select job from emp;

select distinct job from emp;

select job,deptno from emp;

select distinct job,deptno from emp;

--字串的連線

select '員工編號是' ||empno || '姓名是' ||ename ||'工作是'||job from emp;