一個認知:
? ? ? ?占市場份額最多的大型關系型數(shù)據(jù)庫钞诡。
兩個概念:
? ? ? ?數(shù)據(jù)庫:存儲數(shù)據(jù)的倉庫胀糜。
? ? ? ?關系型數(shù)據(jù)庫:數(shù)據(jù)庫中保存的數(shù)據(jù)對象之間可以存在一些聯(lián)系垢油。
三個名詞:
? ? ? ? sql:操作數(shù)據(jù)庫的結構化語言壕探,是一種符合ANSI標準的計算機語言。
? ? ? ? sql*plus:Oracle自帶的可以輸入sql語言的終端与倡,并將最近結果展示在終端上先改。
? ? ? ? plsql:程序化的sql語言。在sql語言基礎上新加來一些邏輯蒸走,如if else仇奶,使之形成sql塊,完成復雜功能比驻。
四種對象:
? ? ? ? table——表:表格该溯,由行(元組)和列(字段)組成,每一行即一條完整的數(shù)據(jù)别惦。
? ? ????view——視圖
????????序列
????????索引
五種分類:
? ? ????針對sql語句進行分類狈茉。
? ? ? ? DDL(數(shù)據(jù)定義語言):create
? ? ? ? DQL(數(shù)據(jù)查詢語言):select
? ? ? ? DML(數(shù)據(jù)操作語言):insert(增) delete(刪) update(改)
? ? ? ? DTL(事務控制語言):commit(提交) rollback(回滾)
? ? ? ? DCL(數(shù)據(jù)控制語言):grant(授予權限) revoke(回收權限)
sqlplus常用命令
1、conn【ect】
用法:(1)conn username/ password 【as sysdba】
? ? ? ? ? ?(2)conn 回車以后再輸入用戶名密碼
建議不要以sys用戶的身份登錄掸掸。
2氯庆、disc【onnect】斷開連接
3、passw【ord】更改密碼
4扰付、show user 顯示當前用戶名
5堤撵、clear scr【een】清屏
6、exit 該命令會斷開與Oracle的連接羽莺,并關閉sqlplus窗口
sqlplus的交互命令
1实昨、edit
說明:可以編輯指定的sql腳本
2、spool
該命令可以將sqlplus屏幕上的內(nèi)容輸入的指定的sql文件中盐固。
spool xxxx.sql
要往xxxx.sql里面輸入的東西
spool.off
sqlplus顯示的位置
1荒给、linesize
說明:設置每一行顯示的寬度,默認是80個字符刁卜。
sql>show linesize
sql>set linsize 字符大小
2志电、pagesize
說明:設置每一行顯示的條目,默認是14行蛔趴。
sql>show pagesize
sql>set pagesize 字符大小
Oracle基本操作:
——Oracle用戶的管理
1挑辆、創(chuàng)建用戶
create user username identified by password;
2夺脾、刪除用戶
drop user username之拨;
用戶管理的綜合案例:
概述:創(chuàng)建出來的briup的用戶是沒有任何權限的,連最基本的登錄數(shù)據(jù)庫的權限都沒有咧叭,需要為它指定相應的權限。
授予權限:grant
回收權限:revoke
grant 具體權限 to username
想要讓briup用戶登錄到數(shù)據(jù)庫:
grant connect to briup
grant create_database_link to briup;
revoke 具體權限 from username
角色
自定義角色:根據(jù)用戶的需求添加權限到某個角色權限烁竭。
預定義角色:預定義好的角色菲茬。
權限
系統(tǒng)權限:用戶對數(shù)據(jù)庫的相關操作權限。
通過system不僅要賦予xiaohong登錄到數(shù)據(jù)庫的權限,還要讓xiaohong能夠給其他人賦予登錄權限婉弹。
連接system睬魂。
create user xiaohong identified by w2019;
grant connect to xiaohong镀赌;
連接xiaohong氯哮。
create user xiaoming identified by w2019;
grant connect to xiaohong with admin option商佛;
grant connect to xiaoming喉钢;
對象權限:用戶是否有對其他用戶的對象進行操作的權限。
用戶口令:profile
Oracle在創(chuàng)建完成之后就會一個default的profile良姆。
用戶的鎖定
指定某一個用戶如果連續(xù)輸錯3次密碼肠虽,就將它鎖定,在2天以后再自動解鎖玛追。
連接system税课。
create profile loak_account limit failed _login_attempts 3 password_lock_time 2;?
將上面創(chuàng)建好的profile賦予給xiaohong。
alter user xiaohong profile loak_account;
xiaohong被鎖定之后痊剖,登錄system韩玩。
alter user xiaohong account unloak;
Oracle中表(表格,二維表陆馁,table)的操作
Oracle在表名的命名規(guī)范:
1啸如、表名以字母開頭
2、表名的大小不能超過30個字符
3氮惯、不能出現(xiàn)Oracle中的關鍵字
4叮雳、名字中只能包含A-Z,a-z,0-9妇汗,_帘不,$
create table tablename (
字段名1 字段類型,
字段名2 字段類型杨箭,
……
字段名N 字段類型)
;
括號和字母之間可以不用空格互婿。
Oracle中的數(shù)據(jù)類型:
1、字符型
char:最大2000個字符 定長
????name char(10)???? ‘Andy? ? ? ’
varchar2:最大可以表示4000個字符 變長
????name varchar2(10)???? ‘Andy’
clob(character lager object)字符型大對象:最大存儲空間4G
2慈参、數(shù)字型
number????-10的38次方——10的38次方
????sal number(5)? ? -99999——99999
????sal number(5,2)???? -999.99——999.99? ? 5表示有5個有效數(shù)字,2表示有兩位是小數(shù)
3驮配、時間型:
????date:年月日時分秒着茸,不不必指定長度
????timestamp:時間戳始衅,可以表示秒之后的小數(shù)
4拴驮、圖片:
????blob? ? 二進制文件蹄衷,圖片他挎,視屏毒费,音頻
????一般存儲相關文件的路徑
案例:創(chuàng)建一個學生表
? ? ? ? ? ?學號?名字? 年齡?? 性別?生日?? 工資
create table student_table(
no number(4),
name varchar2(20),
age number(3),
sex char(3),
brithday date,
sal number(7,2)
);
查看表結構:desc tablename伟阔;
對表的修改操作
添加一個字段:
alter table tablename add(字段名 字段類型(長度))
案例:向之前創(chuàng)建好的學生表中添加一個字段。
alter table student_table add(classid number(2));
修改一個字段:
alter table tablename modify(字段名 字段類型(長度))
alter table student_table modify (no number(5));
alter table student_table modify (no char(5));
修改一個字段的字段名:
alter table tablename rename column 舊名字 to 新名字减俏;
alter table student_table rename column no to student_to;
刪除一個字段:
alter table tablename drop column 字段名;
alter table student_table drop column classid;
對表重命名:
rename oldname to newname;
rename student_table to student;
刪除表:
drop table tablename娃承;
添加數(shù)據(jù):
每個字段都有數(shù)據(jù)
insert into tablename values(
1001,
‘張三’,
20,
‘男’,
‘13-JUN-1995’,
2534.67
);
因為Oracle默認的時間是dd-mm-yyyy怕篷,但是我們想讓它是yyyy-mm-dd历筝,修改語言環(huán)境:
修改語言環(huán)境:
alter session set nls_language='simplified chinese';
alter session set nls_language=english;
修改時間格式:
alter session set nls_date_format='yyyy-mm-dd'
只插入部分字段的數(shù)據(jù):
insert into student_table(no,name, age) values(1002,'李四',22);
修改數(shù)據(jù):
修改一個字段
update tablename set 要修改的字段名=要修改的值
update student_table set sex='女' where no=1001;
修改多個字段
update student_table set no='1008' ,birthday=‘1009’ where name=‘張三’;
案例:將所有女性的工資修改成之前的一半
update student_table set sal=sal/2 where sex='女’;
刪除數(shù)據(jù):
delete from tablename廊谓;可回滾
commit梳猪;不可回滾
delete from student_table;
truncate table tablename蒸痹;不可回滾
設置保存點????
savepoint pointname春弥;
回滾到某個保存點????
rollback to pointname;
案例:刪除員工表中員工編號為0020號的員工信息叠荠。
? ? ? ? ? ?delete from briup_emp where empno=0020匿沛;
? ? ? ? ? ?刪除員工表中員工工資小于5000的員工信息。
? ? ? ? ? ?delete from briup_emp where sal<5000榛鼎;
刪除表:
drop table tablename;
Oracle中的查詢:用得最多的逃呼,最復雜的
查看表結構
desc tablename;
查詢某表中所有的數(shù)據(jù)(效率低)
select * from tablename;
查詢某表中的某一個或某幾個字段
select 字段名1者娱,字段名2抡笼,…… from tablename;
select ename from briup_emp;
select ename,sal from briup_emp;
案例:查看名為Andy的工資和工作。
? ? ? ? ? ?select sal,job from briup_emp where ename='Andy';
? ? ? ? ? ?查看Andy的年工資黄鳍。
? ?????????select sal*12 from briup_emp where ename='Andy';
問題:給字段名起別名推姻。
? ? ? ? ? ?select 字段名1 別名1,字段名2 別名2际起,…… from ……;
? ? ? ? ? ?查看員工號為0001的員工的名字和工種拾碌。
? ? ? ? ? ?select ename,job from briup_emp where empno=0001;
? ? ? ? ? ?select ename||‘的工種是’||job from briup_emp where empno=0001;
案例:查看所有員工的年薪吐葱。
? ? ? ? ? ?select sal*12+comm*12 年薪 from briup_emp;(這么做有問題街望,如果有員工的錢數(shù)為 null校翔,那么最后算完的年薪也為null)
? ? ? ? ? ?select sal*12+nvl(comm,0) from briup_emp;
? ? ? ? ? ?nvl(exp1,exp2):如果exp1這個字段的數(shù)據(jù)為null,則其值為exp2灾前。