刪除SQL約束的方法

2022-03-22 08:25:57 字數 1438 閱讀 3776

在sql資料庫中,如果需要刪除表約束,應該如何操作呢?下面就將為您介紹刪除sql表約束的方法,供您參考,希望對您有所幫助。

--

1)禁止所有表約束的sql

select

'alter table

'+name+

'nocheck constraint all

'from sysobjects where type='u

'--2)刪除所有表資料的sql

select

'truncate table

'+name from sysobjects where type='u

'--3)恢復所有表約束的sql

select

'alter table

'+name+

'check constraint all

'from sysobjects where type='u

'--4)刪除某字段的約束

declare

@name

varchar(100)--

df為約束名稱字首

select

@name

=b.name from syscolumns a,sysobjects b where a.id=

object_id('

表名') and b.id=a.cdefault and a.name=

'欄位名

'and b.name like

'df%'--

刪除約束

alter

table 表名 drop

constraint

@name

--為字段新增新預設值和約束

alter

table 表名 add

constraint

@name

default (0) for

[欄位名

]對欄位約束進行更改

--刪除約束

alter

table

tablename

drop

constraint

約束名--

修改表中已經存在的列的屬性(不包括約束,但可以為主鍵或遞增或唯一)

alter

table

tablename

alter

column 列名 int

notnull

--新增列的約束

alter

table

tablename

addconstraint df_tablename_列名 default(0) for

列名--

新增範圍約束

alter

table tablename add

check(性別 in ('

m','

f'))

unique約束的刪除方法

select from sysconstraints where id object id 表名 drop constraint 約束名 gosysconstraints 包含約束對映,對映到擁有該約束的物件。該系統目錄儲存在每個資料庫中。列名 資料型別 描述 constid int 約束號。id ...

建立SQL約束的方法

create table student 建 式 create table 自定義的表名 欄位名一般為有一定意義的英文 studentname nvarchar 15 格式 欄位名型別 括號裡面的是允許輸入的長度 studentage int int 型的後面不需要接長度 student nvarc...

建立與刪除SQL約束或字段約束

建立與刪除sql約束或字段約束 sql約束控制 1 禁止所有表約束的sql select alter table name nocheck constraint all fromwhere type u 2 刪除所有表資料的sql select truncate table name from sy...