常見的數(shù)據(jù)庫(kù)
小型數(shù)據(jù)庫(kù) access(微軟)
中型數(shù)據(jù)庫(kù) mysql(瑞典MySql AB) sql server(美微軟)informix(美IBM)
大型數(shù)據(jù)庫(kù) sybase(美sybase) oracle(美oracle) db2(美IBM)
Oracle基本操作
一、Oracle數(shù)據(jù)庫(kù)基本命令:
1.SQL->show user; 顯示用戶 2.SQL->connscott/tiger;用戶連接 3.SQL->disc; 斷開連接 4.SQL->passw; 修改密碼 5. SQL->exit; 退出 6.SQL->start D:test.sql; 運(yùn)行D盤下test.sql腳本7.SQL->edit D:test.sql; 編輯D盤下test.sql腳本
8. SQL->spool D:test.sql;
SQL->select * from emp;
SQL->spool off;
二、Oracle管理用戶
1.創(chuàng)建用戶(以DBA身份)
SQL->create user name identified bypassword(以字母開頭)
注意:新建用戶默認(rèn)是沒有任何權(quán)限的,需要DBA賦予權(quán)限(1、系統(tǒng)權(quán)限:對(duì)數(shù)據(jù)庫(kù)的相關(guān)權(quán)限;2、對(duì)象權(quán)限:用戶對(duì)其他用戶數(shù)據(jù)對(duì)象(表、視圖等)操作的權(quán)限)
2. grant賦予用戶權(quán)限
SQL->grant connect tousername;(把角色connect賦予username(dba 、resource))
SQL->grant select on emp tousername;(emp表的用戶scott或sys、system把select權(quán)限賦予username)
username 用戶可以進(jìn)行select操作了,SQL->select * fromscott.emp;(查詢emp表的內(nèi)容)
SQL->grant select on emp tousername;(把全部權(quán)限賦予)
3.revoke回收用戶權(quán)限
SQL->revoke select on emp from username;(scott收回用戶username對(duì)用戶scott表emp的select權(quán)限(必須是scott賦予username的權(quán)限))
4.權(quán)限的傳遞
對(duì)象權(quán)限:SQL->grant select on emp to username withgrant option;
系統(tǒng)權(quán)限:SQL->grant select on emp to username withadmin option;
5.給用戶修改密碼
SQL->password username;(password scott;sys修改scott密碼)
SQL->password;(scott修改自己密碼)
6.使用profile管理用戶口令
賬戶鎖定
SQL->create profile lockname limitfailed_login_attempts 3 password_lock_time2;(以dba身份)創(chuàng)建配置文件,名字為lockname。
SQL->alter user username profile lockname;
賬戶解鎖: SQL->alter user username accountunlock;
刪除profile :SQL->drop profile lockname;
Oracle表的管理
一、表和列的命名規(guī)則
· 必須以字母開頭
· 不能使用oracle的保留字
· 只能使用A-Z,a-z,0-9,$,#等
· 長(zhǎng)度限制為30個(gè)字符內(nèi)
二、oracle的數(shù)據(jù)類型
1.字符型
char() 定長(zhǎng) 特點(diǎn):查詢速度快,效率高 ;浪費(fèi)空間 最大:char(2000)
varchar2()可變長(zhǎng)度 特點(diǎn):節(jié)省空間,效率低 最大varvhar(4000)
clob()字符型大對(duì)象 最大4G
2.數(shù)字類型
number 可以表示整數(shù)和小數(shù)
number(5,2)一個(gè)小數(shù)有5位有效數(shù),2位小數(shù)-999.99~999,99
number(5)一個(gè)5位整數(shù)-99999~99999
3.日期類型
date 包含年月日時(shí)分秒
圖片類型
blob 二進(jìn)制數(shù)據(jù) 可以存放圖片、聲音
三、表的操作
1.創(chuàng)建表
SQL> create table student (
2 sID number(4),
3 sName varchar2(20),
4 sSex char(2),
5 sBirthday date,
6 sPay number(7,2) 區(qū)別于sql server 無(wú)逗號(hào)。
7 );
Table created
SQL> create table class(
2 cId number(2),
3 cName varchar2(20)
4 );
Table created
2.修改表
添加字段 SQL> alter table student add (cIdnumber(2));
刪除某個(gè)字段
SQL> alter table student drop column cId;
Table altered
查看表結(jié)構(gòu) SQL> desc student;
修改字段長(zhǎng)度 SQL> alter table student modify (cIdnumber(5);
刪除表 SQL> drop table student;
修改表名字 SQL>rename student to stu;
3.添加數(shù)據(jù)
SQL>insert into studentvalues(1,'小明','男','11-5月-2011',12345.1,12);
1 row inserted
日期格式默認(rèn)為DD-MON-YY 天-月份-年
可以修改默認(rèn)日期格式
SQL> alter session set nls_date_format='yyyy-mm-dd';
Session altered
修改一個(gè)字段
SQL>update student set sSex='男' wheresId='2';
1 row updated
添加空值
SQL> insert into student (sId,sname,sbirthday)values (4,'小明',NULL);
1 row inserted
查詢時(shí)注意:SQL> select * from student where sBirthdayis null;
刪除數(shù)據(jù) SQL>delete fromstudent;刪除所有記錄,表結(jié)構(gòu)還在,如果再刪除之前設(shè)置回滾點(diǎn)還可以回滾。
回滾操作
SQL>savepoint a;
SQL>delete from student;
SQL>rollback to a;
· SQL>drop table student;刪除表結(jié)構(gòu)和數(shù)據(jù)
· SQL>delete from student where sId=1;刪一條記錄
· SQL>truncate tablestudent;刪除所有記錄,表結(jié)構(gòu)還在,速度快對(duì)于大型表適用
oracle 表的基本查詢
SQL> set timing on;顯示執(zhí)行時(shí)間
SQL> desc dept; 查看表結(jié)構(gòu)
一、單表查詢
取消重復(fù)行
SQL> select distinct deptno ,job from emp;
NULL處理
SQL>select sal+nvl(comm,0) fromemp;當(dāng)comm為null時(shí)變?yōu)?
SQL Server中為select sal+isnull(comm,0) from emp;
where子句
SQL>select ename,hiredate from emp wherehiredate>'1-1月-1989';
like語(yǔ)句
SQL>select ename,sal from emp where ename like'S%';
%:零個(gè)或多個(gè)字符
_:任意單個(gè)字符
邏輯操作符 or and
order by 子句
SQL>select * from emp order by sal;默認(rèn)為升序
SQL>select * from emp order by sal asc;升序
SQL>select * from emp order by sal desc;降序
使用列的別名排序
SQL>select ename,(sal+nvl(comm,0))*12 as "年薪"from emp order by "年薪" desc;
Oracle復(fù)雜查詢
max(),min(),avg(),sum(),count()
group by用于對(duì)查詢結(jié)果分組統(tǒng)計(jì)
having子句用于限制分組顯示結(jié)果
SQL>select avg(sal),max(sal),deptno from empgroup by deptno having avg(sal)>2000 order byavg(sal) asc;
二、多表查詢
1.笛卡爾積
SQL>select e.ename,e.sal,d.dname from emp e,deptd where e.deptno=d.deptno;
2.自連接
自連接指在同一張表的連接查詢
SQL>select worker.ename,boss.ename from empworker,emp boss where worker.mgr=boss.empno;
3.子查詢(嵌套查詢)
嵌入在其他sql語(yǔ)句中的select語(yǔ)句。
單行子查詢
SQL>select * from emp where deptno=(select depnofrom emp where ename='SMITH');
多行子查詢

SQL>select * from emp where job in (selectdistinct job from emp where deptno = 10 );
多行子查詢中使用all
SQL>select ename,sal,deptno from emp wheresal>all (select sal from emp where deptno=30);
等價(jià)
SQL>select ename,sal,deptno from emp wheresal>(select max(sal) from emp where deptno=30);
多行子查詢中使用any
SQL>select ename,sal,deptno from emp wheresal>any (select sal from emp where deptno=30);
等價(jià)
SQL>select ename,sal,deptno from emp wheresal>(select min(sal) from emp where deptno=30);
多列子查詢
SQL>select ename,sal from emp where(deptno,job)=(select deptno,job from emp where ename='SMITH');
SQL>select a2.ename,a2.sal,a2.deptno,a1.mysalfrom emp a2,(select deptno,avg(sal) mysal from emp group by deptno)a1 where a2.deptno=a1.deptno anda2.sal>a1.mysal;
注意表取別名是不加as,列可以。
SQL> select a2.ename,a2.sal,a2.deptno,a1.mysalfrom emp a2 join (select deptno,avg(sal) as mysal from emp group bydeptno) a1 on a2.deptno= a1.deptno wherea2.sal>a1.mysal ;
4.分頁(yè)查詢
SQL> select * from (select a.*,rownum rn from(select * from emp) a where rownum<=10 ) wherern>=6;
SQL> select * from (select a.*,rownum rn from(select ename,sal from emp order by sal) a whererownum<=10 ) where rn>=6;
5.查詢結(jié)果創(chuàng)建新表
SQL>create table mytable (id,ename,sal) as selectempno,ename,sal from emp;
合并查詢
集合操作符union
union all
intersect
minus 差集
6.創(chuàng)建新的數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)配置助手Database Configuration Assistant
愛華網(wǎng)



