oracle中建立資料表入門篇

來源:互聯網
上載者:User

通過使用所選的工具,建立以下使用者:

使用者名稱 phpuser
口令 phpuserpw
系統許可權 CREATE TABLE

 代碼如下 複製代碼
CREATE VIEW
CREATE SEQUENCE
CREATE TRIGGER

角色 (Oracle Database 10.x) CONNECT

 代碼如下 複製代碼
RESOURCE

下面是一組用於建立該使用者的樣本 SQL 命令。這些命令假定資料庫具有 USERS 和 TEMP 資料表空間。

 
 代碼如下 複製代碼
drop user phpuser cascade;

create user phpuser identified by phpuserpw;

grant connect, resource to phpuser;

alter user phpuser default tablespace users temporary tablespace temp account unlock;
設計範例資料庫的結構
要排列和儲存所需的所有資料,您需要使用兩個表:
  • 一個是 wishers 表,用於儲存註冊使用者的名稱和口令
  • 另一個是 wishes 表,用於儲存心愿說明
wishers 表包含三個欄位:
  1. id - 許願者的唯一 ID。該欄位用作主鍵
  2. name
  3. 口令
wishes 表包含四個欄位:
  1. id - 心愿的唯一 ID。該欄位用作主鍵
  2. wisher_id - 心愿所屬的許願者的 ID。該欄位用作外鍵
  3. description
  4. due_date - 請求心愿時的日期
這些表通過許願者的 ID 相關聯。除了 wishes 表中的 due_date 以外,所有欄位都是必需的。
建立 Oracle 資料庫結構描述
  1. 以建立的使用者身份登入到資料庫。
    如果通過 NetBeans IDE 進行串連,請使用新使用者的名字和口令建立一個串連。確保選擇的架構具有與使用者相同的名稱。(請參見“串連到 Oracle 資料庫”教程的建立到 Oracle DB 的串連部分。)
  2. 要建立 wishers 表,請運行以下 SQL 查詢:
       
     代碼如下 複製代碼
    create table wishers (
    id number not null,
    name varchar2(50) unique not null,
    password varchar2(50) not null,
    constraint wishers_pk primary key(id)
    );
    要建立 wishes 表,請運行以下 SQL 查詢。請注意,將建立一個外鍵以將心愿與許願者相關聯。
  3.    
     代碼如下 複製代碼
    create table wishes (
    id number not null,
    wisher_id number not null,
    description varchar2(255) not null,
    due_date date,
    constraint wishes_pk primary key(id),
    constraint wishes_fk1 foreign key(wisher_id) references wishers(id)
    );
    驗證是否將新表添加到資料庫中。如果使用 NetBeans IDE 串連到資料庫,請轉至“服務”視窗中的 jdbc:oracle:thin:@localhost:1521:XE [PHPUSER 上的 phpuser] 串連節點。將在“表”節點中列出新表。(如果未顯示這些表,請按右鍵串連,然後選擇“重新整理”。)
注意:您可以在此處下載一組 SQL 命令以建立 Oracle 資料庫表。
添加序列和觸發器以增加 ID 值
在使用 Oracle 資料庫時,您必須指定一個序列以增加值。要在表中添加新成員時增加值,請添加一個觸發器。
  1. 要為 wisher 表添加序列,請運行以下 SQL 命令:
       
     代碼如下 複製代碼
    create sequence wishers_id_seq start with 1 increment by 1;
    要在添加新的許願者時在 wishers 表的 ID 列上觸發序列,請運行以下 SQL 命令:
  2.    
     代碼如下 複製代碼
    create or replace trigger wishers_insert
    before insert on wishers
    for each row
    begin
    select wishers_id_seq.nextval into :new.id from dual;
    end;
    /
  3. 為 wishes 表添加一個序列。
       
     代碼如下 複製代碼
    create sequence wishes_id_seq start with 1 increment by 1;
    添加一個觸發器,以在添加新的心愿時在 wishes 表的 ID 列上運行序列。
  4.    
     代碼如下 複製代碼
    create or replace trigger wishes_insert
    before insert on wishes
    for each row
    begin
    select wishes_id_seq.nextval into :new.id from dual;
    end;
    /
注意:您可以在此處下載一組 SQL 命令以建立 Oracle 資料庫表,包括序列和觸發器。
輸入測試資料
要測試應用程式,您需要使用資料庫中的某些資料。下面的樣本說明了如何添加兩個許願者和四個心愿。
  1. 添加一個名為 Tom 且口令為 tomcat 的許願者。
       
     代碼如下 複製代碼
    insert into wishers (name, password) values ('Tom','tomcat');
    添加一個名為 Jerry 且口令為 jerrymouse 的許願者。
  2.    
     代碼如下 複製代碼
    insert into wishers (name, password) values ('Jerry', 'jerrymouse');
    commit;
    添加心愿。
  3.    
     代碼如下 複製代碼
    insert into wishes (wisher_id, description, due_date) 
    values (1, 'Sausage', to_date('2008-04-01', 'YYYY-MM-DD');

    insert into wishes (wisher_id, description)
    values (1, 'Icecream');


    insert into wishes (wisher_id, description, due_date) values (2, 'Cheese', to_date('2008-05-01', 'YYYY-MM-DD'));

    insert into wishes (wisher_id, description)
    values (2, 'Candle');
    commit;
    驗證是否添加了測試資料。如果使用 NetBeans IDE 查看測試資料,請在相關表上單擊滑鼠右鍵,然後從操作功能表中選擇“查看資料”。

聯繫我們

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