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

Commit a41c780

Browse files
authored
Merge pull request #21715 from WilliamDAssafMSFT/20220413-with-check-check
20220413 1812 refresh articles, account for trusted FK
2 parents d260636 + baeaff1 commit a41c780

4 files changed

Lines changed: 208 additions & 206 deletions

File tree

docs/relational-databases/tables/disable-foreign-key-constraints-with-insert-and-update-statements.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: "Disable Foreign Key Constraints with INSERT and UPDATE Statements"
33
title: Disable Foreign Key Constraints in INSERT and UPDATE Statements
44
ms.custom: "seo-lt-2019"
5-
ms.date: "10/21/2021"
5+
ms.date: "04/13/2022"
66
ms.prod: sql
77
ms.prod_service: "database-engine, sql-database, synapse-analytics, pdw"
88
ms.reviewer: ""
@@ -18,7 +18,7 @@ author: WilliamDAssafMSFT
1818
ms.author: wiassaf
1919
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
2020
---
21-
# Disable Foreign Key Constraints with INSERT and UPDATE Statements
21+
# Disable foreign key constraints with INSERT and UPDATE statements
2222
[!INCLUDE [sqlserver2016-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi-asa-pdw.md)]
2323

2424
You can disable a foreign key constraint during INSERT and UPDATE transactions in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. Use this option if you know that new data will not violate the existing constraint or if the constraint applies only to the data already in the database.
@@ -29,7 +29,7 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
2929
## <a name="Security"></a><a name="Permissions"></a> Permissions
3030
Requires ALTER permission on the table.
3131

32-
## <a name="SSMSProcedure"></a> Use SQL Server Management Studio
32+
## <a name="SSMSProcedure"></a> Use SQL Server Management Studio
3333

3434
#### To disable a foreign key constraint for INSERT and UPDATE statements
3535

@@ -42,6 +42,11 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
4242
4. Select **Close**.
4343

4444
5. To re-enable the constraint when desired, reverse the above steps. Select **Enforce Foreign Key Constraint** and select **Yes** from the drop-down menu.
45+
46+
6. To trust the constraint by checking the existing data in the foreign key's relationship, select **Check Existing Data on Creation Or Re-Enabling** and select **Yes** from the drop-down menu. This would ensure the foreign key constraint is trusted.
47+
48+
- If **Check Existing Data on Creation Or Re-Enabling** is set to **No**, the foreign key does not check existing data when it is re-enabled. The query optimizer is therefore unable to consider potential performance improvements. Trusted foreign keys are recommended because they can be used to simplify execution plans with assumptions based on the foreign key constraint. To check whether foreign keys are trusted in your database, see a sample query later in this article.
49+
4550

4651
## <a name="TsqlProcedure"></a> Use Transact-SQL
4752

@@ -70,8 +75,27 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
7075
CHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
7176
GO
7277
```
78+
79+
5. Verify that the constraint in your environment is both trusted and enabled. If `is_not_trusted` = 1, then the foreign key does not check existing data when it is re-enabled or re-created. The query optimizer is therefore unable to consider potential performance improvements. Trusted foreign keys are recommended because they can be used to simplify execution plans with assumptions based on the foreign key constraint. Copy and paste the following example into the query window and select **Execute**.
80+
81+
```sql
82+
SELECT o.name, fk.name, fk.is_not_trusted, fk.is_disabled
83+
FROM sys.foreign_keys AS fk
84+
INNER JOIN sys.objects AS o ON fk.parent_object_id = o.object_id
85+
WHERE fk.name = 'FK_PurchaseOrderHeader_Employee_EmployeeID';
86+
GO
87+
```
88+
89+
You should set the foreign key constraint to trusted if existing data in the table complies with the foreign key constraint. To set the foreign key to trusted, use the following script to trust the foreign key constraint again, noting the additional `WITH CHECK` syntax. Copy and paste the following example into the query window and select **Execute**.
90+
91+
```sql
92+
ALTER TABLE [Purchasing].[PurchaseOrderHeader]
93+
WITH CHECK
94+
CHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
95+
GO
96+
```
7397

7498
## Next steps
7599

76-
- [ALTER TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-transact-sql.md).
77-
100+
- [ALTER TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-transact-sql.md)
101+
- [View Foreign Key Properties](view-foreign-key-properties.md)
Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
description: "View Foreign Key Properties"
3-
title: "View Foreign Key Properties | Microsoft Docs"
2+
description: "You can view the foreign key attributes of a relationship with SQL Server Management Studio or T-SQL queries."
3+
title: "View Foreign Key Properties"
44
ms.custom: ""
5-
ms.date: "03/14/2017"
5+
ms.date: "04/13/2022"
66
ms.prod: sql
77
ms.prod_service: "database-engine, sql-database, synapse-analytics, pdw"
88
ms.reviewer: ""
@@ -12,33 +12,16 @@ helpviewer_keywords:
1212
- "foreign keys [SQL Server], attributes"
1313
- "displaying foreign keys attributes"
1414
- "viewing foreign keys attributes"
15-
ms.assetid: b0e57cb7-9b26-4b96-b76a-1f59f5f498c5
1615
author: WilliamDAssafMSFT
1716
ms.author: wiassaf
1817
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
1918
---
20-
# View Foreign Key Properties
19+
# View foreign key properties
2120
[!INCLUDE [sqlserver2016-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi-asa-pdw.md)]
2221

2322
You can view the foreign key attributes of a relationship in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)].
24-
25-
**In This Topic**
26-
27-
- **Before you begin:**
28-
29-
[Security](#Security)
30-
31-
- **To view the foreign key attributes of a specific table, using:**
32-
33-
[SQL Server Management Studio](#SSMSProcedure)
34-
35-
[Transact-SQL](#TsqlProcedure)
36-
37-
## <a name="BeforeYouBegin"></a> Before You Begin
38-
39-
### <a name="Security"></a> Security
40-
41-
#### <a name="Permissions"></a> Permissions
23+
24+
### <a name="Permissions"></a> Permissions
4225
[!INCLUDE[ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../../relational-databases/security/metadata-visibility-configuration.md).
4326

4427
## <a name="SSMSProcedure"></a> Using SQL Server Management Studio
@@ -50,18 +33,19 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
5033
2. In the **Foreign Key Relationships** dialog box, select the relationship with properties you want to view.
5134

5235
If the foreign key columns are related to a primary key, the primary key columns are identified in **Table Designer** by a primary key symbol in the row selector.
53-
36+
37+
<a name="TsqlExample"></a>
5438
## <a name="TsqlProcedure"></a> Using Transact-SQL
5539

5640
#### To view the foreign key attributes of a relationship in a specific table
5741

5842
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
5943

60-
2. On the Standard bar, click **New Query**.
44+
2. On the Standard bar, select **New Query**.
6145

62-
3. Copy and paste the following example into the query window and click **Execute**. The example returns all foreign keys and their properties for the table `HumanResources.Employee` in the sample database.
46+
3. Copy and paste the following example into the query window and select **Execute**. The example returns all foreign keys and their properties for the table `HumanResources.Employee` in the sample database.
6347

64-
```
48+
```sql
6549
USE AdventureWorks2012;
6650
GO
6751
SELECT
@@ -70,15 +54,19 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
7054
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name
7155
,OBJECT_NAME (f.referenced_object_id) AS referenced_object
7256
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name
73-
,is_disabled
74-
,delete_referential_action_desc
75-
,update_referential_action_desc
57+
,f.is_disabled, f.is_not_trusted
58+
,f.delete_referential_action_desc
59+
,f.update_referential_action_desc
7660
FROM sys.foreign_keys AS f
7761
INNER JOIN sys.foreign_key_columns AS fc
7862
ON f.object_id = fc.constraint_object_id
7963
WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee');
8064
```
8165

8266
For more information, see [sys.foreign_keys &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/sys-foreign-keys-transact-sql.md) and [sys.foreign_key_columns &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/sys-foreign-key-columns-transact-sql.md).
83-
84-
### <a name="TsqlExample"></a>
67+
68+
## Next steps
69+
70+
- [ALTER TABLE Statement](../../odbc/microsoft/alter-table-statement.md)
71+
- [Disable foreign key constraints for replication](disable-foreign-key-constraints-for-replication.md)
72+
- [Disable Foreign Key Constraints with INSERT and UPDATE Statements](disable-foreign-key-constraints-with-insert-and-update-statements.md)

docs/t-sql/data-types/ntext-text-and-image-transact-sql.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
description: "ntext, text, and image (Transact-SQL)"
3-
title: "ntext, text, and image (Transact-SQL) | Microsoft Docs"
2+
description: "The ntext, text, and image data types are deprecated data types for storing large non-Unicode and Unicode character and binary data."
3+
title: "ntext, text, and image (Transact-SQL)"
44
ms.custom: ""
5-
ms.date: "07/22/2017"
5+
ms.date: "04/13/2022"
66
ms.prod: sql
77
ms.prod_service: "database-engine, sql-database"
88
ms.reviewer: ""
@@ -19,7 +19,6 @@ helpviewer_keywords:
1919
- "ntext data type"
2020
- "ntext data type, about ntext data type"
2121
- "image data type, about image data type"
22-
ms.assetid: b0d8769c-7598-4f97-8162-ace5f182b5bc
2322
author: MikeRayMSFT
2423
ms.author: mikeray
2524
---
@@ -28,7 +27,8 @@ ms.author: mikeray
2827

2928
Fixed and variable-length data types for storing large non-Unicode and Unicode character and binary data. Unicode data uses the UNICODE UCS-2 character set.
3029

31-
>**IMPORTANT!** **ntext**, **text**, and **image** data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use [nvarchar(max)](../../t-sql/data-types/nchar-and-nvarchar-transact-sql.md), [varchar(max)](../../t-sql/data-types/char-and-varchar-transact-sql.md), and [varbinary(max)](../../t-sql/data-types/binary-and-varbinary-transact-sql.md) instead.
30+
> [!IMPORTANT]
31+
> The **ntext**, **text**, and **image** data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use [nvarchar(max)](../../t-sql/data-types/nchar-and-nvarchar-transact-sql.md), [varchar(max)](../../t-sql/data-types/char-and-varchar-transact-sql.md), and [varbinary(max)](../../t-sql/data-types/binary-and-varbinary-transact-sql.md) instead.
3232
3333
## Arguments
3434
**ntext**
@@ -50,12 +50,18 @@ The following functions and statements can be used with **ntext**, **text**, or
5050
|[SUBSTRING &#40;Transact-SQL&#41;](../../t-sql/functions/substring-transact-sql.md)|[UPDATETEXT &#40;Transact-SQL&#41;](../../t-sql/queries/updatetext-transact-sql.md)|
5151
|[TEXTPTR &#40;Transact-SQL&#41;](../../t-sql/functions/text-and-image-functions-textptr-transact-sql.md)|[WRITETEXT &#40;Transact-SQL&#41;](../../t-sql/queries/writetext-transact-sql.md)|
5252
|[TEXTVALID &#40;Transact-SQL&#41;](../../t-sql/functions/text-and-image-functions-textvalid-transact-sql.md)||
53-
53+
54+
> [!CAUTION]
55+
> When dropping columns using the deprecated NTEXT data type, the cleanup of the deleted data occurs as a serialized operation on all rows. The cleanup can require a large amount of time. When dropping an NTEXT column in a table with lots of rows, update the NTEXT column to NULL value first, then drop the column. You can run this option with parallel operations and make it much faster.
56+
5457
## See also
55-
[CAST and CONVERT &#40;Transact-SQL&#41;](../../t-sql/functions/cast-and-convert-transact-sql.md)
56-
[Data Type Conversion &#40;Database Engine&#41;](../../t-sql/data-types/data-type-conversion-database-engine.md)
57-
[Data Types &#40;Transact-SQL&#41;](../../t-sql/data-types/data-types-transact-sql.md)
58-
[LIKE &#40;Transact-SQL&#41;](../../t-sql/language-elements/like-transact-sql.md)
59-
[SET @local_variable &#40;Transact-SQL&#41;](../../t-sql/language-elements/set-local-variable-transact-sql.md)
60-
[Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
58+
- [Data Types &#40;Transact-SQL&#41;](../../t-sql/data-types/data-types-transact-sql.md)
59+
- [LIKE &#40;Transact-SQL&#41;](../../t-sql/language-elements/like-transact-sql.md)
60+
- [SET @local_variable &#40;Transact-SQL&#41;](../../t-sql/language-elements/set-local-variable-transact-sql.md)
61+
- [Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
62+
63+
## Next steps
6164

65+
- [CAST and CONVERT &#40;Transact-SQL&#41;](../../t-sql/functions/cast-and-convert-transact-sql.md)
66+
- [Data Type Conversion &#40;Database Engine&#41;](../../t-sql/data-types/data-type-conversion-database-engine.md)
67+
- [ALTER TABLE (Transact-SQL)](../statements/alter-table-transact-sql.md)

0 commit comments

Comments
 (0)