1. 程式人生 > >oracle基礎入門知識

oracle基礎入門知識

1、Oracle連線: 使用者  conn/@資料庫名

 2、檔案操作命令:

                       (1)說明:執行sql指令碼

                         SQL>@  D:\a.sql  或者  SQL>START   D:\a.sql                  

          (2)edit:該命令可以編輯指定的sql指令碼

           SQL>edit d:\a.sql 

3spool:該命令可以將sql*plus螢幕上的內容輸出到指定檔案中去

           SQL>spool d:\b.sql

並輸入SQL>spool off

3、設定長寬命令:

1linesize【設定顯示行的寬度,預設是80個字元】

2pagesize【設定每頁顯示的行數目,預設是14

     SQL>show linesize

     SQL>set linesize 90

4、清屏:clear

一、建立使用者:

需要dba才能使用resource建立表

如:新增小明新使用者

SQL>connectsyetem/mannger

SQL>create userxiaomingidentified by m123

:密碼必須是字母開頭

createuser“建立”identified by“由...鑑定”

 二、修改密碼 (自己修改自己密碼直接password,修改其他使用者需要dba許可權)

SQL>alter user 使用者名稱 identified by 新密碼;(其他使用者)

SQL>passwordm1234;(自己)

三、刪除使用者(其他使用者刪除需要dropuser許可權)

SQL>drop userxiaomingcascade

四、許可權:

1.系統許可權:使用者對資料庫的許可權

2.物件許可權:使用者對其他使用者的資料物件操作的許可權

(資料物件:表、儲存過程、觸發器、檢視...

grant:授予revoke:收回

SQL>grantselect on empto xiaoming

SQL>revoke select on emp fromxiaoming

3.角色:connectdbaresource(建立)

角色分為兩部分:預定義角色和自定義角色

4.對許可權維護:

如:希望xiaoming使用者可以去查詢scottemp/還希望小明可以把這個許可權繼續給別人(傳遞)

1)如果是物件許可權,就加入with grant option ,但是不能授予角色

SQL>conn scott/m123

SQL>grant select on emp to xiaoming with grant option

2)如果是系統許可權

systemxiaoming許可權時:

  SQL>grant connect to xiaoming with admin option

*3)如果scottxiaomingemp表的查詢許可權回收,xiaohong也會收回許可權;物件許可權級聯回收

五、使用profile管理使用者口令(dba許可權)

1.賬戶鎖定(使用者最多輸入密碼次數,鎖定時間)

如:指定xiaoming最多3次嘗試登陸,鎖定時間2

(格式:SQL>createprofile 建立規則名稱limit failed_login_attempts次數password_lock_time時間;

SQL>alter 使用者名稱 profile 建立規則名稱;)

SQL>createprofileaaa1 limit failed_login_attempts 3password_lock_time 2

SQL>alter user xiaoming profile aaa1

注:輸錯3次以後再正確輸入還是無法進入

2.賬戶解鎖

SQL>alter user 使用者名稱account unlock

3.終止口令(讓使用者定期修改密碼)

如:給xiaoming建立profile檔案,要求使用者每隔10天要修改登入密碼,寬限期2

SQL>create profile myprofile password_life_time 10 password_grace_time2;

SQL>alter user xiaoming profile myprofile

4.口令歷史(不能使用以前用過的密碼)

1)建立profile

SQL>create profile myprofile password_life_time 10 password_grace_time 2password_reuse_time 10;

password_reuse_time:口令可重用時間10天”

(2)分配給某個使用者

3)刪除profile

SQL>drop profile myprofile cascade

***第六章、表的管理

1、表建立條件

.必須以字母開頭

.長度不能超過30字元

.不能使用orcale的保留字(如:oracle

2、字元型

.char定長最長2000字元(查詢速度快)

.varchar220變長最大4000字元(節省空間)

.clob字元型大物件,最大4G

3、數字型別

number範圍:-1038次方~1038次方

number5,2):一個小數有5為有效數,2為小數

number5):一個5位整數

4、日期型別

date包含年月日和時分秒

timestamp精確十分高

to_char時間格式轉換

to_date字串與時間互換

5、圖片

blob二進位制資料可以存放圖片/聲音4G

(一般圖片和聲音不放在資料庫裡,如果要求保密性高就將圖片儲存路徑存在資料庫裡)

6、建立表

查看錶:desc student

檢視資料庫中所有的表:

a.select * from all_tables;系統裡有許可權的表

(表名 selecrt table_name fromall_tables)

b.select * from dba_tables;系統表

c.select * from user_tables;當前使用者下的表(當表過多時,不要操作此步驟)

如:學生表

SQL>create table student

xhnumber4),-學號

xmvarchar220),-姓名

sexchar-性別

birthdaydate-出生日期

salnumber7,2),獎學金

);

班級表

SQL>createtableclasse

classid number2),=

cnamevarchar

);

1)新增欄位

SQL>alter table student add (classid number(2));

(2)修改欄位的長度

SQL>alter table student modify (xm varchar2(30));

(3)修改表的名字

SQL>rename student to stu

4)新增資料

日期:日--如:03-5-2005

修改命令:SQL>alter session set nls_date_format='yyy-mm-dd';

修改後:SQL>insert into student values ('A002'、’MIKE‘、‘男’、‘1997-08-18,10’);

查詢時間:select to_char(sysdate,'yyyy-mm-dd') from dual;

5)插入空值

SQL>insert into student xhxmsexbirthdayvalues3,‘aa’,‘女’,null);

SQL>select * from student where birthday is null

6)改欄位

SQL>update student set sal=‘女’ where xh=A001’;

SQL>update student set sal=‘女’,classid=5 where xh=A001’;

7)刪除資料

SQL>delete from student;(寫日誌,可以恢復,恢復速度慢)

恢復資料:SQL>savepoint 儲存點;(設定儲存點,儲存點個數無限制)

SQL>rollback to 儲存點;(回到儲存點)

SQL>drop table student;(刪除表的資料和結構)

SQL>truncate table student(表結構還在,不寫日誌,無法恢復,刪除速度快)

***第七章、表的查詢

1、查看錶:desc 表名;

2、查詢表:

SQL>select列名 from 表名;(查詢速度快,查詢指定的列)

SQL>select * from 表名;(查詢速度慢)

3、取消重複行(distinct重複)

SQL>select distinct deptno 列名 from 表名;

4、使用算術表示式

如:顯示每個員工的工資

SQL>select sal*13ename from 表名;

SQL>select sal*13 ‘年工資’,ename from 表名;(修改列名錶示形式)

5、處理null值(用nvl函式)

SQL>select sal*13+nvlcomm0*13‘年工資’,enamecommfrom 表名;

(顯示員工的年工資,且獎金為空的用0替代,反之則用原本的數字代替)

6、使用where語句

如:顯示工資高於3000的員工

SQL>select enamesal from 表名 where sal>3000;

如:查詢1982.1.2入職的員工

SQL>select enamehiredate from 表名 wherehiredate>2-1-1982’;

如:顯示工資在20002500的員工

SQL>select ename,sal from 表名 where sal>=2000 and sal<=2500;

7、使用like操作符

%:表示任意0到多個字元_:表示任意單個字元

如:顯示首字母為s的員工姓名和工資

SQL>select enamesal from 表名 where ename like S%’;

如:顯示第三個字元為大寫0的所有員工的姓名和工資

SQL>select enamesal from 表名 where ename like __O%'; (下劃線要兩個)

8、在where條件中用in

如:顯示empno(編號)123,345,800.。。的員工的情況

SQL>select * from 表名 where empno in123,345,800);

9、使用is null

如:顯示沒有上級的員工

SQL>select * from 表名 where mgr is null

10、使用邏輯操作符號

如:查詢工資高於500或是崗位為manager的員工,同時滿足他們的姓名首寫字母為大寫的

SQL>select * from 表名 where sal>500 or job=manager’)and enamelike J%’);

11、使用order by

如:如何按照工資的從高到低的順序顯示員工的資訊

SQL>select* fromemp order bysal desc

如:按照部門號升序而員工的工資降序排列

SQL>select* from emp order bydeptno ascsal desc

12、使用別名排列

SQL>select namesal*12 “年薪” from emp order by “年薪”;

13、日期函式

months_betweendate1,date2)函式:返回兩個日期之間的月份數

last_day(date):返回指定日期對應月份的最後一天

trunc(sysdate.'yyyy'):返回當年第一天

trunc(number,decimals):返回處理後的數值

number:待做處理的數值decimal:保留小數點後的位數

如:trunc89.985,2)=89.98

注:dicaimal可以為負,表示小數點左邊指定位數後面的部分截去

如:trunc(89.9845,-1)=80

七、表的複雜查詢

1、如何顯示員工中最高工資和最低工資

SQL>select maxsal),minsal from emp

如:查最高工資員工的姓名

SQL>select namesal from emp where sal=selectmaxsal from emp);

SQL>selectminsal),maxsal from emp

如:顯示工資高於平均工資的員工資訊

SQL>selcetavgsal from emp

SQL>selectsalname from empwhere sal>selcetavgsal from emp);

2group byhaving語句

group by :對查詢的結果分組統計(有分組函式就加)

having:限制分組顯示結果(相當於where

如:顯示每個部門的平均工資和最高工資

SQL>select avgsal),maxsal),deptno from emp group by deptno

如:顯示每個部門的每種崗位的平均工資和最低工資

SQL>select avgsal),minsal),deptnojob fromemp group by deptnojob

如:顯示平均工資低於2000的部門號和它的平均工資

SQL>select avgsal),maxsal),deptno from emp group by deptno having avg(sal)<2000;

3、順序:group by - having - order by

八、多表查詢

1、顯示僱員名,僱員工資及所在部門的名字

SQL>select a2.namea2.sal from emp a2,empt a1 where a1.deptno=a2.deptno

多表查詢查詢的條件:至少不能少於(表的個數-1

如:顯示部門號為10的部門名、員工名、工資

SQL>select a2.name,a1.dname,a2.salfrom emp a2,empt a1wherea1.deptno=a2.deptno and a1.deptn