Oracle起步學習(四)--表的管理,操作

一、Oracle表的管理

1、表名和列的命名規則

a、必須以字母開頭

b、長度不能超過30個字符

c、不能使用Oracle的保留字

d、只能使用如下字符: A-Z,,a-z, 0-9,$,#等

2、數據類型(char、varchar)

a、字符型

i、char定長最大2000字符(雖然比較佔空間,但是效率極快

eg:char(10) '小韓' 前四個字符放‘小韓’,剩下的6個空間補齊空格。

ii、varchar2(20)  變長 最大4000字符

iii、clob  字符型大對象 最大4G

b、數字類型

i、number 範圍  -10的38次方  到  10的38次方,可以表示整數,也可以表示小數

ii、number(5,2) 表示一個小數有5位有效數,2位小數;範圍 :  -999.99 --- 999.99

iii、number(5) 表示一個五位整數;範圍: -99999 ---- 99999


c、日期類型

i、date 包含年月日、時分秒

ii、timestamp  這個是Oracle對 date數據類型的擴展、更加精確


d、圖片

i、blob 二進制數據,可以存放圖片/聲音  4G (儘量不要放入數據庫中,存放地址即可,除非爲了高度機密)


2、建表

--學生表  建表

a、手動添加

create table student ( --表名

xh number(4), --學號

xm varchar2(20), --姓名

sex char(2), --性別

birthday date, --出生年月

sal number(7,2) --獎學金

);


b、工具添加



創建表2 

create table classes(

classId number(2),

cname varchar2(40)

);


1.1、添加一個字段 : alter  tablestudentadd(classId bumber(2));

1.2、修改字段長度 :alter tablestudentmodify(xm varchar2(30));

1.3、修改字段的類型/或名字(不能有數據):alter table studentmodify(xm char (20));

1.4、刪除一個字段(不要輕易使用):alter tablestudentdrop column sal;

1.5、刪除表:drop table student

1.6、修改表名:rename student to stu;


2、添加數據

2.1所有字段的插入

insert into studentvalues(1001,'張三',‘男’,‘01-5月-05’,123,1);  --注意了:日期格式:要帶橫線哦。



2.2 或者設定格式: alter session set nls_date_format='yyyy-mm-dd';(查看錶結構)

insert into student values (1001,'張三',‘男’,‘2005-05-01’,123,1);  --注意了:日期格式:要帶橫線哦。


2.3添加部分

insert into student (xh,xm,sex)values (1002,'李四',‘男’);  

2.4添加空值

insert into student (xh,xm,sex,birthday) values (1005,‘花花',‘男’,null);  


tip:  查詢 生日爲空的人:

select * from student where birthdayis null;    --注意了select * from student wherebirthday is null;  這裏可不是   birthday = null。

2.5修改一個字段內容

updatestudentsetsex=’女' where xh=1001;

2.6修改多個字段內容

update student set sex=’女',xm='李二' where xh=1001;


(2014-7-9 00:27:26  8/31 00:39 未完待續)


3、保存會滾點:savepoint  fristname;

4、刪除數據(表結構還存在):delete from tablename;

5、回滾操作:roollback  to fristname;

6、刪除表(表結構也刪除了):drop table student

7、刪除數據(表結構還存在):truncate table student;

注意:這個區別於 delete from   於,truncate table不寫日誌,較快



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章