You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can create a check constraint in a table to specify the data values that are acceptable in one or more columns in [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)].
28
-
29
-
**In This Topic**
30
-
31
-
-**Before you begin:**
32
-
33
-
[Security](#Security)
34
-
35
-
-**To create a new check constraint using:**
36
-
37
-
[SQL Server Management Studio](#SSMSProcedure)
38
-
39
-
[Transact-SQL](#TsqlProcedure)
40
-
41
-
## <aname="BeforeYouBegin"></a> Before You Begin
42
-
43
-
### <aname="Security"></a> Security
44
-
45
-
#### <aname="Permissions"></a> Permissions
46
-
Requires ALTER permissions on the table.
47
-
48
-
## <aname="SSMSProcedure"></a> Using SQL Server Management Studio
49
-
50
-
#### To create a new check constraint
51
-
52
-
1. In **Object Explorer**, expand the table to which you want to add a check constraint, right-click **Constraints** and click **New Constraint**.
53
-
54
-
2. In the **Check Constraints** dialog box, click in the **Expression** field and then click the ellipses **(…)**.
55
-
56
-
3. In the **Check Constraint Expression** dialog box, type the SQL expressions for the check constraint. For example, to limit the entries in the `SellEndDate` column of the `Product` table to a value that is either greater than or equal to the date in the `SellStartDate` column or is a NULL value, type:
57
-
58
-
```
59
-
SellEndDate >= SellStartDate OR SellEndDate IS NULL
60
-
```
61
-
62
-
Or, to require entries in the `zip` column to be 5 digits, type:
63
-
64
-
```
65
-
zip LIKE '[0-9][0-9][0-9][0-9][0-9]'
66
-
```
67
-
68
-
> [!NOTE]
69
-
> Make sure to enclose any non-numeric constraint values in single quotation marks (').
70
-
71
-
4. Click **OK**.
72
-
73
-
5. In the **Identity** category, you can change the name of the check constraint and add a description (extended property) for the constraint.
74
-
75
-
6. In the **Table Designer** category, you can set when the constraint is enforced.
|Test the constraint on data that existed before you created the constraint|**Check Existing Data On Creation Or Enabling**|
80
-
|Enforce the constraint whenever a replication operation occurs on this table|**Enforce For Replication**|
81
-
|Enforce the constraint whenever a row of this table is inserted or updated|**Enforce For INSERTs And UPDATEs**|
82
-
83
-
7. Click **Close**.
84
-
85
-
## <a name="TsqlProcedure"></a> Using Transact-SQL
86
-
87
-
#### To create a new check constraint
88
-
89
-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
90
-
91
-
2. On the Standard bar, click **New Query**.
92
-
93
-
3. Copy and paste the following example into the query window and click **Execute**.
94
-
95
-
```
96
-
ALTER TABLE dbo.DocExc
97
-
ADD ColumnD int NULL
98
-
CONSTRAINT CHK_ColumnD_DocExc
99
-
CHECK (ColumnD > 10 AND ColumnD < 50);
100
-
GO
101
-
-- Adding values that will pass the check constraint
102
-
INSERT INTO dbo.DocExc (ColumnD) VALUES (49);
103
-
GO
104
-
-- Adding values that will fail the check constraint
105
-
INSERT INTO dbo.DocExc (ColumnD) VALUES (55);
106
-
GO
107
-
108
-
```
109
-
110
-
For more information, see [ALTER TABLE (Transact-SQL)](../../t-sql/statements/alter-table-transact-sql.md).
111
-
112
-
### <a name="TsqlExample"></a>
27
+
You can create a check constraint in a table to specify the data values that are acceptable in one or more columns in [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)].
28
+
29
+
**In This Topic**
30
+
31
+
-**Before you begin:**
32
+
33
+
[Security](#Security)
34
+
35
+
-**To create a new check constraint using:**
36
+
37
+
[SQL Server Management Studio](#SSMSProcedure)
38
+
39
+
[Transact-SQL](#TsqlProcedure)
40
+
41
+
## <aname="BeforeYouBegin"></a> Before You Begin
42
+
43
+
### <aname="Security"></a> Security
44
+
45
+
#### <aname="Permissions"></a> Permissions
46
+
Requires ALTER permissions on the table.
47
+
48
+
## <aname="SSMSProcedure"></a> Using SQL Server Management Studio
49
+
50
+
#### To create a new check constraint
51
+
52
+
1. In **Object Explorer**, expand the table to which you want to add a check constraint, right-click **Constraints** and click **New Constraint**.
53
+
54
+
2. In the **Check Constraints** dialog box, click in the **Expression** field and then click the ellipses **(…)**.
55
+
56
+
3. In the **Check Constraint Expression** dialog box, type the SQL expressions for the check constraint. For example, to limit the entries in the `SellEndDate` column of the `Product` table to a value that is either greater than or equal to the date in the `SellStartDate` column or is a NULL value, type:
57
+
58
+
```
59
+
SellEndDate >= SellStartDate OR SellEndDate IS NULL
60
+
```
61
+
62
+
Or, to require entries in the `zip` column to be 5 digits, type:
63
+
64
+
```
65
+
zip LIKE '[0-9][0-9][0-9][0-9][0-9]'
66
+
```
67
+
68
+
> [!NOTE]
69
+
> Make sure to enclose any non-numeric constraint values in single quotation marks (').
70
+
71
+
4. Click **OK**.
72
+
73
+
5. In the **Identity** category, you can change the name of the check constraint and add a description (extended property) for the constraint.
74
+
75
+
6. In the **Table Designer** category, you can set when the constraint is enforced.
0 commit comments