1、表明和列的命名規則
(1)必須以字母開頭
(2)長度不能超過30字符
(3)不能使用oracle的保留字
(4)只能使用如下字符:A-Z,a-z,0-9,$,#等
2、oracle支持的數據類型
(1)字符型
char 定長(最大2000字符)
eg、char(10)‘小韓’ 前四個字符放‘小韓’,後添6個空格補全,浪費空間,但是速度快
varchar2 變長(最大4000字符)
eg、varchar2
(10)‘小韓’ ,只給其分配4個字符,可以節省空間,但是速度慢
clob(character large object)字符型大對象(最大4G)
(2)數字型 number(範圍是 -10的負38次方~10的38次方),可以表示整數也可以表示小數
eg、number(5,2)表示一個小數有5位有效位,2位小數,範圍是-999.99~999.99
number(5)表示一個五位整數,範圍是 -99999~99999
(3)日期類型 data和timestamp
data:包含年月日和時分秒
timestamp:是對data數據類型的擴展,更加精確
(4)圖片類型 blob:二進制數據,可以存放圖片或者聲音,4G
一般來講,項目中的圖片和視頻不會放到數據庫中,但是如果這個圖片或者視頻需要安全性更高的話,需要放到數據庫中。
3、創建新的oracle表: create table classes( classId number(2),cname varchar2(40));
建學生表
create table student(---表名
xh numer(4),---學號
xm varchar2(20),---姓名
sex char(2),---性別
birthday date,---出生日期
sal number(7,2)---獎學金
);
建班級表:
create table classes(
classId number(2),
cname varchar2(40)
);
(1)查看錶的信息:desc stu;//查看某張表
(2)添加一個字段:alter table
student add(classId number(2));
(3)修改字段的長度:alter table
student modify(xm varchar2(30));
(4)修改字段的類型或者是名字(表中不能有數據):
alter table student modify(xm char(30));
這個操作比較危險,當表中有數據的時候最好不要這樣做,這張表是空的時候可以這樣做。
(5)刪除一個字段: alter table student drop column sal ;
(6)修改表的名字: rename student to stu;
(7)刪除表: drop table stu;
4、向表中添加數據
(1)向表中添加數據
insert into stu values(1,'小貓','女','11-12月-1999',234.7,12);
oracle 默認的日期格式是‘DD-MM-YY’,如果想用年月日,需要進行修改,
alter session set nls_date_format = 'yyyy-mm-dd';//這個時候在輸入年月日就可以了
(2) 向表中添加部分字段
insert into stu(num,name,sex) values(1,'小貓','女');
(3)插入空值
insert into stu (num,name,sex,birth) values(1,'小貓','女',null);
如果要查詢sex是空的需要這樣寫:
select * from stu where sex is null;
如果要查sex非空的時候需要這樣寫:
select * from stu where
sex is not null;
5、修改數據
(1)修改一個字段
update stu set sex='女' where num=1;
(2)修改多個字段
update
stu set sex='男', birth=‘1980-09-21’ where num='1';
(3)修改含有null的字段
update
stu
set sex='男'
where num is null;
(4)修改不含有null的字段
update stu set sex='男' where num is not null;6、刪除數據
(1)只刪除數據不刪除表:delete from stu;
刪除所有記錄,但是表結構還在,可以恢復數據的,但是速度慢
savepoint a;//保存點。在刪除數據之前寫一句這個,刪除之後就可以將數據找回來
delete from stu;
rollback to a;
設置保存點: savepoint a;
回到保存點: rollback to a;
保存點只有一個,新的保存點會自動覆蓋舊的保存點
(2)數據和表都刪除:drop table stu;
(3)刪除一條數據:delete from stu where num=1;
(4)刪除表中所有數據但是表結構還在:truncate table stu;
這種方法無法找回刪除的記錄,但是速度快。