Oracle對資料表的基本操作

來源:互聯網
上載者:User

標籤:uid   logs   使用   isa   select   穩定性   外鍵   sid   名稱   

選擇主鍵的原則:

  • 最少性
  • 盡量選擇使用單個鍵作為主鍵
  • 穩定性
  • 盡量選擇數值更新少的列作為主鍵

1、建立資料表(CREATE TABLE)
--建立資料表Studentcreate table Student(SID number(2) constraint PK_SID primary key,--指定該列為主鍵列,並指定主鍵名為PK_SIDSName varchar2(16) not null)--建立資料表Classcreate table Class(CID number(2) constraint PK_CID primary key,--指定該列為主鍵列,並指定主鍵名為PK_CIDCName varchar2(16) not null)
2、重新命名、刪除資料表
--將資料表Student重新命名為Stualter table Student rename to Stu;
--刪除資料表Student
drop table Student;
3、添加、重新命名、刪除欄位、修改欄位資料類型
--為資料表Student添加欄位SGender和SCIDalter table Student add (SGender char(2));alter table Student add (SCID number(2));--刪除資料表Student中的欄位SGenderalter table Student drop column SGender;--將資料表Student中的欄位SID重新命名為StuIDalter table Student rename column SID to StuID;--修改資料表Student中欄位SID的資料類型alter table Student modify SID number(2);
4、添加、刪除欄位約束
--為資料表Student中的欄位SGender添加約束,並指定該約束的名稱為ch_gender,指定該列的值只能是‘男‘或‘女‘alter table Student add constraint ch_gender check(SGender=‘男‘ or SGender=‘女‘);--刪除資料表Student中約束名為ch_gender的約束alter table Student drop constraint ch_gender;
5、查看、添加、重新命名、刪除、禁用、啟用主鍵
--查看資料表Student中已定義的主鍵select * from user_cons_columns where table_name=‘STUDENT‘;--將資料表Student中的欄位SName設為主鍵列,並指定該主鍵的名稱為PK_Namealter table Student add constraint PK_Name primary key(SName);--刪除主鍵名為PK_Name的主鍵alter table Student drop constraint PK_Name;--將主鍵名PK_StuID重新命名為PK_SIDalter table Student rename constraint PK_StuID to PK_SID;--禁用主鍵alter table Student disable primary key;--啟用主鍵alter table Student enable primary key;
 6、查看、添加、重新命名、刪除、禁用、啟用外鍵
--查看資料表中已存在的外鍵select owner,constraint_name from user_constraints where constraint_type=‘R‘--P 主鍵 R 外鍵--添加外鍵alter table Student add constraint FK_SCID foreign key(SCID) references Class(CID)--刪除外鍵alter table Student drop constraint FK_SCID--重新命名外鍵alter table Student rename constraint FK_SClassID to FK_SCID--禁用外鍵alter table Student disable constraint FK_SCID--啟用外鍵alter table Student enable constraint FK_SCID

 

 




 

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.