科帮网

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

动态微博

查看: 1714|回复: 0

Oracle增删查改 day05

[复制链接]

45

主题

5

听众

119

金钱

三袋弟子

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-06-03 21:43:49 |只看该作者 |倒序浏览
复习:SQL语句
2 @% q1 G1 J" h& i" pselect
7 k/ ]# j  y/ Z& r  v$ BDML: insert / update / delete9 C$ {6 E4 ~  {2 g; P
3 E  R# b2 _5 h6 X. z
CRUD4 k  w$ {1 _  j- ~* o3 I, O
C:Create
5 j. [& g  c3 k2 ~/ c3 I$ `) L9 kR: Retrive. Y  L8 F. D0 \# x3 @5 H7 @3 O. {
U: Update
" |) c. T+ |4 V3 J# j* KD: Delete# F) f, W; r: Y' L8 ?

! F1 |( s/ I+ l, L7 D, A: `DDL: create / drop / truncate / alter7 g$ O- T! f, ]' n; s. a0 _

' ^+ R( }; D$ H/ c! ZTCL: commit / rollback / savepoint7 g8 z) w: m* v; V8 P- @6 q
; b2 P/ o$ U0 l% ~
DCL: grant / revoke
5 x: |0 R) s4 N- z- K* K2 O2 `2 W2 Q! v/ x0 y1 R
grant: 赋予权限  Z+ [2 A" G! O0 q0 z& U! _
revoke: 剥夺权限! O) s8 h4 P  V( p. D* {
数据库中的用户: openlab   hr    scott
. h9 z; K) w' l+ G* s" Z" q" S                 emp5 V% X( M* l* Y0 h! A. c9 {

. p& n! O, |* A7 o+ e! y假设现在的用户是openlab& z8 M% `- b4 t+ o6 d, E
SQL>grant select on emp to scott;
: B  R% [! j3 s: Q* FSQL>revoke select on emp from scott;1 Q- z' Q/ T: T7 _( @
9 r/ V7 z5 E' ^2 R9 b* D; c- s
scott的会话:
2 h$ H7 u0 n) q/ ]2 L8 L3 kSQL>select * from openlab.emp;
0 g2 M7 Y$ K: o/ z) {" NSQL>select * from emp;9 P" `; E2 p* A4 S1 X" Z2 ^% V
$ u! O9 J- `# s. Q; H

7 w; v) ]! a! OOracle数据库的用户:
$ n' Y+ X8 \9 A5 Z$ n" ^/ osys4 r3 A1 e0 \- C( j6 p3 X
system
7 X8 _  Z: g0 K- `0 s: u$ M" ^+ \+ }7 s* ?" h- \/ C. p! v
scott/tiger7 Y4 [9 ?7 Y- }; c( B1 P& H. X
openlab/open123
, R) Q0 a; }; ]' H; A/ L, u, g' _7 `/ kninglj/******- Z; |$ g$ a. d9 Y
exam/exam1232 u0 t1 j( p  k+ T9 M

1 n9 X" V4 Z( J& P* e2 d& |6 w: l1 zC:>sqlplus scott/tiger@192.168.0.26:1521/tarena+ [- I0 m- a; D2 E6 S
SQL>
( L) ]9 N0 K/ W- F3 R8 v7 E/ i+ \' W! _9 A9 b

3 p7 O- @$ b! k- q$ I今天的内容:
9 E+ S6 h" g6 ?0 a- Q1、约束条件
; t) a& A/ A# l/ E( S3 y# c2、数据库的其他对象8 K. u& P; m" ?5 |- X/ }
+ ?4 h3 _: N" x5 l
一、约束条件 Constraint
" }  o. U0 ^7 e. L' f) f' j1。主键约束:Primary key, 简称PK, R! S4 X8 X# [% K; I
--建表时增加主键约束条件4 f/ b; e$ ?2 L% k4 V+ Z" q9 I$ |
create table dept_ning1(
+ M6 p1 r) f! j( U# Q7 ^" A$ _deptno number(2) primary key, --列级约束条件
0 i$ o: C+ I" k  u# P2 p" kdname varchar2(20),* V3 Z# a8 [9 h$ I/ j
location varchar2(40)
* d" b5 Z: ?1 {' A);# }  h. B, F0 I9 d5 M6 x5 k
insert into dept_ning1 ! K/ q4 j9 h- e4 Z( c
values(10,'developer','beijing');
0 j" C4 @# f/ Q5 d' ]8 [insert into dept_ning1 " b' m" k9 N: l6 ~
values(10,'market','shenzhen');# y( `) q& l6 u/ A) b& p. k0 b/ G; n$ g
--如果插入重复编码,会提示:
; F0 u- `7 E2 i7 O6 S5 X+ m) OORA-00001: unique constraint (NINGLJ.SYS_C00634053) violated
$ l+ w$ }9 q  \' q8 G! H其中,SYS_C00634053是数据库自定义的主键名
& y2 ]  k+ r. D9 {--约束条件如果没有显式命名,
) `' m! Q5 q6 R' D2 z7 p数据库给约束条件命名:SYS_C*****
. F* u$ b5 |( R, O  E2 I; t- h9 k+ L: ?: d, p: x$ i
--在建表时自定义约束条件名7 T) T$ R$ U) o- r9 V
--建议命名规则:表名_列名_约束条件的类型
  I( G+ v* t8 {- V9 Q7 B7 ^create table dept_ning2(0 ~' }; L# l! u( i( H. Y
deptno number(2),9 W8 D% j9 x; a% m$ B- E
dname varchar2(20),; p  `- f3 l/ Z& Q
location varchar2(40),
7 I7 F, L5 G2 M' _constraint dept_ning2_deptno_pk - i* D# U; L# |, N
primary key (deptno) --表级约束条件
. P; Y, ?  h  {0 Q  C);
$ D/ j, l7 z9 \- g2 `--当插入重复编码时,会提示具体的约束条件名字错误。- [' i8 L  Q! A2 r5 ~5 ^
--方便定位出错的原因
4 f1 \. I' E" r8 X$ z  g
7 Q' d9 \# ?3 P& L主键约束:primary key = 不能重复 + 不能为空
" w' {$ y# k1 p- f- G! J( x! h; |
- t( @; S! I& f& o5 z& t2、非空约束: not null,简称NN. k- T, @! V& y8 M' b* J" C) W0 D
学生姓名必须提供,但是可以重复. k; L2 L! P7 i" l( M/ I
--只能定义在列级
0 U) {% D( F& _! X4 |create table student_ning(
! D, Z0 V/ P3 k7 U. ?id number(4) primary key,
' H" i* y$ d$ b+ J3 [7 F, _name varchar2(10) not null,6 ~. q7 @9 q$ g7 `: b5 A0 y
age number(2)
4 J0 z  ^! w$ p. F% a. }( p);
6 B) x( C$ z: E' yinsert into student_ning ) N* L% d+ k4 r7 N! A, k& f9 b
values(1, 'zhangwei', 20);
7 d; }3 e9 U6 o0 A--名字可以重复
: H3 S0 e% ]5 {0 C% e+ b/ _* Zinsert into student_ning
1 N# i$ H2 t6 W; ?; Evalues(2, 'zhangwei', 19);9 a) V% O1 [* t3 r& J3 y! U
--提示name列不能为NULL. H" \& x& R; ^8 ]& s
insert into student_ning
. H; i' d- f' r4 Z* u' W8 }- Jvalues(3, null, 18);, L: e5 X) W& e# ^& |

' F% h8 _) O7 d1 \3、唯一约束:Unique,简称UK6 S: f) M8 ]2 D  T8 h
create table student_ning1(
. s, c2 c3 _5 r# T$ p+ V# bid number(4) primary key,
5 w( C1 w5 [* t( X8 k; D, E, tname varchar2(10) not null,
7 c- k  g0 q, l/ Z$ `2 Eemail varchar2(30) unique,* r9 i. ~" d  _& _
age number(2)
: o' n' ]7 B( I- G);. S. Z7 |8 Y+ n# b- y& l( ^
insert into student_ning1 # B+ I6 z2 ^( |6 f* o' X
values(1,'amy','amy@doctor.com', 19);0 m1 `8 R( Y3 W1 v. [6 C# v+ P
--ORA-00001: 唯一约束条件被违反4 [; R5 `4 o1 r& {4 j# \5 w
insert into student_ning1 4 J: C( O9 [/ \$ X. g
values(2,'rory','amy@doctor.com', 19);
4 X1 s  o  h# ^; ?
5 p& h$ H% E# H/ V$ ~+ ^--唯一约束建立在表级8 ?' H" S) S2 \' m/ z2 V% c
--主键约束建立在表级
0 ]+ o0 }; L4 z* N- l. ]* R, vcreate table student_ning2(; T0 Q; z4 J% Q) [  S
id number(4),
% K0 E4 n: U; Y4 L" s" Dname varchar2(10) not null,
- W2 d  y+ ^1 ~" V, uemail varchar2(30),0 E0 k4 \2 b; h% H5 S1 x  m
age number(2),
0 K( C: I, D9 w7 P( sconstraint student_ning2_id_pk' g8 O" M4 x3 r+ N
primary key (id),
5 _& T. D+ u6 f2 N" q. jconstraint student_ning2_email_uk5 Q% j1 @) w8 w( l
unique (email)
$ o- i. j  v& B) F( u);2 M% o1 C0 `2 W( [9 o* q

) i- Y4 }0 Z. k; \3 g4 r5 winsert into student_ning2: h/ [& r! r# F
values(1,'amy','amy@doctor.com',19);
3 m) @. J5 u$ T4 m/ t" y" Z* w# [; V2 o  D( ]8 s3 z( ?6 u( v
--unique约束只要求不能重复,可以为NULL
* s$ U. c' e5 D: finsert into student_ning2
7 F% Q, a' ^" ]2 r2 S; ~- Cvalues(2, 'rory', null, 20);6 w3 e9 d3 n1 S% I3 L7 r7 B" b; t
! H% n1 @' @, D# _: ^
--不管是insert还是update,email都不能重复。' I) @5 Q+ j$ ~% b
update student_ning2 set email = 'amy@doctor.com'
! o4 {8 K* ]4 R2 i6 j( v2 Twhere id = 2;$ }# r! _$ N7 f

5 Q7 W9 G5 Z4 o0 x$ w4、检查约束 check 简称 CK
. d& J) _% @2 {% s6 N0 R5 Icreate table student_ning3(
! `3 l5 q- c! `: R. M* vid number(4),
7 T  Z4 Q3 }; j/ ~name varchar2(10) not null,
4 N9 e; Y' g* ^0 @# `, i% memail varchar2(30),8 X$ [4 L% t; f! `. F( b
age number(2),9 c1 v: q+ i- c, t# ]- N% g
gender char(1), --'F':女生; 'M':男生
7 p# {) p% [' h6 P0 jconstraint student_ning3_id_pk4 c( v; d( K0 z8 \
primary key (id),+ T3 w8 S. L0 r4 Z* S
constraint student_ning3_email_uk
7 l+ i. p5 e% Qunique (email),
) d1 ^- w/ E7 [. @: iconstraint student_ning3_age_ck' ]# w( R+ P0 r% V5 S- U
check (age > 10),* N/ J5 O+ \: j2 w
constraint student_ning3_gender_ck
# X: U  Z; u5 k2 @! e/ p4 T- a/ `check (gender in ('F', 'M', 'f', 'm')), c: d3 C! r  n$ R  c& n) |* K( A
);6 N+ E$ l5 A; c, g; \7 }* r
insert into student_ning3
& K- |) [. u: Y% a& z# f+ d% g) jvalues(1,'amy',null,19,'F');; c/ D( A6 e, u
insert into student_ning3
! n- B: a- z& O. U+ @2 Xvalues(2,'rory',null,8,'M'); --违反check约束 age > 10
, T0 x- ^' I! B" Dinsert into student_ning3
7 W" d0 K" W; @values(3,'doctor',null,50,'A'); --违反check约束 gender in ('F','M'): ~. t% k3 Y' Y

6 T) `+ t4 v1 n" q$ t* |' @- MPK / NN / UK / CK / FK2 ?; l  w& Z2 h

6 Y( W$ S5 W' ]7 d4 q/ Y5.外键. Foreign key, 简称FK
4 [* H* I' ]" r+ H6 ~7 ~" r5 wcreate table major_ning (  L1 Q  J3 ]+ }
id number(2) primary key,
* i2 @0 N/ ^! c. W1 w5 @% Qname char(20)! V6 w; w# U1 y. M0 n
);
' N' u  a; a  Qinsert into major_ning values(1, 'computer');0 b9 ?+ G* q$ I9 M
insert into major_ning values(2, 'history');
$ l4 g7 |: V. l. e8 Uinsert into major_ning values(3, 'music');1 P1 {( f' b1 W% C8 D$ [% w
insert into major_ning values(4, 'sing');
" [  {& |1 B2 `7 u' ~5 Scommit;1 M$ {9 _+ w9 ~8 w
create table student_ning4(
1 y5 ]2 K9 p6 s3 `3 d& S" ]sid number(3),$ R+ O9 r! d6 M! m, f
name varchar2(20) not null,
' Q- |2 o& f) [/ }' ]# Eemail varchar2(30),
$ ^9 g% u+ T4 y0 G* B' n/ V9 J; igender char(1)," V6 Q* e" C, w* P& B" K( e
majorid number(2),0 i" ~6 C5 d: t- u7 z! O
constraint stu_n4_sid_pk primary key(sid),! Q5 U7 G8 b4 F  `
constraint stu_n4_email_uk unique (email),5 P7 x; B6 x9 O- R& _( W' g
constraint stu_n4_g_ck check (gender in ('F','M')),
* ?5 x& e+ p; Hconstraint stu_n4_mid_fk foreign key % E% @7 \* \3 l6 J% D( B! I- j- P6 M
  (majorid) references major_ning(id)
( }+ A8 M/ G  U);% _. P' R5 f1 I0 P! p2 n

: K4 v# K* R* u# Ainsert into student_ning4- `& a3 N; f0 q, q7 z
values(101,'amy',null,'F',1);4 R+ n6 z, e8 M% `, _$ j4 e
--新增数据,不存在9这个专业  Z4 \/ {* J1 s. g7 u+ V
insert into student_ning4- ]! k- T  G: ]& T+ j
values(102,'river',
/ o; t* Z! [5 A' L'river@sina.com','F', 9);# F# |3 j* s, G6 ~' J  l' J* A
--提示错误:1 ]. B0 j+ m- z4 N! l; I- `
ORA-02291: integrity constraint (NINGLJ.STU_N4_MID_FK)
% `* o& j% q9 E# Y violated - parent key not found
7 M1 T1 V  ]( m# C+ ]# ~insert into student_ning4  ^* m/ h6 x9 w& s9 h
values(102,'river',5 Q+ K# w! \5 f" Q' O
'river@sina.com','F', null);$ i2 C* w; t$ Z  k) u" J* F
( o9 y0 d8 s1 x. ^2 F
--有学生属于专业1(computer)
+ l$ ]( ^+ H1 b6 F9 y" zdelete from major_ning where id = 1;# U) D- K" A  [8 _: b
--ORA-02292: child record found
; L9 K) ?% l- X- S9 R7 V* h) \+ f* G$ L3 W9 b3 z
create table student_ning5(/ y# U4 t: u% ~3 L, N, v
sid number(3),
2 ?8 r1 t# v+ v# Y" |name varchar2(20) not null,( v/ |) V4 L- O  N* |
email varchar2(30),2 j9 Z0 J8 H! s) b0 V7 I" y0 I) i4 Q' }
gender char(1),
# A/ G8 s6 J$ H8 Z# a) c0 y7 fmajorid number(2),
5 Z7 v) ^7 p/ f; k+ |constraint stu_n5_sid_pk primary key(sid),
8 I7 J1 m4 }. L! k, }constraint stu_n5_email_uk unique (email),
, R8 s' q( |2 u! gconstraint stu_n5_g_ck check (gender in ('F','M')),, p0 C" l# r+ m6 p
constraint stu_n5_mid_fk foreign key ! S! f$ s. R" P7 q0 _8 U) t9 G
  (majorid) references major_ning(id), O0 x* D# g& T2 L( ]# Y8 X1 _8 [
  on delete set null);
# s# s4 i- c3 f9 Y; V  m& m2 B/ b7 x( m
insert into student_ning56 }4 V2 ~+ N+ t# \3 k& Y8 U: Z
values(101,'amy',null,'F',4); --amy是4专业的学生5 k( G9 w1 p/ q7 q8 O; L
--删除编码为4的专业
" S% t# g. ~; I' \+ S1 D' \3 `! B9 Pdelete from major_ning where id = 4;
, }& y8 Y; y0 |2 w--amy的专业被设置为NULL
. z/ E* M8 g# [+ W: B1 n4 Y& H% Z8 J" wselect * from student_ning5;
* ]' d5 H; G5 D7 L
- f" X! t6 `0 {" Q( b# J; Y1 s* T0 qcreate table student_ning6(5 H  _0 F: t* c2 e  W0 |
sid number(3),5 R# l% f. ?+ d) E
name varchar2(20) not null,' W1 T" s$ z8 d" m% v- q* r9 c
email varchar2(30),9 g7 K! p/ u2 y8 p6 A* p/ F
gender char(1),6 B8 [$ Y! [& N
majorid number(2),
2 a5 F/ ^0 f$ Q* _. Uconstraint stu_n6_sid_pk primary key(sid),
7 [) X8 d: b$ O7 p' g  tconstraint stu_n6_email_uk unique (email),
3 u+ b3 u6 `7 }% N+ k' H. Uconstraint stu_n6_g_ck check (gender in ('F','M')),$ T: {4 a. r( D7 F( W5 s
constraint stu_n6_mid_fk foreign key , ?* m5 o; w" b6 I" `' _
  (majorid) references major_ning(id); q& r0 g: c3 H6 _  n
  on delete cascade);* n  N) K! v* }' w+ X; O; [
3 z3 e. a/ U* z$ j" P4 `0 g
--复制表,不复制约束条件。
3 b- H$ i1 @: U0 s. \( ]create table 表名 as 查询语句
: ^& t: D- o4 t' Y- ^- M% S- J- Y3 C- D7 @) J( P! S: A# X
--建立约束条件的时机
0 v( ^6 g' q" X1 v--建表同时建立约束条件:0 m+ h$ F; [: d" k+ {
create table student(, `) _, a+ E0 X4 ], f+ w, y# f0 H
id number(3),& |- b, O* f* A  f
name char(20) not null,8 ]1 J' J, r( P7 i# X' q
majorid number(2),
% X6 R1 F/ [: Q! L5 ~/ Oconstraint stu_id_pk primary key(id),
2 Q! G" M9 z! e) U% gconstraint stu_mid_fk foreign key(majorid)
8 ~2 s, X6 \1 p; K, Q1 U1 p& l   references major(id)1 P# _7 O& }$ c9 `8 R
);. T, p5 O" s$ m& `. j
--在创建完表以后创建约束4 @! N) F, P2 x1 u
create table student(/ t+ ]$ s: u2 N9 E, U
id number(3),
4 d  E1 P2 _/ D6 J6 l- Lname char(20) not null,
* a9 \, U3 D& Qmajorid number(2));* A/ x% |- ?3 e3 {
alter table student0 B7 q' U0 C* m9 h# H& s: Y
  add constraint stu_id_pk primary key(id);
+ b7 b- R: D& u& `/ s# C5 B9 Kalter table student) e; c. b  I7 P" N8 }
  add constraint stu_mid_fk foreign key(majorid)/ v0 H' D$ f7 P7 D" [
  references major(id);- M  `/ E) C! F
: y8 f$ _( c' k* C& Q$ Y
-----------脚本文件begin---------( P3 t' Y3 p* x! V0 |
alter table student drop constraint stu_mid_fk;4 a8 k6 |5 M( E/ ^" I$ i
drop table student;0 q) l. E- {" D3 r8 E
drop table major;0 N& \" s0 i; [: O
create table major(....);
+ u& T$ d1 @2 Q  T  ~create table student(....);  R1 m+ O( `/ O$ ^' f) ]8 z
alter table student add constraint ....* b- p7 v; F9 Z( U5 U* l
-----------脚本文件end------------
5 o/ A' p! X; Q, O+ R
4 X, _! U/ m& U/ @user_tables  :用户所有的数据表
* u% t3 ?" Y% r# G% L: Iuser_constraints:用户所有的约束条件  D4 l9 s) H( I1 u
user_objects :用户所有的对象(表、视图、索引...)
" Y2 |* M3 ~% W1 ~' {1 e. Q- j$ u" S1 e5 N7 B( d
all_tables  :用户能访问的数据表,
) M4 r8 D! b; g) |             包括自己的和别的用户允许自己访问的* ~+ l: o5 g: E. C
all_constraints:用户能访问的约束条件9 E" N# Z, Y8 k  Z( u
all_objects :用户能访问的对象(表、视图、索引...)
5 u. [# ?" i8 w! p% p- w; n9 \! n; b8 O& {8 n: ~" J1 X4 P5 h3 |+ Q" ]
PK / FK( I: [) N; ^# U: W: ?& h7 W1 }
NOT NULL / UNIQUE. r0 \9 E6 H! |, t2 q+ O
CHECK; \" R! H4 B( D
其中:CHECK和NOT NULL可以在程序级别控制& B' T) R" E* b% b7 e

' o. f/ a3 T: |' `6 H: U; v二、数据库的其他对象& b+ q1 m4 @3 F6 j0 t" ]$ A; G
表 Table& X. B$ t: T9 i% [5 P; R0 A+ {
视图 View7 T0 j% b# |2 t1 k
索引 Index  z5 N6 n: U7 H& @7 W. m- R7 R
序列 Sequence; T8 H: _; [. g) o
过程 Procedure
. W3 j' @$ R) N) P- K. n函数 Function
8 q; ^( V1 d, |/ z* A包 Package, X, o, N0 h3 r2 Y2 T: l* X) A
触发器 Trigger
& M3 m) g! o4 W7 |/ U5 R同义词 Synonym
5 p9 S( t0 z: p- `6 ?- I% Q" C3 t..../ q' Z' I, W  Q) x7 r
. E  I0 K0 R2 [0 o2 h. _  x4 ?  B4 f& |
1.视图View
1 W8 `. O( d0 `7 Zcreate view v_emp_ning
0 F2 |( I3 Z3 o, ~& Uas: w0 b& |8 V: ^- Q: l7 N2 T7 F
select empno, ename, job from emp_ning$ z- {) x( Y8 N, N9 s
where deptno = 20;% c4 u, ?. S* i0 ?* Z; f7 S$ j
--使用和表相同
8 {+ N2 b: y2 Bdesc v_emp_ning
* S- \1 G" |8 T) y, E% G; Aselect * from v_emp_ning;; k+ D5 r5 g' w0 N5 c( X9 j
--视图的好处:简化查询;隐藏数据表的列
1 y0 h5 V8 T# T
& }: z2 ~& y3 S7 }create view v_emp_count
( |9 S( u+ _& a7 ?. _# Das
7 \; T( q; X& y$ ?1 H- \9 nselect deptno, count(*) emp_num
  R8 K8 p- o; O6 t5 Z5 ?( D9 n  ifrom emp_ning
& j: j& O! T! Kgroup by deptno;- A( W3 ^5 H# |- V& @8 H+ n/ I' E
--修改基表数据7 B1 i% L4 L# x! `% t+ U; P
update emp_ning set deptno = 10
# v6 ]% Z1 y; h- }. r& |6 i where deptno is null;
3 f& ~# c! _2 D* \2 Z--视图查询到的是修改后的数据。. t+ k, j/ }  s9 q  o
--视图不包含任何数据。是基表数据的投影。
. R- \/ N, R9 p$ S. x& i' uselect * from v_emp_count;$ G5 Z! _* n+ B
% F% Q9 w2 v# |! q  u1 Q) T
--创建或修改视图
4 h$ z4 ?' _1 M% E! d( `, Tcreate or replace view v_emp_count
2 `* S% x1 Q7 [2 r0 q- eas
3 T2 `$ o) P2 gselect deptno, count(*) emp_num,
. ^' T3 U6 G; h- l; f* psum(salary) sum_s,
1 P" P/ V4 ?& v* _avg(nvl(salary,0)) avg_s,
3 L" p  W1 |( t8 F! S: Rmax(salary) max_s,% M, g) I0 x1 ^8 y
min(salary) min_s4 c% G: l( K, E$ b
from emp_ning; y( ^, G7 o, f4 I5 i
group by deptno;  H$ W6 S' H- x8 K. W7 h
0 H  u; Q9 [6 t. j7 p# g7 p5 O
--查询视图的定义" {# a8 F' R6 p  ^4 k8 k
select text from user_views8 `% C, s$ x6 H3 L
where view_name = 'V_EMP_COUNT';9 g8 \/ Y& U& r. T7 Y+ Q8 u* r4 G. F
--如果视图对应的sql语句显示不全3 |; i; |- C! X+ S1 O
set long 1000% G$ y9 ]9 a. o
  m& x0 d8 m3 X6 G! T9 j. t
2.索引 Index :用来提高查询效率的机制。
& u) P5 E5 ?" A1 ^9 E  s全表扫描: Full Table Scan: 查询效率极低
: S' `1 A' I9 B/ P" x- d索引查询:比全表扫描快。
% Y' D" L- {: u# [; d+ K3 c, r
: B$ F' D* N( z& `7 x7 h9 v) X, z索引的结构:数据 + 地址
* T5 V. P/ n/ M4 X- e            张三 + Room2034 p. t* d! v6 @) f/ t1 p

& J$ J5 n3 t1 [7 E1 y" v! L对于数据变更频繁(DML操作频繁)的表,0 j1 q+ H, g1 y  D2 r: A
索引会影响性能。) a$ \- B5 W& S( y
+ E! G/ Z# e" Y" i# n
如果数据表有PK/Unique两种约束,索引自动创建+ I) ]4 Y% V! q' }+ u1 p+ M
除此以外,索引必须手动创建。
5 F& B2 t, ^1 n) ecreate table student_ning7(0 ^, Q0 W& S1 F5 U3 u3 `
id number(4),* s' T/ X9 |7 w% i" C2 f- x
name char(20),4 \5 f4 ], D% K3 j, D
email char(40),) ^) l5 m  @: E  V1 L: M9 l
constraint stu_n7_id_pk primary key(id),& D' n* b; W% {8 F! u+ j$ K. O1 l! o
constraint stu_n7_email_uk unique(email)
- c7 B7 u. {# E; _6 k0 J4 K- e);
& A6 e& ^7 ~) jselect constraint_name , h/ p  g5 o0 l# v6 |
from user_constraints; T) h& U2 k% e! t$ l) P
where table_name = 'STUDENT_NING7';
% u& B; U- t7 v5 t1 g2 d
$ k0 l' r  U+ x/ s--查询student_ning7表上的索引,2 h3 W# ~5 ^; }
和主键/唯一约束条件同名,数据库自动创建的索引。
. g; r& ]) q" W% s: I- l' vselect index_name from user_indexes
* x. y' r* F2 Z0 A2 V& awhere table_name = 'STUDENT_NING7';
9 \9 o( @# C0 [& z, I6 E& d7 S" }* t  B4 {; h* ~7 i; f1 Y" _
--凡是id或email上的查询,会使用索引# K$ ?, ?7 P4 r" t. [, n+ C2 G
select * from student_ning72 d8 h1 i# R  J/ H" |5 F
where id = 1001;8 d2 X/ d7 V& W. G) @

9 b7 X. i' ?+ b  d  l0 \--这种查询用不到索引:全表扫描
* o4 I! |1 [+ z1 w& Q' Vselect * from student_ning7
& m4 T6 @" n$ L; i# ywhere name = 'zhangsan';9 k4 \' R" c# Q, B! ~# w5 h
9 {  @! ~6 `% e0 s
--创建基于名字字段的索引,索引名自定义3 m  p5 F' y: K6 {- L2 C
create index 索引名 on 表名(列名);
$ |/ `3 l' [% bcreate index idx_stu7_name ! Q" I( I7 H  o+ R) `
on student_ning7(name);$ f' O) _7 d2 q! o' _
- ^1 M4 Y) |( j& L: N' q: }% c
索引:, S, t0 x) Q8 W6 i: t* [
1)了解索引的工作原理; F' n8 z( l; s' D
2)pk/uk自动创建索引
; z* T  e( y/ C% a3)根据查询情况决定手动创建哪些索引。
6 X6 ~$ q8 k; L4 g6 t
8 c+ t, }) N$ I( {% R+ K4 d8 h9 S* I; J, x1 |
3、序列 Sequence --Oracle独有的
! ]4 F9 ]% X7 d--产生从1开始的数字值,步进是1
: ]& d- r6 q6 p: o+ }, i- g1 rcreate sequence myseq_ning;
: o9 i" z( }; Iselect myseq_ning.nextval from dual;
% |$ x! |! U0 f  r# o7 o
" ]# Q- k& ]- e) N! e+ y) Q序列的特性:产生连续的不同的数字值
( {9 N$ h! k/ g- B4 J0 a3 [9 O9 G用来作为数据表的主键。8 I% c$ f9 N. l

7 w# p/ S1 c: N2 z--使用序列产生的值作为表的主键值/ w1 K7 [, [" t& g" W; }0 K
insert into student_ning7(id,name)
9 P  \8 p- l& rvalues(myseq_ning.nextval, 'amy');
6 N$ ]3 U$ c  L& a( j' h) B
' b  T% e8 c5 ]" wstudent_ning7和序列myseq_ning的关系:
% v) @2 P5 K6 b" h/ Q  o- z是数据库中的独立对象
$ A* E. H+ B' `7 C1 X--表可以用序列产生的值作为主键,也可以不用% }3 X* d. E& Q) H
--序列可以为一个或多个表产生主键,也可以不用1 {: K4 y! M) S- I4 @
insert into student_ning7(id,name)4 R3 X& {' A) g  A
values(100,'river');
/ K8 x: T* u8 c  M. S5 dinsert into dept_ning2 Y+ x5 g. X2 x2 S; J4 H: i5 ?
values(myseq_ning.nextval,'market','bj');$ J: l  A' {( |/ G9 _+ o! ?

$ T- s; r- b9 S--建议:一个序列为一个表产生主键; Q( s7 r/ t6 J! ~

3 K, i, l) F3 \2 n! w  o5 t0 L+ O希望主键值从1000开始,步进是2?  s' n* W3 h+ N7 d0 w  v& P) Q' U
create sequence myseq_ning1  B4 ?! a" m+ H5 D) [% h) N7 f/ I  b
start with 1000
* W% u9 z! G5 V: {% G# Kincrement by 2;
9 I3 c5 j/ b) `# q+ I7 @1 F# `  }6 Y! }: E+ l+ D/ \# T
insert into student_ning7: p% U% _4 Z! h
values(myseq_ning1.nextval, 'song', null);6 o- [- E' w2 E9 u; Q
* q6 B1 ~( w. l: }4 Y! x
--删除序列,对曾经产生过的数据没有任何影响。
  {) Y- _% H4 a* w. rdrop sequence myseq_ning1;
2 `$ ?) \0 g% h) G
9 c& M' C7 P2 }  uSQL> edit1 X6 [' q4 J& _
, k3 v2 {8 |/ k% _# q: ?

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


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

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

   

关闭

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

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