Oracle數據庫表管理

1、創建表:

CREATE [GLOBAL TEMPRARY] TABLE table_name (

      column_name type [CONSTRAINT constraint_def DEFAULT default_exp]

      [,column_name type [CONSTRAINT constraint_def DEFAULT default_exp] ...]

)

[ON COMMIT {DELETE | PRESERVE} ROWS]

TABLESPACE tab_space;            

  • GLOBAL TEMPRARY:臨時表。有效期有ON COMMIT指定。
  • table_name:表名
  • columne_name:列名
  • type:列類型
  • constraint_def:列約束
  • default_exp:列默認值
  • ON COMMIT:指定臨時表的有效期。DELETE:臨時表數據在事務的末尾被刪掉。PRESERVE:臨時表數據在會話末尾被刪掉。
  • tab_space:所屬表空間,如果沒有,則爲默認表空間。

具體示例:

CREATE TABLE order_status2 (

      id INTEGER CONSTRAINT order_status2_pk PRIMARY KEY,

      status VARCHAR2(10),

      last_modified DATE DEFAULT SYSDATE

);

2、修改表:

  1. 添加列:ALTER TABLE order_status2 ADD modified_by INTEGER;
  2. 修改列:ALTER TABLE order_status2 MODIFY status VARCHAR(20);
  3. 刪除列:ALTER TABLE order_status2 DROP COLUMN status;
  4. 添加約束:ALTER TABLE order_status2 ADD CONSTRAINT check_name check_def;(ALTER TABLE order_status2 ADD CONSTRAINT ck_order_status CHECK (status in ('status1','status2')))【添加主鍵:ALTER TABLE order_status2 ADD CONSTRAINT pk_order_status2 PRIMARY KEY (id)】【添加外鍵:ALTER TABLE order_status2 ADD CONSTRAINT fk_order_status2_another_tbl FOREIGN KEY (status) REFERENCES another_tbl(column_name)】
  5. 刪除約束:ALTER TABLE order_status2 DROP CONSTRAINT pk_order_status2;
  6. 啓用約束:ALTER TABLE order_status2 ENABLE CONSTRAINT pk_order_status2;
  7. 禁用約束:ALTER TABLE order_status2 DISABLE CONSTRAINT pk_order_status2;

3、相關係統視圖:

     可以通過相關的系統視圖來查看錶和約束的定義。

  1. user_tables:用戶表的相關信息
  2. user_tab_columns:用戶表的列信息
  3. user_constraints:用戶表約束相關信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章