oracle中的五種約束

2021-06-20 21:21:19 字數 1016 閱讀 6985

1.not null (非空)--防止null值進入指定的列,在單列基礎上定義,預設情況下,oracle允許在任何列中有null值.

2.check (檢查)--檢查在約束中指定的條件是否得到了滿足.

3.unique (唯一)--保證在指定的列中沒有重複值.在該表中每乙個值或者每一組值都將是唯一的.

4.primary key (主鍵)--用來唯一的標識出表的每一行,並且防止出現null值,乙個表只能有乙個

主鍵約束.

5.poreign key (外部鍵)--通過使用公共列在表之間建立一種父子(parent-child)關係,在表上定義的外部鍵可以指向主鍵或者其他表的唯一鍵.

用例:create table student (

stu_id 

varchar2(20) 

not null,

stu_name 

varchar2(10) 

not null,

gender 

varchar2(2),

age 

number(3) 

default,

classno 

varchar2(2) )

alter table student add constraint pk_stu primary key(stu_id)

alter table student add constraint ck_stu_gender check(gender = '男' or gender = '女')

alter table student add constraint un_stu_name unique(stu_name)

alter table studend add constraint fk_stu_classno student(classno) reference class(class_no)

注:最後乙個約束中,class是另外一張表,class_no是它的主鍵。student表的classno參照class表中的class_no。

Oracle的五種約束

1.非空 not null 約束 所定義的列不絕對不能為空 例如 將已經建立好的表book中的bookname欄位修改為不為空 利用 alter table.modify not null alter table book modify bookname not null 2.主鍵 primary ...

Oracle的五類約束

1 語法1.1 行級定義 1.2 表級定義 2 not null 只能行級定義.sql create table tn0 a number 4 not null,b varchar2 20 sql create table tn1 a number 4 constraint nn tn1 a not...

資料庫中的五種約束

資料庫中的五種約束 五大約束 1.主鍵約束 primay key coustraint 唯一性,非空性 2.唯一約束 unique counstraint 唯一性,可以空,但只能有乙個 3.檢查約束 check counstraint 對該列資料的範圍 格式的限制 如 年齡 性別等 4.預設約束 d...