Oracle資料庫常用操作總結(一)
--oracle cs架構軟體 --客戶端 --tns --協議 --ip --埠 --資料庫名字
--監聽如果出了問題,先刪除所有監聽,再重建。netca。tns檔案中名字不能重複,
--oracle預設自帶兩個管理員使用者 sys system 這兩個使用者在登入時候必須使用sysdba角色,一般我們用system來進行管理, --但是如果system搞不定,在用sys
--角色就是許可權的集合
--normal 普通使用者登入時候的許可權 --sysoper 系統操作員許可權 --sysdba 最NB oracle管理員的許可權
--oralce 服務的開啟與關閉 --oracle的主服務是OracleService資料庫名字 select status from v$instance;--檢視資料庫的例項的執行情況,如果是open,說明資料庫開啟狀態 --資料庫的狀態 close nomount mount open shutdown immediate;--關閉資料庫,出現oracle例程已關閉說明資料庫已經關了 startup ;--開啟資料庫,資料庫已經開啟 --oracle監聽服務的開啟與關閉 --oralce的監聽服務Oracle。。。TNSlistener --啟動監聽 --lsnrctl start --關閉監聽 --lsnrctl stop --檢視監聽狀態 --lsnrctl status
--資料庫的物件(objects)觸發器,佇列,表(資料庫中存數資料的容器),檢視,索引等等。
--使用者 --建立使用者 --語法:create user 使用者名稱 identified by 密碼;在建立使用者時候oracle會預設把名字小寫改為大寫,建立使用者不能同名 --新建一個使用者叫matasha密碼為shuiya create user matasha identified by shuiya; --建立一個使用者叫bulutuo密碼aolifo create user bulutuo identified by aolifo;
--授權 一般來說是管理員來授權 --授權語法grant 角色 to 使用者,預設建立一個普通使用者時候,只給connect和resource許可權 connect --連線資料庫 resource --使用資料庫資源 dba --最NB --給bulutuo授權可以登入並且使用資料庫資源 grant connect,resource to bulutuo;
--收回許可權 --revoke 許可權 from 使用者名稱 revoke connect,resource from misamisa;
--在同桌電腦上建立一個使用者,然後用自己電腦登入同桌資料庫(用建立的使用者)
--1.在server端建立使用者並且授權
--2.配置客戶端tns,連線server端資料庫
---------c------------/---------------s tns2 建立使用者並授權1 172.11.1.250 172.11.1.181(orcl) create user bbs identified by bbs; grant connect ,resource to bbs; --在寫sql的時候要加分號
--表空間 --儲存資料庫物件的容器 --表空間分為 資料表空間和臨時表空間 --資料表空間--存放資料位置 --臨時表空間--存放臨時資料的地方(快取) --建立資料表空間的語法 create tablespace 表空間名 datafile '檔案位置' size 大小 【 autoextend on --自動擴充套件功能,假如我的資料表空間滿了,那麼自動擴充套件 next 大小 --每次自動擴充套件的大小 maxsize 大小 --最大大小 】 create tablespace bank_wawa2 datafile 'F:\tb\bank_wawa2' size 5m; --建立表空間bank_wawa 資料檔案儲存在F:\tb\bank_wawa.dbf 大小為5m,bank_wawa 名字一定要明確是什麼功能。。dbf 資料庫檔案的意思,但是不 --強制使用 database file,強調檔案儲存路徑要寫,大小(預設)要寫。
create tablespace shanghai1 datafile 'F:\tb\shanghai1.dbf' size 5m autoextend on next 1m maxsize 10m; --建立表空間shanghai 資料檔案儲存在F:\tb\shanghai.dbf,預設大小5m,自動擴充套件功能開啟,空間不足時候,每次自動擴充套件1m,最大大小10m
--臨時表空間 create temporary tablespace 表空間名字 tempfile '檔案位置' size 大小 【 autoextend on --自動擴充套件功能,假如我的臨時表空間滿了,那麼自動擴充套件 next 大小 --每次自動擴充套件的大小 maxsize 大小 --最大大小 】 create temporary tablespace shanghai1_tmp tempfile 'F:\tb\shanghai1_tmp.dbf' size 5m autoextend on next 1m maxsize 10m;
--建立使用者 create user 使用者名稱 identified by 密碼 [ default tablespace 資料表空間名字 temporary tablespace 臨時表空間名字 ]; --建立一個使用者 dollar 指定表空間 設定密碼 create user dollar identified by dollar default tablespace shanghai temporary tablespace shanghai_tmp ;
--如果新建使用者需要制定表空間,那麼,在建立使用者之前必須要建立表空間。 --舉個栗子 --建立使用者 --1.建立資料表空間和臨時表空間 create tablespace shenzhen datafile 'F:\tb\shenzhen.dbf' size 5m; create temporary tablespace shenzhen_tmp tempfile 'F:\tb\shenzhen_tmp.dbf' size 5m; --2.建立使用者指定表空間和密碼 create user renminbi identified by renminbi default tablespace shenzhen temporary tablespace shenzhen_tmp; --3.授權 grant connect,resource to renminbi;
修改使用者資訊 --改密碼(管理員 使用者本身) --語法: alter user 使用者名稱 identified by密碼; --忘記密碼 cmd --sqlplus / as sysdba --conn 使用者名稱/密碼 可以登入 alter user renminbi identified by test; --更改使用者renminbi的密碼為test
--解鎖和鎖定使用者 alter user 使用者名稱 account unlock(lock); alter user 使用者名稱 identified by 密碼 account unlock;
--解鎖scott使用者 alter user scott account unlock; --解鎖scott使用者並且指定密碼為tiger alter user scott identified by tiger account unlock; --鎖定使用者 無法登入 alter user scott account lock;
--修改使用者預設資訊 alter user 使用者名稱 default tablespace 資料表空間名字 temporary tablespace 臨時表空間名字; --資料表空間一定要指定為資料表空間
--把renmibi使用者的預設資料表空間修改為bank_wawa alter user renminbi default tablespace bank_wawa;
--用自己建立的使用者登入(普通使用者)
--刪除表空間 drop tablespace 表空間名字
--建表 建立一個excel --表結構 列的資訊 --查看錶結構的命令 --1.desc 表名 --2.右鍵表名 view --建立表的語法 /* create table 表名( 列名1 資料型別 (約束), 列名2 資料型別 (約束) );*/ --建立(複製表) --資料型別 就是對資料的一種約束 --日期 1986-03-23 --數字 1234567 --字元 hello --關鍵字 藍綠色 oralce內部是有定義的所以不建議使用 --建立一個使用者表名字為users 裡面有2個列 一個叫cname列 資料型別為數字 ,money 數字型別 create table users1( cname number, money number );
--約束 對資料的一種規範化操作 --主鍵約束 --主要針對於外來鍵約束,它實現的功能就是通過一個條件只能找到唯一的一個數據 --唯一約束 --現的功能就是通過一個條件只能找到唯一的一個數據 --檢查約束 --檢查該列的資料是否滿足檢查條件 /*外來鍵約束(參考約束) 參考另外一張表裡面的資料 預設值等等 語法 create table 表名( 列1 資料型別 constraint 約束名 primary key, 列2 資料型別 unique, 列3 資料型別 default 預設值, 列4 資料型別 constraint 約束名 check (列4=資料 and(or) 列4=資料。。) 列5 資料型別 constraint 約束名 references 被參考的表(被參考的列) 列 6 資料型別 not null(null) ); constraint 約束 後面跟的都是約束的名字,如果在新建表,裡面有約束的情況下不指定約束名字,oralce會預設給這個約束一個名字
--產看約束的方法 1.view 2.小箭頭*/ --建立表money,裡面有一個列,是主鍵約束,沒有指定約束名字 create table money ( money number primary key );
--建立表moenyforme 裡面一個列 主鍵約束,指定約束名字(工作習慣) --約束名一般是約束型別_表名_列名 方便故障時候排查,約束不能重名 create table moneyforme ( money number constraint pk_moneyforme_money primary key );
--建立表atm 裡面2個列,一個為唯一約束 create table atm ( loc varchar2(20), money number constraint un_atm_money unique );
--建立表baoan裡面1個列預設值為交通 create table baoan ( bname varchar2(20) default '交通' );
/*預設值的資料型別一定要和列一樣 字元型別資料必須加單引號 數字型別直接寫 sysdate
char 定長 varchar2(10)可變長度*/
--檢查約束 --建立一張表student 裡面要求性別為男或者女不能為泰國 create table student ( cname varchar2(20), sex varchar2(20) constraint ck_student_sex check(sex='男' or sex='女') --check後面檢查的條件只能是本列 );
--外來鍵約束 --兩張表 一張是被參考表,另外一個是參考表 --1.被參考表一定要存在並且被參考列一定是主鍵約束或者唯一約束
--2.參考表的列與被參考表的列的資料型別一定要一致
--建立被參考表 create table bck ( bclass varchar2(20) unique );
create table ck ( id number, cclass varchar2(20) constraint fk_ck_cclass references bck(bclass) );
create table all1 ( money number constraint pk_all_money primary key, money1 number constraint un_all_money1 unique, cname varchar2(20), bname varchar2(20) default '交通', sex varchar2(20) constraint ck_all_sex check(sex='男' or sex='女'), cclass varchar2(20) constraint fk_all_cclass references bck(bclass) );
--刪除列 alter table 表名 drop column 列名 alter table STUDENT drop column cname; --修改列的資料型別 alter table 表名 modify 列 資料型別 (約束) alter table STUDENT modify cname number; --刪除約束 alter table表名 drop constraint 約束名 alter table STUDENT drop constraint CK_STUDENT_SEX; --重新命名列 alter table 表名 rename column 舊列名 to 新列名 alter table STUDENT rename column cname to ccc; --在修改的時候需要注意約束。
create table test1 ( bname varchar2(20) default '交通' not null unique );
alter table test1 rename to test2;
--事務 一件事情的開端與結尾,有開始必然要有結束
--開端 --DML語句 : Data Manipulation Language 資料操縱語言 使使用者能夠查詢資料庫以及操作已有資料庫中的資料的計算機語言
--結束 --commit; 提交資料,永久儲存 --rollback; 回滾
增 1. insert into 表名 values (值1,值2。。。) insert into atm values('usa',100); commit;
insert into atm(loc) values('usa1'); commit;
insert into atm(money) values(200); commit;
--插入資料時,如果列沒做約束,該列無資料插入,則預設為空 --插入字元型別資料,必須用單引號 --日期型別資料 固定日期 to_date 系統當前日期 sysdate create table bbs( cname varchar2(20), id number, bday date );
insert into bbs values ('黑哥真自戀',100,sysdate); commit;
insert into bbs(bday) values (to_date('1983-10-12','YYYY-MM-DD')); commit;
--to_date函式 insert into bbs(bday) values(to_date('1999-09/09 18:01:02','YYYY-MM/DD HH24:MI:SS')); commit; --YYYY 表示年 --MM 表示月 --DD 表示日 --hh24 24小時顯示 --mi 分鐘 --ss 秒
--測試驗證主鍵約束與唯一約束 create table vi( co number unique, bo number primary key );
insert into vi values(1,1); commit; insert into vi values(2,2); commit;
create table sushe( susheming varchar2(10) default'漢', sushetel number check (sushetel >= 6330000 and sushetel <= 6339999) );
--預設值的測試 insert into sushe values(null,null); commit;
insert into sushe(sushetel) values(6338888); commit;
--檢查約束 範圍外的值插入 insert into sushe(sushetel) values(100); commit;
--外來鍵參考約束測試 --向被參考表中插入資料 insert into bck values('leohao'); insert into bck values('muji'); commit;
--向參考表中插入資料 insert into ck values(10,'muji'); insert into ck values(10,'moua'); commit;
alter user scott account unlock; --scott 使用者預設密碼 tiger
--查詢 select 列 from 表; --可以是一列或者多列,列之間用,連線,全表查詢用*
select * from atm t; --別名不是必須存在 --查詢atm表中所有行所有列的資料 *表示全表查詢
--單獨查詢atm表中money列的值 select money from atm; --查詢atm表中money列與loc列的資料,按查詢順序排列 select money,loc from atm;
--語法結構 --select 列 from 表 where 過濾條件 --select後面 from前面是與列有關的,where後面是與行有關的 --where後面的過濾條件,列和值資料型別要一致
select * from atm where loc = 'usa'; --查詢 atm表中loc為 usa 的所有資訊
select * from atm where money = 200; --查詢 atm表中money為 200 的所有資訊
select * from bbs where bday = to_date('1983-10-12','YYYY-MM-DD'); --查詢 bbs 表中 bday為 ‘1983-10-12’的所有資訊
/*關係運算符 and 同時滿足條件 or 滿足一個條件 查詢的時候可以用多個條件*/
--查詢20號部門工資為800的人的所有資訊 select * from emp where deptno = 20 and sal = 800;
--在多次使用and和or的時候,建議用括號
--查詢工資為800或者1600的人的所有資訊 select * from emp where sal = 800 or sal = 1600;
--where後面的結果 一定是true 或者false,如果結果為true,返回所有滿足條件的內容 --如果結果為false,沒有資料返回,也不會報錯 select * from emp where 1=1; select * from emp where 1=2;
--複製表 create table 表名 as select * from 表 where 條件 create table emp2 as select * from emp;
--複製表結構 create table emp3 as select * from emp where 1=2;
--簡單查詢補充 /*比較運算子 > < = >= <= != <> --不等於 */
select * from emp where sal > 800; select * from emp where sal <>800; select * from emp where sal !=800;
--錯誤的寫法 =< => --查詢工資範圍在1500到2000的人的姓名 select ename from emp where sal >= 1500 and sal <= 2000;
--between and 區間範圍 包括兩個點 select * from emp where sal between 1500 and 2000;
--in any all
--in 在。。。裡面 --工資在800,1500,2000的人所有資訊 select * from emp where sal in (800,1500,2000); --未查詢到in括號內的值,不予返回
--any 任何一個 select * from emp where sal > any (800,1500,2000); select * from emp where sal > 800 or sal > 1500 or sal > 2000; select * from emp where sal >800;
--all 所有 select * from emp where sal > all (800,1600,2000); select * from emp where sal >800 and sal > 1600 and sal > 2000; select * from emp where sal > 2000;
--模糊查詢 --like %代表任意字元 _ 代表一個字元 --條件不完整,一般用於字元型別 --只記得姓不記得名字
--查詢以SCO開頭的人的所有資訊 select * from emp where ename like 'SCO%'; --SCO區分大小寫
--查詢以TT結尾的人的所有資訊 select * from emp where ename like '%TT';
--查詢中間為O的人的所有資訊 select * from emp where ename like '%O%';
--查詢名字為5個字元的人的所有資訊 select * from emp where ename like '_____';
--查詢O後面帶有兩個字元的人的所有資訊 select * from emp where ename like '%O__';
--空值 NULL --空值 就是一個未知數 有可能是8 也有可能是100萬 也可能是1986 --空值 不能用於比較 select * from emp where sal = null; select * from emp where null+1 > NULL; select * from emp where 1 > NULL;
--查詢空值 is null 或者 is not null select * from emp where comm is null; select * from emp where comm is not null;
--空值處理函式nvl 僅用於顯示 --nvl 判斷該列是否為空,如果為空設定成什麼 --不在資料庫中做修改 select * from emp; select comm,nvl(comm,100000) from emp;
--函式 資料庫中的資料不會變化,用於顯示 --upper(列) 把資料變成大寫 --lower(列) 把資料變成小寫 --initcap(列) 把資料首字母奕大寫 select ename,upper(ename) from emp; select ename,lower(ename) from emp; select ename,initcap(ename) from emp;
--查詢scott使用者的所有資料 select * from emp where lower(ename) = 'scott'; select * from emp where upper(ename) = 'SCOTT'; select * from emp where INITCAP(ename) = 'Scott';
--修改資料 --update 表名 set 列 = 值 where 條件
--修改emp2表中名字為SCOTT的人,工資改為100 update emp2 set sal = 100 where ename = 'SCOTT'; commit;
--如果不用WHERE字句 ,那麼預設修改的就是整個列的值 update emp2 set sal = 20; commit;
--刪除表 drop table 表名
--刪除資料 --delete 可以刪除部分資料 DML語句 ,可以恢復 --delete 表名 where 條件
delete emp2 where ename = 'SCOTT'; commit;
--truncate 截斷 DDL語句 只能全表刪除,資料不可以恢復 truncate table emp3; delete table 表名
/*dml:資料庫操作語句,常見的增、刪、改、查(最重要) 事務:通俗理解就是做一件事情的過程,這個過程有兩種結果:成功、失敗,在資料庫中發出一條dml語句即開啟一個事務,開啟事務就一定要結束事務, 結束事務的兩種方式: a1)、commit,提交事務,所有的操作都生效,操作的結果直會永久儲存在資料庫中 a2)、rollback,回滾事務,所有的操作都不會生效,操作的結果直接扔掉,不會永久儲存到資料庫中
基本sql(結構化查詢語言)語言:ddl、dml、dcl、tcl dml:資料庫操作語句 insert delete update select(重點) DDL:資料庫定義語句 create alter drop rename truncate DCL:資料庫控制語句 grant revoke TCL:事物控制語句 commit rollback savepoint */ --簡單查詢 select * from emp where ename = 'SCOTT'; select * from emp where sal > 800; select * from emp where hiredate > to_date('1981/04/02','YYYY-MM-DD'); select * from emp where sal > any(100,600,1600); select * from emp where sal > all(100,600,1600); select * from emp where sal in (800,1500); select * from emp where ename like 'AL%';
create table mm ( mm number );
create table nn ( nn number );
insert into mm values(1); insert into mm values(2); insert into mm values(3); insert into nn values(1); insert into nn values(2); insert into nn values(4); commit;
--內聯查詢 兩張表都有的資料 --內聯的寫法
--1. --select 列 from 表1,表2 where 表1.列 = 表2.列 select * from mm,nn where mm.mm = nn.nn;
--2. --select 列 from 表1 inner join 表2 on (表1.列 = 表2.列) select * from mm inner join nn on (mm.mm = nn.nn);
--假如有三張表用內聯,用兩個關聯來進行處理 --舉個例子 emp dept 表 --查詢20號部門的員工姓名和部門位置 select e.ename,d.loc from emp e,dept d where e.deptno = d.deptno --建議連線 and e.deptno = 20; --條件過濾 select emp.ename,dept.loc from emp,dept where emp.deptno = dept.deptno and emp.deptno = 20; --查詢 30號 部門的人名 工資 部門號 部門名字
/* --1.做多表查詢的時候,首先分析需要查詢的列有哪些,分別來源於哪些表 30號部門 emp dept 人名 emp 工資 emp 部門號 emp 部門名字 dept --確定使用兩張表,必須建立關聯關係 */
/* 2.分析多表的關係 SCOTT>desc emp emp表 名稱 是否為空? 型別 EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) SCOTT>desc dept 名稱 是否為空? 型別 ----------------------------------------- -------- --------------------
DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13) */
select * from emp,dept where emp.deptno = dept.deptno; --表建立關係
3.查詢需要的列 select emp.ename, emp.sal, dept.deptno, dept.dname --查詢所需要的列 from emp, dept --從哪些表中查詢 where emp.deptno = dept.deptno --建立關聯 and dept.deptno = 30; --條件過濾
select e.ename,e.sal,e.deptno,d.dname from emp e,dept d where e.deptno = d.deptno and e.deptno = 30;