科帮网

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

动态微博

查看: 1716|回复: 0

Oracle增删查改 day05

[复制链接]

45

主题

5

听众

119

金钱

三袋弟子

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-06-03 21:43:49 |只看该作者 |倒序浏览
复习:SQL语句
7 ~2 \* k) s3 M. Q$ tselect$ C! G2 ?: G# f% U/ E" Y
DML: insert / update / delete
" X5 g7 O( }4 t4 T: z' b3 g+ q$ u: o( B. i' C" p* y+ R" o6 K* o
CRUD
3 z9 i' i; A' ^! D9 R) SC:Create8 C, I3 B. ~5 r$ Y7 a# S" o3 ^7 R
R: Retrive
  G$ ?4 L  g' J6 t- U" Q9 t8 wU: Update( l2 h! ~( i: _) }
D: Delete
) _! [1 X& X1 d1 c) {6 U: D8 Q/ L' t! @# @4 M& _2 h
DDL: create / drop / truncate / alter
8 _3 i3 X3 I9 I5 `$ Y- z" T0 w% f0 }! Q1 p& P# `& Y* J& q
TCL: commit / rollback / savepoint, m2 v2 Q7 R. E" P
  S  D. h: e- b4 B' z
DCL: grant / revoke$ O8 ^) i! W/ p! Z+ z7 g0 {6 `4 M
2 t3 c6 Z0 o. H: \! b0 k3 D
grant: 赋予权限9 W6 N  m) I& x; t6 ]* T
revoke: 剥夺权限. m/ P5 c5 u! p+ y7 P7 @
数据库中的用户: openlab   hr    scott
3 I* ?" l' n9 [' [                 emp/ p7 F0 M& B7 z5 K% }

9 p+ k5 p6 t: G! Z5 S假设现在的用户是openlab$ k) g! H; U' E' V# [. s
SQL>grant select on emp to scott;8 ^, T# c* e5 ]* `4 z: M) }3 s4 `4 Y2 w+ m
SQL>revoke select on emp from scott;: x2 D7 m: a4 v* f6 ], q9 B

- V% h6 v: u9 O8 K% Iscott的会话:
- U4 g" g. k% L7 ~SQL>select * from openlab.emp;
( R' {' r  z& c+ s/ Q  k7 OSQL>select * from emp;
& z' E1 C' d5 T* w: d) y  Q9 u
1 B$ J, \" i$ u! A
7 B8 F6 x- \: }' D- K$ S9 }# S9 iOracle数据库的用户:
, \! R; O, X$ N- fsys
: @- ~0 \, H* C0 `0 y1 Jsystem# e% S4 {2 j5 V
: t( F7 L" Y# B6 |/ M9 U7 ~. D. K
scott/tiger( ^) g9 k( ?: k, A: }2 W
openlab/open123
# p! h9 d1 E8 P$ Tninglj/******/ C  V$ ]) s. \$ F( m3 ?: Z
exam/exam123
. R# Z- [8 }  l9 ?* `- `4 t" L3 Y8 j( C8 d- w! V* L
C:>sqlplus scott/tiger@192.168.0.26:1521/tarena4 x9 U& C. B4 Q8 y, P
SQL>
9 \2 r0 D# i- {& Z: ~/ G& i
6 W6 V" I) M3 k4 j. f& X3 h) v% ^* z& J2 G! Q( k
今天的内容:
. p2 J3 [- @8 o1、约束条件& Z& o% M& C5 ?! u. A( z
2、数据库的其他对象
8 [! O) F) a3 y- R5 G- d7 }( R+ i6 Z6 _6 d
一、约束条件 Constraint
9 |9 I  p" `1 `; |. Q: V1。主键约束:Primary key, 简称PK
0 V2 V, t( S: |8 Y8 w--建表时增加主键约束条件
! {$ J# H$ H& H* O! q5 pcreate table dept_ning1(
& f2 X9 |, K* T" @6 y# \4 `; b2 Bdeptno number(2) primary key, --列级约束条件
# Z4 p6 n' p, c/ i" Z- s6 Kdname varchar2(20),
- E' `$ p) {6 f, a% a: [location varchar2(40)8 I' h" |8 H! F. s
);+ l, n4 Y+ E9 r# U, H9 b. y0 H
insert into dept_ning1 ' M2 J* J0 ?, T
values(10,'developer','beijing');
- a7 l: r  ]" L6 j6 h# a! W% |' E) A1 _insert into dept_ning1
- t- j" E& B& \4 ?values(10,'market','shenzhen');) C' _# a, y7 q! K, O  L
--如果插入重复编码,会提示:
! D( z' D2 t4 ^/ B4 J0 R: E- O4 KORA-00001: unique constraint (NINGLJ.SYS_C00634053) violated
# d# B' N9 V* J! r9 v+ _8 L其中,SYS_C00634053是数据库自定义的主键名- @$ R% R1 b- W: C' j; m
--约束条件如果没有显式命名,
) ~! |8 l0 T+ O0 H3 [数据库给约束条件命名:SYS_C*****
  W% t, h  s! N" Q+ [  Q% N7 S$ a( q( z6 d, f: h
--在建表时自定义约束条件名1 P4 ?' F9 O6 d7 ]
--建议命名规则:表名_列名_约束条件的类型4 b- @9 i9 B7 a3 R" G" S- S
create table dept_ning2(
# _+ W4 ^) E4 f: Q# t' j6 A, z3 bdeptno number(2),2 o* N0 R* n) b( W- o9 c& ^
dname varchar2(20),
1 u6 C6 W) ^6 l4 llocation varchar2(40),( U# s& K! S/ h# T/ M6 U
constraint dept_ning2_deptno_pk
: h5 w- U. x) t1 ?1 h( |% F- }primary key (deptno) --表级约束条件# h3 g, x( B+ |' F
);1 @' `8 b* c! a. ^
--当插入重复编码时,会提示具体的约束条件名字错误。
& Q& t3 L; u- |* }; Y2 M+ [--方便定位出错的原因
3 _! w& X- f! X/ d
" S# x  d" x+ h0 l# o6 x9 D主键约束:primary key = 不能重复 + 不能为空
/ P$ I, o8 q1 Q% D* L% Z* B: Z  j  S( V/ r
2、非空约束: not null,简称NN3 r, x, w8 U1 d0 h8 q8 W1 u) M
学生姓名必须提供,但是可以重复& H% O" T, z) L
--只能定义在列级  k2 t9 o% A4 V; @* r7 h: I& ^
create table student_ning(1 x" j4 Q, e1 T. D+ a
id number(4) primary key,7 c9 F2 c( ]' Z" O8 @7 z: o
name varchar2(10) not null,
3 M' [+ @+ H3 K! O/ }& p6 aage number(2)* C- l( ?. T- l) U7 M' [
);7 f3 {; y5 d+ u0 y) ?* a
insert into student_ning
# {- c  F5 G8 G! Wvalues(1, 'zhangwei', 20);
! Q, V) J& C( R5 h5 f' c--名字可以重复
# B5 F! h$ ]4 ]1 v. n( V3 Hinsert into student_ning 5 w: h0 R0 n+ D8 d) R, [- }2 L" l8 ^
values(2, 'zhangwei', 19);
2 O, t- a1 [3 ^1 L" u--提示name列不能为NULL
) J' d2 A( F# n7 J$ }& sinsert into student_ning
4 g8 o0 O8 |; Y) \% svalues(3, null, 18);
" {! s) j! x; Z8 [0 C. b4 q% |8 d, P$ Z5 h
3、唯一约束:Unique,简称UK
) [. \- E' ]3 i6 C8 Qcreate table student_ning1(# z3 e% b$ H! h+ I5 @  f6 w
id number(4) primary key,
3 ]0 \/ k3 c$ O* \  |8 e- p3 O0 _name varchar2(10) not null,: u4 D4 C$ t1 t/ L
email varchar2(30) unique,5 L, y3 c7 ^  m8 C/ r) d" g
age number(2)* l7 G% A3 a4 H' q3 I! v! U# g
);: e9 X2 Q& ^* N7 V7 Q
insert into student_ning1 6 f- e+ D  U7 |/ M2 Y8 ~: Q# z
values(1,'amy','amy@doctor.com', 19);
2 [- d# ~/ L0 g* L. O) ^--ORA-00001: 唯一约束条件被违反+ K7 |. L/ \4 R% h  A; n2 l
insert into student_ning1 3 T* {9 W6 g- F, Z" e
values(2,'rory','amy@doctor.com', 19);
/ U1 T0 E. X1 D# @* t6 A/ X2 z; ^: r$ ]
--唯一约束建立在表级* @( d/ L, k$ t" y% q/ F) l2 g4 Y
--主键约束建立在表级
, I) g9 m$ ^6 ]; Hcreate table student_ning2(1 L) x: ?% \- k; w* G! n
id number(4),6 P/ m  i, ?4 B) w: U1 C; D
name varchar2(10) not null,( V3 ~! M) x( }1 i
email varchar2(30),
' A; _, H$ r3 \% Jage number(2),& g3 j7 }' Y! ~$ R3 k
constraint student_ning2_id_pk% A( j) f7 Y+ A. g* v: g2 ]
primary key (id),
9 Y  J9 H/ r& F! M* R* ]constraint student_ning2_email_uk
: D; H1 L: |1 D; hunique (email)
6 y- \; Q$ s& O  J' Z3 |6 g; k) b);
6 Z( i. U  D$ F) p- {9 v$ V
  f- a  {' f1 z$ t: Y0 ginsert into student_ning2
4 g. V9 I* P* U6 P* evalues(1,'amy','amy@doctor.com',19);1 D$ ~$ @; ^0 Z# T/ t/ y

7 W4 _! r7 Z6 l; z--unique约束只要求不能重复,可以为NULL% t' h2 e# x. H
insert into student_ning2. e7 B0 x$ K, G* U
values(2, 'rory', null, 20);  N3 R1 H  n$ B; Y
$ s( b* A* a" U; D+ x2 ?! k  Z2 L& R8 I
--不管是insert还是update,email都不能重复。8 f& g2 O* d, n4 U' G7 H' B8 e" _
update student_ning2 set email = 'amy@doctor.com'  H- r" [# \/ E  E) `
where id = 2;/ r) m! J  Z$ r# S, q

: D; @+ q4 C: q. r7 _- E# z4、检查约束 check 简称 CK
6 V1 J# E. g! g, ~- o% ~6 q- E! S$ ]create table student_ning3(
: Z4 V7 ^' }  a2 P# _5 }id number(4),
; f& w8 [  }0 _9 }# jname varchar2(10) not null,
; w( @6 P6 B) d0 yemail varchar2(30),
6 o# w& M3 ~  Z7 Z7 }age number(2),/ @$ W9 j1 i& p7 {& M; Z, ^
gender char(1), --'F':女生; 'M':男生
! J2 D7 n) I* D  ~% n4 N" {  tconstraint student_ning3_id_pk
4 t( z& Q* p4 l% l* zprimary key (id),
. \, L. i+ L" v* j$ V8 aconstraint student_ning3_email_uk$ M+ ]  o( D! e+ g
unique (email),
6 h, R; \* c8 z; W3 G4 T: r" Oconstraint student_ning3_age_ck; w( @1 p7 r0 K( q) z
check (age > 10),1 c) n2 A5 V  @/ l1 b, y) M
constraint student_ning3_gender_ck& h0 U8 s1 q8 {  A
check (gender in ('F', 'M', 'f', 'm'))
* v! ~! ^+ ~+ v8 H$ s);! ?8 N: F; E; n$ y4 P5 I6 e
insert into student_ning3
: i6 J* ~0 G6 a6 C# H, H( Xvalues(1,'amy',null,19,'F');/ C: Q) E2 ]2 Y! C
insert into student_ning3
% K. }7 b9 i$ [2 yvalues(2,'rory',null,8,'M'); --违反check约束 age > 10" m* g! @# W/ J8 M. B3 s' ^) e
insert into student_ning3 9 x8 J0 i# y9 L& v; \8 [
values(3,'doctor',null,50,'A'); --违反check约束 gender in ('F','M')
/ M5 W2 B: r8 |! Y$ i# n
( q4 b6 t# c( r  M8 ~4 P0 t: ~PK / NN / UK / CK / FK
2 d$ ?" G- D1 r$ X+ m$ p$ A* j  A. {/ n$ B" a" F
5.外键. Foreign key, 简称FK
& W* ]$ T4 N* Y$ Y9 ?" tcreate table major_ning (
5 g8 E- N5 Z3 {. \) J- B: ^7 o/ i( @id number(2) primary key,
% [# e# Z. ]- P- k1 }name char(20)
- @0 k; Q& j$ s& J- l);
! A& p. p# J5 H) q* H6 Cinsert into major_ning values(1, 'computer');
  w2 L( Y$ w- B( winsert into major_ning values(2, 'history');
: N$ Q9 F" ]. i& w( u* L+ Oinsert into major_ning values(3, 'music');' r  m0 p% k: [
insert into major_ning values(4, 'sing');/ F; u* c5 L' N! b  C5 _7 z; E* O
commit;5 d8 b( d, q/ u; B! {$ D
create table student_ning4(' u9 V6 X5 ]$ F7 m
sid number(3),0 p4 m! k2 [; R4 q, t7 q8 {
name varchar2(20) not null,/ p$ Z1 V' \" u" K' j) W
email varchar2(30),5 n( p; m" q; f
gender char(1),
: T0 Q, e& B( H( h* n! W. \! nmajorid number(2),+ M. }6 L! R0 q" }
constraint stu_n4_sid_pk primary key(sid),* M: r+ g% D4 K- f
constraint stu_n4_email_uk unique (email),
: P9 X) ?! _* Z3 P: ]+ i0 Mconstraint stu_n4_g_ck check (gender in ('F','M')),
5 n* E* |, A8 V  G: H3 nconstraint stu_n4_mid_fk foreign key
/ Y8 B- U" P5 g1 x  (majorid) references major_ning(id)
& w  a: D# B# u7 Y: {# O);! |6 w+ ^6 W3 J7 F8 n: H

& F, o9 ?) ]; linsert into student_ning46 K8 ]6 a6 Z' u
values(101,'amy',null,'F',1);
) k) y( _+ \% a7 C! E--新增数据,不存在9这个专业) K% Q5 G5 i# W. Q  P( \$ o
insert into student_ning4
4 D; T0 u$ a8 s* O! Avalues(102,'river',
; R0 w6 y, I; G. I'river@sina.com','F', 9);/ ?/ f: {3 W: U" W
--提示错误:
% R8 ^8 U, ^3 c* q4 KORA-02291: integrity constraint (NINGLJ.STU_N4_MID_FK)* ?" c) ~7 _9 o" M3 T2 i
violated - parent key not found
9 I6 v; C/ v9 s3 E9 ^$ t' e" tinsert into student_ning4, N0 h6 q% ]. X" r& D  ?8 d8 {( `
values(102,'river',; f0 @" t  _+ H8 w# R4 x
'river@sina.com','F', null);; @% q3 H9 g3 j2 k4 L# e$ I
, J9 _, _$ F7 C' R& _. G4 k3 Y
--有学生属于专业1(computer)7 a+ L  j: w6 z2 {. R6 t  W
delete from major_ning where id = 1;
" V4 l0 d% r0 S, J8 a9 \3 D' A--ORA-02292: child record found
' k: k# A3 W" F- L/ e4 a
+ y3 f9 t- `1 M3 Pcreate table student_ning5(" n5 Z. Q; G. ^& c& S. Q* I( A2 X
sid number(3),
2 F; L! T) _# a5 ]name varchar2(20) not null,
# ^) H( [7 h% \7 Semail varchar2(30),
# ^& q6 {* ~8 K. U8 o% v4 {gender char(1),: p8 W6 _+ Z! ]4 A
majorid number(2),/ X1 ^7 v+ `$ r
constraint stu_n5_sid_pk primary key(sid),
6 x" q( L0 `1 t2 ]" A+ R4 ^constraint stu_n5_email_uk unique (email),* a* ~( ], r9 i9 M$ J5 l  M2 t
constraint stu_n5_g_ck check (gender in ('F','M')),
2 {0 b1 t: F- p9 bconstraint stu_n5_mid_fk foreign key & R; t6 X& U' W+ P$ M: n
  (majorid) references major_ning(id)
" g, F* f7 I% _- q: R1 _  on delete set null);
( X7 N6 a+ |# l( d0 A
( W; X' d( \7 K4 p/ o1 rinsert into student_ning5
( \- J' ^( A; B# _9 M8 rvalues(101,'amy',null,'F',4); --amy是4专业的学生% U! l% D% m) |$ _6 f9 u2 E
--删除编码为4的专业0 L: v2 M5 R* r* v! q
delete from major_ning where id = 4;
( B4 M) z" {+ g$ u$ i! B! V--amy的专业被设置为NULL
8 ?' [5 ?0 }# o1 ~6 E1 H, Zselect * from student_ning5; 0 M& x# O  [: O, C" W( t
, x  U$ l2 i& Q  b
create table student_ning6(2 d8 H, g4 V! t8 }7 D1 Y. Z: W6 s
sid number(3),
# i, ^) q( I' {" r  I# k- _$ h+ Lname varchar2(20) not null,5 f% G' G, u- X2 N, |
email varchar2(30),3 w% T3 \& |( _$ J+ S" ~
gender char(1),
+ i/ ]* s4 H+ c6 f, l0 S/ Bmajorid number(2),
. w; ~0 {0 S0 n, N8 X% Oconstraint stu_n6_sid_pk primary key(sid),3 w8 P' o. E5 [2 X6 x% S+ K
constraint stu_n6_email_uk unique (email),/ i) L. e0 Y% V6 F! U
constraint stu_n6_g_ck check (gender in ('F','M')),
* O; v4 B8 F5 i8 o3 G& iconstraint stu_n6_mid_fk foreign key
( _8 n6 `( r2 F  (majorid) references major_ning(id)
% O; m9 r: b1 H; d( V% z  on delete cascade);
3 U6 f1 }% |2 |9 g2 U+ {: A2 e8 w* }8 O0 S) i3 [' N
--复制表,不复制约束条件。
/ P5 @. J; g5 Qcreate table 表名 as 查询语句
, ?4 R) v8 G& _
* w  E( p# ]$ D7 M. I--建立约束条件的时机
1 s4 p0 M% I/ J  A% r+ p7 D/ [/ I--建表同时建立约束条件:
* G; C2 v0 h) n6 ~create table student(
5 @3 P, R; m3 lid number(3),
$ G+ h, C5 Y6 O3 T5 Jname char(20) not null,
, h# E7 I  p% i, E6 u7 \# N6 Kmajorid number(2),& g0 e: h  V9 S) N4 K- l7 Q
constraint stu_id_pk primary key(id)," v- ~* J  M, E* J. A
constraint stu_mid_fk foreign key(majorid)9 g% m( Y2 Z6 u& F9 c
   references major(id)
' g, h$ I6 ^* T* _) R2 g' Q);+ g! G. I5 B. u+ k8 r
--在创建完表以后创建约束+ m+ Q% X) O& v: B1 L9 k
create table student($ d3 k" ~2 w* D4 v
id number(3),
6 ]; I6 ]! f% k7 Oname char(20) not null,
) a: L0 k! F$ L0 j: j, F: x& Imajorid number(2));
$ g0 c; Z7 Y4 D& X3 D. z/ z7 |alter table student
2 [; m3 R* g" C- S8 I  add constraint stu_id_pk primary key(id);' W9 [5 y7 ?. e+ f# y  ]
alter table student9 }4 ?9 [3 ^& h- p& d+ ^+ j, h
  add constraint stu_mid_fk foreign key(majorid), |" ?4 z" Q' \+ k( ^, u7 K, p2 Q
  references major(id);
0 T0 S' C: q* _, }
; v6 Z% k0 T1 m& ]( A0 G& t0 u1 W-----------脚本文件begin---------- y. \  E4 ~# h( Z9 \6 }
alter table student drop constraint stu_mid_fk;
3 n2 H7 s' z9 R0 idrop table student;
- @& K* c* {$ o' Bdrop table major;# N8 p9 y1 c& L+ }+ w. S& [# d/ ~
create table major(....);/ \9 k3 f/ @2 u
create table student(....);
5 p+ D! w/ G, }7 M3 P8 Dalter table student add constraint ....
6 \9 p' Y) O- a2 Z) B3 ?, U2 p-----------脚本文件end------------
+ X& V" N8 r9 f% Y" Q! ^3 l- d' M2 a) P7 [8 C. U* D
user_tables  :用户所有的数据表7 g+ b3 |% k9 `* h- r
user_constraints:用户所有的约束条件
$ b6 ?. Z$ `1 Luser_objects :用户所有的对象(表、视图、索引...)' B' i1 L8 ^4 ^4 r/ D
4 E& y- m4 H! a7 W+ c
all_tables  :用户能访问的数据表,- y+ L0 r, U* t9 s1 Z( Z
             包括自己的和别的用户允许自己访问的
- y: m8 g" W# ~( `all_constraints:用户能访问的约束条件
5 C% D6 u8 x) T7 T; {8 vall_objects :用户能访问的对象(表、视图、索引...)3 `- a/ i( Z1 P4 Y$ O

& o3 E& F6 _- s$ ^0 S' LPK / FK
) l% N4 ?* P; tNOT NULL / UNIQUE0 s+ p$ F; v0 s' j# I- O
CHECK
6 f. T% r" g5 h) Z2 d( }4 c9 K其中:CHECK和NOT NULL可以在程序级别控制. r$ S* A3 |+ d0 G! ?3 M1 s
2 U; E$ a' n8 [8 s; o2 R0 T* Y
二、数据库的其他对象
# N% I* |  B% n2 W. i$ n: q表 Table
  \4 A" b+ w7 j/ K* Z! Z0 R视图 View0 l  }+ m) ~0 \  {9 g
索引 Index3 E0 `! Z) k( J+ G$ E& `2 K
序列 Sequence
5 s! H  p( |: ^9 R2 |- e  e; u* F过程 Procedure
$ \+ h. p2 f6 j' j  N函数 Function% w, q1 |7 O  x! L- t% S7 ]8 r9 E
包 Package
* r7 ]& T/ L" Y! s1 S% q; F触发器 Trigger
) F' J+ x2 j! Y同义词 Synonym
( J: Y1 z' p  F9 s5 B, T....( [) F- t" A2 d, I' M
) J7 q( @! o/ \* D" \
1.视图View' x1 A& ]4 W! I
create view v_emp_ning
0 |  D7 z1 q. r0 nas8 J7 T# A) E5 E) a# S& _" A
select empno, ename, job from emp_ning
  d5 O% {( p7 n- Fwhere deptno = 20;/ I( k; S( v8 I" e0 Z+ Q  [  [
--使用和表相同3 D6 N8 D; F+ w+ K% N% B
desc v_emp_ning
' B: Y7 A  C& k- a2 ~6 q' Q: aselect * from v_emp_ning;8 z  l1 `( \1 }7 X; c
--视图的好处:简化查询;隐藏数据表的列
( {5 W( F& O1 S% r) G( ^4 @& ^% X3 \: T& B$ a
create view v_emp_count  R. H5 x2 S: C
as
1 G* k! g. R" Q' q1 zselect deptno, count(*) emp_num( T' N5 D6 o0 T' w
from emp_ning
" u  G4 ]  @1 A( p0 N# sgroup by deptno;
, P+ U& I) O0 Q--修改基表数据
9 S2 @+ l& L, e) B update emp_ning set deptno = 10
) C* ?5 N  D* a7 |  m! p+ G6 H where deptno is null;
9 g$ y# |8 K1 ^% f; [- e9 J1 v--视图查询到的是修改后的数据。
# d- Q, N. H# y--视图不包含任何数据。是基表数据的投影。3 i5 m; G: B0 K( c
select * from v_emp_count;
" k4 R$ Z/ O, T' G% U6 G% M. b7 U
--创建或修改视图- d1 \5 }1 Z% h
create or replace view v_emp_count7 {( C+ p8 i% C
as2 D" ]) G6 S7 E0 B( g' y9 x- T
select deptno, count(*) emp_num,
5 W, }8 b# k- r; A0 msum(salary) sum_s,  h( Q, V) I5 j: _% \$ g
avg(nvl(salary,0)) avg_s,
' F% ^; h' l  y. f$ y5 hmax(salary) max_s,
9 Q4 h2 v3 S. z( U' [min(salary) min_s, Y1 D+ _- P6 f9 |' @; o% b
from emp_ning
* d7 j0 K- t, L! sgroup by deptno;4 V# U5 v9 w$ d
9 I6 Y+ G8 m) J. j
--查询视图的定义
5 Y) T$ B5 B/ z- W, _. S/ o& b6 oselect text from user_views
- P% D7 E! b+ |1 C) ~* R, b3 Nwhere view_name = 'V_EMP_COUNT';
0 R3 a7 t- |% J0 p" r& O; c/ x--如果视图对应的sql语句显示不全5 m1 L3 L/ o; V. @
set long 1000  Z" J% |) J$ G4 w- s  n7 p+ B6 V
9 V& j- a2 M& O1 q5 A0 w
2.索引 Index :用来提高查询效率的机制。# Q& I8 ^) c" i+ h) E
全表扫描: Full Table Scan: 查询效率极低, T+ P  G0 w, \# [- G: B/ A+ Z
索引查询:比全表扫描快。
9 D; s/ a- Z: r2 J2 w
5 ^  w9 n8 K8 x' i, ]" u9 K$ _索引的结构:数据 + 地址
. g. p! K5 y! Y# ?4 s4 D            张三 + Room203- P' j/ P+ g; d7 r) y* U
6 z# t- t: x) X8 u5 o9 j2 E
对于数据变更频繁(DML操作频繁)的表,
  v2 i: ~# s" d+ o. f6 R! ?索引会影响性能。
1 c5 E. @3 e* P6 D" R% s6 [( X  B2 @5 i# p0 m9 y5 ?3 n
如果数据表有PK/Unique两种约束,索引自动创建# w! |- t! Y* F" M
除此以外,索引必须手动创建。
6 S# a: r& F9 N2 ~9 G" M! R, Ccreate table student_ning7(
7 I+ \4 o. T' Qid number(4),& y/ b: |: V$ J& v3 S/ h
name char(20),# X0 E; q% D8 p
email char(40),
6 L" l- \+ ^! h& f/ Y: j7 D! Vconstraint stu_n7_id_pk primary key(id),
& c) J9 ?4 K! Jconstraint stu_n7_email_uk unique(email)
3 i: J) B+ f: l);2 K. i& w6 H1 N! k' t& d
select constraint_name
/ W2 y# c5 O* E) G" p+ I: g5 Mfrom user_constraints
9 f# O& i9 f' P* J' Zwhere table_name = 'STUDENT_NING7';
) A  d6 ]  n7 I* \# q  E6 Y7 N# Q; V& j" [
--查询student_ning7表上的索引,
1 B1 F( x  {$ D9 o5 C! m: I和主键/唯一约束条件同名,数据库自动创建的索引。
* ~; |7 h9 X! H# Oselect index_name from user_indexes- Y0 O3 U9 E* x+ C1 f
where table_name = 'STUDENT_NING7';
7 V; C# H; S. R% @* t# P
& B9 Y+ P' m: R8 {! T! L--凡是id或email上的查询,会使用索引1 X# j0 R: Y6 O# A
select * from student_ning7
3 F4 \. E: X0 Z4 p- bwhere id = 1001;4 p) s, d. a& ]! ?" ?) l

% I$ K; U; E1 s--这种查询用不到索引:全表扫描
7 Q  C0 W& i2 q! S6 O' v) _+ nselect * from student_ning7
( @% r5 D! u  I4 n$ t+ O. U4 t0 T5 uwhere name = 'zhangsan';
  O, i( r7 t, Y- ^
/ H) Q# k4 c' ?- f7 p0 L--创建基于名字字段的索引,索引名自定义3 G: @" z2 K5 y3 Y
create index 索引名 on 表名(列名);
# r7 s5 R' t; h, Lcreate index idx_stu7_name
; l' n) [+ K5 R$ u# u- A+ x& ^1 v/ |on student_ning7(name);
8 P6 k* S/ W+ Z# u
) K9 p0 j2 l; r9 }6 T' Y索引:- ^& p" }& q7 l8 k: z
1)了解索引的工作原理: l+ N( U9 D/ i# O! M( c6 e; i" \2 B
2)pk/uk自动创建索引
$ U8 V# c2 P: c3)根据查询情况决定手动创建哪些索引。
  b5 `2 g) L6 I$ p) p: _- h* H( I, S* ?& L; Q+ `; C( z

# @* _0 J" p: x3 O/ V2 K6 o/ [; o3、序列 Sequence --Oracle独有的
0 L" t( ~6 l7 I% c4 _--产生从1开始的数字值,步进是1+ X, A3 \8 d, ?5 Z4 D! z, ]
create sequence myseq_ning;. J1 L" H$ Q! \; Z  I
select myseq_ning.nextval from dual;
. a3 p7 N1 `" g" O( {) \7 G
; v) X8 I9 X1 X9 E" H) [; S序列的特性:产生连续的不同的数字值4 I) J# L% @/ B0 H2 e2 ^
用来作为数据表的主键。
+ }$ M' @0 T3 L/ o7 s1 l2 S. m9 a: a1 t3 {  q
--使用序列产生的值作为表的主键值# }; K0 D8 _1 g0 s- k. |% U
insert into student_ning7(id,name)( P& _: _. ]. O
values(myseq_ning.nextval, 'amy');
( @% d3 v% e' X0 h( M1 D% y2 R9 O
0 D( m( \8 |/ o/ Y$ V/ \( Wstudent_ning7和序列myseq_ning的关系:4 d& w0 f1 z- [) c. _) ]
是数据库中的独立对象3 v6 }" U7 c  E$ o8 ]
--表可以用序列产生的值作为主键,也可以不用
" @1 O/ O" V# G/ W5 a--序列可以为一个或多个表产生主键,也可以不用7 M  ~8 ~, |' Y" K: b1 ^& b4 \
insert into student_ning7(id,name)( F; J1 |; `6 e# X, ^- g8 Q" J" L; K
values(100,'river');: @' k$ h) _: ?2 Q* Z/ ~
insert into dept_ning
7 {: r& v, r+ u0 }$ k! }0 `- Pvalues(myseq_ning.nextval,'market','bj');
; z, W, N! k" D; P$ \0 l% F9 `8 C) ]5 U8 V/ r' [) U
--建议:一个序列为一个表产生主键; z. H1 F/ u" ~- c' u+ @

/ m2 y6 |8 s0 K! Z, `- I- [希望主键值从1000开始,步进是2?0 Q$ h' c7 ]" l0 M; N% K
create sequence myseq_ning1  w- j2 D) l* `3 Y
start with 10008 z" P. n0 x% ^
increment by 2;8 \; ]% N$ w1 z

2 s& W9 P) ?+ l' pinsert into student_ning7
$ d5 ?# [# ]9 |# H8 {- _# n1 P6 [1 uvalues(myseq_ning1.nextval, 'song', null);
) a4 q; L) T: a' ?  A/ }' Y$ U/ g) @& z( [# d: ]. i
--删除序列,对曾经产生过的数据没有任何影响。
# C5 i5 }2 v4 F. ^9 f* B! T) {drop sequence myseq_ning1;7 `* w+ v% {/ E0 I
+ g$ H1 Q5 L$ _% u0 Z# ]
SQL> edit: }7 _* A0 S6 ~5 U
8 X# Z: `- B/ G

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


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

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

   

关闭

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

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