新增刪除約束指令碼 fn get sql

2022-02-16 08:30:10 字數 898 閱讀 1069

新增約束:alter table table1

add constraint  pk_name   primary key (name)  --新增主健約束,名稱作為主健;

alter table table1

add constraint  pk_name  unique (name)  --新增唯一約束,名稱不能重複;

alter table table1

add constraint df_address default('地點不詳') for saddress --新增預設約束,地點不詳;

alter table table1

add constraint  ck_age  check (ages between 20 and 60)--新增檢查約束,要求年齡在20-60間;

alter table table1

add constraint  fk_stuno foreing  key(stuno)  references table2(stuno) --新增外健約束,主表table2和從表table1建立關係,通過stuno;

刪除約束: alter table table1

drop constraint   pk_name;

--檢查鎖語句

use master

declare @spid int;

declare @sql_handle binary(20);

set @spid = 91

select @sql_handle = sql_handle

from sysprocesses as a with (nolock)

where spid = @spid

select text

from ::fn_get_sql(@sql_handle)

約束新增和刪除

約束的目的就是確保表中的資料的完整性。常用的約束型別如下 主鍵約束 primary key constraint 要求主鍵列唯一,並且不允許為空 唯一約束 unique constraint 要求該列唯一,允許為空,但只能出現乙個空值 檢查約束 check constraint 某列取值範圍限制 格...

新增 刪除約束 Oracle

增加一列或者多列 alter table 表名 add column name datatype 修改一列或者多列 修改列的型別或者是長度 alter table 表名 modify column name datatype 刪除一列 alter table 表名 drop column colum...

MySQL 新增約束,修改約束,刪除約束

alter table 新增,修改,刪除表的列,約束等表的定義。檢視列 desc 表名 修改表名 alter table t book rename to bbb 新增列 alter table 表名 add column 列名 varchar 30 刪除列 alter table 表名 drop ...