اسکریپت: لیست تمامی constraint های تعریف شده روی جداول یک دیتابیس (PK,UK,FK,Check,Default)

اسکریپت: لیست تمامی constraint های تعریف شده روی جداول یک دیتابیس (PK,UK,FK,Check,Default)

نوشته شده توسط: زهرا فرهنگی
۲۳ آبان ۱۳۹۷
زمان مطالعه: 5 دقیقه
۰
(۰)

مقدمه

دستور زیر تمامی Constraintهای تعریف شده روی جداول(و Vewها) شامل Primary Keys, Unique Key Constraintها و ایندکس‌ها, Foreign Keyها و Check و Default Constraintهای یک دیتابیس را نمایش خواهد داد.

select table_view,
object_type,
constraint_type,
constraint_name,
details
from (
select schema_name(t.schema_id) + '.' + t.[name] as table_view,
case when t.[type] = 'U' then 'Table'
when t.[type] = 'V' then 'View'
end as [object_type],
case when c.[type] = 'PK' then 'Primary key'
when c.[type] = 'UQ' then 'Unique constraint'
when i.[type] = 1 then 'Unique clustered index'
when i.type = 2 then 'Unique index'
end as constraint_type,
isnull(c.[name], i.[name]) as constraint_name,
substring(column_names, 1, len(column_names)-1) as [details]
from sys.objects t
left outer join sys.indexes i
on t.object_id = i.object_id
left outer join sys.key_constraints c
on i.object_id = c.parent_object_id
and i.index_id = c.unique_index_id
cross apply (select col.[name] + ', '
from sys.index_columns ic
inner join sys.columns col
on ic.object_id = col.object_id
and ic.column_id = col.column_id
where ic.object_id = t.object_id
and ic.index_id = i.index_id
order by col.column_id
for xml path ('') ) D (column_names)
where is_unique = 1
and t.is_ms_shipped <> 1
union all
select schema_name(fk_tab.schema_id) + '.' + fk_tab.name as foreign_table,
'Table',
'Foreign key',
fk.name as fk_constraint_name,
schema_name(pk_tab.schema_id) + '.' + pk_tab.name
from sys.foreign_keys fk
inner join sys.tables fk_tab
on fk_tab.object_id = fk.parent_object_id
inner join sys.tables pk_tab
on pk_tab.object_id = fk.referenced_object_id
inner join sys.foreign_key_columns fk_cols
on fk_cols.constraint_object_id = fk.object_id
union all
select schema_name(t.schema_id) + '.' + t.[name],
'Table',
'Check constraint',
con.[name] as constraint_name,
con.[definition]
from sys.check_constraints con
left outer join sys.objects t
on con.parent_object_id = t.object_id
left outer join sys.all_columns col
on con.parent_column_id = col.column_id
and con.parent_object_id = col.object_id
union all
select schema_name(t.schema_id) + '.' + t.[name],
'Table',
'Default constraint',
con.[name],
col.[name] + ' = ' + con.[definition]
from sys.default_constraints con
left outer join sys.objects t
on con.parent_object_id = t.object_id
left outer join sys.all_columns col
on con.parent_column_id = col.column_id
and con.parent_object_id = col.object_id) a
order by table_view, constraint_type, constraint_name
  •  Table_View: نام جدول یا View به همراه نام Schema
  •  Object_Type: نوع Object

– Table
– View

  • Constraint_Type: نوع Constraint

– Primary key
– Unique key
– Foregin key
– Check constraint
– Default constraint

  • Constraint_Name: نام Constraint یا ایندکس
  •  Details: جزییات این Constraint

– Primary key – شامل اسامی ستون های شرکت کننده در PK
– Unique key – شامل اسامی ستون های شرکت کننده در UK
– Foregin key – نام جدول اصلی
– Check constraint – عبارت(فرمول) تعریف شده برای constraint
– Default constraint – نام ستون و مقدار/عبارت تعریف شده برای constrain

یک نمونه از اجرای دستور فوق روی دیتابیس AdventureWorks

برای بدست آوردن اطلاعات بیش‌تر در مورد اسکریپت های پرکاربرد SQL ، به مقاله زیر مراجعه کنید.

چه رتبه ای می‌دهید؟

میانگین ۰ / ۵. از مجموع ۰

اولین نفر باش

title sign
دانلود مقاله
اسکریپت: لیست تمامی constraint های تعریف شده روی جداول یک دیتابیس (PK,UK,FK,Check,Default)
فرمت PDF
1 صفحه
حجم 1 مگابایت
دانلود اسکریپت
title sign
معرفی نویسنده
زهرا فرهنگی
مقالات
51 مقاله توسط این نویسنده
محصولات
0 دوره توسط این نویسنده
زهرا فرهنگی

کارشناس پایگاه داده، در حال کسب تجربه در زمینه‌های تحلیل انباره داده، BI، بهینه سازی پایگاه‌های داده

پروفایل نویسنده
title sign
دیدگاه کاربران

هر روز یک ایمیل، هر روز یک درس
آموزش SQL Server بصورت رایگان
همین حالا فرم زیر را تکمیل کنید
دانلود رایگان جلسه اول
نیک آموز علاوه بر آموزش، پروژه‌های بزرگ در حوزه هوش تجاری و دیتا انجام می‌دهد.
close-link
وبینار رایگان SQL Server؛ مسیری به سوی فرصت‌های شغلی بی‌شمار       پنج‌شنبه 30 فرودین ساعت 15
ثبت نام رایگان
close-image