1. 程式人生 > >oracle新增表約束的2種方法

oracle新增表約束的2種方法

方法1:在列的級別上定義約束

create table product (

Pname varchar2(20) unique,

Pdate date not null,

PID number(5) primary key,check(PID>10000 AND PID<=99999),

Ploc char(30) default '北京')

要點就是在列的結尾直接新增,如果同一列有多個約束,通過逗號來分隔

有個問題,通過數字字典來查詢約束,我們得到的是系統提供的名字並且約束的型別不好區分。

SQL> select constraint_name,constraint_type from user_constraints

where table_name = 'PRODUCT';

CONSTRAINT_NAME                                         CONSTRAINT_TYPE
------------------------------------------------------------ --                                                                                                                                        
SYS_C009539                                                  C                                                                                                                                         
SYS_C009540                                                  C                                                                                                                                         
SYS_C009541                                                  P                                                                                                                                         
SYS_C009542                                                  U

U:表示unique

P:primary key

C:check或者default

檢視列的預設值:

SQL> select column_name,datA_dEfault from useR_tab_columns where table_name = 'PRODUCT';

COLUMN_NAME                                                  DATA_DEFAULT                                                                                                                              
------------------------------------------------------------ --------------------------------------------------------------------------------                                                          
PNAME                                                                                                                                                                                                  
PDATE                                                                                                                                                                                                  
PID                                                                                                                                                                                                    
PLOC                                                         '北京'  

方法二在表級別上定義約束:

Create table product(

PID number(5),

PNAME varchar2(20),

PDATE date constraint NN_PDATE_PRODUCT not null,

PLOC char(30) default ‘北京’,

FKDEPTNO number(5),

Constraint UK_PNAME_PRODUCT unique(PNAME),

Constraint PK_PID_PRODUCT primary key(PID),

Constraint CK_PID_PRODUCT check(PID>10000 AND PID<9999),

Constraint FK_DEPTNO foreign key (FKDEPTNO) references dept(DEPTNO)

);

SQL> select constraint_name,constraint_type from user_constraints where table_name = 'PRODUCT';

CONSTRAINT_NAME                                              CO                                                                                                                                        
------------------------------------------------------------ --                                                                                                                                        
NN_PDATE_PRODUCT                                             C                                                                                                                                         
CK_PID_PRODUCT                                               C                                                                                                                                         
PK_PID_PRODUCT                                               P                                                                                                                                         
UK_PNAME_PRODUCT                                             U                                                                                                                                         
FK_DEPTNO_PRODUCT                                            R    

刪除約束

Alter table product drop primary key;

Alter table product drop constraint PK_PID_PRODUCT;

Alter table product drop unique (PNAME);

Alter table product modify(PDATE,null);

Alter table product modify(PLOC,default null)/modify(PLOC ,default ‘威海’);--更改預設值

Alter table product drop constraint FK_DEPTNO;

外來鍵、check約束只能使用約束名來刪除,其他約束如果有約束名也可以使用約束名來刪除

增加約束

Alter table product add primary key (PID);

Alter table product add constraint PK_PID primary key(PID);

Alter table product add unique(PNAME);

Alter table product modify(PDATE, not null);

Alter table product modify(PLOC ,default ‘威海’);