/ M2 S* N' o2 v/ y, Minsert into student_ning2* k6 F P. o5 w9 A7 v
values(1,'amy','amy@doctor.com',19); 8 O# q7 B; H& E; s9 d- n% o/ I, f% O
--unique约束只要求不能重复,可以为NULL" z8 \8 N2 a9 ~) h
insert into student_ning2 ' {2 J( U) d; z6 Qvalues(2, 'rory', null, 20);0 U0 Q6 p ]3 |! F
- S2 G% ~% q: h1 ~4 P7 s--不管是insert还是update,email都不能重复。8 a/ t! T8 I9 j, R' R
update student_ning2 set email = 'amy@doctor.com'$ z9 S# B6 _) g
where id = 2;$ b, i0 Z' ?! s& R+ W
' y0 }! N/ [' _( U/ _" C
4、检查约束 check 简称 CK 0 b' O: _% C& z C _% Gcreate table student_ning3(1 B+ Z& b7 S' y, J' l; [, {( O1 }
id number(4),8 U- v L* p1 b
name varchar2(10) not null,8 ? c, z) |# e1 {" j
email varchar2(30), 3 o; A! N) M3 D1 Q7 ~; Tage number(2), 3 ]/ P1 ~; P8 P) H7 i k( j+ z7 Qgender char(1), --'F':女生; 'M':男生3 Z$ w7 V4 ?# L5 V! q
constraint student_ning3_id_pk % b& d T8 t! `/ Y2 Q1 }5 G8 w# Eprimary key (id),- v! N2 Y( v% }. H
constraint student_ning3_email_uk( h1 Q+ H2 u5 i7 Z8 p: E
unique (email), # f! E- M1 u4 y! Y: Bconstraint student_ning3_age_ck " }4 P5 s- C3 d5 e% q% E: I8 hcheck (age > 10), ' x' M B. l- L5 s$ T2 xconstraint student_ning3_gender_ck, D& k5 k z4 I! _! F& Z" z
check (gender in ('F', 'M', 'f', 'm')) , l7 g( F) h7 B);# n) |) N8 v, |4 M/ \. x# P' C
insert into student_ning3 ( k2 X7 c; ?5 ]+ z4 p" }7 _8 K9 ^values(1,'amy',null,19,'F');3 N; |# U1 @1 u3 R* ]
insert into student_ning3 0 A, ?1 }1 Z2 w+ ~' ^. I- u/ b$ _values(2,'rory',null,8,'M'); --违反check约束 age > 10 % ~6 o. v3 p- F' @1 qinsert into student_ning3 - ?) P2 p. D6 R! [$ kvalues(3,'doctor',null,50,'A'); --违反check约束 gender in ('F','M')0 j( a& v4 G5 D( W6 }
) F* p$ p7 S. W4 X- M$ JPK / NN / UK / CK / FK 3 ?# m5 ~4 u' e& p* _7 P' @% }0 y- o1 N& f X5 U) t0 k4 Q
5.外键. Foreign key, 简称FK5 T: M( Y% Z q, K; t
create table major_ning ( % K! W; w1 c- s+ L2 ?id number(2) primary key, % @; j% N2 d0 @0 W. r9 s7 pname char(20)9 T9 u2 B( m; y1 z! h7 N! s% y( U
); # o) [6 x/ U8 S: Pinsert into major_ning values(1, 'computer'); $ y. b4 A5 k& O$ Z0 D' zinsert into major_ning values(2, 'history'); ' }) d8 T$ ` ?6 \3 Pinsert into major_ning values(3, 'music');, Z2 F- x6 G) w: _& ~
insert into major_ning values(4, 'sing'); 4 y4 p7 _3 A0 mcommit;& l! Q# |& k) G6 V. Z
create table student_ning4( % ~1 n/ w, v, {1 ]+ psid number(3),& t* c: y- p3 u9 m9 V
name varchar2(20) not null,5 @( x2 ^: \( O Q. U
email varchar2(30), . |4 g6 I. i9 n4 T, Ogender char(1), , N) O- U' W, w3 o& tmajorid number(2),) S* j3 z) L3 T+ |5 ^1 |
constraint stu_n4_sid_pk primary key(sid),; M4 j4 b2 s$ F" r: G5 h
constraint stu_n4_email_uk unique (email), 4 i/ U8 {# e: L. p( k# c' \! hconstraint stu_n4_g_ck check (gender in ('F','M')), 1 I7 Q% N6 e, x9 u0 lconstraint stu_n4_mid_fk foreign key 2 O4 ^ @( W1 @5 z; s (majorid) references major_ning(id) 6 x9 _5 X: \& @& [: x);. E* H& u( G% n* ?5 s
2 v2 n( o7 e- k* |" P- Dinsert into student_ning40 t9 t, H0 c) X! A- g
values(101,'amy',null,'F',1); ; ^5 P2 ~, e! v8 \* |/ x4 b--新增数据,不存在9这个专业 . i% J% I. E2 F! q' l ]insert into student_ning4- f1 G2 n1 E1 C& S& m
values(102,'river',! F# z+ r- R5 h" O) Z0 ?
'river@sina.com','F', 9);4 `/ V) e9 c0 M& c( b7 k
--提示错误: ! e7 D1 v+ ^5 \: T" o5 t7 n4 XORA-02291: integrity constraint (NINGLJ.STU_N4_MID_FK) $ |5 F' w: e4 ~9 ^+ S& k violated - parent key not found" }* a [8 }: f; I
insert into student_ning4 5 l1 P' \/ y7 W3 rvalues(102,'river',, u7 q2 U( ^& I: X6 p
'river@sina.com','F', null); 3 f% k" n( D+ S1 u ( J3 C( M. Z$ D5 C3 u& g" F `--有学生属于专业1(computer)1 U& m; U" B; |- T- c$ c
delete from major_ning where id = 1;/ X# ?# o% K$ t4 p. [6 |
--ORA-02292: child record found 5 [7 P1 \' u' |$ V9 F ( p, _- a ? Ucreate table student_ning5( 7 l) m. F; C# s c2 S3 esid number(3),4 D8 E e& Y- W) T
name varchar2(20) not null, * A* H3 y# h" aemail varchar2(30),* P7 _# |: G, c
gender char(1),/ K" j- X, K7 {0 v- X
majorid number(2), " _1 c1 N* ~6 U6 Q. A& ^9 zconstraint stu_n5_sid_pk primary key(sid),( Q# X2 v: e8 j- J
constraint stu_n5_email_uk unique (email), ; h/ ]; i& G$ v) @% z0 P ^. tconstraint stu_n5_g_ck check (gender in ('F','M')), 4 c) J) y1 \1 Aconstraint stu_n5_mid_fk foreign key 2 I. {4 {; |7 M (majorid) references major_ning(id) ' X8 b- m. @+ P, ^, z9 n Y; e on delete set null); ! g" w! W, L& O: r* y / K* Z- O k6 v# e2 f" _8 Vinsert into student_ning5 % V: X1 W! t" Q) Lvalues(101,'amy',null,'F',4); --amy是4专业的学生3 Q, _( k! t$ ?# x' G: o! M/ ~! \ F
--删除编码为4的专业0 G% U9 i! [9 C2 }
delete from major_ning where id = 4;) m1 K/ c' _& I. V* E9 _: O
--amy的专业被设置为NULL. ^' N, p1 ^1 z# e' E# k
select * from student_ning5; # A9 J& o+ l/ C* h. z9 l) u& \2 ^4 y 0 P$ @ X6 _ D" n' q( E: bcreate table student_ning6( ^- c9 l5 s/ F
sid number(3),( g" W& F( s" E" E# `
name varchar2(20) not null, 6 z$ ?; [" z+ E% p$ [7 ^! Lemail varchar2(30), y n( z" n4 Hgender char(1),* i. K7 b" ?! a6 }0 @: o0 @
majorid number(2), - E' C+ m7 @1 m3 n" d, W9 oconstraint stu_n6_sid_pk primary key(sid), ( |2 q% W" G3 f; L; Z( q5 Hconstraint stu_n6_email_uk unique (email),9 f# }/ K4 f2 l
constraint stu_n6_g_ck check (gender in ('F','M')), / z0 `5 X4 h- O. k# @# ]. |constraint stu_n6_mid_fk foreign key ) j% d# P9 C- J/ I6 Q' \( t
(majorid) references major_ning(id)7 l* h0 }" @" v% m9 ?
on delete cascade);) T: O+ O2 w; d$ D/ g _, o1 ~& q( O
- v5 W: K. ] i6 K$ b--复制表,不复制约束条件。 * |. j+ y8 r) o. s, z9 acreate table 表名 as 查询语句 1 Q5 f. l/ a j5 X" g1 f- l1 U3 z+ F# K9 s3 a
--建立约束条件的时机 # K6 `4 a' x. r8 R2 F( i- z/ u--建表同时建立约束条件: `2 J7 Y; o" y. r$ g) Gcreate table student(2 M: o0 k8 D9 B# b. g
id number(3), . a. ~9 r6 U& @2 D3 c3 K. ]name char(20) not null, 1 H, M* T0 ]' L: T! N( F: mmajorid number(2), Q; i0 g: s9 P l0 ~6 a9 A' s
constraint stu_id_pk primary key(id), _- s$ w3 v5 r* e% o1 a" E6 mconstraint stu_mid_fk foreign key(majorid). H! b7 b' j6 _. n r
references major(id) ' s) {* |3 [' m3 });9 y1 O) @% {) `1 O2 N
--在创建完表以后创建约束 L' p) B0 U& ^create table student(# {% B6 y1 c6 e' {
id number(3), & |2 K3 G5 j1 c5 u9 {name char(20) not null, ; t( C) S0 N; p2 [9 T) Q% F/ ?& umajorid number(2)); ' N! @' L0 j7 u! e) ?! ]# halter table student, o2 G( l; ?: h
add constraint stu_id_pk primary key(id); % r' g8 o) [; o7 |! L1 h9 h- o6 Kalter table student" ~- t* U! O% }' v" N$ f
add constraint stu_mid_fk foreign key(majorid) * X6 |0 P9 l/ D0 p$ \% N references major(id);" f1 n ~$ A) a- k, @
' M, N8 f! f1 u4 R, L; ^: m) A
-----------脚本文件begin--------- 2 k" F$ E+ r3 \! c3 k walter table student drop constraint stu_mid_fk;/ s2 J) r+ _) n7 [5 Q
drop table student; 2 q# l2 L2 U* H5 f. X; Hdrop table major;8 M+ L/ t% }' M+ @8 _
create table major(....);: q7 i0 |8 g2 i6 n$ U$ U3 {
create table student(....); 5 Y8 h: U3 @. X* \9 P. k9 z5 R: [2 yalter table student add constraint .... ( {5 E+ N- W0 r7 \-----------脚本文件end------------) I& }* t# I* ?! I; W
# ~ B5 o" T! K" T. e! c' J1 Y
user_tables :用户所有的数据表 7 a% v; c: C6 }9 ]6 R S: Guser_constraints:用户所有的约束条件0 A5 o& @- ?9 K2 q
user_objects :用户所有的对象(表、视图、索引...). a, e% N4 H& ?& n4 S% w* ?' R
; K% ~/ }: g8 v7 B
all_tables :用户能访问的数据表, 2 Z) N0 u* l/ M0 u1 Z& Q" d 包括自己的和别的用户允许自己访问的 & Z2 O: D. B1 U1 ?% H9 J& X. xall_constraints:用户能访问的约束条件 . W& a: S3 \: i- Q( \all_objects :用户能访问的对象(表、视图、索引...) 6 n( y+ k+ o/ A/ K ~2 `3 I/ r0 K$ E) k% L( v9 j
PK / FK. `0 q6 f* F( w9 S4 U; t$ _$ W# ]
NOT NULL / UNIQUE+ t& P( ]3 M( ?
CHECK3 h) z: r/ l" S
其中:CHECK和NOT NULL可以在程序级别控制 # Z2 ^$ X* A; K/ ~" U6 k. w- A- S3 j9 C! d! W+ T, E* o- ]1 t
二、数据库的其他对象 ! ^; n& h1 u& u; ^2 P6 i表 Table$ ^0 [7 S0 I9 i0 v/ x3 @
视图 View , g- Q5 W6 b4 ?' D7 m, N& C9 e [索引 Index & s1 k# x5 U" Q* I$ g5 u序列 Sequence" c2 ?0 a* Z- @ Y7 N
过程 Procedure9 W4 S4 R% _7 B7 d5 I5 E
函数 Function# \1 ~7 w, q& a" i+ O
包 Package. R" B6 Z- {. | s2 f% h) q( S
触发器 Trigger3 R7 s! e$ q4 z1 W$ \
同义词 Synonym, X; U, y* d+ t0 c- R$ V6 b6 }3 h
.... 3 r, W3 t2 {8 Z, \9 | " R1 j H% a9 I1.视图View # x4 K, F- s& f+ I2 `create view v_emp_ning 9 n" G1 e: }; b, _& ~& O" f/ xas C. k$ o2 V: _( b# z' M8 mselect empno, ename, job from emp_ning* z$ E6 s$ a, o! n
where deptno = 20;9 A/ o# i1 [- f0 \
--使用和表相同4 f1 E# o7 } F. M. ?/ k1 D
desc v_emp_ning7 j. m5 b3 w4 y& X, E9 E: _
select * from v_emp_ning; , \& ?4 s. w9 A! k$ e) _--视图的好处:简化查询;隐藏数据表的列3 }9 N* q$ G6 P+ N7 p! s
, G/ L: t: y; u ]) Q
create view v_emp_count " s) c7 s/ s4 x' M) P( H/ qas" v" M0 f& J D. W4 K
select deptno, count(*) emp_num : m5 b3 g5 L, d( a5 t, U4 H: hfrom emp_ning , |) U T7 ]. \group by deptno;+ w& ^) N: ?1 I3 ?9 V/ k6 |
--修改基表数据 ! X8 S0 p& u- { update emp_ning set deptno = 10+ g, q) h0 B9 t+ m
where deptno is null; ( N) G$ C+ e" I2 U. ~! G--视图查询到的是修改后的数据。 & t5 t' {; t. t+ C+ d$ S--视图不包含任何数据。是基表数据的投影。2 r& W3 s$ M# ]+ ]5 k% d
select * from v_emp_count; - O/ n5 a, V- C% r . \: p, q* D! t" C" B9 t* f--创建或修改视图% V& x) b8 |) N" @3 B+ M
create or replace view v_emp_count s( [* t7 X+ a% P& @2 |
as1 c n* S6 U0 Z, Z
select deptno, count(*) emp_num, + T0 | P" W3 G. J4 ^/ X* qsum(salary) sum_s,* x1 s9 B) S; t6 F* u! X7 h9 a
avg(nvl(salary,0)) avg_s, 7 v/ X; @8 c7 Y& y+ [( ?max(salary) max_s, ( w) @/ S+ L% ^5 u1 I3 umin(salary) min_s/ n- m! Y. q( w; |- s
from emp_ning 2 z. c9 b+ Y" \" cgroup by deptno; U: p, n: W9 A% S* t+ m
$ @& N5 ^6 \6 |2 ~
--查询视图的定义6 ] @: w* i J$ ?6 r
select text from user_views( V" b% D/ p( H0 v
where view_name = 'V_EMP_COUNT'; [+ E( h# e& g- B% O* a--如果视图对应的sql语句显示不全 ! {1 p% h; \5 G( w3 g7 ]1 { i6 I9 X1 wset long 1000 ; k/ B8 @0 _) u0 Z" j3 N3 B8 X0 ^
2.索引 Index :用来提高查询效率的机制。. k' t2 M7 D" w, h# W
全表扫描: Full Table Scan: 查询效率极低 + B. L) s# U) V* q索引查询:比全表扫描快。 / d3 @# B# E4 T2 K 7 C+ M$ |2 m5 t; ~8 k8 C索引的结构:数据 + 地址9 g$ l' U) ?& s* H8 k/ h" W: `' d$ S
张三 + Room203/ i2 I$ ~' H* w, W; U' B' v
. Q' D4 f9 T h9 x+ N* b8 E* @
对于数据变更频繁(DML操作频繁)的表, 7 b1 U% o7 ~! q% Y4 Q索引会影响性能。3 {8 V' k" p8 V) G
! A, f! b& ~4 d6 V' W4 Z如果数据表有PK/Unique两种约束,索引自动创建 , q/ g9 o+ g4 D, s除此以外,索引必须手动创建。# s$ D0 ]& n3 F9 s" t
create table student_ning7( ) _* P$ t- f! G8 R0 ]" bid number(4),1 m! W) I9 E* V
name char(20), & t- w3 v6 O, w& y6 J" C9 Y2 aemail char(40),6 F7 g5 ~! L5 H6 t1 D( i
constraint stu_n7_id_pk primary key(id), " q4 `0 K7 j8 l. Mconstraint stu_n7_email_uk unique(email)$ C7 D/ v! C; ]2 U0 Y: L7 Q
);& d# ]$ I3 i; U/ P2 H) z+ W
select constraint_name # c& O0 T$ k$ ~$ y f E+ b: D! ~from user_constraints 0 Y) ^+ C* F& |8 ]where table_name = 'STUDENT_NING7';3 N; v2 |: _- m4 ^+ P" g: S, z. i
" L5 n1 B3 I7 n1 g: i* |/ B
--查询student_ning7表上的索引, ! n/ A9 v4 v" f6 J和主键/唯一约束条件同名,数据库自动创建的索引。 ; U6 y+ C& s4 Jselect index_name from user_indexes; U7 \( Q5 Z/ T; C
where table_name = 'STUDENT_NING7'; : s7 Q0 s0 }7 B6 W1 E 4 h9 K* U6 K+ @, Y. y1 U--凡是id或email上的查询,会使用索引% L+ ^" h, w( l0 L, u: l
select * from student_ning7 2 s; T S* B: F. Pwhere id = 1001; , ^* x' A( j5 S; m/ Q; G8 q! M3 F2 L3 a2 W( s' A _2 I+ G- K
--这种查询用不到索引:全表扫描 $ ^8 |7 i/ r. t z- d- L- hselect * from student_ning73 |! F$ w& f5 |0 Y( ?6 V6 q6 Z; P# W
where name = 'zhangsan'; 3 K3 a' L" u( y5 J {0 R$ [6 k0 f9 R3 r2 F6 l- D: E# c+ i) e
--创建基于名字字段的索引,索引名自定义 ) Z2 `! I- r$ o& xcreate index 索引名 on 表名(列名); " h m7 {' H; @1 mcreate index idx_stu7_name % J: A" c7 ], ^8 n8 z m
on student_ning7(name);3 a- x5 M+ t3 H& i8 D; s' ]( ?! ` f
+ g2 h& m/ k! N7 D4 g
索引:0 N6 k& p" y ]+ c( ]: n
1)了解索引的工作原理0 o; ~6 u4 t( [
2)pk/uk自动创建索引- A1 [/ ? C" `8 i9 l5 ^# h* L) O
3)根据查询情况决定手动创建哪些索引。 " y- G7 V0 ~$ Z' S/ y% a9 T" B( |& H4 `. B# n
* r4 R( n$ z' r$ V, W6 O
3、序列 Sequence --Oracle独有的 9 C4 Y& h# ^0 [ A--产生从1开始的数字值,步进是1 ; e# C* p% i4 }. Zcreate sequence myseq_ning; 2 I$ K/ ~! `0 x" U" C6 Cselect myseq_ning.nextval from dual;/ M/ G- V& i) }6 @6 H4 }
8 }% x1 l4 N: E. t+ }4 p
序列的特性:产生连续的不同的数字值 $ g; Q3 W* b5 A6 m; i: l9 r4 Z用来作为数据表的主键。8 g1 B K2 h2 T; w2 J3 r
$ J! p) l% y. E" @+ ^: F
--使用序列产生的值作为表的主键值/ w: R* L- \- q. t- [' }
insert into student_ning7(id,name) 8 N. m0 i& K: V0 |3 S0 U: d6 avalues(myseq_ning.nextval, 'amy');& N! {" q* h1 d. ]
3 ?0 f9 `0 J( w9 q5 r) w& ~+ t3 zstudent_ning7和序列myseq_ning的关系:( _2 l* c, ` T" I
是数据库中的独立对象 ; L9 a/ b" s/ G A; v6 ], q/ B0 F: J--表可以用序列产生的值作为主键,也可以不用 : O! t" p/ ?! n& {2 m% ~# Q6 \- B--序列可以为一个或多个表产生主键,也可以不用 ! D2 g' F( f( j' ~& iinsert into student_ning7(id,name) , X% b% ]: B; zvalues(100,'river'); & w: u1 w' y$ F4 ~# _5 J3 p' F' kinsert into dept_ning 6 t* q7 C" T2 l" w( vvalues(myseq_ning.nextval,'market','bj'); 4 g; F7 g% B4 O9 f+ s ( }9 @& L! ^* f8 a0 l--建议:一个序列为一个表产生主键 0 V. a. ]5 a$ R1 D& |" H4 K9 x; d; f2 V2 X: m' f. n' F
希望主键值从1000开始,步进是2?& D- K% I) s, O4 x; F7 k- z
create sequence myseq_ning16 `$ @2 m0 I( }
start with 1000 + ?" m% h; ^/ I2 g! e& J6 e6 [% Tincrement by 2; ; P4 |# R) s8 f! ]- g$ R- b k2 [- J$ Y" F M
insert into student_ning7 # }; |9 h% K7 O+ g) q, _, X, Pvalues(myseq_ning1.nextval, 'song', null);' t6 p- o7 _8 p, L
. b# x; R$ j3 y9 y% d& u* |* X( ~
--删除序列,对曾经产生过的数据没有任何影响。0 J' N' }" d; g2 G
drop sequence myseq_ning1;# I: O$ L i. I- E& k/ B! c
% s3 w: x; P2 ]; N6 T+ MSQL> edit 0 {2 ^; C0 H6 C% e # X* M x( @9 o6 \2 T