oracle 資料庫基本操作——資料定義語言 (Data Definition Language)

來源:互聯網
上載者:User

標籤:str   index   foreign   span   www.   除了   查詢   錯誤   使用者   

  熟悉Oracle上機環境及Oracle用戶端的配置;熟練掌握和使用DDL語言,建立、修改和刪除資料庫表、主鍵、外鍵約束關係和索引。

  1. (建立資料庫表)  建立教學資料庫的四個資料庫表,其中Student表中不包含SSEX(C,2) 欄位,Sname 欄位為Sname(C,8)且可為空白。
    create table student   (sno char(5),    sname varchar2(8),    sage number(2) check(age between 12 and 60),    sdept char(2),    sclass char(2),    constraint student_pk primary key(sno));
    create table course   (cno char(3) not null primary key,    cname char(16),    ctime number(3));
    create table score(   Sno char(5),    cno char(3),    grade number(3),    constraint score_pk primary key(sno,cno),    constraint s_sno_fk foreign key(sno) references student(sno),    constraint s_cno_fk foreign key(cno) references course(cno),    constraint score_ck1 check (grade >= 0 and grade <= 100));
    create table teach(    tname char(8) not null,    tsex char(2) not null,    cno char(3) not null ,    tdate date not null,    tdept char(2) not null,    constraint teach_pk primary key(cno),    constraint teach_cno_fk foreign key(cno) references course(cno));
  2. (修改資料庫表)  在Student表中增加SEX(C,2) 欄位。
    alter table studentadd(sex char(2)); 
  3. (修改列名)  將Student表中列名SEX修改為SSEX。
    alter table student rename column sex to ssex;
    modify(ssex char(2) check(ssex in (‘男‘,‘女‘)));
  4. (修改資料庫表)  將Student表中把Sname 欄位修改為Sname(C,10)且為非空。
    alter table studentmodify(sname char(10)not null);
  5. (建立索引)  為Score表按課程號升序、分數降序建立索引,索引名為SC_GRADE。
    create index sc_grade on score(cno,grade desc);
  6. (刪除索引)  刪除索引SC_GRADE。
    drop index sc_grade;
  7. (建立資料庫表)  建立資料庫表S1(SNO,SNAME,SD,SA),其欄位類型定義與Student表中的相應欄位(SNO,SNAME,SDEPT,SAGE)的資料類型定義相同。
    create table s1(
      sno char(5) not null,
      sname char(10) not null unique,
      sd char(2),sa number(2));
  8. (修改資料庫表)  刪除成績表Score的參照完整性條件約束關係。
    alter table score drop constraint s_sno_fk;
    alter table score drop constraint s_cno_fk;
  9. (修改資料庫表)  添加成績表Score的參照完整性條件約束關係。
    alter table Score add(constraint s_sno_fk foreign key(sno) references student(sno),  constraint s_cno_fk  foreign key(cno) references course(cno));
  10. (修改資料庫表名)  將資料庫表S1改名為Student_Temp。

     rename S1 to student_temp;
  11. 查看錶的約束條件
    select constraint_name, table_name, r_owner, r_constraint_namefrom all_constraintswhere table_name = ‘score‘;
  12. 刪除表中的列
    alter table studentdrop column spec;
  13. 查看使用者下所有的表名
    select table_name from user_tables;  //目前使用者的表 select table_name from all_tables;  //所有使用者的表 select table_name from dba_tables;  //包括系統資料表 select * from user_indexes //可以查詢出所有的使用者表索引
  14. 查看錶中所有的列名
  15. select column_name,data_type ,data_length,data_precision,data_scale from user_tab_columns where table_name=‘STUDENT‘;//表名必須大寫
    //或者
    desc student;

  在定義外鍵約束條件時,不能把其他表中沒有的屬性定義在本表的外鍵中,否則會生辰一個錯誤;

  在建表時,因為約束條件的名稱被重複定義,導致表建不起來:改進方法,將課本上的約束條件名稱家上表名首碼,避免重複性定義問題;

  經常會遺漏分號,導致cmd中格式髒亂;

  在設定一個屬性的類型時,如果設定為not null則不能再更改它的屬性為null;

  在oracle中漢字佔得是三個位元組,所以姓名出現四個字就超出了範圍,需要修改其範圍,性別應該修改為3個位元組;

  姓名不可以設定為not null unique ,如果出現重名就會發生錯誤,因為當時設定了unique最後還得重建表,刪除此約束條件;

 

刪除表資料有兩種方法:delete和truncate。

delete的用法如下:

delete from <表名> [where條件]

truncate的用法如下:

truncate table <表名>

delete和truncate的區別如下:

1、delete可以刪除表中的一條或多條資料,也可以刪除全部資料;而truncate只能將表中的全部資料刪除。

2、delete刪除表資料後,識別欄位不能複用。也就是說如果你把id=10(假如id是識別欄位)的那行資料刪除了,你也不可能再插入一條資料讓id=10.

3、truncate刪除表資料後,標識重新恢複初始狀態。預設為初始值為1,也就是說,truncate之後,再插入一條資料,id=1.

 但是在使用truncate刪除資料時可能受到外鍵的限制,必須從子集開始刪除,才可以刪除資料;

 

參考:http://www.cnblogs.com/laipDIDI/articles/2615210.html
http://www.jb51.net/article/82660.htm

 

 

oracle 資料庫基本操作——資料定義語言 (Data Definition Language)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.