我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 盖世程序员 > Oracle增删查改 day05
总共48086条微博

动态微博

查看: 1556|回复: 0

Oracle增删查改 day05

[复制链接]

45

主题

5

听众

119

金钱

三袋弟子

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-06-03 21:43:49 |只看该作者 |倒序浏览
复习:SQL语句6 e1 `& i( O8 Z5 C8 s: t
select
8 ~: g) y$ z: @DML: insert / update / delete8 J0 h, u$ u5 C* ]
1 r6 H, h) v' c' V6 W
CRUD
7 V  @. q* ~! b, \) ^2 h6 N/ s3 F! F+ EC:Create, k' n+ E& U$ Z: X* y& Z& Y; ~
R: Retrive& I8 V+ p% w+ r' C
U: Update. {+ X2 Z4 r: r, \3 d- C
D: Delete! u2 x0 R- ?; d
. l; N: J: J( z5 p5 t3 J' G9 m
DDL: create / drop / truncate / alter
7 m* u0 I. @" E& q! z+ F4 f( v& E, {
TCL: commit / rollback / savepoint' O1 s8 i; e1 A1 D# G
3 J: V$ f) t3 w+ I5 R. w
DCL: grant / revoke
2 M5 G$ Q3 M- j/ a  u1 b: \+ ~# `- f, M2 r
grant: 赋予权限
! l7 E% j3 K& I; crevoke: 剥夺权限
* K4 Y  W! k% D/ Q4 T* A) O数据库中的用户: openlab   hr    scott( a3 ]. j6 z( P+ b. |
                 emp% e9 I2 Z5 G% q' d' p
9 M/ ^1 |3 k4 D4 K" ]  ~
假设现在的用户是openlab( r( N% `7 y; }9 b) m5 p  ~
SQL>grant select on emp to scott;2 ?1 O" i0 }* ?% K: `
SQL>revoke select on emp from scott;( p! R7 c; P3 r
" j/ w6 f0 F2 |+ \1 L5 `+ j# b2 z
scott的会话:
8 ?: m2 x3 u& M# vSQL>select * from openlab.emp;7 e8 }( L2 K' O. T
SQL>select * from emp;
! h8 S! {( R3 l. ]: V
6 l7 R1 v% |" [+ e& A' y; C# [
6 x$ C5 k' P6 p/ q0 `) y1 fOracle数据库的用户:& X6 M$ d. l3 R7 `& f. K. u
sys
( }- M% [6 z: |8 Vsystem; w3 B* U6 `$ W# P

7 A2 R1 M# b  N* Xscott/tiger) o% v: y$ F) `: V
openlab/open123
5 u( u. ]) m# i6 h$ J/ r: v6 {ninglj/******6 C. W  W" N9 N3 O/ e" L3 a3 U
exam/exam123" u# \4 ?( E7 d' S# f6 B

- C0 U0 P* V( yC:>sqlplus scott/tiger@192.168.0.26:1521/tarena
; I& m8 n" T! m5 A8 PSQL># R. X4 O' U7 p+ z! S
# x" u5 |$ v6 R. B+ l3 j

& }  I  I) {3 m' ]' Z' c今天的内容:' D4 N# N7 i1 i& ]9 w3 M
1、约束条件
+ W( [( I3 S7 f7 T. C: |2、数据库的其他对象; i5 X" c( [) D1 E/ |

$ T# Q; W2 x/ N- x5 F# y' p一、约束条件 Constraint$ a# L% r3 i2 M$ q4 Z
1。主键约束:Primary key, 简称PK9 p" F, V; n0 Y) Y- T6 r
--建表时增加主键约束条件5 B. r: x& J( C8 K, N8 P+ k, N
create table dept_ning1(
; t% ^* H0 |% g. C# Z" O0 Edeptno number(2) primary key, --列级约束条件+ w; N, G' o8 b8 w6 N7 x, y
dname varchar2(20),
/ e# S! M5 g# k. s4 tlocation varchar2(40)2 u$ w6 B# q3 m0 Y3 s
);
/ G. i6 D  D1 p4 Yinsert into dept_ning1 8 M) b; V% E1 ?! X2 i6 F7 L
values(10,'developer','beijing');. \& l5 d6 Z6 E& [, i
insert into dept_ning1
/ E6 [: o6 A* Z1 T9 j0 ]5 Kvalues(10,'market','shenzhen');% M6 L$ z: z% M2 J, s
--如果插入重复编码,会提示:& c# G. _+ S  @/ f5 n; Q
ORA-00001: unique constraint (NINGLJ.SYS_C00634053) violated
$ Y) I: w0 B) B. P% f9 M2 j其中,SYS_C00634053是数据库自定义的主键名$ p- l4 y6 A' L  m
--约束条件如果没有显式命名,/ K- G1 ~6 k9 y; H; H) E; c1 j
数据库给约束条件命名:SYS_C*****
* D* |7 ~1 i6 j. {2 w# ^1 S
1 o% O5 A' L$ b" _# Z--在建表时自定义约束条件名. c+ y& ~9 Q% \: i: V$ ?" f; v9 s( |0 w4 g
--建议命名规则:表名_列名_约束条件的类型
) P$ Y8 A0 i! T$ e$ ~create table dept_ning2(; m9 O2 L9 Q. O
deptno number(2),( o" B$ d  j# p  ?( t6 o( |
dname varchar2(20),
& x' P; C  a3 D$ X; \# D$ llocation varchar2(40),
+ `- g( }  B5 S+ Z2 jconstraint dept_ning2_deptno_pk
6 |, W, [2 Q1 G2 F+ }; K' ?primary key (deptno) --表级约束条件$ m3 e* I4 C/ {/ M+ a
);: [' B0 S: l! [* V- C2 m) b
--当插入重复编码时,会提示具体的约束条件名字错误。9 t1 ^3 x) b7 G: t& x( p
--方便定位出错的原因7 `) W5 |( p! T  B; W
  N5 N# y' N/ y8 d" d. H3 u
主键约束:primary key = 不能重复 + 不能为空. Z" ~3 z. g# q5 |7 V

" M: r% _$ R8 X+ T7 q( O1 Z2、非空约束: not null,简称NN
: G7 o& Q- }7 k! Z) G学生姓名必须提供,但是可以重复
: D! k' A- q: u, d- f--只能定义在列级; h/ D- C+ D2 f3 J% g
create table student_ning() s  U' q" n/ E0 O. D
id number(4) primary key,
" i0 [1 s4 f, V9 D. \7 sname varchar2(10) not null,
" p3 ^! u2 E* N# `- B. Yage number(2)
. f* [9 p) C$ m);  V9 {! w; o/ E4 j' s" |6 H% Z
insert into student_ning
; g# ~$ ^. O+ ?6 B9 `" svalues(1, 'zhangwei', 20);% y% j5 s' F5 z% H) R
--名字可以重复! n* g+ D! U; D
insert into student_ning : m- g- F: ^- `/ v  e3 F* K7 E
values(2, 'zhangwei', 19);3 a% O* D* u, V8 W" |; M
--提示name列不能为NULL5 ~8 G& g' S2 q6 V# T; X
insert into student_ning & X8 e# d# E8 ~: X5 ?9 s
values(3, null, 18);
# `1 B1 w! [1 H' d' M9 K7 b/ r* ^4 y/ o. L# F* s3 l
3、唯一约束:Unique,简称UK, n( c) ^0 ?/ o+ o7 t% a
create table student_ning1(4 P. X/ I0 t- W2 l2 N( e
id number(4) primary key,- U4 b5 Q2 Y" Z% I" z
name varchar2(10) not null,5 H: @5 G3 g+ G0 [% z! g1 [
email varchar2(30) unique,
4 I. j0 G0 k8 uage number(2)* z# z& q& n. P: S1 s0 n
);
% l- n. Q3 A0 b8 f$ Sinsert into student_ning1
: ?, X  ?; O/ M% a' r! X  Uvalues(1,'amy','amy@doctor.com', 19);
. ?8 i* `: K( O$ V- y, p--ORA-00001: 唯一约束条件被违反
, p0 @/ n2 R/ i. D, s+ Pinsert into student_ning1
# T4 M9 B( |* D* U. G' ?0 H2 Vvalues(2,'rory','amy@doctor.com', 19);
, O/ S( J- r# s% w" n, O
8 i/ y3 m8 b4 Z( q--唯一约束建立在表级4 l9 N  p. P- L, U! h
--主键约束建立在表级
0 I; O4 D, N. W5 k5 K$ m1 T9 Qcreate table student_ning2(
! H5 j3 L2 k6 d7 q: K5 Kid number(4),
6 ^- D. e/ w5 k) U8 l7 Qname varchar2(10) not null,3 X6 P) ]4 M% Q
email varchar2(30),
% I) Q5 p  N8 D7 M/ D- l1 m: Bage number(2),$ D6 N6 |5 G4 q5 J
constraint student_ning2_id_pk
* C- Q& f3 H/ i! S( nprimary key (id),
3 k$ Y& G( M+ G! Nconstraint student_ning2_email_uk1 u. K4 j8 `6 A0 Q! r, u, S* n6 A" X
unique (email)
& J7 d: C0 a, u3 s* t/ ]/ X);
& g: M6 ^, B/ _. y  A
- Q' n1 g; T5 n! binsert into student_ning2
" E. E" ^9 A& l$ i$ |values(1,'amy','amy@doctor.com',19);9 ^9 {- ]. ~. h" o5 C  n
: |% x  Y0 X" `8 [1 V
--unique约束只要求不能重复,可以为NULL7 s1 {( p5 T6 M1 l. _$ p/ g  {
insert into student_ning2* M, s; d2 O3 l% f( C
values(2, 'rory', null, 20);
2 _2 I# B- a6 I! f. [( I8 i0 D
* ~# _9 N! @2 [. c3 a--不管是insert还是update,email都不能重复。
) `# T9 Y. r( m/ [' dupdate student_ning2 set email = 'amy@doctor.com'6 \' @% j0 X9 _: V# E
where id = 2;/ l; Z- k9 C4 |( O2 O: i

: L0 @4 F" |* e  \' }( k% K8 r4、检查约束 check 简称 CK
6 f% A( ~8 s1 l, R& E4 wcreate table student_ning3(
, ^! U- O" h( T# U7 p; \id number(4),% g5 p' m( I$ s9 e. @: c
name varchar2(10) not null,1 u4 r) h8 ]! }( E1 R6 K. E5 {
email varchar2(30),
" s9 o+ {+ }5 s4 |& I5 k  Y* }" Q) Wage number(2),
) O' b% H) u0 @7 zgender char(1), --'F':女生; 'M':男生" i( N3 g( O8 ~, t) R
constraint student_ning3_id_pk: e; H3 v* e# A  F# ^+ L* s$ a
primary key (id),, a% H/ P+ ?8 x. ^! w
constraint student_ning3_email_uk8 V6 g9 A! f: k0 a. d
unique (email),
0 u7 K6 b. p. N9 Uconstraint student_ning3_age_ck( m1 u: B: t: M: \0 W7 S5 K
check (age > 10),
* Y# U2 h8 r0 p7 k. Aconstraint student_ning3_gender_ck- k- v5 e# T6 H" E& Q% m
check (gender in ('F', 'M', 'f', 'm'))  H: \2 e8 O/ r, @' K5 z  I
);9 L% U6 ]  T$ N4 ]1 `) ^
insert into student_ning3, D. w% U& w& l
values(1,'amy',null,19,'F');
" D( t5 r. h; s5 |insert into student_ning3
  g. p) s! e' B! f" b+ T9 qvalues(2,'rory',null,8,'M'); --违反check约束 age > 106 t# w: x7 g1 {, I
insert into student_ning3
. o: m8 N+ d. u. u: w, rvalues(3,'doctor',null,50,'A'); --违反check约束 gender in ('F','M'), b  h: A* \1 a! ~. |! \

: c; }  m# h% qPK / NN / UK / CK / FK
, V' m8 N0 |9 o  l, U+ }  [& O  X7 k! l: Y# c
5.外键. Foreign key, 简称FK
. f, o7 D4 c0 p$ @create table major_ning (% ^3 V8 I0 N, [- G7 ]# Y& ?- W
id number(2) primary key,
+ T, `2 S/ f& Mname char(20)
+ G! o# [+ h7 ~4 G);
: h9 Q( F) w8 p2 N* h2 h% Uinsert into major_ning values(1, 'computer');
7 C4 U5 ^" E2 U5 i5 w% jinsert into major_ning values(2, 'history');( a. s( j2 O! \+ E
insert into major_ning values(3, 'music');
! Z6 S9 j: {! G/ |* }insert into major_ning values(4, 'sing');
$ [' H6 A3 ?9 G2 L. o* icommit;/ T- m$ P7 K6 b
create table student_ning4(1 t9 y3 E4 M0 j) Q% k  K! s
sid number(3),
( n1 j8 y- u( B5 F# I5 Iname varchar2(20) not null,0 `* u% W# y9 p, n5 P2 c
email varchar2(30),
6 q; P" K; P( h  N* e$ C  kgender char(1),1 d9 r8 I/ Z9 j/ J
majorid number(2),: x- X" r6 B1 a" F7 l4 U
constraint stu_n4_sid_pk primary key(sid),
5 H1 n6 a5 m5 V$ J. b8 e! w* Y; ?constraint stu_n4_email_uk unique (email),
+ k7 V! P4 z4 L8 [2 i# @+ [constraint stu_n4_g_ck check (gender in ('F','M')),4 `0 ~* e6 s5 {* b. C# M
constraint stu_n4_mid_fk foreign key ! c/ `1 }8 u1 a/ d! L4 H6 {2 U; I
  (majorid) references major_ning(id): j% }( h! f# m& {" Q: s$ M
);0 b7 N6 ^; R3 ^; ~/ [/ u( N

3 P8 s3 f0 B$ i# dinsert into student_ning4
* y7 Y% u' r6 g- S- f6 h; g, Y1 v6 xvalues(101,'amy',null,'F',1);
" ]& d# X2 {4 W5 A' \3 h! @. s--新增数据,不存在9这个专业7 U2 M" h4 C1 f- P
insert into student_ning4
7 }) T4 w2 c! s3 c- l- z; Y4 ~9 Hvalues(102,'river',
! s$ J8 i8 I/ {'river@sina.com','F', 9);
* Z  g: x' P5 r4 b" d% p--提示错误:6 b8 L: S7 E" D2 [. u
ORA-02291: integrity constraint (NINGLJ.STU_N4_MID_FK)
: u. `" P9 g; e- h+ R violated - parent key not found
9 V+ N* R  v2 K- U4 G3 N: ?3 @insert into student_ning46 \: D3 H' ]( {
values(102,'river',# C' W. n, x+ e! L  ]  k
'river@sina.com','F', null);8 M, s8 y2 l* F9 r& Z' M6 Q5 H$ Q
* R' h  y0 `$ S
--有学生属于专业1(computer)
# w0 r' V- }' w- [( [  Z0 Rdelete from major_ning where id = 1;: A+ M, y1 _8 X) n
--ORA-02292: child record found  W! `9 @5 d# e6 L5 Y4 n8 x

4 t4 W8 Y0 u0 i2 Screate table student_ning5(1 y6 e# e  b9 j& O$ a
sid number(3),. |! g, k& g6 }1 C1 W% I
name varchar2(20) not null,
1 q$ A" H1 F1 y& femail varchar2(30),/ N  h" J( i7 S$ `* [' _! l
gender char(1),
+ x) x  m% e$ m# @$ g0 B3 U* xmajorid number(2),. e8 L/ Y3 d0 t! o% X4 L/ Z  [
constraint stu_n5_sid_pk primary key(sid),) z! N) F: d# m3 u% r$ T& \
constraint stu_n5_email_uk unique (email),2 ?0 E, d+ _/ k( `; D
constraint stu_n5_g_ck check (gender in ('F','M')),
! A0 c) `: i6 d, _6 y% gconstraint stu_n5_mid_fk foreign key ' a; [3 N% s$ w1 [8 f  B
  (majorid) references major_ning(id)
$ j( @( a; P" `  on delete set null);0 {1 V3 ]/ s' n
, @. n- k: [1 ^1 s3 V3 I
insert into student_ning5
) [% D$ |0 l4 A. }. h# P3 C" {1 xvalues(101,'amy',null,'F',4); --amy是4专业的学生8 O3 v  z+ ~9 N5 J. M1 ?, Q4 m' K
--删除编码为4的专业/ _- B4 C2 x! Z9 H
delete from major_ning where id = 4;! b8 {2 d! M3 }* q( q
--amy的专业被设置为NULL! }, a. w) E* Y4 Q/ `) p& f
select * from student_ning5; 1 w7 _9 B" l0 R7 s, A; P

5 x2 Y8 F9 k# z/ K+ D7 o6 t' k& x. Wcreate table student_ning6(( M0 }! `9 x5 A+ @( P
sid number(3),
& w# x6 E/ ]0 {( V: i( s5 I* B1 bname varchar2(20) not null,
9 l2 T* a6 l2 E. V. Pemail varchar2(30),
3 v, d' o! C: `' j7 }+ agender char(1),. j: @6 ^5 g5 I4 B, \* }/ y
majorid number(2),
8 u* ^" e! C0 c* b9 Bconstraint stu_n6_sid_pk primary key(sid),- O6 u5 K* d* x+ u3 |7 u' u) T9 y
constraint stu_n6_email_uk unique (email),
0 h, B# M' O# V9 Fconstraint stu_n6_g_ck check (gender in ('F','M')),& @" O3 G) H) H% u6 K
constraint stu_n6_mid_fk foreign key ) Q, K9 Y+ G( }! x' y
  (majorid) references major_ning(id)7 O. ]7 w4 }( K7 g' K# D( w
  on delete cascade);6 D' d2 S9 _$ I  f: D1 ~( K( g
! l2 R1 g8 l" e; t* X
--复制表,不复制约束条件。
, F& u: [2 g8 `7 j: ~create table 表名 as 查询语句: R+ H+ X2 B" P7 r6 i$ M
8 @  `0 a. p  E' U- v
--建立约束条件的时机4 W: d4 l( ~6 V4 P
--建表同时建立约束条件:. |6 L. s2 i& ^; q
create table student(& R9 ^. u7 F8 y+ L
id number(3),; A' s/ C. R6 M( H$ G3 a
name char(20) not null,
0 \# M! `+ p; U" j+ ?0 ymajorid number(2),& d4 c% ]9 _1 G8 X$ u. `
constraint stu_id_pk primary key(id),
/ B$ _$ n8 L% X. iconstraint stu_mid_fk foreign key(majorid)
( n6 C9 [3 a5 d0 J   references major(id)
# E* u+ s& I$ U! {* B- f);: @% I" Q3 l1 @5 |/ g
--在创建完表以后创建约束
9 b/ K3 M+ y+ w: X/ P7 f, zcreate table student(
' P* d3 e+ X& E( o, E2 S, }6 {/ e. Vid number(3),
: B1 N" B. \) D( Pname char(20) not null,
9 [+ h- |1 y4 ~/ Gmajorid number(2));2 @7 T8 R) f) p' y2 R
alter table student
4 L& R- Q7 F" G+ j6 X! _4 Q4 {! O  add constraint stu_id_pk primary key(id);# x# h# F; ^7 ^4 s
alter table student2 M: C% N3 c. k: n# A8 P4 z3 s
  add constraint stu_mid_fk foreign key(majorid)& ]" a* W& X" N0 c9 F
  references major(id);
; I/ e4 N' E9 P7 |) i1 y! M% z6 |- Q4 D1 m4 J
-----------脚本文件begin---------  Z4 p8 Q5 f: g: O& c3 p8 h
alter table student drop constraint stu_mid_fk;# _6 G: F' ?# K
drop table student;
; Y' p) S+ x) i+ ^+ t# cdrop table major;
; j+ r4 y/ y. {9 q+ W& d2 }( K& Icreate table major(....);
9 m1 R* G/ c% M: o8 t3 Y3 i9 t+ v/ [create table student(....);
* D1 q3 K- u7 Y) l" a! ]alter table student add constraint ....' |3 ~- ~+ y0 t+ \/ T* E2 m0 W
-----------脚本文件end------------
1 D0 r3 f5 y' F! s: p
6 J. \3 W: l/ Y! ^! F* _user_tables  :用户所有的数据表4 E4 t( g4 X9 b- z; f
user_constraints:用户所有的约束条件! S; o3 ^( X5 E# A% ~; N- z
user_objects :用户所有的对象(表、视图、索引...)$ D' }1 Z; H7 W* g/ D5 w! h
3 I' q/ o* n4 A' N: I3 }
all_tables  :用户能访问的数据表,
' d0 X" F0 y) t0 N& Z             包括自己的和别的用户允许自己访问的
( b" X7 G  X$ M: j7 M3 h- wall_constraints:用户能访问的约束条件" @  Q+ M7 N" }9 q
all_objects :用户能访问的对象(表、视图、索引...)
$ ~; J2 t7 V% Q. m- N$ }7 ]+ L( l. i5 t/ L5 W8 `1 h5 u
PK / FK
7 _( B1 V& B+ j* I, V/ RNOT NULL / UNIQUE
) i) J6 G7 H4 b8 I* ^CHECK
+ Z- p- s( c) E0 [( C其中:CHECK和NOT NULL可以在程序级别控制
$ Y3 c% X; Z. p" g
7 N( b; I2 V  E1 y$ H& Y: {二、数据库的其他对象
! {$ o7 L* q2 I" s  L( }7 R表 Table
9 t' O$ h# o. |" f! U' m- \7 t视图 View8 @$ H- L4 e( O. j
索引 Index
# ~. v2 u6 O) Q; k$ N  J序列 Sequence
6 s0 K( R; c( l; ^3 Q4 d( j5 G过程 Procedure
6 i, Y& }2 E% c  ?* D+ b% e, ~函数 Function# f1 i' p; P, A/ V2 ?
包 Package: I- D$ X0 Y: ?, g6 O" E6 ~- @
触发器 Trigger
1 e! f. e: c/ K3 {! x. B) ~同义词 Synonym) k, N+ M/ m( Z  @
....
! k( N; i, g- A2 {! v( o! R5 `5 q7 b7 Y0 J% N4 q
1.视图View" E/ g  H/ I8 v; A
create view v_emp_ning
" ?( q$ Z( `* E5 g) I1 ~as
$ ?% Z! H; I( t3 A, R* T5 iselect empno, ename, job from emp_ning& A, O- v( z* W! I
where deptno = 20;
) k8 I6 A6 U) e" U3 @& r$ \--使用和表相同
2 [- P# B4 I& {& ]desc v_emp_ning
, E9 w! C. e/ `9 {4 yselect * from v_emp_ning;
  j9 j* S' R' G( A4 J7 M1 q7 r--视图的好处:简化查询;隐藏数据表的列2 Z+ F# G* k7 Q1 _2 Y0 u! [
; ~4 @; w  W0 c1 I+ h2 B
create view v_emp_count# }, t$ I# i, V
as
4 e% O) t7 h  Eselect deptno, count(*) emp_num0 x5 f6 B9 U/ W/ n5 n
from emp_ning: w1 s4 x' y( ?) \5 S
group by deptno;
" B- B7 ^$ n* b* H1 Y6 C: C, V--修改基表数据5 I; v- s7 Q8 Z1 h1 J
update emp_ning set deptno = 104 ]4 Z7 }# a; i) H4 ]5 l4 @3 y
where deptno is null;
  O& j# L0 G; f7 ~. N: n/ J- |--视图查询到的是修改后的数据。% t4 ?8 q  L, \5 l) d. A! w
--视图不包含任何数据。是基表数据的投影。, S! L# [; @& c/ v
select * from v_emp_count;
: Y0 I& R! g( K& v, b7 ~9 f8 t; S- m/ ~( a) e. a/ M+ X* `
--创建或修改视图
  q% N1 H2 |0 V! C9 W5 R% Ncreate or replace view v_emp_count
4 a4 b4 O8 B) F; ?0 w. g, Xas
" B' l  T8 E  d6 l5 Rselect deptno, count(*) emp_num,) d0 M3 R, X! r% X" x: a4 X
sum(salary) sum_s,0 j" _& J, F, X  n' A
avg(nvl(salary,0)) avg_s,# N! q6 _1 {. F. K  T) N  D
max(salary) max_s,
' S0 [3 Q% n: {2 e' Cmin(salary) min_s
# c4 [. [1 s( Tfrom emp_ning
1 t/ A& Z3 R9 i3 pgroup by deptno;
) F: e4 _  r- m. Z: C& Y7 t3 W
+ ^  a/ j5 j) _--查询视图的定义1 ?# _- e9 C4 I# ?. a3 W$ H
select text from user_views7 t, f8 Z* w8 H% |1 a) A
where view_name = 'V_EMP_COUNT';5 m9 b' B5 C" q  }( [8 T2 v$ v8 e
--如果视图对应的sql语句显示不全# ]* O4 y: ]/ T% F) E  g6 a
set long 1000
1 ?& p& d. \! F9 p' L6 i: w: h1 W
2.索引 Index :用来提高查询效率的机制。; ]$ q8 }1 t7 B# I5 P
全表扫描: Full Table Scan: 查询效率极低
$ {/ p5 u+ u* }& z5 C3 F索引查询:比全表扫描快。
% k) A/ J5 M- E5 r! @' S0 ^( q- X8 u: e/ p: f4 b8 y
索引的结构:数据 + 地址
+ u& A- _1 S+ y: q) c3 X1 F            张三 + Room203
" o: j# j4 r8 ]- R& Z
, c: x$ L9 Z' y  u. h对于数据变更频繁(DML操作频繁)的表," Q6 [4 D/ o0 U+ N5 K; t
索引会影响性能。5 N2 u8 u' p& T9 \+ Y' _
4 f1 [' J) U5 ~7 v2 b6 p
如果数据表有PK/Unique两种约束,索引自动创建9 |) ~' G' M& r9 t/ x
除此以外,索引必须手动创建。
1 F# ?  ?( t1 U  Z" kcreate table student_ning7(0 W3 w# X0 H! h9 q7 _
id number(4),  B/ p' Q2 Y) U# y9 e
name char(20),
* n0 @% ]4 f, X/ Yemail char(40),
" I; d2 |' @) {: b5 Aconstraint stu_n7_id_pk primary key(id),
8 g- I5 @, M; n7 V5 M' b# h6 G7 yconstraint stu_n7_email_uk unique(email)
* T# I) K; X4 V; U);
8 _9 X# v8 E) Zselect constraint_name
2 P; y: B" N/ |  G1 pfrom user_constraints' \% }( s+ V/ T; j4 n, a( S
where table_name = 'STUDENT_NING7';) O* a; w$ x/ U0 q; |
- `! a- u2 k: z2 O4 s0 V
--查询student_ning7表上的索引,  [% F( z2 C( D2 `0 X1 U3 R
和主键/唯一约束条件同名,数据库自动创建的索引。
, L) d9 u4 V9 m! f+ aselect index_name from user_indexes
% x  {: J8 m! P% u9 Cwhere table_name = 'STUDENT_NING7';
# K1 A' }, k. g* h0 a
" x1 A6 M- A2 j8 H: V--凡是id或email上的查询,会使用索引
. B- N+ s' L6 u: T8 S8 P6 I6 sselect * from student_ning7; G% B; S/ p+ O7 A
where id = 1001;! E; T; P# i+ x( e
$ r4 g" ^7 n% }8 d
--这种查询用不到索引:全表扫描8 q/ M# s/ K* i0 h: h' ^' B
select * from student_ning7
4 K( C: u% b: Kwhere name = 'zhangsan';
. |- S0 L- `& p; L* c: }! V% e
2 G" _; l: M) l* R* s& K( f--创建基于名字字段的索引,索引名自定义
: F# A. a, u# }4 S+ p7 J) }create index 索引名 on 表名(列名);; x( ]# A3 j2 h" `% b: k
create index idx_stu7_name
% Y8 }- v7 D4 c8 W: o7 H+ Zon student_ning7(name);* H: J: K8 {+ I9 g8 V) p
1 G. ~) m9 {1 B! ^
索引:
& z3 r. g- L+ @+ s' \+ i5 [1)了解索引的工作原理
$ R* t/ O: |9 _/ r/ k2)pk/uk自动创建索引
2 G3 [+ I% j+ O2 x1 Q) x! V3)根据查询情况决定手动创建哪些索引。4 l5 |3 {& G5 ~; S: {* E1 X5 W  L

# o/ G/ `- x: [$ [' _$ f. d
% N+ n6 Y# v  o3、序列 Sequence --Oracle独有的6 J9 C) v4 y: x: u$ @1 t; L" R8 y
--产生从1开始的数字值,步进是1& Y7 _8 O/ _  ]; Y) N( N
create sequence myseq_ning;
4 b4 f6 Q3 o' g) Y; Cselect myseq_ning.nextval from dual;
, C0 m+ F5 N% P8 m# o; u$ m  M. t$ I6 v, b; v! c$ X7 s
序列的特性:产生连续的不同的数字值
/ C4 A7 v- I7 w( C用来作为数据表的主键。
1 f9 B( G# Z3 \+ _& v
7 z  [5 `2 ]4 I$ a4 b! T: y--使用序列产生的值作为表的主键值, a+ _* A$ b9 ?1 ~
insert into student_ning7(id,name)' k/ R$ @: }3 l3 X3 d% r
values(myseq_ning.nextval, 'amy');
0 W; S8 ~0 e2 l- u2 F3 U4 |& ~1 L( G7 J
student_ning7和序列myseq_ning的关系:4 D  U  w1 c' |
是数据库中的独立对象8 Q" v: c6 u# G$ R
--表可以用序列产生的值作为主键,也可以不用' w/ ]% H& \# F: V3 X
--序列可以为一个或多个表产生主键,也可以不用3 \) F$ Q' q" _. K+ H  G
insert into student_ning7(id,name)
9 K% W* y$ b+ K. v  V( n6 Mvalues(100,'river');
, D8 z, z5 ?  L4 jinsert into dept_ning
% x8 d3 D) N0 l7 ivalues(myseq_ning.nextval,'market','bj');5 R! F/ K& W7 y% T! t0 i
) n4 P2 z$ ^+ Q! j5 G( f1 N
--建议:一个序列为一个表产生主键2 l( G  `, P# {5 M0 v8 X7 Z
- a9 ?  i' w4 E1 e& O
希望主键值从1000开始,步进是2?5 d" K/ _/ g! g9 f, P( R3 D- o; l9 J
create sequence myseq_ning1( J9 s. f4 S; E3 ?$ K: }  b( P
start with 1000
- ?8 x4 `' n* s+ m% W* sincrement by 2;
2 d3 {; Z. o2 X! q( a6 M, f7 m4 ]6 f( Z9 _
insert into student_ning78 H% I* H$ _8 h* q- n! d' ]
values(myseq_ning1.nextval, 'song', null);$ `, b$ @0 R% z

$ R5 y! Q/ |+ O--删除序列,对曾经产生过的数据没有任何影响。
0 b" n0 K, r. i7 v* ddrop sequence myseq_ning1;$ B4 `! J: F4 R$ n
: o  [/ [& F; J' M! ~1 j
SQL> edit
  c, o# G7 E6 m4 s( h
8 y( p7 q* v9 ]7 y5 R" m  i4 H4 X& K

我的日常 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与我的日常享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和我的日常的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、我的日常管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

关闭

站长推荐上一条 /1 下一条

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表