我的日常

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

动态微博

查看: 1650|回复: 0

Oracle增删查改 day05

[复制链接]

45

主题

5

听众

119

金钱

三袋弟子

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-06-03 21:43:49 |只看该作者 |倒序浏览
复习:SQL语句4 X2 U8 T/ ~' V' W6 _9 D
select  q" ?3 q% G# j  F
DML: insert / update / delete0 n3 M; B' R: H0 p+ ]( [; t* ~
9 V% X' [2 p" z, |$ N) A# o  \) t; s
CRUD
; w4 q% Y' Q/ e4 NC:Create+ }8 }: N& i( M9 }8 u4 \, k* ]! d; u0 M
R: Retrive
6 s+ e. ^4 a" }2 L* yU: Update
) A8 A. v' @7 J6 K; FD: Delete
* F; t+ b. _5 D, x$ C. a, R3 }# P8 j+ i( f  z$ h
DDL: create / drop / truncate / alter
9 o. J5 X# R0 P" n  w1 V( K: o
2 s( Z/ u2 C, l  f4 uTCL: commit / rollback / savepoint1 s6 M7 S2 o* ^6 ~) u& ~6 B; W

! Y. ]+ V# R5 Y9 ?2 n3 N. @DCL: grant / revoke6 b& s! r0 C' \, Z. t6 n: k
1 J$ D: P6 I% }
grant: 赋予权限; n. s( l3 u# I5 n
revoke: 剥夺权限+ x, k/ h5 d: M; x. f0 H" P
数据库中的用户: openlab   hr    scott) k) ^: C# @' ?/ q) c3 p
                 emp( w- f2 p1 j) Y. t/ l% |
4 b+ c1 T% r8 U+ d2 n0 H
假设现在的用户是openlab
4 s6 m5 ?) [) v: R7 JSQL>grant select on emp to scott;, v& i8 q% d% s' |: A( L3 c2 |
SQL>revoke select on emp from scott;3 ]' `! j2 }" B# D

6 Z0 Y, V  x- z, H+ o) E% \, nscott的会话:; Q+ ]) P/ d8 z* J9 ~* Y5 U# P
SQL>select * from openlab.emp;9 g( W$ b7 F! U' A5 k# w- L
SQL>select * from emp;9 K! }. v. w* B
/ ]) q. l/ {5 B' O

2 v4 w( T) q$ {7 t4 K1 j/ l7 EOracle数据库的用户:
. l4 e+ g# q4 X: K$ o# ksys6 I- L% d2 o4 n8 O% F, G9 @
system
  j6 R$ X- l' t) _: X
' h0 v7 T5 I3 M3 o( \scott/tiger
' `# x7 u. n- M/ x8 jopenlab/open123
1 ]- @2 A5 f4 x. ^1 B! N0 z/ H0 ?# hninglj/******
) n. n3 r- i* d$ |9 Iexam/exam123
/ p9 _# [, z. z, H+ R: s3 r! F+ W) }6 [1 E3 u& |; J( O6 e
C:>sqlplus scott/tiger@192.168.0.26:1521/tarena0 A4 W" _8 p& Z0 d2 O% T3 {
SQL>  R6 E) t" G$ P5 t$ T: J' {8 E8 M
6 a1 B& D. Y6 g
3 e/ O5 T: X2 l) R" Q% h
今天的内容:
2 t) Z1 }  r9 S) t7 l6 g8 j1、约束条件
' L' m: x0 c3 L6 q$ b3 t4 V0 u2、数据库的其他对象" H4 }! h9 z; a
3 D* D/ c/ t( J, O- b" I& \
一、约束条件 Constraint
2 H- ]( n+ V- j) Q. U4 x1。主键约束:Primary key, 简称PK+ `; @  {: Z$ r
--建表时增加主键约束条件7 w# F6 |  Q5 Q# U% `9 }: q
create table dept_ning1(
& H. v2 f5 E  W) P8 l3 a6 Hdeptno number(2) primary key, --列级约束条件
/ W4 X0 s& N7 f8 @+ m$ p9 sdname varchar2(20),8 d8 U6 j& _' Z$ r: M3 Y4 @, e
location varchar2(40)
6 y. x  V' u: D9 k2 F) Q$ X; F);
' l2 w" S! Y8 a8 {' F8 g* ]5 l7 kinsert into dept_ning1 ; D* H% I1 t6 A% s# R, e
values(10,'developer','beijing');
5 k# h6 y# E+ d3 pinsert into dept_ning1
# M! _# o$ D7 q% X% z2 |: Cvalues(10,'market','shenzhen');
2 I" B- c. d4 r--如果插入重复编码,会提示:
( K0 M6 {* V( t( r$ \: V; c5 X4 IORA-00001: unique constraint (NINGLJ.SYS_C00634053) violated
6 v; w) p9 Z) h0 u$ q2 C$ b其中,SYS_C00634053是数据库自定义的主键名$ g; O5 H/ l% Z( ^( U
--约束条件如果没有显式命名,; n/ E, @2 L3 c2 _7 {: ^
数据库给约束条件命名:SYS_C*****
% z$ [0 v! N: F3 B9 Y4 N
5 C  g5 N" H; c7 m--在建表时自定义约束条件名4 c% E1 O$ E6 J. H8 T
--建议命名规则:表名_列名_约束条件的类型
( ]6 y4 a3 E0 F" f: }" q, V* Pcreate table dept_ning2(
/ ~% t& K  t# @deptno number(2),
* A, l9 D0 b& b$ d9 ?+ {- Sdname varchar2(20),
( f+ |" P2 |/ Rlocation varchar2(40),6 A- X$ N5 q4 s& s! K7 b
constraint dept_ning2_deptno_pk
, Q( j0 F& V1 z% W& p+ H" f7 gprimary key (deptno) --表级约束条件
5 Z6 o( L4 J2 M6 V3 j' m);  I9 w3 [8 U, |9 F
--当插入重复编码时,会提示具体的约束条件名字错误。2 y$ U: j$ i" H+ \% O' ?  E
--方便定位出错的原因# ^3 D# S4 q+ L6 u0 E9 N

* D  u) g/ s# I) n: ]& ]主键约束:primary key = 不能重复 + 不能为空
' |+ A/ }- T3 {. {2 O. Z% e' T. D" Z. a5 A1 h) j
2、非空约束: not null,简称NN
) E9 \2 b6 |8 L/ y0 D6 J; b$ H学生姓名必须提供,但是可以重复
* H0 I% f7 {+ u, [# @) a--只能定义在列级
% T! p' R" o9 m3 k% Y% C& l, Kcreate table student_ning(* M5 {/ ^1 r( X' R' ]
id number(4) primary key,4 n5 |/ s* ^& R. p$ J% l
name varchar2(10) not null,( c" b! X2 h/ g; O$ |% f, Q; K8 K
age number(2)
! H5 i/ x! {3 P* O) g);
) r- x2 ^" r+ W! Finsert into student_ning 4 Z, j7 x! S3 u; I, p7 S+ B
values(1, 'zhangwei', 20);9 X# R4 A; J. E* B. e1 ]! J- G/ W
--名字可以重复
- b- e0 q; C, N" u7 F6 Uinsert into student_ning
& i, k5 t% ?* ~! Z5 e) Svalues(2, 'zhangwei', 19);6 i$ B" r  P5 S. i& M- g' b
--提示name列不能为NULL! H: K% Y+ R  m% e" J
insert into student_ning $ ?" B  L" p: h0 L  v
values(3, null, 18);
9 J+ B& j5 K) l! s, X) I; Q4 K  Q* [" M0 ^* w$ O' \) v* q
3、唯一约束:Unique,简称UK
% `3 h* {/ ^8 {" `create table student_ning1(4 q% z/ K* N3 P
id number(4) primary key,% |& ]4 U% V- @: }0 ~' D# ~
name varchar2(10) not null,
4 D  K  J* @6 f; u) Y/ P. {email varchar2(30) unique,1 z( f+ D( o: g0 w* Z! G
age number(2)3 v6 [, q- g$ D/ }
);
' I' {) b! I0 tinsert into student_ning1 2 |) ^0 `! M& U9 q8 Y4 ^
values(1,'amy','amy@doctor.com', 19);6 v. F, x6 O. t  _0 X
--ORA-00001: 唯一约束条件被违反% \5 V; {5 o4 H+ n7 Z
insert into student_ning1 0 U3 o8 E: B/ @& U. d- \& k
values(2,'rory','amy@doctor.com', 19);
+ V  a1 v, x3 ?8 m: N5 @" \# M' x
) C3 L: ]$ Q/ Y--唯一约束建立在表级" i/ v& N. b4 e) G, P  J" ?  Z% W
--主键约束建立在表级0 y  P' y. Q5 y, f9 E
create table student_ning2(2 }. D7 J% f) C  V
id number(4),
  X7 \% i9 j, K6 S, s; J  ~name varchar2(10) not null,
/ F! N! a1 p4 i; |0 w' ~0 [email varchar2(30),9 P4 e& Y/ m$ R' h/ G1 P3 v
age number(2),4 J. X7 G9 `8 q$ U! w
constraint student_ning2_id_pk
! @8 b2 D8 ]# d0 N8 x- [3 gprimary key (id),
9 ~9 m8 m  N' B7 p4 A# Gconstraint student_ning2_email_uk0 |" D" g+ y/ h, f3 C! z  y, F0 x6 l
unique (email)( Y5 Q  S' X# I
);! d1 C- y$ u( X' `
" [" |7 l+ O$ Z$ n
insert into student_ning2
+ ]% }% q4 H7 x3 P; q8 xvalues(1,'amy','amy@doctor.com',19);
& s5 [" x& j0 F8 p  t3 S* y/ ^
# w6 m- {! `- I8 Z* x# e--unique约束只要求不能重复,可以为NULL
1 d2 h& x; o  E) M9 f: D. Yinsert into student_ning23 n; ?0 a  i( `2 U3 p; d; s
values(2, 'rory', null, 20);) T8 G2 A% M  d0 v! P7 G
. G8 H* A6 |+ Q& d: G# }
--不管是insert还是update,email都不能重复。) l1 a1 D$ w( {" L. `% K
update student_ning2 set email = 'amy@doctor.com'1 @. W0 {3 S% M; L  F
where id = 2;# h8 l# @& D' n6 e) f9 B! z3 F
5 Q6 U* s& e$ e& [3 j
4、检查约束 check 简称 CK
: F% D8 _# \3 ucreate table student_ning3(
6 ~" Q( e4 v8 W6 z9 aid number(4),
2 P# b+ [: t6 ^; {! Jname varchar2(10) not null,/ v! B  w3 q# V: _: B* C2 V3 P
email varchar2(30),
% \! u4 E6 g& R: }# Gage number(2),0 N! _9 f) u1 V+ V# e
gender char(1), --'F':女生; 'M':男生3 o. A1 u7 \7 ?: x
constraint student_ning3_id_pk4 c3 q/ j0 V  }# r- g  A  G9 m4 L
primary key (id),5 z. \' t; Y/ n( }# ?% ]% m
constraint student_ning3_email_uk7 I/ z' ?9 o* M+ X  G- T
unique (email),8 E9 v: m% g; M% H) |
constraint student_ning3_age_ck' b, m1 p9 }1 L( h9 i
check (age > 10),
, A* Y/ F9 V* P" N+ tconstraint student_ning3_gender_ck
8 A$ S) U. [2 m+ Y! @$ E$ v7 H8 W' Hcheck (gender in ('F', 'M', 'f', 'm'))" S1 u. U6 _+ H3 i3 |2 `& |
);# w. ^! c  A: l* m3 \* K$ u4 ~
insert into student_ning37 o. R) U5 Z! |- l8 B
values(1,'amy',null,19,'F');
' t2 ?! _6 t) j" n; }: _insert into student_ning34 s( O; h  `# T4 F0 B( W4 w  v
values(2,'rory',null,8,'M'); --违反check约束 age > 10% l/ o6 S4 e- J5 I% E$ U/ Q7 d
insert into student_ning3 , _) y1 d6 C( o4 t- `* l! F1 y& O
values(3,'doctor',null,50,'A'); --违反check约束 gender in ('F','M')
8 x  J' g8 B$ y# @& W" P6 |
& I0 V9 Z4 t' F7 z1 BPK / NN / UK / CK / FK
  O8 y1 ?, q0 @+ m3 V7 @$ J* I; K+ H! n- G
5.外键. Foreign key, 简称FK+ e# ~7 [# {  D+ y
create table major_ning (, F$ s9 V$ N1 Y
id number(2) primary key, 3 p) v: b( D! e) B& ?
name char(20)  o; H( J4 [* }2 ]8 S2 Q( Q: |9 }
);  t( q8 f& t; s+ F
insert into major_ning values(1, 'computer');
0 l7 h' c% S* H7 F0 s( ]insert into major_ning values(2, 'history');
0 G- e+ c4 H' }insert into major_ning values(3, 'music');3 g- e  e) E) N! O- v. C
insert into major_ning values(4, 'sing');) Y9 w+ ?& _7 T- H$ e
commit;
, D- W, P4 i  |; ocreate table student_ning4(
( M5 m( K) C$ c4 E! y7 c  h/ ~sid number(3),
% v" f' e0 E! @# G" Y- a' o4 Xname varchar2(20) not null,
4 |. T3 Y+ r) O# {& B$ j& Y9 demail varchar2(30),
0 J1 F; N& k6 s* ygender char(1),
3 L' r, j7 s" s7 a; ?majorid number(2),
# N; A8 r: r9 U+ Aconstraint stu_n4_sid_pk primary key(sid),
, x+ }4 X1 x3 K! {- e# r3 Rconstraint stu_n4_email_uk unique (email),
9 t" Y/ c2 ]- C/ q# ~constraint stu_n4_g_ck check (gender in ('F','M')),8 H4 t* O" o9 R: U; n3 d* I
constraint stu_n4_mid_fk foreign key 0 j- _# t5 |0 z3 z
  (majorid) references major_ning(id)
* m4 q! h$ @2 Y( J);* Y6 q( K: C! z$ }; v$ I

+ D7 E. n; R! Ginsert into student_ning4
0 r- X6 s/ q/ O: N: Ovalues(101,'amy',null,'F',1);/ r3 B( e' u; D  L
--新增数据,不存在9这个专业1 W( A* e% j% j) G0 O* a) N
insert into student_ning41 f4 K% f6 L) \
values(102,'river',
  a# q0 W" b% z2 J! I  I'river@sina.com','F', 9);1 b3 `6 P( h$ G  j, G5 g$ a1 R
--提示错误:) t" ^; q* s8 |4 g- R% w
ORA-02291: integrity constraint (NINGLJ.STU_N4_MID_FK)
0 [) ^( {! o0 L0 _0 R violated - parent key not found
2 S, S( n  x2 Qinsert into student_ning4
8 H3 }' V& S, bvalues(102,'river',# ?; {& E4 [. i' I1 p5 [, m3 W
'river@sina.com','F', null);4 W; n3 C+ h6 F6 K2 _# e& p
8 j5 `  h' Z/ h5 S& e: G
--有学生属于专业1(computer)
6 K5 {1 ?/ ^( wdelete from major_ning where id = 1;3 Q8 e; s8 ?: I8 l& [
--ORA-02292: child record found
# H" C* {3 d% S. C9 U. ~
. q/ `3 j' J2 a2 a2 k. v/ {create table student_ning5(7 D2 {2 T4 w/ n' m1 u
sid number(3),0 O& [0 T, }3 o7 e4 e5 z
name varchar2(20) not null,
. o" L, v0 N- y: c3 @email varchar2(30),% }! T) p+ Q9 p* k
gender char(1),6 N4 n( f1 F# P% v( D1 S& J
majorid number(2),
" \# f' [: |/ D& o, L8 N: V; |constraint stu_n5_sid_pk primary key(sid),
1 j) M7 Q- F0 c! wconstraint stu_n5_email_uk unique (email),# ^- o, _8 [  v6 Q4 I
constraint stu_n5_g_ck check (gender in ('F','M')),6 O2 H( ^; z9 }, i2 x  V7 D
constraint stu_n5_mid_fk foreign key
1 S7 s" Y! a2 @: |  (majorid) references major_ning(id): A" y* e* C$ ?; V. F
  on delete set null);5 h# U% ?: S' B
, |2 t, P' m( h2 V& \5 S
insert into student_ning50 m' F" W6 R/ t( T9 J
values(101,'amy',null,'F',4); --amy是4专业的学生8 a- A4 w4 i+ o$ C3 Q4 R' Q3 j
--删除编码为4的专业) H% X, a# S+ M
delete from major_ning where id = 4;
* A- z% r' ^5 o--amy的专业被设置为NULL
+ V. L: I* y, X+ V* Gselect * from student_ning5; ; H$ r& k$ X3 g
$ B. {# ?# Q+ ^* ~
create table student_ning6(
* V( V" s/ ^/ J. |/ a" ~0 w! h) ksid number(3),; K# @: J2 T" i6 x5 H
name varchar2(20) not null,4 m) Z5 y" K2 ^
email varchar2(30),4 {5 H) l" X. e% Q
gender char(1),# \5 `$ u  a! |) O! A  j
majorid number(2),
) m# I+ _; m6 \2 g; Z1 l: [7 }constraint stu_n6_sid_pk primary key(sid),- ~( k  K$ W1 J! f1 ]
constraint stu_n6_email_uk unique (email),
0 b( K  Q; [% ?3 }8 s( Tconstraint stu_n6_g_ck check (gender in ('F','M')),
* I& P0 ]5 G  \4 X; j, }. u) sconstraint stu_n6_mid_fk foreign key 6 d8 b. m0 a7 |3 Q" ]
  (majorid) references major_ning(id)
4 g( H7 O2 b9 s& @4 Y  Q. G2 E  on delete cascade);
* w% I' a( F# j( C8 {
3 I: L# P: G) Z/ k, C! g5 I--复制表,不复制约束条件。
+ E3 {) q; L. K- T# @8 |; I! X" Jcreate table 表名 as 查询语句
6 M( y* S+ {; d( w; x( l2 b, S3 B+ Q7 J, Y( @9 i( g$ y/ i5 R
--建立约束条件的时机
0 B% ^+ l; e7 @2 A4 l--建表同时建立约束条件:% ]* h5 s% y3 U# C) I( |6 Y
create table student(
2 G( _) ]# e4 m5 Sid number(3),
' Q2 s( l6 f& ]  y$ X3 B2 r! {9 _( `name char(20) not null,* n4 a7 ~' p9 w7 Y; |
majorid number(2),- S3 E$ m8 v! d! N* a* r
constraint stu_id_pk primary key(id),) R- K" {# Q* R. o; j! N+ K
constraint stu_mid_fk foreign key(majorid)
; b; n; a) m6 C   references major(id)
0 r. @& ]- u2 G0 l);
# L: w0 V) O3 A) P+ B  M) s, r--在创建完表以后创建约束
8 V' Q+ G! A" G8 y- K2 \3 H' ^create table student(
  J. ~) X3 i$ nid number(3),
2 e5 H  p. i# X6 [& }2 \name char(20) not null,8 x6 C0 p% w% _# |% G* c: b7 V
majorid number(2));
2 Q5 I4 F1 n& ualter table student
- J6 l; q7 G; r4 x# S  add constraint stu_id_pk primary key(id);) f. v9 D3 K+ }
alter table student5 J3 I. [# |9 @- P+ ?
  add constraint stu_mid_fk foreign key(majorid)
' i; F+ B5 T  r. l  references major(id);% v  K! ~8 q( A0 `& @5 H4 G
* T6 q+ a5 Y+ m1 ~# m  d
-----------脚本文件begin---------! w' G" ]; y1 k4 E# k
alter table student drop constraint stu_mid_fk;
" @: r' G4 f8 A' \+ Y: pdrop table student;
$ n6 d/ U8 R7 q1 d, Sdrop table major;
6 K/ t, O/ y  D8 fcreate table major(....);
7 k& [- f. J( {- o& N# D: I; F. screate table student(....);, j  w/ j  V) m2 N+ t9 s
alter table student add constraint ....- X& U* H: R. X' G3 n& b4 L9 q; F
-----------脚本文件end------------
0 g/ X6 e/ O# r  V* m5 m
9 ~$ |5 \: }$ p5 e$ j6 {user_tables  :用户所有的数据表
$ z9 r* U0 b8 G. |4 B$ Xuser_constraints:用户所有的约束条件' Z/ H2 C$ |: d0 a
user_objects :用户所有的对象(表、视图、索引...)4 J0 |3 y) [" v$ Z( U

8 l& P" X% ^( M7 S! d& Y5 r' u7 tall_tables  :用户能访问的数据表,
: q/ N- S# [! o! j             包括自己的和别的用户允许自己访问的, s$ j( u; L- J" z! }- V0 s
all_constraints:用户能访问的约束条件$ j; D& g1 f3 Q
all_objects :用户能访问的对象(表、视图、索引...): A: f" ~: }4 X% x
3 ?1 L' V! P$ Z) J2 [: q
PK / FK
- k  A2 J  T( O' z5 z4 k: Y9 _( wNOT NULL / UNIQUE
$ ^' M! y+ L- o* B+ [& i$ P6 GCHECK6 u% V' l  k( H4 O& G0 i$ w% ^3 G
其中:CHECK和NOT NULL可以在程序级别控制- g, s3 A, D- }1 Q* ^8 U; S0 E

& j0 M* {% d. v0 D. g* k, g二、数据库的其他对象8 K, f" h$ Z2 t
表 Table
2 E$ q" Z1 I4 i% e视图 View
, r# t+ I* N; i2 m" h. C  j索引 Index
  s, b' D' U0 ]& M/ f+ V序列 Sequence
, J$ l5 ?/ R. A( a  {过程 Procedure
- e7 S: n5 Z3 E% u( j函数 Function
9 g* N2 p  z8 ]# R% k包 Package
9 a. T& S+ V' ]! ~( x6 F触发器 Trigger
+ L% Y# A& [3 Z' [" f同义词 Synonym
- p" e2 {' A% G/ D8 P) O2 o....
; y3 V, Y, |0 |3 l
  i  v+ |4 G' l4 v$ e1.视图View
4 A" S) N& k$ g$ R9 lcreate view v_emp_ning# e+ s& l3 h1 r0 }1 H
as
& K! y& N1 i) z) K: Jselect empno, ename, job from emp_ning
; }- u3 }- N* }8 ewhere deptno = 20;7 n0 H% f* s8 U) K: P8 Y8 h
--使用和表相同
$ @# S* `3 i; j. N( wdesc v_emp_ning+ _+ I0 l4 o7 s8 U+ `: c3 m& I& |
select * from v_emp_ning;+ ]- ]7 `- s) K( b% H
--视图的好处:简化查询;隐藏数据表的列
, m3 o" c; o: U3 @& J4 z4 N
2 Y* F1 {# i& V$ a; d$ B" R7 O2 f  Gcreate view v_emp_count! ]# B! E# \9 K: x* p) l$ d7 H# }
as$ K; s/ j7 }+ U& ?& K2 r5 P
select deptno, count(*) emp_num6 }- @. Z* A3 @9 u
from emp_ning
$ A2 Z; U( S% \4 j6 b& Sgroup by deptno;
! i8 w2 h: Q! ~! h$ z/ [, h4 L# h& S. j, t--修改基表数据
, ~# t* c1 q0 J! s: W  o* V3 M1 X( B update emp_ning set deptno = 10  w5 v0 w0 U0 j9 z" ?3 s
where deptno is null;5 c% |! s& F7 V& y6 e6 {/ Y: a) Q
--视图查询到的是修改后的数据。. [) |& ~8 Y. v1 H  B9 V0 P8 j& z
--视图不包含任何数据。是基表数据的投影。
* P2 W# ~- V( h4 o- Hselect * from v_emp_count;
$ Z: Z  Y. [4 Z# m
* O7 V6 |, Q0 f- p! m--创建或修改视图; u3 P; I. s" i
create or replace view v_emp_count
7 V! o* A" R% ^* u6 T) I, [as# M! b' c  |; S) ]/ G
select deptno, count(*) emp_num,
! l0 d* ~) N0 a6 Fsum(salary) sum_s,6 m$ p' f6 Q1 M  }4 ~
avg(nvl(salary,0)) avg_s,. h3 o. N# _  |+ T
max(salary) max_s,
; n4 Y; |- n: i$ r* jmin(salary) min_s7 ^% f! }; g/ J- R4 t
from emp_ning9 U7 J/ R6 \3 Z" e" W' G
group by deptno;1 R! J' x: Y: \$ s/ ^
9 s' T" I7 F/ K) J
--查询视图的定义5 X; f5 O5 R) N+ r
select text from user_views& ?% ^& s1 ^& b$ K
where view_name = 'V_EMP_COUNT';
6 b! N; {& K5 L" @' R8 ]6 m# C--如果视图对应的sql语句显示不全
& w* E& b6 l. v) x- Yset long 1000" J: Q$ {  n; Q; j
! D  x5 ^+ M9 e) X/ q
2.索引 Index :用来提高查询效率的机制。& U) b: V4 c$ z, O. W# s! t
全表扫描: Full Table Scan: 查询效率极低" {2 f8 q; o; R7 e( R% x* @
索引查询:比全表扫描快。
+ ?3 {' w+ x8 l, u) e' S! ^. w
, e% ~- O1 _" N: L+ Z6 J索引的结构:数据 + 地址
5 T5 y* N: B9 x7 `            张三 + Room203
: E) J  t  y5 `4 I* n
& v/ F' N) e" ]: |对于数据变更频繁(DML操作频繁)的表,
0 @  X0 _3 _, z) @! S+ `6 L8 c索引会影响性能。
4 l5 l! x% x0 ?/ w5 A, c2 `+ J# r& f! x
如果数据表有PK/Unique两种约束,索引自动创建1 k* N& \  }# W# @2 D! P7 A1 k
除此以外,索引必须手动创建。
6 S) n' c+ ~- P: F8 _# N! I2 ycreate table student_ning7(, B) b% h' k: ]" a. b, {7 ?' e
id number(4),
* D2 t% s5 _+ ~4 jname char(20),( I9 L! `3 d3 M. {
email char(40),
; c  W  z7 r# H0 qconstraint stu_n7_id_pk primary key(id),
- ^+ f* h$ j. d5 tconstraint stu_n7_email_uk unique(email)  m- I" ~, ^3 ~  h' l( K' U; y4 f
);1 K- p4 }$ U4 t* c
select constraint_name , O) [, H. O0 Z' r6 k6 Y9 ^4 W7 o" Y
from user_constraints  i! R5 t' Y* @5 O% ?3 j
where table_name = 'STUDENT_NING7';  f! E4 i; Z; t# V' @8 Y; M

3 n+ u  {% h8 K' x--查询student_ning7表上的索引,( ?: Z3 r" X/ ?& B* k' `3 y
和主键/唯一约束条件同名,数据库自动创建的索引。* m3 Y, v' i5 S* a9 N2 W, v% w
select index_name from user_indexes1 t2 `; h5 p% u% q
where table_name = 'STUDENT_NING7';! v4 b8 u, N6 j- c, x# x

: p7 z' H9 L, a* H--凡是id或email上的查询,会使用索引. v2 n1 y- K# u) }8 u, z# p8 D
select * from student_ning7- b/ A: A; H. P$ R) ^7 ^* F
where id = 1001;
' G0 m1 D& \: S: c
" D3 [  W! U) `' F' k1 c--这种查询用不到索引:全表扫描
0 W5 E) H1 g% W+ F% u# s* _select * from student_ning7& R5 m( ?( Q, a0 ^' L
where name = 'zhangsan';
  S* i; N9 }1 q" @) M5 v5 A, ~. ~* o# r- P: M& I% K
--创建基于名字字段的索引,索引名自定义
6 g6 g6 P1 S6 Xcreate index 索引名 on 表名(列名);
1 V' _7 \+ n; wcreate index idx_stu7_name
! x) @, t+ h/ Ron student_ning7(name);
( Q* Z& j' a/ o  y& d1 ?* x6 D, p- b/ A  Q9 a+ V6 Y
索引:9 b1 c1 F4 D! s$ `  V$ N8 H
1)了解索引的工作原理2 O& d* X9 m) f
2)pk/uk自动创建索引
& w4 f7 t  h4 n1 Y8 c$ Q3)根据查询情况决定手动创建哪些索引。, S% t" u8 r2 p" q- X* g
; B! H) `/ Q6 n

2 H' e5 P* q2 T1 l/ ^  d3 H3、序列 Sequence --Oracle独有的  A& {8 q- @, ?. m
--产生从1开始的数字值,步进是14 I) S( X- X  g# R7 W' @
create sequence myseq_ning;
7 a7 W, c9 D! t: I# c! c  D% Y3 K* Dselect myseq_ning.nextval from dual;
  L  D4 }0 z' @8 m/ @4 C5 A  w6 L3 h/ M$ s8 z. Q
序列的特性:产生连续的不同的数字值  V* k3 }+ b+ g8 _0 T
用来作为数据表的主键。
- J, Q7 L# q4 y) C" l6 P# j+ E, c8 T+ [9 q2 B# F1 E
--使用序列产生的值作为表的主键值8 @2 Q/ M3 j9 g) k+ Z7 i. W
insert into student_ning7(id,name)! Y6 f5 w: _7 b
values(myseq_ning.nextval, 'amy');
9 _& y, o1 l: E4 n/ n  e3 z1 N! P, f4 q* F$ |1 j
student_ning7和序列myseq_ning的关系:! Z1 C2 `. l3 _% b1 O
是数据库中的独立对象
' d  z+ [$ i+ Q" `* |/ x--表可以用序列产生的值作为主键,也可以不用
4 t# p9 p3 L/ I--序列可以为一个或多个表产生主键,也可以不用, a  O1 z1 v6 v, s4 Z
insert into student_ning7(id,name)
" ?" L' x% e$ c3 H, H1 fvalues(100,'river');
- P  \& K8 w' zinsert into dept_ning+ y8 O# Q" G, M! B% b
values(myseq_ning.nextval,'market','bj');
* s8 o; m9 R3 y6 L( E
/ E( X# t% R9 N; U* ?- ?--建议:一个序列为一个表产生主键/ s2 X! n* b' r3 M
; H1 t/ ?# M1 H$ C" G
希望主键值从1000开始,步进是2?2 c& g  B5 s, z  l3 J- R* ^- S* i- T
create sequence myseq_ning1
! ?) v& [* _5 T0 Y$ tstart with 1000
! e1 D0 w) ^9 e) aincrement by 2;
. z$ M2 U6 `5 u; Y5 d, k" Y  j9 \' U: F0 v! q, x! C( F
insert into student_ning7
4 G7 \+ d; D+ A" j( Qvalues(myseq_ning1.nextval, 'song', null);
6 f6 M. E" c$ k# t( X' O  t' \6 H+ w' W6 G. r% X8 n
--删除序列,对曾经产生过的数据没有任何影响。) o( @( X" D6 {9 U7 J
drop sequence myseq_ning1;
: j+ Q' Q: ~6 Q2 y  `3 p, f4 p% _* Q* I" a! \1 T
SQL> edit) l! [; l; n1 K; K

9 S! m3 A* B4 z5 V4 O! u1 G

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


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

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

   

关闭

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

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