该用户从未签到
|
数据库: DataBase,DB$ _& L1 i- v0 e* o8 N" ^( d
存放数据,管理数据的存储仓库。是有效组织在一起的数据集合。! i' D8 K! \1 F3 x+ E5 I5 e8 e* L( O
oracle是一种数据库软件。
! q: a4 ~ n" I* W) k# p) M# C- `( _! R( W
Oracle db2(IBM) sybase(sybase)
. h& G" D3 c! Q' [2 K) w- {) R5 Ssql server(MS) mysql(Oracle)
, e' O; m+ [/ a$ q1 uaccess+ q$ C# W% J1 k6 _ y+ [6 f
, E" i$ H1 b) t0 r
数据库管理系统(Database Management System): DBMS+ ~6 K8 c! J, k0 d: y- d7 _1 a, q
0 a$ I% Q3 h4 L& f, ~* K
SQL:Structured Query Language
n' g8 {4 A. y4 v: S: }2 h/ j0 Z结构化查询语言:和数据库沟通的语言。, u1 d6 ^1 b, c, O, i: L( ^) |
, ~+ B% h. Y& j& W, CDBA:Database Administrator 数据库管理员4 c% q- E( F9 v) P/ r# Y
# C. W. A4 x* w' STable:表:数据库的基本存储单元1 A" N. N4 E2 [
横向:行,row,record
2 k( ^9 q$ E. K+ c, W- R$ e纵向:列,column,field d; R; d4 E+ h1 Z
, V6 M+ B- k7 L
任务:把用户数据存入数据库的表中。
: e' F- U7 ]8 b K实现:
: X2 {+ h6 \8 h) K/ D% F0 L1.建立一个连接9 w8 z S+ D- H# a
1)数据库所在的服务器的地址:192.168.0.26
# M4 ]6 @+ n( a! F1 `telnet 192.168.0.26
$ ?4 f% x# O8 @# O( J服务器的帐号/密码:openlab/open123
, J- @. ~# r9 k4 p; B2)数据库访问用户:
& [' a, J. ^( Z6 p1 c5 e$ \: Topenlab/open123
2 t' S. D9 s6 f8 M: q; y数据库默认帐户:scott/tiger
- H) V1 S: w# l$ W* `3)oracle数据库的连接工具:
+ Q3 D9 @3 n+ `( lSQLPlus:Oracle数据库的客户端工具
& n% p. _" ^ e( h& v3 t" g6 |( x位于数据库的服务器上,192.168.0.26! v9 Y# D% z7 s0 K& p+ ~
$ sqlplus openlab/open1231 a4 e9 s) r8 i/ m3 A: T
SQL>
9 ` V4 S* T% C" A/ e% l& S如果提示SQL>表示登录成功6 ]* h/ }. N1 l- {( X
9 Y& M" O5 O7 A; ^( h7 W0 [3 a" j" x D' c4 Y) Y$ J
2.把数据表建立起来。: k& D4 V- m/ ~% P
定义数据结构:
& e' V/ _% K2 B/ x1 L7 F( tcreate table user_ning1(
% s$ a/ l4 K# U# l U0 |, Uid number(4),) g0 D% B/ M ]3 @+ Z# o* n
password char(4),+ Y! L7 |* b; X# V8 M8 J+ U
name char(20),
9 @* C# [+ v" b0 jphone char(20),4 C- w& F8 m8 S& w' z
email varchar2(50)$ L; U' n' r2 y; F; C
);5 V% z( J0 M2 v; O5 q8 e d
提示Table Created. 表示表已成功创建。
; i7 v7 O6 v q: T/ g
: c9 w& B# h2 ~, Y数据类型:& W+ E7 z, D4 v& f7 m# t
1)数字: 3 }/ g8 W) U" q" z4 ?
number(n),最长n位
9 x$ |) D9 A8 Mnumber(n,m),最长n位,小数点后m位0 [6 \$ ~0 a& ^+ c- A; Q
number(7,2): 99999.99
' @/ P4 e! c& W$ A4 W
( Q7 R3 R! N$ R2)字符串:, _4 ~4 o) P) p5 Q' d
char: 定长字符串7 g" l8 y# E( d
varchar2:变长字符串
, J. X l4 f! n8 T( Ychar(10)
9 s$ k: {: U. R: S. ]9 jvarchar2(10)* q5 f4 K7 T. c% c" n) N9 y6 E
3)日期' b, b9 o$ `! e
date
/ V2 S$ L5 a8 M$ Z/ v, ^; a6 T3 |! N& Y1 ]8 @9 {
清屏: clear scr
5 o; m% S$ P& K4 R; C
5 l+ }, N( ~( Q5 U- \; ?( g3.把用户数据存入数据表中。
9 k' t9 R% b f dSQL>insert into user_ning1
8 S: x- m- b+ z$ J/ j) |. y values(1001,'1234','liucs','13600000000','liucs@sina.com');) H+ L- D1 M5 I( n
6 V. p- N8 H ?insert into user_ning1 values(1004,'1234','liyi','98765432',
$ ~- x. }6 X }'liyi@sina.com');
1 i6 w" R; a! b* ~2 X
( d _$ A3 ^# J8 |8 P5 }; p2 e K 错误语句:
( \6 d( o/ y) q9 ^. f( Minsert into user_ning1 values(1001,'1234','liucs','13600000000','my email', 25); h& g8 s+ f, b. v
- y+ P e* j7 C& r; x! _; `- S. k三种SQL语句:+ L. a( J2 w5 t1 J. Z
1)create table...+ b; k1 ?! j# c7 {
2) insert into...values...8 W1 W4 Z9 X2 H1 }7 n
3) select ... from...
; D- ~# G0 A5 q( t% G1 X$ d$ l% p
SQLPlus命令:4 m" y3 {8 n( l3 b8 S
设置每行数据的显示长度:8 B' D; O/ @6 U0 i; ]% Y3 d0 T
SQL>set linesize 200/ A4 Y1 l! C/ \0 \' p
设置列宽:10个字符/ I: [% \$ R* n8 `, t# {
SQL>column 列名 format a10- B$ Q# N, S/ X7 h
查看表结构:desc:describe 描述
- u& t" Z( J- M, c" n$ ~SQL>desc user_ning1
" v# k7 t3 o( `3 O
& ^, p0 _+ f# L( w规范数字宽度为4位:6 |1 O+ s9 ]6 N& ~
SQL>column id format 9999( r4 s ~. D; O" [7 n( N6 ?* }
简写:
( r4 a5 a" T* }SQL>col id for 99999 y- M. ]* E) W, C6 ?+ Y1 ?
9 d' f3 W. u0 p: a
--*表示查询全部列
: p( d; h7 |6 e7 b+ \0 {! d9 \select * from user_ning1;; X8 U9 f* V9 h/ o
* C% T* I3 }3 |% m5 h- G+ s$ Q
select name,email from user_ning1;$ e' n! @& Y; I3 N+ ?: n4 `
7 O4 _6 {. C8 I! P
--查找1001/1234考生的名字?
; B) ]1 l/ o; l$ I0 K" Fselect name, email from user_ning1
2 {1 y" ]5 ~5 R- ^ D4 O5 L. gwhere id = 1001 % [. ], C% }7 E+ B4 e3 f6 v
and password = '1234';
4 H* T; j; d* V, ~# Z! U! Q* Y8 L7 _. C1 z3 w1 ]
数据准备:* l- V- c3 |7 y% x
1.表dept_ning:
- _. D, {- {8 l& Acreate table dept_ning(
8 n" s- W: Y1 g$ }' V1 c8 \deptno number(2),
) J3 D! i) Y$ l* `8 G& V9 ?dname char(20),
1 B: g' A5 W, v# [4 m) f6 T' ~7 Y, ?location char(20));
. H k$ r( y1 t" S' W6 G! e$ D% \* v" g6 l4 H$ H- s2 ?- \- j! D
drop table dept_ning;
% X$ {; R1 P2 G# V7 o: _! c
2 w! t2 p X9 Z7 Q; H, `增加数据:8 `& I N0 l# \& }. ^
insert into dept_ning values(10,'developer','beijing');
- h" L! z" A. d- J, v: p1 Rinsert into dept_ning values(20,'account','shanghai');
, |" ?0 W* F' hinsert into dept_ning values(30,'sales','guangzhou');. \& Q. b7 y+ T( i: E1 k4 J
insert into dept_ning values(40,'operations','tianjin');+ i/ M& J, K& }. ^
: b8 F7 L# [. I! x
commit;% U0 N; ?# g# c( O9 I: W- {
" A! S' }7 s6 d* y& _8 T2 Qselect * from dept_XXX;& B2 D6 q' }! D& g: e6 }$ B
7 L+ [/ E- n9 w8 L
2.emp_ning
4 \* f7 ?0 d8 [3 e ^. j0 ccreate table emp_ning(
* L/ Y! z3 Y& |- U8 q) Q& J, Dempno number(4),
2 M+ n/ R9 z5 ~6 q) m' pename varchar2(20),3 g- R2 T- m. |# b# g( c
job varchar2(15),' K( g3 v( Q0 L, j
salary number(7,2),
; \4 W M. M/ r1 M: T. Q( O; H$ Abonus number(7,2),
: h! D! h/ [1 C( h# }; c" Thiredate date,
( u! ^: I' m# G+ v0 H8 F' v9 ]7 dmgr number(4), z+ q3 g# F. b- ?% \: b) E5 e
deptno number(10). T* N$ O9 J1 H
);
# |4 ^/ U% s1 n7 n$ u; D& F" \5 e, m! X4 Z0 _8 {- {
insert into emp_ning values(1001, '张无忌', 'Manager', 10000, 2000, '12-MAR-10', 1005, 10);
( v2 Q, d5 J9 _$ t Jinsert into emp_ning values(1002, '刘苍松', 'Analyst', 8000, 1000, '01-APR-11', 1001, 10);$ m; K# R" ~& A% K5 o& c
insert into emp_ning values(1003, '李翊', 'Analyst', 9000, 1000, '11-APR-10', 1001, 10);
, I( a& V% A. O: H+ C5 C( Yinsert into emp_ning values(1004, '郭芙蓉', 'Programmer', 5000, null, '01-JAN-11', 1001, 10); ( U2 A. O: H8 D
insert into emp_ning values(1005, '张三丰', 'President', 15000, null, '15-MAY-08', null, 20);( |- o0 B* o% m, R- s
insert into emp_ning values(1006, '燕小六','Manager', 5000, 400, '01-FEB-09', 1005, 20);: B, ~; `" G8 V# K* @
insert into emp_ning values(1007, '陆无双','clerk', 3000, 500, '01-FEB-09', 1006, 20);! b$ \4 v3 A5 K$ t
insert into emp_ning values(1008, '黄蓉','Manager', 5000, 500, '1-MAY-09', 1005, 30);
0 f5 G* O9 d( T1 _) D+ _% linsert into emp_ning values(1009, '韦小宝','salesman', 4000, null, '20-FEB-09', 1008, 30);( I0 c: p9 s) ^# ~8 {4 C4 _! Z4 e6 p
insert into emp_ning values(1010, '郭靖','salesman', 4500, 500, '10-MAY-09', 1008, 30);. V& i+ }, k7 G2 x3 ?( `& q: _
! w# H1 q6 n C7 Sset linesize 150
- X( ^( Q& K4 Ncol empno for 9999
4 l; ]5 u [) ~col mgr for 9999! |7 q0 m" r( i _% [
col deptno for 99
5 Q8 S9 z0 J4 S! ^2 P$ Ncol salary for 99999.99; S8 w+ Q3 ], G, c6 C
/ V& Q$ k+ n0 \% q( Wselect * from dept_ning;) t2 m e# M0 e' k/ y% f* s9 o7 K5 i
select * from emp_ning;/ m" O* @3 m+ d1 a8 x, g8 l; G( B
# j/ V0 m" r' ]一、学习查询语句。
7 ~- B3 r' l6 `: H) U# R0 r1. 计算员工的名字、月薪和年薪?
, @/ W: }" N/ t" I& |4 ~6 v3 Z6 lselect ename, salary, 9 }% h5 e! m6 A' H6 Q5 P
salary * 12 year_sal
3 O. [0 u! n7 h2 V& U; x3 @3 rfrom emp_ning;- I1 v1 T. H- p
) p0 P/ ^. ]* v8 @, i2.计算员工的月收入?
" w v& w2 L9 A ]; I* z" z/ @空值和任何数据做算数运算,结果为空(null)' {, P( \: N0 @; u
select ename, salary, bonus,8 v7 t3 B5 Q% m3 }/ c$ V4 M, i
salary + bonus month_sal
6 T; f# ]/ s g3 \) m6 r. ^from emp_ning;
$ K6 b8 X& L4 v' w6 C/ a, d# K/ m, }& N7 Q6 J6 Q `3 J
select ename, salary, bonus,
$ l5 \# v) {( G/ Q) @ salary + nvl(bonus, 0) month_sal
) W) O3 E1 [+ {# O7 u( Lfrom emp_ning;
' q* C- W; @/ m3 B2 B! S3 z6 X8 i( O* i# |8 }2 |
public double nvl(double d1, double d2){! X- q' w; {# [. c2 J& w
if (d1 != null)& Z# C) g0 N; {
return d1;
! [) l- z z- p7 J \2 q$ M- u: { else 0 w' S S3 D; F1 L$ ^! q
return d2; 6 C: `+ p) L3 _, I k, o, k
}
( m# g: O* C* j, z dpublic String nvl(String s1,String s2)
0 q* i1 Y6 {; w, G{1 h2 K8 M" t$ d' T; V4 _! m
if (s1 != null)9 a1 M% Y7 a7 _( e* z6 C4 G
return s1;
- x% R, M; A* ~. s1 K else! k+ Z" J" e# b# g/ t) l% I
return s2;. ], w* E( o: g0 W- k# q& N* K
}
' n% d2 `) ^5 o( v i" h- \public Date nvl(Date d1, Date d2){4 ^; ]; L0 Z: T$ d2 r
return (d1 != null) ? d1 : d2;! J8 F8 z& h- P0 @2 R
}
) {- o7 J% L. T4 P0 v# c/ r
' Z2 R# O. h" ~, i, cinsert into emp_ning- w$ b; q, S# d8 O, q
values(1011,'余泽成',null, null, null,
$ q& n1 Z" Y) hnull, null, null);
6 ?5 _4 |9 p" T* ]' \简写为:
! E$ v* v9 _1 C4 H1 zinsert into emp_ning(empno, ename)& N9 z& d6 t: ~# d7 W: y
values(1011,'余泽成');
$ W* d9 s5 V3 u( h, U7 A+ I3 ^- s( E# k" j9 W+ L. f
查询emp_ning表,如果没有职位,显示'no position',如果有职位,显示员工的职位。
0 m1 `4 f: {% n7 Pselect ename, nvl(job,'no position')
: b( } b" \1 k6 t4 zfrom emp_ning;
' r, G6 Y1 L, W, [! x
( h9 z$ K- L" j( P查询emp_ning表,如果没有入职时间,显示2011年10月10日。如果有入职时间,显示。7 D, K% d5 i# @# n% N4 m
select ename, nvl(hiredate, '10-OCT-11') from emp_ning;: ~2 K. `1 }0 b3 q! w/ @5 h
8 u* _( Q& g: n# C$ |
nvl(bonus, 0)8 I# M# a, I4 B& m+ A- C8 E
nvl(job, 'no position')
# a i7 O+ K4 u! n/ o# Dnvl(hiredate, '10-OCT-11')
) F$ I: o0 t, |5 G9 C( q: q: V* N1 \( ~0 Q6 C* x; O* J
--复制表emp_YYY为emp_XXX# { P: V6 [& Z8 [* X
create table emp_XXX- M+ m6 b$ G+ v# G, `, N, U" x
as
3 R [$ | {% x' O; j4 }8 sselect * from emp_YYY;+ P |" ^; a# Q! T
6 C1 E8 f+ ~5 ]( M
3. 机构中有多少种职位?" ?( T5 r. x+ F) q! P" {
select distinct job from emp_ning;
) D7 p# G% N. B' G( n
" l( N |4 i/ @" w7 ^! U" `员工分布在哪些部门?
4 Y Y" R( O1 b0 R$ u9 n2 O: rselect distinct deptno from emp_ning;
" }: l( W* Q% E& o
. m( U! O- W+ @7 }distinct必须/只能跟在select后边。
# Z% C8 G# {5 M$ V" T) q( R% P4 G4 q' u: M: H
4.薪水高于10000元的员工数据?
9 S+ ]+ Y' `% }0 j2 @select * from emp_ning$ E% @; D. H C" r
where salary > 10000;
( I" J: U( a( Z7 C; E( F
' A7 D& [8 ~- @" ^9 r8 j5 Y5.职位是Analyst的员工数据?- a& g" a9 c# n! n$ u' Y3 K$ G
select * from emp_ning R: t, ^4 l3 g7 N+ @
where job = 'Analyst';
* z% _1 d& M& Q0 E6 K5 `, I+ ^) o: O+ \( ]4 }" E4 y* [# x# V+ @
analyst ANALYST anaLyst6 L- ]2 i% S* H% d* ~! t/ n& U
7 T2 ?' q0 |0 H, Z# A Rselect * from emp_ning
* O5 @$ G- }2 |8 ewhere lower(job) = 'analyst';' L$ v7 X4 ]1 r- K. S; C+ |
5 P) O6 t4 B( N' S' `& X# S
select * from emp_ning% E# r2 l& c* n7 }- A1 T
where upper(job) = 'ANALYST';
8 r/ D) X/ d' O- Y0 E! ^4 K0 y1 r- \5 n1 Q2 }- w( O4 t/ X( i
--如果数据是analyst,查不出结果
9 R ~" y; Y9 \( P: s1 R--SQL语句大小写不敏感,数据大小写敏感
- L8 x0 n9 N* T+ T/ {! h$ g/ T# }
6.薪水大于5000并且小于10000的员工数据
& i F* Y% V. U8 |. j! T?; K$ h8 ~5 }6 w5 H* J4 U5 M
select * from emp_ning
$ V0 A6 n' U7 Bwhere salary >= 5000
/ D2 L" B- @& ]$ h" u! q2 |7 Uand salary <= 10000;1 y# @ x: ^, b# E
2 M; L y6 J" K7 G0 G4 v
--在区间中:between 低值 and 高值# J9 Y/ a$ `" g4 N! o2 p( @
--闭区间:[低值,高值]" Q/ q6 @! f- k( s" G Z
select * from emp_ning
% @* J2 ?) w+ |9 J" H9 gwhere salary between 5000 and 10000;
- Q- H; S6 e0 @; ]% f& {+ ]; j% ], b" D1 n- V) X9 c8 w
入职时间在2011年的员工?
0 F$ E0 W/ X2 t) o['01-JAN-11','31-DEC-11']( a* E4 L( h; M5 T$ ^- w) A3 w' q
select * from emp_ning
' M( o3 K2 ~# vwhere hiredate between '01-JAN-11') f& p; A! J( y( v3 F4 J6 o
and '31-DEC-11';
6 `( S! n1 ]$ s% I1 i
% Z( s1 ]% X( J7.列出职位是Manager或者Analyst的员工# a6 ^: e9 F& A2 y% S3 X
?
: J. v8 V6 n* y8 a, h% s% rselect * from emp_ning
1 s! G9 x$ x' X& \4 W0 t+ L* @where job = 'Manager', A! _5 A8 @+ D: q0 ]
or job = 'Analyst';" D7 E M, A7 p3 }( u: i( W/ L
--等价:in (list),在列表中。, j! d9 [) ?! i. J, y
select * from emp_ning) c' m6 K) }% ?5 V4 [6 m4 J
where job in ('Manager','Analyst');; K2 T0 A- I: x7 ^
" n/ W/ q, l' _+ k1 F6 g8.列出职位中有sales字符的员工数据?; l1 I' O. |7 }" V6 ?
salesman
$ x7 G( o4 @- _8 Osales7 l' D% I6 Z8 t3 a; ]
before sales
6 d% r @+ |$ @: F" k& ^3 mafter sales
# W. f% x3 A @' Q# pbefore sales engineer, ~9 g3 C- G# U$ v* _+ m
/ |) p$ X. U9 f8 w! d
--%: 0到多个字符,跟like配合,模糊匹配
' ~. v- U" B+ d) Fselect * from emp_ning
; P& o* A/ a- U S" [, t2 X* m6 Cwhere job like '%sales%';
2 q# i4 S, y% s2 x' _
, ^, X/ |: ]3 @! F/ \9.查询哪些员工没有奖金?# [3 `) [! t7 h
select * from emp_ning. H" Y; u6 z) S$ J! s: ~; `& \7 e
where bonus is null;
6 T9 r: y* a( @6 H, [ J
" b: I0 w/ ~* P7 p哪些员工有奖金?. R7 a+ }; Z: h/ o- C; c
select * from emp_ning- l% X& C# e: z9 j" x
where bonus is not null;
% Q" y/ u$ e* q5 j: P/ {- y F9 X. z/ Q: }# `2 h
小结:
+ r, Y8 o, \( W; u1)create table ...0 a9 N: D9 _( a+ ^# `" Z J0 N
drop table 表名;
/ O# T! }. }# k* d2)insert into 表名 values(...);
- F3 x; M& H2 Y. W$ u3)select distinct | * | 列名 | 算数表达式 | 别名* o5 B# R! R+ I) u; f
from 表名
- H0 e: H) Z3 d9 L9 T, d! p% [ where 条件1 or 条件2 and 条件3;
$ `- n2 F# v a! `$ ]# ]7 @- n' V" ~0 |2 b# \% ]" {3 k& C
> >= < <= between...and... in like2 B+ s. h% [/ g3 ~
5 S- l: Z( h; H2 h- e0 _8 v) }0 j
|
|