五、oracle 表的管理

來源:互聯網
上載者:User

標籤:

一、表名和列名的命名規則
1)、必須以字母開頭
2)、長度不能超過30個字元
3)、不能使用oracle的保留字
4)、只能使用如下字元 a-z,a-z,0-9,$,#等

 

二、資料類型
1)、字元類
char 長度固定,最多容納2000個字元。
例子:char(10) ‘小韓’前四個字元放‘小韓’,後添6個空格補全,如‘小韓      ’
varchar2(20) 長度可變,最多容納4000個字元。
例子:varchar2(10) ‘小韓’ oracle分配四個字元。這樣可以節省空間的。
clob(character large object) 字元型大對象,最多容納4g
char 查詢的速度極快浪費空間,適合查詢比較頻繁的資料欄位。
varchar 節省空間的
2)、數字型
number範圍-10的38次方到10的38次方,可以表示整數,也可以表示小數
number(5,2)表示一位小數有5位有效數,2位小數;範圍:-999.99 到999.99
number(5)表示一個5位整數;範圍99999到-99999
3)、日期類型
date 包含年月日和時分秒 oracle預設格式1-1月-1999
timestamp 這是oracle9i對date資料類型的擴充。可以精確到毫秒。
4)、圖片
blob 位元據,可以存放圖片/聲音4g;一般來講,在真實項目中是不會把圖片和聲音真的往資料庫裡存放,一般存放圖片、視頻的路徑,如果安全需要比較高的話,則放入資料庫。

 

三、怎樣建立表

--建立表
--學生表
create table student (
   xh number(4), --學號
   xm varchar2(20), --姓名
   sex char(2), --性別
   birthday date, --出生日期
   sal number(7,2) --獎學金
);

--班級表
create table class(
  classid number(2),
  cname varchar2(40)
);

      

--修改表
--添加一個欄位
sql>alter table student add (classid number(2));
--修改一個欄位的長度
sql>alter table student modify (xm varchar2(30));
--修改欄位的類型或是名字(不能有資料) 不建議做
sql>alter table student modify (xm char(30));
--刪除一個欄位 不建議做(刪了之後,順序就變了。加就沒問題,應該是加在後面)
sql>alter table student drop column sal;
--修改表的名字 很少有這種需求
sql>rename student to stu;

          
--刪除表
sql>drop table student;

           

--添加資料
--所有欄位都插入資料
insert into student values (‘a001‘, ‘張三‘, ‘男‘, ‘01-5 月-05‘, 10);
--oracle中預設的日期格式‘dd-mon-yy’ dd 天 mon 月份 yy 2位的年 ‘09-6 月-99’ 1999年6月9日
--修改日期的預設格式(臨時修改,資料庫重啟後仍為預設;如要修改需要修改註冊表)
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呢?
--錯誤寫法:select * from student where birthday = null;
--正確寫法:select * from student where birthday is null;
--如果要查詢birthday不為null,則應該這樣寫:
select * from student where birthday is not null;

         

--修改資料
--修改一個欄位
update student set sex = ‘女‘ where xh = ‘a001‘;
--修改多個欄位
update student set sex = ‘男‘, birthday = ‘1984-04-01‘ where xh = ‘a001‘;
--修改含有null值的資料
不要用 = null 而是用 is null;
select * from student where birthday is null;

        

--刪除資料
delete from student; --刪除所有記錄,表結構還在,寫日誌,可以恢複的,速度慢。
--delete的資料可以恢複。
savepoint a; --建立儲存點
delete from student;
rollback to a; --恢複到儲存點
一個有經驗的dba,在確保完成無誤的情況下要定期建立還原點。

drop table student; --刪除表的結構和資料;
delete from student where xh = ‘a001‘; --刪除一條記錄;
truncate table student; --刪除表中的所有記錄,表結構還在,不寫日誌,無法找回刪除的記錄,速度快。


五、oracle 表的管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.