sql新增約束的限制

2021-06-14 02:27:24 字數 905 閱讀 8033

sql**?

12

3

4

5

6

7

8

9

10

--新增性別約束

alter table author add constraint ck_age check(***='男' or ***='女')

--刪除性別約束

alter table drop constraint ck_***

--如果新增check約束時不希望檢查資料庫內的資料是否符合約束條件

alter table author add constraint ck_age check(***='1' or ***='0')

--禁用年齡約束和性別約束

alter table author nocheck constraint ck_age,ck_***

--啟用約束

alter table author check constraint ck_age,ck_***

1.在新增check約束時,會預設檢查資料表裡的資料是否符合check約束。如果有資料不符合約束條件,新增約束會失敗。如果希望新增check約束時不檢查資料庫裡的資料是否符合check約束,使用with nocheck。

2.alter table不允許刪除或者更改參與架構繫結檢視的表中的列

3.刪除列時,必須在刪除所有基於列的索引和約束後,才能刪除列。

4.alter column不會在列上繫結或取消繫結任何規則

5.新增uniqueidentifier欄位型別,需要使用newid()

SQL 新增約束

sql 約束用於規定表中的資料規則。如果存在違反約束的資料行為,行為會被約束終止。約束可以在建立表時規定 通過 create table 語句 或者在表建立之後規定 通過 alter table 語句 建立表的時候新增約束 create table table name column name1 d...

SQL中約束的新增

create database school use school create table student sno char 10 primary key,sname char 10 not null s char 2 check s 男 or s 女 default 男 sage int che...

sql語句新增約束

主鍵約束 primary key constraint 要求主鍵列的資料唯一,並且不允許為空。唯一約束 unique constraint 要求該列唯一,允許為空,但只能出現乙個空值。檢查約束 check constraint 某列取值範圍限制 格式限制等,如有關年齡的約束。預設約束 default...