I have a column called MealType
( VARCHAR
) in my table with a CHECK
constraint for {"Veg", "NonVeg", "Vegan"}
That'll take care of insertion.
I'd like to display these options for selection, but I couldn't figure out the SQL query to find out the constraints of a particular column in a table.
From a first glance at system tables in SQL Server, it seems like I'll need to use SQL Server's API to get the info. I was hoping for a SQL query itself to get it.
7条答案
按热度按时间uxh89sit1#
Easiest and quickest way is to use:
bfrts1fy2#
This query should show you all the constraints on a table:
can replace the select statement with this:
mdfafbf13#
vptzau2j4#
You can use
to get all the constraints for the table.
"sp_help" return far more information.
w8biq8rn5#
Thanks to orgtrigger for his example! I improved it to be able to remove unnecessary constraints (and then create their modified versions, if needed). Maybe this code will be useful for anybody.
sirbozc56#
Below is helpful for check and default constraints. I use it for implicit constraints to offer up guidance for what the name should be. If you remove everything after the where clause, it should be good for any check/default constraints.
frebpwbc7#
Query to fetch constraint information: