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、修改表:
- 添加列:ALTER TABLE order_status2 ADD modified_by INTEGER;
- 修改列:ALTER TABLE order_status2 MODIFY status VARCHAR(20);
- 刪除列:ALTER TABLE order_status2 DROP COLUMN status;
- 添加約束: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)】
- 刪除約束:ALTER TABLE order_status2 DROP CONSTRAINT pk_order_status2;
- 啓用約束:ALTER TABLE order_status2 ENABLE CONSTRAINT pk_order_status2;
- 禁用約束:ALTER TABLE order_status2 DISABLE CONSTRAINT pk_order_status2;
3、相關係統視圖:
可以通過相關的系統視圖來查看錶和約束的定義。
- user_tables:用戶表的相關信息
- user_tab_columns:用戶表的列信息
- user_constraints:用戶表約束相關信息