1. 表名和列名命名規則
(1). 必須以字母開頭;
(2). 長度不能超過 30 個字符;
(3). 不能使用 oracle 保留字;
(4). 只能使用如下字符 A-Z,a-z,0-9,$,# 等。
2. Oracle中的數據類型
(1). 字符類
char:定長,最大支持 2000 字符,查詢速度快(採用完全匹配方式查詢)。
varchar:變長,最大支持 4000 字符,查詢速度慢(採用一位一位匹配方式查詢)。
clob:字符型大對象,最大支持 4G。
(2). 數字類
number:範圍由 -10 的 38 次方到 10 的 38 次方。可以表示整數,也可以表示小數。
例:number(5,2),表示總共有5位有效數字,2 位小數,範圍從 -999.99 到 999.99。
number(5),表示 5 位整數,範圍從 -99999 到 99999。
(3). 日期類型
date:包含年月日和時分秒,oracle 中默認格式是 24-6月-2010。
timestamp:這是 oracle 對時間類型的擴展,可以精確到毫秒。
(4). 圖片
blob:二進制數據,可以存放圖片、聲音,最大支持 4G,一般情況下,在真實項目中是不會把圖片和聲音往數據庫裏存放,一般存放圖片或者聲音的路徑,如果安全性要求比較高的話,則放入數據庫。
3. 創建表
創建表使用 craete table 語句。
例:create table student (
id number(4),
name varchar2(30),
sex char(2),
birthday date,
sal number(7,2)
);
4. 修改表
(1). 添加一個字段
例:alter table student add (classid number(2));
(2). 修改字段的長度
例:alter table student modify (name varchar2(50));
(3). 修改字段的類型或是名字(不能有數據)
例:alter table student modify (name char(30));
(4). 刪除字段
例:alter table student drop column sal;
(5). 修改表的名字
例:rename student to stu;
5. 刪除表
刪除表使用 drop table 語句。
例:drop table student;
6. 插入數據
(1). 所有字段都插入數據
例:insert into student values (1001, 'skycloud', '男', '01-6月-88', 2000);
oracle中默認的日期格式是 'dd-mon-yy'; dd是日子(天),mon是月份,yy是2位的年份。
修改日期的默認格式(臨時修改,數據庫重啓後仍爲默認,如要修改需要修改註冊表)
例:alter session set NLS_DATE_FORMAT = 'yyyy-mm-dd';
修改後,可以使用熟悉的日期格式插入日期數據
例:insert into student values (1002, 'skycloud1', '男', '1987-07-01', 1500);
(2). 插入部分數據
例:insert into student (id, name, sex, birthday) values (1003, 'skycloud2', '女', '20-12-88', null);
如要要查詢 student 表中生日爲 null 的記錄,該怎麼查詢?
錯誤案例:select * from student where birthday = null;
正確案例:select * from student where birthday is null;
如果要查詢 student 表中生日不爲 null 的記錄,該怎麼查詢?
select * from student where birthday is not null;
7. 修改數據
(1). 修改一個字段
例:update student set sex = '女' where id = 1002;
(2). 修改多個字段
例:update student set name='skycloud3', sex='女', birthday='01-07-1987' where id=1002;
8. 刪除數據
(1). delete from student; -- 刪除所有記錄,表結構還在,寫日誌,可以恢復的,速度慢。
(2). delete from student where id=1003; -- 刪除一條記錄。
(3). drop table student; -- 刪除表結構和表數據。
(4). truncate table student; -- 刪除表中所有記錄,表結構還在,不寫日誌,無法找回刪除的記錄,速度快。
(5). savepoint a; -- 創建保存點。
(6). rollback to a; -- 恢復到保存點a。
一個有經驗的 dba,在確保無誤的情況下,要經常創建保存點。