資料庫的約束

2021-06-25 20:50:03 字數 625 閱讀 8399

資料庫的約束條件:

新增約束:

alter table 表名

add constraint 約束名 約束型別 具體說明

學生編號,主鍵約束:

add constraint pk_stuno primary key (stuno)

學生身份證號,唯一約束:

add constraint uq_stuid unique(stuid)

學生位址,預設約束:

add constraint df_stuaddress default(『輸入位址』) for stuaddress

年齡在10-40之間,檢查約束:

add constraint ck_stuage  check(stuage between 10 and 40)

外來鍵約束:

add constraint fk_stuno foregin key (stuno) references stuinfo(stuno)

刪除約束:

alter table 表名

drop constraint 約束名

例如,刪除檢查約束:

alert table stuinfo

drop constraint ck_stuage

資料庫的約束

什麼是資料庫的約束?我認為資料庫的約束就是限制資料庫表中的約束條件。約束一共分為5種型別,分別為 sql server中有五種約束型別,分別是check約束 default約束 primary key約束 foreign key約束和unique約束。用於限制輸入一列或者多列的值的範圍,通過邏輯表示...

資料庫的約束

新增相關約束 建立主鍵約束 if exists select from sysobjects where name pk studentid alter table students drop constraint pk studentid alter table students add cons...

資料庫的約束

概念 對錶中的資料進行限定,保證資料的正確性 有效性和完整性 分類1 主鍵約束 primary key 2 非空約束 not null 3 唯一約束 unique 4 外來鍵約束 foreign key1 建立表時新增約束create table stu1 id int name varchar 5...