|
该用户从未签到
|
复习:SQL语句
2 a9 M; o/ D+ i* }$ K, _. V4 [select- O0 a, T( e- k% _
DML: insert / update / delete
+ s3 N! V% e+ ^8 [# t: Z1 ^" s1 J& h9 N; O) u: B1 z; k
CRUD
. [. y0 T! p$ Z9 J9 k' LC:Create) B5 l8 A7 {$ V: D; c
R: Retrive
& C* C8 i" n1 |$ `2 V5 zU: Update
( d; E/ h- ^' g0 j" xD: Delete. b% v5 U: `. j* S Q( |, n
# r: s, y R* t2 L( j( Y9 r n* l
DDL: create / drop / truncate / alter
1 Z! ^( }3 O. j9 t4 R Y6 {5 K: o7 S- d) V* |
TCL: commit / rollback / savepoint
7 ]% v* B! N5 `0 z. w( m
2 H) s4 @- ~! g: {! ]* P8 nDCL: grant / revoke' ~1 x" z) I+ t5 v
: E5 H3 J. ~7 N
grant: 赋予权限
& U+ l6 p# y! }) Zrevoke: 剥夺权限
( a$ c% Q$ m; j* D数据库中的用户: openlab hr scott
) g9 D' N; f+ E' F emp
2 T ~+ V6 p9 ~# e/ c* X% [
, _+ S$ e9 {2 P: E假设现在的用户是openlab1 T( a$ d, L- y3 a( p
SQL>grant select on emp to scott;
' v& ?( N5 J, G( jSQL>revoke select on emp from scott; O! [& s, H$ R i$ X
9 I, u% b2 Q4 Z. p& N' Y6 t0 I
scott的会话:
Y t8 Z8 o6 R2 YSQL>select * from openlab.emp;
# h5 Q( [/ j1 E4 x8 wSQL>select * from emp;1 P$ f8 R N1 C; }% g P
$ ~1 W2 R1 s* g( ?& t" w: u. l
6 d" T U9 O3 e+ a
Oracle数据库的用户:, }6 Z, B4 ~- L7 g1 J- R* |
sys0 T6 X/ Q( u+ x/ i
system5 b1 o6 h+ t; Q" | `, ~% ]9 U8 {* p
+ l, Q+ ^- c- b# Q5 @+ K' F$ q. o
scott/tiger J- o3 f! J! y7 I K1 Z' U
openlab/open1236 B( u! Q, N; p9 w! m
ninglj/******! j* M' s9 y3 ]% T4 P. l6 e
exam/exam123* f* V0 _/ q: g9 x7 V: g
: t' ]; Y1 x4 k# V! |/ P7 Y# VC:>sqlplus scott/tiger@192.168.0.26:1521/tarena
' j" j) {, Z% W: ~' O- J" I$ NSQL>
9 ]! C4 h4 ]: r! N v" u" t1 I
) Q, s4 E0 k- v% Q- o( J: q+ b
3 ~/ u7 B2 S6 ~4 D5 n今天的内容:2 y4 g* T6 h, J4 x- m9 W+ J4 ^* m
1、约束条件* S& a, Y+ j* z. U. f6 F& I+ C9 Z
2、数据库的其他对象
& W( _2 l* ]. R; q; s* z/ Q
" K8 V4 X6 O0 W5 O3 R一、约束条件 Constraint0 l- L# Q8 ?; K/ g. o
1。主键约束:Primary key, 简称PK# ?) W# M* @1 Y
--建表时增加主键约束条件
. N" p- p4 E# N+ v! T: i9 lcreate table dept_ning1(
6 F9 [$ d' Q: X, edeptno number(2) primary key, --列级约束条件. a% i6 T6 f' }9 m! B& ?5 C- |
dname varchar2(20),
) L( f% g- d7 ?, vlocation varchar2(40)5 [7 c5 x3 M" B0 ?8 l$ m! x& E( w
);4 p* t' j7 t" d
insert into dept_ning1
8 A$ i% Z2 B& ~, U5 t- D/ zvalues(10,'developer','beijing');
4 k9 Y& Y- }4 O3 a* @( D. [insert into dept_ning1 . F. l$ L9 |) H: B o8 l( E
values(10,'market','shenzhen');, Q4 P3 I; L V! h; O6 j+ W7 z
--如果插入重复编码,会提示:( D" C: T1 j7 Z' B1 f# ~
ORA-00001: unique constraint (NINGLJ.SYS_C00634053) violated% ^. V5 \1 y4 r$ E, Q
其中,SYS_C00634053是数据库自定义的主键名5 W( \' u7 V8 R0 B5 T, s4 n
--约束条件如果没有显式命名,, P$ O/ j" P" t
数据库给约束条件命名:SYS_C*****3 z4 z% f& ?3 H1 X; _2 k
1 I2 e+ g. T- ]" o: o1 x
--在建表时自定义约束条件名9 C4 q6 L# f3 W5 X% v
--建议命名规则:表名_列名_约束条件的类型
4 o1 b. v- g. {! q; i% h: vcreate table dept_ning2($ G3 v* b7 y6 L
deptno number(2),
7 X; K& ~+ J4 _& J6 Hdname varchar2(20),& y: ?1 D8 m$ n8 |) d
location varchar2(40), R$ m' ]+ l3 A
constraint dept_ning2_deptno_pk
) R/ ]8 s* ]6 r( m: w& V4 |primary key (deptno) --表级约束条件& _+ J; |- w! u! ~; J3 l
);- X+ E, o) l9 ? Y& [! O
--当插入重复编码时,会提示具体的约束条件名字错误。; O/ E+ ^1 U9 c5 w7 G, b
--方便定位出错的原因
5 W6 x3 L0 ?# q) d
1 E3 g' w5 T: M$ a3 ~: ^) \9 o主键约束:primary key = 不能重复 + 不能为空
. }0 X1 \2 _; ]) M& K5 c
' s1 P6 u+ ^8 n2、非空约束: not null,简称NN; G% z A6 a" v! C/ W4 o; X+ Y
学生姓名必须提供,但是可以重复) w# t$ @" I( } E7 D3 r3 @
--只能定义在列级* p" E; H$ {0 h( n
create table student_ning(# I. C8 m2 r u' [' r$ b6 p& t. V: A
id number(4) primary key,- u% u: b2 K# I
name varchar2(10) not null, V0 Z; ?! P, ]+ ^5 `% K
age number(2)9 c5 [2 [/ c3 h) l/ }
);# f8 R1 a+ M3 I" U$ b9 c
insert into student_ning 6 P7 P% w u Y! c6 O. W
values(1, 'zhangwei', 20);
% k1 H. T( H7 H; z2 P0 `--名字可以重复
% Y, g$ t$ b' Einsert into student_ning
8 }! B% d# V1 B; U( Zvalues(2, 'zhangwei', 19);
5 {; M/ D0 f1 c/ T; Z. @--提示name列不能为NULL% ^* V& l3 H2 \- U9 I5 k& W x9 ^
insert into student_ning
: q8 x2 `* m% K' jvalues(3, null, 18);
9 p2 _( c* m* R( H1 s, R; N
4 H$ A3 W0 i5 n. y- I3 ?! b! \3、唯一约束:Unique,简称UK
; G7 `- V4 I9 Ocreate table student_ning1(
`: U6 Y9 V( V, q( L, a' kid number(4) primary key,
* ~5 m. N3 z0 g% G3 gname varchar2(10) not null,
; \$ h4 l: G! ?8 Semail varchar2(30) unique,: n! y. T' ?( ?# ]( v+ O* j
age number(2)
0 z9 b+ v# u, p/ P) j! [ f);5 a! W% [3 }1 e+ a1 }8 a( D
insert into student_ning1 & J, C6 e% k( Z
values(1,'amy','amy@doctor.com', 19);- }! ^" l2 ^, f( c. ^& L+ l4 b
--ORA-00001: 唯一约束条件被违反
" H! X7 Y; u0 z# t5 g+ v9 w4 cinsert into student_ning1 5 b8 p5 i0 B4 x* Z0 [' w; H
values(2,'rory','amy@doctor.com', 19);4 J$ n, L0 \9 @5 ~ h$ V
* C6 \3 o4 M! C
--唯一约束建立在表级4 X; X6 W+ m+ _1 G8 w7 e0 M1 _
--主键约束建立在表级
: T: T4 H0 u! y! x8 Acreate table student_ning2(0 j! Z; @" i2 V s$ B3 n' L
id number(4),
: D9 s) i) X) @% o% Y; Y% |; ?$ @8 |name varchar2(10) not null,
, z7 B( E; {1 N, n( [email varchar2(30),3 ?3 G/ W% ]) @' ~/ L
age number(2),
+ }5 K8 @& l( H8 x+ b$ ]! Pconstraint student_ning2_id_pk
4 Z4 }5 o u/ f- X2 ^0 }primary key (id),1 ~$ c% \1 L) _- z" n' N& n8 Z
constraint student_ning2_email_uk5 l) l G2 Q& s
unique (email)4 q3 f: H" M( @+ Y
);/ ?( W8 S& E. p
- G/ Q# N! I2 x [ t
insert into student_ning2
J, n3 @5 K# [6 Q# Cvalues(1,'amy','amy@doctor.com',19);* g R4 C3 z; g, f- F( r* Z
$ o& n7 ^3 q, ?, Y' F- |--unique约束只要求不能重复,可以为NULL: q7 ~* H3 B: G9 ]' A2 ]1 ~
insert into student_ning20 P& n5 b* b; S2 z1 [
values(2, 'rory', null, 20);
/ a* u! \# z, e. K& ^( a! @. G- E& }4 m+ o" V5 T' D
--不管是insert还是update,email都不能重复。+ C* h7 Q2 u* A9 C) m+ d
update student_ning2 set email = 'amy@doctor.com'
* I) P% G9 T. Awhere id = 2;9 @; k5 M: m' ?! D
3 N6 |$ F2 @) ?" [! u0 A+ b4、检查约束 check 简称 CK
' H5 M2 ?0 S$ a/ U( \9 {6 ?5 e9 W' Fcreate table student_ning3(( {* `* w! v/ }4 ]4 J# o
id number(4),
1 D1 l. I4 _" e ]name varchar2(10) not null,9 T U* `0 V1 Y1 f4 l
email varchar2(30),
6 B4 ?) \% t; x" F0 B6 z4 Bage number(2),
: P" ^7 g0 q: }$ q7 u! Z9 cgender char(1), --'F':女生; 'M':男生
9 v: ^ G3 |% z6 ?constraint student_ning3_id_pk
: G8 u6 Y# D6 @, d1 Hprimary key (id),
. m5 n5 n- r* g* |0 wconstraint student_ning3_email_uk" T3 y, s! @1 G) y! W
unique (email),( s3 U. L3 C8 Y* c
constraint student_ning3_age_ck+ _1 n) Z, T' Q- ]' { }& U1 r
check (age > 10),
1 ~+ [1 k6 q/ }; i8 K& Dconstraint student_ning3_gender_ck- w# B9 C# F: A) k
check (gender in ('F', 'M', 'f', 'm'))
" Y( v7 ~1 ?/ n4 \1 \1 Y);
1 `# ~" o- B( f: Vinsert into student_ning32 g, }$ y3 Y) l# w; K! @
values(1,'amy',null,19,'F');
8 E. G5 C5 t4 k0 r% o& n7 z/ s5 n' f* f1 Minsert into student_ning3) p+ b, `, s& D8 W
values(2,'rory',null,8,'M'); --违反check约束 age > 10
% b) a, B! X G. i* b( `insert into student_ning3 5 j* A- o3 ^" ^
values(3,'doctor',null,50,'A'); --违反check约束 gender in ('F','M')
: n7 y2 }8 S7 o7 G2 e l8 J3 J$ d c, z$ v8 z4 i
PK / NN / UK / CK / FK# W$ L8 S. x% f, R8 P: G8 h, a
" t7 d& Y- \) Q* _3 k
5.外键. Foreign key, 简称FK0 x! ^% n; M" `
create table major_ning (
9 o$ t/ S6 y( ]id number(2) primary key,
0 o6 @+ l+ J3 C2 p9 I1 [name char(20), z$ |1 ~7 |' \1 d5 g& f
);
, D0 b/ q6 j1 |5 Qinsert into major_ning values(1, 'computer');! T1 Q" c: H1 o T2 Y
insert into major_ning values(2, 'history');
: Z0 |; v3 m: l$ a: H" Y' qinsert into major_ning values(3, 'music');+ S2 g2 F" c/ f0 V. A8 P, T6 p
insert into major_ning values(4, 'sing');
2 ?! u* U. U$ a3 ycommit;
' w" k+ D9 L5 Ycreate table student_ning4(
) L6 g ?9 G a, L% t2 }# U' Jsid number(3),/ C8 m1 r+ r D l5 n/ Y
name varchar2(20) not null,
$ n8 R# r% _2 U$ Y! vemail varchar2(30),
( E J' S" e% E0 _6 Ogender char(1),
" Q" i: k1 `' s" smajorid number(2),
" `# h# p) u$ g. Q$ jconstraint stu_n4_sid_pk primary key(sid),6 x0 a0 q4 o& V! z6 M7 R' F
constraint stu_n4_email_uk unique (email),) K8 d$ j/ q/ p1 P1 L- s
constraint stu_n4_g_ck check (gender in ('F','M')),
* v$ l' y# `+ H. f6 kconstraint stu_n4_mid_fk foreign key
" Q( A+ ?/ [4 t* j. C, ~9 C! r6 V& J (majorid) references major_ning(id)
* ]( W0 M0 J* p);9 q3 Q# c! j- U
! E+ v8 G" g8 H! `; m7 yinsert into student_ning4& d+ L N. v" J
values(101,'amy',null,'F',1);2 m6 x8 D" @7 O
--新增数据,不存在9这个专业
6 Q: U- r8 a: i' T* E! u. `insert into student_ning4# \7 ~$ d( }6 _4 g4 Y5 ~( m# `
values(102,'river',/ T% a5 [5 C' X1 D+ {; H: a
'river@sina.com','F', 9);
+ ?: b5 m8 `) m) ?+ i--提示错误:5 W; J9 k [' G* B# b
ORA-02291: integrity constraint (NINGLJ.STU_N4_MID_FK)
! q" Y) a8 D4 | violated - parent key not found
- U9 y- b) F: ]! `* Y$ rinsert into student_ning4: }' k. ]) J2 d3 o1 W) p; e
values(102,'river',
2 K- O1 Z9 q) x' S2 I E( S$ D'river@sina.com','F', null);9 h: U8 }6 Z' N' R
6 t4 E" {) Q3 d--有学生属于专业1(computer)
7 `8 o0 ]' b; g1 Xdelete from major_ning where id = 1;
% p* a2 {* s. f5 S* t- U! L- S--ORA-02292: child record found, }; v" J( U# T8 p% h, W
/ e- s C2 J1 t2 b% Pcreate table student_ning5(' z+ E. g7 ^6 t% D, @
sid number(3),
7 h$ C7 Z1 C+ ]9 @name varchar2(20) not null,' P9 [! c' v$ C# z" Y1 a
email varchar2(30),% K# L/ C F7 x$ w
gender char(1),8 Z9 a0 f* ^; g0 g1 W$ J5 `1 N
majorid number(2),
5 [, M$ X) s" U) ] ^constraint stu_n5_sid_pk primary key(sid),
- _; `1 v X x0 m3 ] T. ?7 kconstraint stu_n5_email_uk unique (email),
; O3 f, f1 v# w( Yconstraint stu_n5_g_ck check (gender in ('F','M')),' z" M& T3 v6 G1 @5 l& b0 V
constraint stu_n5_mid_fk foreign key 6 \# g, f, R, W! z% T* `0 w" o
(majorid) references major_ning(id)
: W+ t0 B' B4 U9 N! U& Q on delete set null);4 @. O5 |4 U/ e5 `
* b+ N; h& ^1 P. J: [1 n$ F5 b1 p0 uinsert into student_ning56 a! }. Q$ ^3 q2 p# E
values(101,'amy',null,'F',4); --amy是4专业的学生
# o# a9 Q2 Z$ L; c--删除编码为4的专业, x+ Z9 q) j' t
delete from major_ning where id = 4;$ A% ^" w/ a" m) @5 ]3 T
--amy的专业被设置为NULL; O; l$ _4 v W( k' ]
select * from student_ning5; 9 R# R% [: Z5 i; I0 m: l- W9 a
+ w* N# u7 s9 t) @) I4 c# K- B1 ucreate table student_ning6(
$ L- d+ u) e, x8 j7 _7 Z8 V; ^. Ssid number(3), c" W; w& U6 S: p& Y2 }* {
name varchar2(20) not null,
# P1 c4 G% y( a; T! X8 xemail varchar2(30),
8 j# M7 E: @( V/ H/ F, Fgender char(1),! w$ R$ T; V& W7 C' P
majorid number(2),5 p7 X# F e4 q
constraint stu_n6_sid_pk primary key(sid),
# m' i" A3 u) b4 N1 j, Vconstraint stu_n6_email_uk unique (email),
# q& b% C1 W/ S% Oconstraint stu_n6_g_ck check (gender in ('F','M')),
, @5 `& M ^% N+ Wconstraint stu_n6_mid_fk foreign key ( i/ E' P/ \; s
(majorid) references major_ning(id)
7 H. t# K9 F P9 j' B0 { on delete cascade);
, ~$ K9 c2 I" J" ]* m$ P" o8 J/ @6 {5 g! N' t8 u
--复制表,不复制约束条件。* K" f( `8 u- J2 ~' J0 D1 g+ ]
create table 表名 as 查询语句9 r' r. y7 w: b5 n# M
5 l5 y/ v" |4 }7 G: w2 ]6 p
--建立约束条件的时机! s- Z/ L7 x7 ^6 b
--建表同时建立约束条件:
9 S, x1 y) ]! Pcreate table student(0 b& z$ e- z' s4 m+ T; V
id number(3),% K1 t. W- P; T0 f* M" }; }$ p
name char(20) not null,4 T3 w. `' t, o g
majorid number(2),7 S ^ d, I2 x8 V
constraint stu_id_pk primary key(id),' Q9 A2 s' S6 _+ [- ?
constraint stu_mid_fk foreign key(majorid)
, s) q0 V9 g% z- H7 f- X; i references major(id)! l% o6 P9 A/ ?
);6 l _+ P# W5 z
--在创建完表以后创建约束
/ O! v/ f' x4 @1 w8 Zcreate table student(; o8 u3 O/ j7 ?& y" ]- a- c
id number(3),
8 }! X6 e( E+ A0 fname char(20) not null,. l4 F J* d: }; S- H9 v$ W
majorid number(2));
0 m* V3 M0 {: Z! L& g) o% p0 Z1 b3 z, lalter table student: u6 p5 d2 t# I1 \& Q
add constraint stu_id_pk primary key(id);8 d9 {; }! b+ c# t/ q
alter table student
& A$ v3 z! q. U- C5 w add constraint stu_mid_fk foreign key(majorid)/ m0 _ j7 j$ C- E. }! n: H- m
references major(id);
& m/ @1 k2 Q! N$ i3 Q' {: V$ d
! W# A3 Y2 ~9 S-----------脚本文件begin---------
% Q L9 x& O% x; H1 P7 L- Jalter table student drop constraint stu_mid_fk;
+ _/ g3 r( R# Ndrop table student;
$ C! X0 r5 i0 X$ \7 gdrop table major;5 t( q6 Q% T* \- _
create table major(....);
1 X; P* _6 \$ @create table student(....); G3 L. W. \) g5 q% }1 _, W4 d
alter table student add constraint ....
4 v) i s4 t# n! g: g-----------脚本文件end------------4 Q+ }: h$ x8 ^0 e7 ~* C# i
+ `% y' ?- g0 b/ Q7 \user_tables :用户所有的数据表
+ d9 r$ `- D2 D4 H' P: r. nuser_constraints:用户所有的约束条件6 l: S% U9 R6 T) ]" r: `$ p4 K
user_objects :用户所有的对象(表、视图、索引...)! B6 O0 d" `, q6 ?% ?
! J" ?( _0 y, _/ t( i
all_tables :用户能访问的数据表,
4 a1 r; j( K0 C. _/ ]* ^ 包括自己的和别的用户允许自己访问的
. y' ~3 W5 f4 uall_constraints:用户能访问的约束条件
, w1 h8 p/ B% ~; B3 u9 m# ]5 [all_objects :用户能访问的对象(表、视图、索引...)* ^3 E' a) m; @* h" N1 j" j
2 c# ?% U* k( O" S+ U) p' _# g- SPK / FK( |: h2 G4 ]; a% \
NOT NULL / UNIQUE
1 }7 }# {3 m0 J" ~2 [8 H( {CHECK
w# m; X- q" O* d) C, x其中:CHECK和NOT NULL可以在程序级别控制
0 Z( Y3 `+ E0 i) s( a, F( o
, ~4 `# O* G8 N7 _二、数据库的其他对象
5 T! T3 q! N6 } S3 l表 Table
/ S5 f% d( }, H+ Z6 p$ ]/ ~) U1 p视图 View
! a. {4 N4 p) W8 I- H. F" U索引 Index
' d2 g: _) b% q序列 Sequence! Q0 t2 L- {1 k5 W# `
过程 Procedure
+ G$ j. H- a; F8 N6 z5 V函数 Function* D, n" r7 \' N1 H5 o. J
包 Package
# \8 P$ }& N( r. C M7 J触发器 Trigger
& \6 s5 M7 |1 d+ ]* E同义词 Synonym- @% ~7 @' Z. @& G1 B
..../ B7 `% q0 E6 k8 q6 c8 r& }/ u
3 ]8 @2 q* p: g; q6 Z0 Y
1.视图View
/ o8 _& p4 @% q& M3 `& S( jcreate view v_emp_ning
v) q; E) @, b; i9 f9 ]0 Eas
% k% L8 K. t* {8 {. Uselect empno, ename, job from emp_ning
7 K2 ~$ P( @6 Dwhere deptno = 20;5 ?) ~% G- E9 Q
--使用和表相同/ _3 A/ G7 y7 h! \6 n3 v1 [
desc v_emp_ning
' {5 l7 U p4 ^0 c- s c/ Dselect * from v_emp_ning;, X* W/ @2 d! x# M1 m
--视图的好处:简化查询;隐藏数据表的列$ t# \/ c- [8 {; @! B9 _7 Z2 ^
2 x6 t! J" P( }& ^; H x% d
create view v_emp_count
. {- F+ s0 O& S5 U5 }; fas
, Y! s3 s8 N: X) ]# C' jselect deptno, count(*) emp_num) n6 N. f" L) m. T; }
from emp_ning
T3 [1 y! D6 Z2 h ]' Sgroup by deptno;
7 [, p! c! {( W* ]% W--修改基表数据* n, Q/ g" _2 c! T; W
update emp_ning set deptno = 106 v: {3 c) n C, H" r! n
where deptno is null;
" R" j, H% ^4 R5 K5 {5 G T--视图查询到的是修改后的数据。; m) x& w& p9 X7 m
--视图不包含任何数据。是基表数据的投影。5 G' O/ N; v8 G( \8 _0 y: j
select * from v_emp_count;6 T! F4 c; x6 V7 O
4 G9 d0 O9 z) q" ]
--创建或修改视图
1 v1 A" ^- y/ i3 [5 H# J) S; Lcreate or replace view v_emp_count {# F! d' Y3 Y: U/ @
as
; V1 O8 v' W* V n! l( X! fselect deptno, count(*) emp_num,
/ {. L5 w5 e, {( n# E, ]sum(salary) sum_s,
8 c! A: ^" q6 y5 `) G% q' Savg(nvl(salary,0)) avg_s,
* I" M9 c7 c' J# a1 X7 Z7 lmax(salary) max_s,
5 m4 u" ^! P" t$ T) w; n- omin(salary) min_s! f6 r3 c8 Y- G7 O; Q. V: P4 \
from emp_ning( L& K0 P4 Z# F2 k: Y* i$ `
group by deptno;
9 f' }7 e# a1 e0 }6 ]) s/ T: A/ L* Y. N& G, O3 M/ ?
--查询视图的定义
3 F" o+ B; w$ q0 e3 vselect text from user_views
- [ f" q- N4 B( Cwhere view_name = 'V_EMP_COUNT';
" j0 f: Y; V) T& i! Z--如果视图对应的sql语句显示不全" k* T1 L# U7 v4 @: l; s' P c/ w
set long 10000 r j5 V+ Z& P' H' _
- g7 E' ^ X! W! ]2.索引 Index :用来提高查询效率的机制。
( U. b$ z- \/ `: e全表扫描: Full Table Scan: 查询效率极低- |2 Y% a. ~7 M! u2 Z; A m+ Z5 {
索引查询:比全表扫描快。
; W6 y2 e2 O& x7 z5 b* ~1 V. b! k
7 e" w- o& f0 ^) D9 O8 z索引的结构:数据 + 地址* k+ |2 P; [( w& f
张三 + Room2037 k [/ O, {6 q: |; y4 f2 Z% c" k
4 F6 M: g) }! i5 n* ]8 Q1 Z3 c
对于数据变更频繁(DML操作频繁)的表,
0 L: X) X5 R' e O2 l0 e5 g+ a$ G索引会影响性能。
. v& z" `! n( N* y v- R2 }5 x. T2 e: P4 R
如果数据表有PK/Unique两种约束,索引自动创建
! G% S1 m* {/ V: J* |& ^除此以外,索引必须手动创建。' ~7 `- P- Q3 v+ t1 g. S
create table student_ning7(; Y* `' F! g4 t) ]) t
id number(4),
5 c2 F" C& c( ]$ Zname char(20),2 Q5 k2 @" C. S, n: p6 B$ Z
email char(40),
; c6 N/ V" r! G* z6 ?: ]1 N- }constraint stu_n7_id_pk primary key(id),
2 _! r+ B. p2 _ M: l S, y' nconstraint stu_n7_email_uk unique(email)5 Z4 y1 l, q* H& ?" P
);
" p$ I7 j c0 i1 _select constraint_name
2 r* a2 g" D3 I% t2 F+ n* pfrom user_constraints C X: r8 O( e/ F8 N
where table_name = 'STUDENT_NING7';( ~# [$ R' W* a; l4 N% p
# k) e ?; T; k, D. [2 v" M% x. M--查询student_ning7表上的索引,( X8 d" e. |0 U: J$ f+ J$ y+ N9 R( S
和主键/唯一约束条件同名,数据库自动创建的索引。5 y3 D' x: a: O6 J+ K* x
select index_name from user_indexes
* a! x$ }* E1 W# z- t. ?where table_name = 'STUDENT_NING7';
# k' }+ h& |. k& X
' |6 l$ Z2 r8 x# j4 @--凡是id或email上的查询,会使用索引
3 X. b/ H; Z& U; j4 `select * from student_ning7
0 O% L8 V9 ]5 C S( E qwhere id = 1001;
4 A+ n( h; {! R, b0 s. S& B y2 ~4 k7 s5 z9 }
--这种查询用不到索引:全表扫描
/ u3 E& g6 t/ Q2 s! f3 |" Y* dselect * from student_ning7
9 c0 a0 [0 d' P! u4 {% L# Y9 hwhere name = 'zhangsan';' `) Y6 G& U$ f+ v0 `
1 _! Q& W j" `! ~- Z/ F* D
--创建基于名字字段的索引,索引名自定义
+ [# N Q9 Y1 l; N0 xcreate index 索引名 on 表名(列名);2 C- p1 Y$ v0 @$ c# ?& ~
create index idx_stu7_name . r( i2 q/ Q# K8 }3 ?3 B' w8 K
on student_ning7(name);9 d1 f0 S! \( ]2 U# Z5 I
( B8 A5 F0 @5 R4 ^索引:8 h2 S1 c F$ G& z: c$ l2 V0 f
1)了解索引的工作原理
; V! B! A) ^* `( X. \2)pk/uk自动创建索引
+ {7 W' z( j& l0 t3 v3)根据查询情况决定手动创建哪些索引。
8 E3 F9 X, V! I+ i. O# N! Q
& y+ l% Q1 c9 @8 L- Y; P2 c+ _9 |4 h$ n- H$ y
3、序列 Sequence --Oracle独有的9 |1 N/ C9 r3 n1 M, @. x
--产生从1开始的数字值,步进是1
+ b8 L1 W o; H/ s- J* ]6 }( E# screate sequence myseq_ning;
+ d8 `) Y. o$ P3 oselect myseq_ning.nextval from dual;
% g# {6 {# E" E$ `2 ]9 c! n" v$ @# [- m, k# S: e8 \8 `0 B; V: ]+ K
序列的特性:产生连续的不同的数字值8 |! P) Q% n4 \
用来作为数据表的主键。
, a2 g/ ^3 u0 Q/ f! P
$ O9 P% M* Q+ s2 @--使用序列产生的值作为表的主键值
! z2 x, a2 J" `+ t4 Z B D% Zinsert into student_ning7(id,name)6 R+ }) p2 e7 v
values(myseq_ning.nextval, 'amy');
5 ~ _! S; ~1 N7 B( T- Z. G& U+ i7 B w6 N
student_ning7和序列myseq_ning的关系:
( t2 U- _# X# W" ^是数据库中的独立对象
# X0 g- H' j, [1 \% z0 e4 l$ i7 E--表可以用序列产生的值作为主键,也可以不用" Q+ a d/ W4 Y d y1 E4 \+ L9 s
--序列可以为一个或多个表产生主键,也可以不用
& K5 x/ t2 W& S* L5 ]# h$ ]1 O8 Z6 ^insert into student_ning7(id,name)2 {* @' J! i* K* i( L
values(100,'river');3 X0 |; x, X/ a( s$ e& H
insert into dept_ning( C: N0 ]8 j4 R; @7 H% g9 d' ]: m
values(myseq_ning.nextval,'market','bj');2 S9 a% Z# W' a& F1 V+ D) r! i
4 O h8 O1 ~6 e7 x--建议:一个序列为一个表产生主键; Y5 _9 b* ?3 I5 l, Q
1 o9 ]1 ?9 i& Q1 d( ^3 W" w希望主键值从1000开始,步进是2?2 l% e3 q; S6 n0 \% k
create sequence myseq_ning1/ J; m8 m( u9 m* s! @- G; J- s( ~
start with 1000
$ S( M k: u" p' \: z' [increment by 2;
! g# `0 r: s% v' C! n* V6 x
) K& c/ C8 f& ]/ Q/ Hinsert into student_ning75 @9 G% v6 f2 x8 ^
values(myseq_ning1.nextval, 'song', null);6 j, B' z. e+ m5 e) t% n/ t
+ m7 k2 `0 I0 [, ~* G; b( J--删除序列,对曾经产生过的数据没有任何影响。+ O# P3 S/ E* {* u ]$ X9 Q3 n% f( \
drop sequence myseq_ning1;6 M* ]4 D: ^: G3 X3 l9 W
) g) ?* s0 O, I" ?. d' VSQL> edit
, V- Y) S1 M3 z; E. A: H1 v0 l0 a. N4 J+ C
|
|