Oracle Check約束

最近做一份數據庫的任務,發現MySQL中不支持在創建表定義字段時使用check約束,如果要在MySQL中使用類似字段約束取值,有兩種方式:1.枚舉enum,2.創建觸發器

MySQL關於check約束無效的解決辦法

下面正式記錄一下,Oracle數據庫各種各樣的check約束的使用方法

引用: https://www.cnblogs.com/ChineseIntelligentLanguage/p/6513282.html

1.檢查約束 ( check )
  某列取值範圍限制、格式限制等
  
2.檢查只能是男或者女

create table test29(         
	id number primary key, 
    sex varchar2(2) check(sex in ('男,女'))  
 );   

create table test30(         
     id number primary key,         
     sex varchar2(2) check(sex ='男' or sex='女')  
);    

create table test31(         
     id number primary key,         
     sex varchar2(2)  
   );  

alter table test31 add constraint chkk check (sex ='男' or sex='女');  
alter table test31 add constraint chkk check (sex in('男','女'));  

3.在一個範圍中間

create table test32(       
      id number primary key,       
      age number check(age>0 and age<120)
   );
create table test33(       
       id number primary key,       
      age number check(age between 12 and 30)
   );
   create table test34(       
        id number primary key ,       
        age number
   );
   alter table test34 add constraint ch_test34 check(age>0 and age<120);
   alter table test34 add constraint ch_test34 check(age between 12 and 30);  

4.長度大於某個值

create table test35(       
          id number primary key,       
          password varchar2(10) check(length(password)=6)
   );
   
create table test36(       
          id number primary key ,       
      password varchar2(20)
   );
  alter table test36 add constraint check_test36 check(length(password)=6);

5.數大於某個值

create table test37(      
     id number(10)primary key ,      
     no number(10) check(no>1) 
   );
   
create table test38(       
      id number(10) primary key,      
      no number(10)
   );
alter table test38 add constraint ch_test38 check(no>1);

6.只能是8位字符,前兩位是 0 ,3~4位爲數字,第 5 位爲"_"下劃線,6~8位爲字母

create table test39(       
       id number(10) primary key,       
       password varchar2(20) check((password like '00[0-9][0-9]/_[a-z,A-Z][a-z,A-Z][a-z,A-Z]%' escape '/')and(length(password)=8) )
   );
   insert into test39 values (1,'0011_aaa');
   
   create table test40(       
        id number(10) primary key ,       
        password varchar2(10)check((password like '00[0-9][0-9][_][a-z,A-Z][a-z,A-Z][a-z,A-Z]%')and(length(password)=8) ));
   );
   alter table test40 modify password varchar2(10)check((password like '00[0-9][0-9][_][a-z,A-Z][a-z,A-Z][a-z,A-Z]%')and(length(password)>1) 
   insert into test40 values(1,'0012_abc');

7.電子郵箱要含有@符號check(字段 like ‘%@%’)

 create table test41(       
          id number(10) primary key,       
          email varchar2(10) check (email like '%@%') 
    );
    insert into test41 values(1,'[email protected]');

8.SQL中用check約束一列的首字母爲’s’check(字段 like ‘s%’)

create table test42(       
        id number(10) primary key ,       
        name varchar2(10) check(name like 's%')
     );
 insert into test42 values(1,'sname');

9.檢查約束前3位和後8位均爲數字字符:
check(字段 like ‘[0-9][0-9][0-9]%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]’)

create table test43(       
         id number(10) primary key,       
         no varchar2(10)check(no like '[0-9][0-9][0-9]%[0-9][0-9][0-9][0-9][0-9]')
 );
 insert into test43 values(1,'12345678');

10.如何建立檢查身份證的約束,身份證是18位,最後一位還有可能是X

 create table test44(       
   id number(10) primary key,       
   no values(18) check( length(no)=18 and right(no,17)like '[0-9]' or right (no,17) like 'x' )
 );
insert into test44 values (1,'12345678912345678x');

select 身份證號 from 表名where len(身份證號) = 18 and (right(身份證號,17) like  '[0-9]'or right(身份證號,17) like 'x')

11.如何設置區號由0-9之間的數字組成CONSTRAINT

quhao CHECK (quhao  LIKE '[0-9][0-9][0-9]' 
or quhao LIKE '[0-9][0-9][0-9][0-9]'
or quhao LIKE '[0-9][0-9][0-9][0-9][0-9]'));

解釋:quhao LIKE '[0-9]…[0-9]'的號碼由表示n位從0到9中的數組成。
quhao LIKE ‘[0-9][0-9][0-9]’ 表示3位的區號,如北京010;
quhao LIKE '[0-9][0-9][0-9][0-9]'表示4位的區號,如三門峽0398;
quhao LIKE '[0-9][0-9][0-9][0-9][0-9]'表示5位的區號,如香港00852

12.最後回覆時間 TLastClickT 發貼時間 TTime最後回覆時間 必須晚於 發貼時間 並且小於等於當前時間

使用GetDate()函數獲取當前時間
設計表在TLastClickT上右擊選擇約束,新建,
填入([TLastClickT] > [TTime] and [TLastClickT] < GetDate())
或者TiastReply(回帖時間)大於Ttime(發帖時間)
在創表的同時創建表的時候應該還沒有回帖吧,爲什麼要用默認值?
可以添加一個約束 
alter table topic alter column add check(TlastReply is null or TlastReply > Ttime)

13.定義前八位爲數字或者 -一共是15位,爲CHAR型

alter table 表名add constraint chk check(字段 like'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%'),    
 constraint chklen check(len(字段)=15)    

14.如何限制varchar字段不允許出現單引號的檢查約束 !!!
設表爲TALBENAME,不能有引號的字段爲FIELDNAME 則:

ALTER TABLE tablename ADD CONSTRAINT CK_fieldname CHECK (not fieldname like '%''%')

15.在表中的某列中通過檢查約束,讓其有某些固定的值

 check(sid like 'bd[0-9][0-9][0-9][0-9][0-9][0-9]')
     add const ck_num check(num like '[1][2] [4][_] [0-9][0-9] [0-9][a-z]')

16.如何限制varchar字段不允許出現字符串的檢查約束 !!!

設表名爲TABLENAME,VARCHAR類型的字段爲VAR_FIELD.則有:
 ALTER TABLE [dbo].[TABLENAME] 
 ADD CONSTRAINT [CK_TABLENAME] 
 CHECK (isnumeric([VAR_FIELD]) = 1)
 這樣,在VAR_FIELD只要出現字段一旦出現非數字內容就會報告錯誤。

17.電話號碼的格式必須爲xxxx-xxxxxxxx或手機號11位

 alter 表名 add constraint ck_字段名 check (字段 like '[0-9][0-9][0-9][0-9]_[0-9]......' 
 or length(字段)=11)

18.身份證號是18位且唯一的

 alter 表名 add constraint ck_字段名 check (len(字段名)=18 ),
 constraint uk_字段名 unique(字段名)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章