SELECT
df.
name
'Constraint Name'
,
--df.definition, -- this gives the default value
t.
'Table Name'
c.
NAME
'Column Name'
SCHEMA_NAME(t.schema_id)
'Schema_Name'
FROM
sys.default_constraints df
INNER
JOIN
sys.tables t
ON
df.parent_object_id = t.object_id
sys.columns c
df.parent_object_id = c.object_id
AND
df.parent_column_id = c.column_id
WHERE
=
'Column_Name'
-- Column Name
'Table_Name'
--- put the table name here
select
TABLE_NAME, TABLE_CATALOG, table_schema, column_name from INFORMATION_SCHEMA.COLUMNS
TABLE_NAME, TABLE_CATALOG, table_schema, column_name
from
INFORMATION_SCHEMA.COLUMNS
where
COLUMN_NAME
like
'%Column_Name%'
Above script will
return
View
as
well
with
the
table
, below script will
not
.
and
Table_name
Not
'VW_%'
*
sys.foreign_keys
'alter table '
+ quotename(schema_name(schema_id)) +
'.'
+
quotename(object_name(parent_object_id)) +
' drop constraint '
+quotename(
)
Note: It will exclude primary key and unique key constraints.
sys.indexes i
inner
join
sys.objects o
on
i.object_id=o.object_id
sys.schemas s
o.schema_id = s.schema_id
o.type<>
'S'
is_primary_key<>1
index_id>0
s.
!=
'sys'
is_unique_constraint=0
Below script will get all constraints along with a create script to drop all constraints, we can have conditions to filter specific constraints.
declare
@qry nvarchar(
max
);
@qry = (
'IF EXISTS(SELECT * FROM sys.indexes WHERE name='
''
+ i.
' AND object_id = OBJECT_ID('
'['
+s.
'].['
+o.
']'
')) drop index ['
+i.
'] ON ['
']; '
for
xml path(
));
exec
sp_executesql @qry
Query
to
create
script
delete
all
constraint
')) alter table ['
'] drop Constraint ['
'] ; '
print @qry
[Scehma]=schema_name(o.schema_id), o.
Name
, o.type
sys.sql_modules m
o.object_id = m.object_id
m.definition
'%Text%'