oracle表管理
表名和列的命名規(guī)則
1.必須以字母開頭
2.長度不能超過30字符
3.不能使用Oracle的保留字
4.只能使用如下字符A-Z,a-z,0-9,$,#等
數(shù)據(jù)類型
字符型
char 定長,最大2000字符 (定長,假如長度沒達(dá)到要求,用空格補(bǔ)全其余的位;不過查詢效率最高)
varchar2變長,最大4000字符
clob(character large object) 字符型大對(duì)象,最大4G(超過4000字符)
數(shù)字型
number 可以表示整數(shù)、小數(shù)
例子:
number(5,2)表示一個(gè)小數(shù)有5位有效整數(shù),2位小數(shù)
number(5)表示一個(gè)5位整數(shù)
日期類型
date 包含年月日和時(shí)分秒
timestamp 這是oracle9i對(duì)date數(shù)據(jù)類型的擴(kuò)展(精度更高)
圖片
blob 二進(jìn)制數(shù)據(jù),可以存放圖片、聲音,最大4G
表空間:
表創(chuàng)建:
表的修改:
添加一個(gè)字段
SQL> alter table student(表名) add(classid number(2));
修改字段的長度
SQL> alter table student modify(xm varchar2(30));
修改字段的類型或者名字(不能有數(shù)據(jù))
SQL> alter table student modify(xm char(30));
刪除一個(gè)字段
SQL> alter table student drop column sal(列名);
修改表的名字
SQL> rename student to stu;
刪除表
SQL> drop table student;
表的查詢(重點(diǎn):多表查詢,聯(lián)合查詢;oracle是區(qū)分?jǐn)?shù)據(jù)大小寫)
查看表結(jié)構(gòu)
SQL> desc dept;
查詢所有列
SQL> select * from dept;
查詢指定列
SQL> select ename,sal,job,deptno from emp;
如何取消重復(fù)行
SQL> select distinct deptno,job from emp;
顯示操作時(shí)間
SQL> set timing on;
使用算數(shù)表達(dá)式
SQL> select sal*12 "年工資",ename from emp;
關(guān)于處理null值(nvl函數(shù))
nvl函數(shù)使用格式:nvl(字段名,0)
SQL> select sal*12+nvl(comm,0)*12,ename from emp;
連接字符串(||)
select ename || 'is a' || job from emp;
使用where子句
select ename,sal from emp where sal>3000;
select ename,hiredate from emp where hiredate>'1-1月-1982';
select ename,sal from emp where sal>=2000 and sal<=2500;
使用like操作符
% 表示任意0到多個(gè)字符
_ 表示任意單個(gè)字符
select ename,sal from emp where ename like 'S%';
select ename,sal from emp where ename like '___O%';
在where子句中使用in方法
select * from emp where empno in(123,456,234); //批量處理empno=123 or empno=456 ...
select * from emp where mgr is null;
使用邏輯操作符
select * from emp where (sal>500 or job='MANAGER') and ename like 'J%';
使用order by子句
select * from emp order by sal;//默認(rèn)是從小到大排列,asc
select * from emp order by sal desc;//從大到小的排列
select * from emp order by deptno,sal desc;
select * from emp order by deptno,hiredate desc;
select ename,sal*12 "年薪" from emp order by "年薪" asc;
分頁查詢
oracle表復(fù)雜查詢
添加數(shù)據(jù)
所有字段都插入
insert into student values('A001','張三','男','01-5月-1983',500,12);
oracle中默認(rèn)的日期格式'DD-MON-YY'
DD 日子(天)
MON 月份
YY 2位的年
'09-6月-99' 表示 1999年6月9號(hào)
修改日期的默認(rèn)格式
alter session set nls_date_format = 'yyyy-mm-dd';
插入部分字段
insert into student(xh,xm,sex) values('A003','Johm','女');
插入空值
insert into student(xh,xm,sex,birthday) values('A004','MARTIN','男',null);
查詢空字段
select * from student where birthday is null; 正確查詢方式(birthday is not null)
select * from student where birthday=null; 查詢結(jié)果為空
select * from student where birthday=""; 報(bào)錯(cuò),非法的零長度標(biāo)識(shí)
修改一個(gè)字段
update student set sex='女' where xh='A001';
修改多個(gè)字段(多字段間用逗號(hào)隔開)
update student set sex='男',birthday='1980-04-01' where xh='A001';
修改含有null值的數(shù)據(jù)
update student set birthday='1983-06-17' where birthday is null;
修改SAL字段,數(shù)字減半
update student set sal=sal/2 where sex='男';
刪除數(shù)據(jù)(表還在)
delete from student;
數(shù)據(jù)恢復(fù)
保存節(jié)點(diǎn)(oracle支持多個(gè)保存點(diǎn))
……(插入數(shù)據(jù));
savepoint a;
刪除數(shù)據(jù)
delete from student;
回滾恢復(fù)
rollback to a;
設(shè)置保存點(diǎn):savepoint a;
恢復(fù)保存點(diǎn):rollback to a;
刪除表的結(jié)構(gòu)和數(shù)據(jù)
drop table student;
刪除一條記錄
delete from student where xh='A001';
刪除所有記錄(表結(jié)構(gòu)還在,不寫日志,無法找回刪除的記錄,速度快)
truncate table student;
刪除所有記錄(表結(jié)構(gòu)還在,寫日志,可以恢復(fù)記錄,速度慢)
delete table student;
oracle清屏
SQL> clear screen;
SQL> clea scre;