Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit d38d081

Browse files
authored
Create check constraints, header
1 parent b66f9e7 commit d38d081

1 file changed

Lines changed: 88 additions & 88 deletions

File tree

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Create Check Constraints | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "03/14/2017"
4+
ms.date: "06/28/2017"
55
ms.prod: "sql-server-2016"
66
ms.reviewer: ""
77
ms.suite: ""
@@ -22,91 +22,91 @@ ms.author: "rickbyh"
2222
manager: "jhubbard"
2323
---
2424
# Create Check Constraints
25-
[!INCLUDE[tsql-appliesto-ss2016-all_md](../../includes/tsql-appliesto-ss2016-all-md.md)]
25+
[!INCLUDE[tsql-appliesto-ss2008-asdb-xxxx-xxx-md](../../includes/tsql-appliesto-ss2008-asdb-xxxx-xxx-md.md)]
2626

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-
## <a name="BeforeYouBegin"></a> Before You Begin
42-
43-
### <a name="Security"></a> Security
44-
45-
#### <a name="Permissions"></a> Permissions
46-
Requires ALTER permissions on the table.
47-
48-
## <a name="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.
76-
77-
|**To:**|**Select Yes in the Following Fields:**|
78-
|-------------|---------------------------------------------|
79-
|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 &#40;Transact-SQL&#41;](../../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+
## <a name="BeforeYouBegin"></a> Before You Begin
42+
43+
### <a name="Security"></a> Security
44+
45+
#### <a name="Permissions"></a> Permissions
46+
Requires ALTER permissions on the table.
47+
48+
## <a name="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.
76+
77+
|**To:**|**Select Yes in the Following Fields:**|
78+
|-------------|---------------------------------------------|
79+
|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 &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-transact-sql.md).
111+
112+
### <a name="TsqlExample"></a>

0 commit comments

Comments
 (0)