五、oracle 表的管理
生活随笔
收集整理的這篇文章主要介紹了
五、oracle 表的管理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、表名和列名的命名規(guī)則
1)、必須以字母開頭
2)、長度不能超過30個(gè)字符
3)、不能使用oracle的保留字
4)、只能使用如下字符 a-z,a-z,0-9,$,#等二、數(shù)據(jù)類型
1)、字符類
char 長度固定,最多容納2000個(gè)字符。
例子:char(10) ‘小韓’前四個(gè)字符放‘小韓’,后添6個(gè)空格補(bǔ)全,如‘小韓 ’
varchar2(20) 長度可變,最多容納4000個(gè)字符。
例子:varchar2(10) ‘小韓’ oracle分配四個(gè)字符。這樣可以節(jié)省空間。
clob(character large object) 字符型大對象,最多容納4g
char 查詢的速度極快浪費(fèi)空間,適合查詢比較頻繁的數(shù)據(jù)字段。
varchar 節(jié)省空間
2)、數(shù)字型
number范圍-10的38次方到10的38次方,可以表示整數(shù),也可以表示小數(shù)
number(5,2)表示一位小數(shù)有5位有效數(shù),2位小數(shù);范圍:-999.99 到999.99
number(5)表示一個(gè)5位整數(shù);范圍99999到-99999
3)、日期類型
date 包含年月日和時(shí)分秒 oracle默認(rèn)格式1-1月-1999
timestamp 這是oracle9i對date數(shù)據(jù)類型的擴(kuò)展??梢跃_到毫秒。
4)、圖片
blob 二進(jìn)制數(shù)據(jù),可以存放圖片/聲音4g;一般來講,在真實(shí)項(xiàng)目中是不會(huì)把圖片和聲音真的往數(shù)據(jù)庫里存放,一般存放圖片、視頻的路徑,如果安全需要比較高的話,則放入數(shù)據(jù)庫。三、怎樣創(chuàng)建表--創(chuàng)建表
--學(xué)生表
create table student (xh number(4), --學(xué)號xm varchar2(20), --姓名sex char(2), --性別birthday date, --出生日期sal number(7,2) --獎(jiǎng)學(xué)金
);--班級表
create table class(classid number(2),cname varchar2(40)
);--修改表
--添加一個(gè)字段
sql>alter table student add (classid number(2));
--修改一個(gè)字段的長度
sql>alter table student modify (xm varchar2(30));
--修改字段的類型或是名字(不能有數(shù)據(jù)) 不建議做
sql>alter table student modify (xm char(30));
--刪除一個(gè)字段 不建議做(刪了之后,順序就變了。加就沒問題,應(yīng)該是加在后面)
sql>alter table student drop column sal;
--修改表的名字 很少有這種需求
sql>rename student to stu;--刪除表
sql>drop table student;--添加數(shù)據(jù)
--所有字段都插入數(shù)據(jù)
insert into student values ('a001', '張三', '男', '01-5 月-05', 10);
--oracle中默認(rèn)的日期格式‘dd-mon-yy’ dd 天 mon 月份 yy 2位的年 ‘09-6 月-99’ 1999年6月9日
--修改日期的默認(rèn)格式(臨時(shí)修改,數(shù)據(jù)庫重啟后仍為默認(rèn);如要修改需要修改注冊表)
alter session set nls_date_format ='yyyy-mm-dd';
--修改后,可以用我們熟悉的格式添加日期類型:
insert into student values ('a002', 'mike', '男', '1905-05-06', 10);
--插入部分字段
insert into student(xh, xm, sex) values ('a003', 'john', '女');
--插入空值
insert into student(xh, xm, sex, birthday) values ('a004', 'martin', '男', null);
--問題來了,如果你要查詢student表里birthday為null的記錄,怎么寫sql呢?
--錯(cuò)誤寫法:select * from student where birthday = null;
--正確寫法:select * from student where birthday is null;
--如果要查詢birthday不為null,則應(yīng)該這樣寫:
select * from student where birthday is not null;--修改數(shù)據(jù)
--修改一個(gè)字段
update student set sex = '女' where xh = 'a001';
--修改多個(gè)字段
update student set sex = '男', birthday = '1984-04-01' where xh = 'a001';
--修改含有null值的數(shù)據(jù)
不要用 = null 而是用 is null;
select * from student where birthday is null;--刪除數(shù)據(jù)
delete from student; --刪除所有記錄,表結(jié)構(gòu)還在,寫日志,可以恢復(fù)的,速度慢。
--delete的數(shù)據(jù)可以恢復(fù)。
savepoint a; --創(chuàng)建保存點(diǎn)
delete from student;
rollback to a; --恢復(fù)到保存點(diǎn)
一個(gè)有經(jīng)驗(yàn)的dba,在確保完成無誤的情況下要定期創(chuàng)建還原點(diǎn)。drop table student; --刪除表的結(jié)構(gòu)和數(shù)據(jù);
delete from student where xh = 'a001'; --刪除一條記錄;
truncate table student; --刪除表中的所有記錄,表結(jié)構(gòu)還在,不寫日志,無法找回刪除的記錄,速度快。
轉(zhuǎn)載于:https://www.cnblogs.com/Lightning-Kid/p/3863178.html
總結(jié)
以上是生活随笔為你收集整理的五、oracle 表的管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery-DOM
- 下一篇: cocostudio 实现换行功能的la