oracle 表約束的新增 修改以及約束的禁用啟用

2021-06-05 17:48:36 字數 3526 閱讀 7443

--禁用所有外來鍵約束的sql**

select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints where constraint_type='r'

--啟用所有外來鍵約束的sql**

select 'alter table '||table_name||' enable constraint '||constraint_name||';' from user_constraints where constraint_type='r'  

執行查詢出外鍵,然後執行即可

以下提到的內容都很簡單,所以不詳細說明,僅羅列一些語法:

一、主外來鍵的新增、刪除

1、向表中新增外來鍵約束,把emp表的deptno欄位設定為emp表的外來鍵,引用自dept表

alter table emp  add constraint fk_test foreign key(deptno) references dept(deptno);

2、向表中新增主鍵約束 alter table emp add constraint pk_emp primary key(empno);

3、建立表的同時建立主鍵約束

(1)無命名 create table emp( empno int  primary key not null, ename varchar(20), deptno int);

(2)有命名 create table emp( empno int , ename varchar(20), deptno int , constraint pk_emp primary key(empno));

4、刪除表中已有的主鍵約束

(1)無命名可用 select * from user_cons_columns;

如:select * from user_cons_columns where table_name='emp';

查詢表中主鍵名稱得emp表中的主鍵名為pk_emp

alter table student drop constraint  pk_emp;

(2)有命名 alter table emp drop constraint pk_emp;

二、更改表的結構

1.編輯表的字段

修改乙個列的資料型別(一般限於修改長度,修改為乙個不同型別時有諸多限制):

語法:alter table 表名 modify(列名,資料型別);

eg1:

alter table skate_test modify (author number(10,0) )

在修改列的長度時候,只能編輯比現有字段實際存的長度還要大,否則提示下面的錯誤:

ora-01441: 無法減小列長度, 因為一些值過大

eg2:

alter table skate_test modify (author varchar2(10) )

在修改列的資料型別的時候,所修改的列必須為空,否則提示下面的錯誤:

ora-01439: 要更改資料型別, 則要修改的列必須為空

2.增加乙個列

語法:alter table 表名 add(列名,資料型別);

eg1:

alter table skate_test add(author number(38,0) not null);

3.給列改名:

語法:alter table 表名 rename column 當前列名 to 新列名;

eg1:

alter table skate_test rename column author to authorer_new

4.刪除乙個列

語法:alter table 表名 drop column 列名;

eg1:

alter table skate_test drop column author

5.將乙個表改名

語法:alter table 當前表名 rename to 新錶名;

eg1:

alter table skate_test rename to test_sakte

5.給表加注釋

comment column on 表名.列名 is '注釋內容';   //修改表的列的注釋

comment on table movo_new.test_sakte  is '注釋內容';  //修改表的注釋

三、主鍵、外來鍵等約束的啟用與禁用

這裡以外鍵為例來說明

執行下面的查詢語句,我們會得到當前使用者下所有可以被查詢到的表的外來鍵,並生相應的修改語句。我們指需要把生成的結果copy出來執行就ok了。

不過對於alter語句的使用還是要謹慎,使用之前需要考慮清楚。

--刪除所有外來鍵約束 的sql**

select 'alter table '||table_name||' drop constraint '||constraint_name||';' from user_constraints whereconstraint_type='r'

--alter table emp drop constraint fk_test;

--禁用所有外來鍵約束的sql**

select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints where constraint_type='r'

--alter table emp disable constraint fk_test;

--啟用所有外來鍵約束的sql**

select 'alter table '||table_name||' enable constraint '||constraint_name||';' from user_constraints where constraint_type='r'  

--alter table emp enable constraint fk_test;

這裡有一點需要注意,在上面的查詢語句中where條件後帶的約束型別 constraint_type='r' ,我們這裡指定的是'r',可以猜想到,出來r之外還有其他的約束型別。

下面列出constraint_type的其他幾種型別及相應的含義:

type code

type description

acts on level

ccheck on a table

column

oread only on a view

object

pprimary key

object

rreferential aka foreign key

column

uunique key

column

vcheck option on a view

object

所以,我們需要禁用哪類約束,就將查詢條件做相應的調整就好。

2 修改表和新增約束

目的格式舉例修 改表結構 1.修改列 字段 的資料型別 alter table 表名 modify 列名 資料型別 alter table teachers modify tid varchar2 5 2.增加乙個列 alter table 表名 add 列名 資料型別 alter table te...

修改以及設計好的表

1.修改表字段 use entrancevista alter table ry stay info alter column wza nvarchar 50 alter table ry stay info alter column wzb nvarchar 50 2.新增主鍵 新增主鍵之前先要讓...

Oracle 建表 約束 修改列

增加列 alter table name add columnname type 或者alter table name add columnname type 修改列名 alter table name rename column name1 to name2 刪除列alter table name...