Oracle(修改表結構和約束)


目標:

1.添加和修改列

2.添加,enabledisable,或者remove約束

3.刪除表

4.刪除表中所有數據並回到表定義的初始狀態(截斷表)

5.修改對象的名字

6.給對象添加注釋,從數據字典中查看注釋

用到的命令:

1.Alter table

1.添加和修改列

2.添加和刪除約束

3.enabledisable約束

2.drop table命令移除表中所有行和表結構

3.renametruncatecomment

4.當執行以上DDL語句時,事務自動提交

功能:

1.增加列

   語法:

alter table tb_name

add column datatype [default val] constraint .....

note:

1.如果添加not null(primary key約束要求值也不能為null)約束,需要保證當前表中沒有數據存在。

2.新添加的列,相當於表定義中最后一個定義的列。

    例如:

alter table test add name varchar2(10) default 'test' not null ;

alter table s_stu add (sname varchar2(20),sage number);

alter table husband add sage number constraint husband_sage_check check(sage<=100);

2.刪除列:

語法:alter table tableName drop column column_name;

例如:alter table test drop column name;

3.修改列屬性:(數據類型和約束)

語法:ALTER TABLE table

  MODIFY (column datatype [DEFAULT expr][NOT NULL]

[, column datatype]...);

note

修改列的規則:

1.可以增加字段的寬度或者精度

2.如果列的值為null或者表中沒有數據,可以降低寬度和精度

3.給當前列,后續添加的數據指定默認值。

4.當且僅當當前列中沒有null值時,可以定義當前列為not null.

5.當前列中的值為null時,可以修改列的數據類型

6.如果需要給某個字段添加not null約束,只能使用modify

例如:

alter table test modify id number constraint test_pk_id primary key;

alter table test modify id char(20);

4.增加約束

語法:alter table tb_name add 約束的完整定義

note

1.只能增加能夠使用表級約束的約束

2.不能修改約束

例如:

alter table test add constraint test_pk_id primary key(id);

alter table test add check(gender in ('F','M'));

5.刪除約束:

語法:alter table tb_name drop 約束名。

例如:

alter table test drop constraint test_pk_id;

刪除組件約束時,同時刪除和他依賴的外鍵約束

 alter table test drop constraint test_pk_id cascade;

6.使一個約束失效:

    語法:alter table tb_name disable constraint constraint_name [cascade];

note:添加cascade表明要讓所有的依賴約束都失效。

7.是一個約束生效:

    語法:alter table tb_name enable constraint constraint_name;

note

1.當啟用uniqueprimary key約束時,會自動創建索引。

例如:alter table test enable constraint test_id_pk;

8.刪除表:

   drop table tb_name [cascade constraint];

   note

1.刪除表中所有數據

2.所有的索引被刪除

3.使用cascade constraint,級聯刪除所有的依賴完整性約束

   例如:

drop table test cascade constraint;

刪除之后,可以通過:

select column_name,constraint_name

from user_cons_columns

查看是否約束還在。

9.重命名:rename

重命名表:

rename old_tb_name to new_tb_name;

重命名列:

alter table tb_name rename column old_col_name to new_col_name;

note:

1.重命名可以用來修改tableviewsequencesynonym

2.只有是這個對象的擁有者,才能重命名。

例如:

rename emp to emp2;  將表名重n名為emp2

 alter table emp rename column id to eid;

10.截斷表:truncate

語法:truncate table tb_name

note:

1.清空表記錄

2.釋放當前表所占用的表空間。返回建表初始狀態

3.是一個DDL命令。

4.一旦刪除,事務不能回滾。

例如:truncate table emp;

 

deletetruncate的比較:

delete:可以指定刪除某些列,也可以清空表,但是不釋放表空間,在事務沒有提交之前可以回滾。

truncate:只能清空表,釋放表空間,不能回滾。

11.給表加注釋:comments

語法:

comment on table talbe_name is '注釋內容'

comment on column table_name.column_name is '注釋內容';

note:

1.添加的注釋可以在如下數據字典中查看

ALL_COL_COMMENTS

USER_COL_COMMENTS

ALL_TAB_COMMENTS

USER_TAB_COMMENTS

例如:

comment on table emp is '測試';

comment on column emp.eid  is 'haha';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM