mac 打不开 不受信任_管理不受信任的外键

本文主要探讨了SQL Server中不受信任的外键的概念,解释了它们的定义和影响,包括数据完整性的重要性。文章提供了检测和修复不受信任的外键的方法,并通过示例演示了外键对查询性能的影响。同时,还介绍了如何使用存储过程来管理和修复不受信任的外键。
摘要由CSDN通过智能技术生成

mac 打不开 不受信任

介绍 (Introduction)

Intended audience

目标听众

This article is intended for application developers and database administrators who plan to develop, deploy, and/or assess solutions for Microsoft SQL Server on a Microsoft Windows platform.

本文适用于计划在Microsoft Windows平台上开发,部署和/或评估Microsoft SQL Server解决方案的应用程序开发人员和数据库管理员。

Typographical Conventions

印刷约定

Convention Meaning
Stylized Consolas Font Used for blocks of code, commands and script examples.
Text should be interpreted exactly as presented
Consolas Font Used for inline code, commands or examples.
Text should be interpreted exactly as presented
<italic font in brackets> Italic texts set in angle brackets denote a variable requiring substitution for a real value
Italic font Used to denote the title of a book, article, or other publication
Note Additional information or caveats
惯例 含义
程式化的Consolas字体 用于代码块,命令和脚本示例。
文字应完全按原样解释
Consolas字体 用于内联代码,命令或示例。
文字应完全按原样解释
<括号中的斜体> 尖括号中设置的斜体文本表示需要替换为实数值的变量
斜体字体 用于表示书籍,文章或其他出版物的标题
注意 附加信息或警告

Overview

总览

The subject of this article is untrusted foreign keys. Those three words together have a meaning but each word has to be well understood before that. That’s the reason why this article will start with a definitions section where we will define what is a key, what is a foreign key, and how a foreign key can be “untrusted”. Then, we will cover the way to discover and repair untrusted foreign keys for a given database. Finally, we will demonstrate in detail how foreign keys influence the execution of a query when it does not exist, when it exists and is “trusted” and when it exists and is “untrusted”.

本文的主题是不受信任的外键。 这三个词一起具有含义,但在此之前必须先理解每个词。 这就是为什么本文将从“定义”部分开始的原因,其中我们将定义什么是密钥,什么是外键以及如何“不信任”外键。 然后,我们将介绍发现和修复给定数据库的不可信外键的方法。 最后,我们将详细演示外键如何在不存在,何时存在且被“信任”以及何时存在且被“不信任”的情况下影响查询的执行。

定义 (Definitions)

Before going any further, let’s set the base and answer the following questions: « What is a key? », « What is a primary key? » and finally « What is a foreign key? », which is the topic of this article.

在继续进行操作之前,让我们设置基础并回答以下问题:«什么是密钥? »,«什么是主键? »最后,«什么是外键? »,这是本文的主题。

Then, we can talk about what the difference between a trusted and an untrusted foreign key.

然后,我们可以讨论受信任和不受信任的外键之间的区别。

What is a key for a relational table?

关系表的键是什么?

A key is basically an identifier, something that can uniquely identify a record in a relational table. Keys may consist of a single attribute or multiple attributes in combination. Based on the design of a table, there can be more than just one key which identify a record. We will refer to them as candidate keys.

键本质上是一个标识符,它可以唯一地标识关系表中的记录。 键可以包含单个属性,也可以包含多个属性。 根据表的设计,标识记录的键可能不止一个。 我们将它们称为候选键。

Example:

例:

Let’s have a look at the following table. It represents a list of students with 4 columns: a random numeric identifier and the firstname, lastname and sex of the student.

让我们看一下下表。 它代表一个由四列组成的学生列表:一个随机数字标识符以及该学生的名字,姓氏和性别。

StudentID FirstName LastName Sex
5345664 Adam Kent Male
8795165 Jefferson Elias Male
学生卡 名字 性别
5345664 亚当 肯特郡
8795165 杰斐逊 埃里亚斯

There are two candidate keys here: the StudentID, and the combination of FirstName and LastName columns.

这里有两个候选键: StudentID ,以及FirstName 和LastName列的组合。

Let’s add a table with the courses that are given in a particular school. The table will contain a unique identifier for the course, a title and a description.

让我们添加一个表,其中包含特定学校提供的课程。 该表将包含课程的唯一标识符,标题和说明。

CourseId Title CourseDescription
7897 Networks An introductive course on network topologies and standards
8975 Numerical Analysis Basics on numerical analysis.
课程编号 标题 课程说明
7897 网路 网络拓扑和标准入门课程
8975 数值分析 数值分析基础。

There are, also, two candidate keys here: the CourseId column alone and the Title column alone.

这里也有两个候选键:单独的CourseId列和单独的Title列。

What is a primary key for a relational table?

关系表的主键是什么?

The primary key of a relational table is a key that is considered as the most appropriate one among all the acceptable keys for this table. As a key, it can either be a normal attribute that is guaranteed to be unique such as a unique random alpha-numeric identifier or it can be generated by the DBMS.

关系表的主键是被认为是该表所有可接受键中最合适的键。 作为密钥,它可以是保证唯一的常规属性(例如唯一的随机字母数字标识符),也可以由DBMS生成。

In our example, the most appropriate key to identify a given student record is the StudentID column.

在我们的示例中,标识给定学生记录的最合适的键是StudentID列。

What is a foreign key in a relational table?

关系表中的外键是什么?

A foreign key is a basically a reference to another table in a DBMS. It comprises all the key columns composing the primary key of that « foreign » or « parent » table. One who defines a foreign key creates at the same time a referential constraint that checks data provided as a foreign key reference an existing primary key value in the parent table. This constraint must be validated by the DBMS before accepting any kind of modification on the table. So, we can say that a foreign key ensures data integrity.

外键基本上是对DBMS中另一个表的引用。 它包含构成该“ foreign”或“ parent”表的主键的所有键列。 定义外键的人会同时创建一个引用约束,该约束检查作为外键提供的数据是否引用了父表中的现有主键值。 在接受表上的任何类型的修改之前,必须由DBMS验证此约束。 因此,可以说外键可确保数据完整性。

If we carry on with our example, let’s say we have table called StudentEnrollments which keeps track of the enrollments a student has made for current year and his final mark.

如果继续我们的示例,假设我们有一个名为StudentEnrollments的表,该表可以跟踪学生本年度的入学情况及其最终成绩。

StudentId CourseId FinalMark
5345664 7897 16
8795165 8975 9
5345664 7897 18
学生卡 课程编号 最终商标
5345664 7897 16
8795165 8975 9
5345664 7897 18

Here we have two foreign keys:

这里有两个外键:

  1. StudentID column refers to the primary key in the Students table presented previously ;
  2. StudentID列是指前面显示的“ 学生”表中的主键;
  3. CourseId column refers to the primary key in the StudentEnrollments table also presented previously.
  4. CourseId列是指之前也提供过的StudentEnrollments表中的主键。

As a final question for readers, what could possibly be the primary key we can use for this particular table?

对于读者来说,最后一个问题是,我们可以为该特定表使用什么主键?

Answer: the combination of both foreign keys, i.e. (StudentId,CourseId).

答案:两个外键的组合,即( StudentId,CourseId )。

Note
Usually, foreign key references the primary key in a different table. It’s what have been shown in the examples. It’s also good to use it inside the same table. Such a design can be used to create hierarchical ordering in a table. You will find below an example of this: a table called department which has a unique identifier and can be member of another department.

注意
通常,外键引用其他表中的主键。 这是示例中显示的内容。 在同一张表中使用它也很好。 这样的设计可用于在表中创建分层排序。 您将在下面找到一个示例:名为Department的表,该表具有唯一的标识符,并且可以是另一个部门的成员。

DeptId Name HeadOfDept_id ParentDeptId
10 Finance 2 (null)
20 Accounting 23 10
部门编号 名称 HeadOfDept_id ParentDeptId
10 金融 2 (空值)
20 会计 23 10

Here the Finance department has no parent department but has a child department which is the Accounting.

在这里,财务部门没有上级部门,但有一个子部门,即会计部门。

Disabling a foreign key constraint or the way to untrust a foreign key

禁用外键约束或不信任外键的方法

SQL Server allows you to temporarily disable any CHECK or FOREIGN KEY constraint. Common use cases for this particular features are in the area of copying or importing data to a table faster. The process will work for sure, but one who does this operation must ensure that data he’s copying won’t violate those constraints before activate it again.

SQL Server允许您暂时禁用任何CHECK或FOREIGN KEY约束。 此特定功能的常见用例是更快地将数据复制或导入到表中。 该过程肯定会起作用,但是执行此操作的人必须确保要复制的数据在再次激活之前不会违反这些约束。

Here is the syntax to be used to disable then enable a constraint:

这是用于禁用然后启用约束的语法:

 
ALTER TABLE <SchemaName>.<TableName>
      NOCHECK CONSTRAINT <ConstraintName>;
ALTER TABLE <SchemaName>.<TableName>
      WITH { CHECK | NOCHECK }
      CHECK CONSTRAINT <ConstraintName >;
 

Specifying WITH CHECK in a statement tells to SQL Server the user wants it to validate the constraint against every single row in the table, then, if successful, enable it.

在语句中指定WITH CHECK告诉SQL Server用户希望它验证对表中每一行的约束,然后,如果成功,则启用它。

In contrast, specifying WITH NOCHECK, which is the default for an existing constraint, means that the constraint is enabled but no validation has been made on it. Even if this mode is faster to run, it can lead to severe side effects on performance: SQL Server doesn’t trust the constraint as it has not validated it. We refer to such a foreign key as an « untrusted foreign key ». As a consequence, the query optimizer won’t use the constraint to do his job…

相反,指定WITH NOCHECK (这是现有约束的默认值)意味着启用了约束,但尚未对其进行验证。 即使此模式运行得更快,也可能导致严重的性能副作用:SQL Server不信任该约束,因为它尚未对其进行验证。 我们将这样的外键称为“不受信任的外键”。 结果,查询优化器将不会使用约束来完成工作……

Human error can occur: one can forget to re-enable the constraint.

可能会发生人为错误:人们可能会忘记重新启用约束。

The proof of this will be given in the demo section.

在演示部分中将提供对此的证明。

检测不受信任的外键并采取适当的措施 (Detect untrusted foreign keys and take the appropriate action)

There is a simple way to detect whether a database contains one or more untrusted foreign keys. It’s simply by querying the sys.foreign_keys view in that database and check the is_not_trusted column. If this column is set to 1, it means the constraint is untrusted.

有一种简单的方法可以检测数据库是否包含一个或多个不受信任的外键。 只需查询该数据库中的sys.foreign_keys视图并检查is_not_trusted列即可。 如果此列设置为1,则表示约束不受信任。

Here is a possible version for the query:

这是查询的可能版本:

 
SELECT '[' + s.name + '].[' + o.name + '].[' + i.name + ']' AS keyname
FROM    sys.foreign_keys i
        INNER JOIN sys.objects o ON i.parent_object_id = o.object_id
        INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE   i.is_not_trusted = 1
        AND i.is_not_for_replication = 0
        AND i.is_disabled = 0
 

This method is simple but not practical because you must do it against every single database on a SQL Server instance. Instead, we developed a stored procedure that will return a dataset with all the untrusted foreign keys for every database plus the code to solve the issue.

此方法很简单但不切实际,因为您必须针对SQL Server实例上的每个数据库进行此操作。 相反,我们开发了一个存储过程 ,该过程将返回一个数据集,其中包含每个数据库的所有不受信任的外键以及用于解决该问题的代码。

The procedure is called [Administration].[GetUntrustedForeignKeys]. It creates a global temporary table you can then reuse in any other procedure. This table will contain the information described above.

该过程称为[Administration]。[GetUntrustedForeignKeys] 。 它创建一个全局临时表,然后可以在任何其他过程中重用。 该表将包含上述信息。

You will find below its interface. You can specify a database name. If you don’t, it will run against every accessible database on the instance. There are also some parameters that influence the behavior of the procedure.

您将在其界面下方找到。 您可以指定数据库名称。 如果不这样做,它将针对实例上的每个可访问数据库运行。 还有一些参数会影响过程的行为。

 
PROCEDURE [Administration].[GetUntrustedForeignKeys] (
    @DbName                     VARCHAR(256) = NULL,
    @_NoTempTableDropBefore     BIT = 0,
    @_NoTempTableDropAtTheEnd   BIT = 0,
    @_NoSelectOnTempTable       BIT = 0
)
 

The table that is sent back is of the current form:

发送回的表具有当前形式:

 
        DbName          VARCHAR(256),
        SchemaName      VARCHAR(256),
        TableName       VARCHAR(256),
        ForeignKeyName  VARCHAR(256),
        DDL2Resolve     VARCHAR(MAX) – computed column
 

In addition, we defined another stored procedure built on the previous one that effectively runs the code defined in the DDL2Resolve column.

另外,我们定义了另一个存储过程 ,该存储过程建立在前一个存储过程的基础上,该过程有效地运行了DDL2Resolve列中定义的代码。

It’s called [Administration].[RunCheckUntrustedForeignKeys].

称为[Administration]。[RunCheckUntrustedForeignKeys] 。

外键和性能(演示) (Foreign key and performance (Demo))

Let’s begin this section by creating a database for testing and some tables with primary keys. The tables will be the ones shown in the examples: Students, StudentEnrollments and Courses.

让我们从创建用于测试的数据库和一些带有主键的表开始本节。 这些表格将是示例中显示的表格: 学生 , 学生 报名和课程 。

Database Creation Statements.

数据库创建语句。

 
CREATE DATABASE [TestFK]
 CONTAINMENT = NONE
 ON  PRIMARY  ( 
	NAME = N'TestFK', 
	FILENAME = N'E:\DBASQL2K12T\TestFK.mdf' , 
	SIZE = 131072KB , 
	MAXSIZE = 16777216KB , 
	FILEGROWTH = 262144KB 
)
LOG ON ( 
	NAME = N'TestFK_log', 
	FILENAME = N'L:\DBASQL2K12T\TestFK_log.ldf' , 
	SIZE = 262144KB , 
	MAXSIZE = 16777216KB , 
	FILEGROWTH = 262144KB 
);
GO
 
USE [TestFK]
GO
 
IF NOT EXISTS (
	SELECT name 
	FROM sys.filegroups 
	WHERE is_default=1 AND name = N'PRIMARY'
) 
BEGIN
	ALTER DATABASE [TestFK] MODIFY FILEGROUP [PRIMARY] DEFAULT;
END;
GO
 

Creation of the Students table.

创建 学生 表。

 
use TestFK; 
GO
 
IF(OBJECT_ID('[dbo].[Students]') IS NULL)
BEGIN
	CREATE TABLE [dbo].[Students] (
		StudentID	INT IDENTITY(1,1) PRIMARY KEY,
		FirstName	VARCHAR(128) NOT NULL,
		LastName	VARCHAR(128) NOT NULL,
		Sex			VARCHAR(6)   NOT NULL
	);
END;
 

Creation of the Courses table.

创建 课程 表。

 
use TestFK; 
GO
 
IF(OBJECT_ID('[dbo].[Courses]') IS NULL)
BEGIN
	CREATE TABLE [dbo].[Courses] (
		CourseID			INT IDENTITY(1,1) PRIMARY KEY,
		Title    			VARCHAR(128) NOT NULL,
		CourseDescription	VARCHAR(MAX)
	);
END;
 

Creation of the StudentEnrollments table.

创建 StudentEnrollments 表。

 
use TestFK; 
GO
 
IF(OBJECT_ID('[dbo].[StudentEnrollments]') IS NULL)
BEGIN
	CREATE TABLE [dbo].[StudentEnrollments] (
		StudentID		INT NOT NULL,
		CourseID		INT NOT NULL,
		FinalMark		INT NULL
	);
END;
IF NOT EXISTS (
    SELECT 1 
    FROM sys.indexes 
    WHERE object_id = OBJECT_ID(N'[dbo].[StudentEnrollments]') 
    AND name = N'PK_StudentEnrollments'
)
BEGIN
    ALTER TABLE [dbo].[StudentEnrollments] 
        ADD CONSTRAINT [PK_StudentEnrollments] PRIMARY KEY CLUSTERED (
            [StudentID] ASC,
            [CourseID] ASC
    );
END;
GO
 

As you may have noticed, we haven’t created the foreign key constraints yet. Let’s first get an overview of the way SQL Server handles a query without this foreign key.

您可能已经注意到,我们还没有创建外键约束。 首先让我们概述一下SQL Server在没有此外键的情况下处理查询的方式。

You will find below a query that uses the StudentEnrollments table with the other two tables to get back a view of the name and firstname of a student following a course.

您会在下面找到一个查询,该查询将StudentEnrollments表与其他两个表结合使用,以获取课程学习后学生的姓名和名字的视图。

 
set statistics io on
set statistics time on
select s.FirstName,s.LastName 
from [dbo].[StudentEnrollments] se
inner join [dbo].[Students] s
	on se.StudentID = s.StudentID
inner join [dbo].[Courses] c
	on se.CourseID = c.CourseID ;
 

Here is its actual execution plan.

这是它的实际执行计划。

Here are statistics on I/O and time:

以下是有关I / O和时间的统计信息:

Let’s now create the foreign key references and see if there is a difference.

现在让我们创建外键引用,看看是否有区别。

Create the Foreign Key constraint to the Courses table.

创建 课程 的外键约束

 
USE [TestFK]
GO
 
IF NOT EXISTS (
	SELECT 1 
	FROM sys.foreign_keys 
	WHERE 
		object_id		 = OBJECT_ID(N'[dbo].[FK_StudentEnrollments_Courses]') 
	AND parent_object_id = OBJECT_ID(N'[dbo].[StudentEnrollments]')
)
BEGIN
	ALTER TABLE [dbo].[StudentEnrollments]  
		ADD CONSTRAINT [FK_StudentEnrollments_Courses] 
			FOREIGN KEY([CourseID])
				REFERENCES [dbo].[Courses] ([CourseID])
	;
END;
GO
 

Create the Foreign Key constraint to the Students table.

创建 学生 的外键约束

 
USE [TestFK]
GO
 
IF NOT EXISTS (
	SELECT 1 
	FROM sys.foreign_keys 
	WHERE 
		object_id		 = OBJECT_ID(N'[dbo].[FK_StudentEnrollments_Students]') 
	AND parent_object_id = OBJECT_ID(N'[dbo].[StudentEnrollments]')
)
BEGIN
	ALTER TABLE [dbo].[StudentEnrollments]  
		ADD CONSTRAINT [FK_StudentEnrollments_Students] 
			FOREIGN KEY([StudentID])
				REFERENCES [dbo].[Students] ([StudentID])
	;
END;
GO
 

The newer foreign keys change the execution plan of the query we used for test as you will see below. But first, the previous query:

较新的外键将更改我们用于测试的查询的执行计划,如下所示。 但是首先,上一个查询:

 
set statistics io on
set statistics time on
select s.FirstName,s.LastName 
from [dbo].[StudentEnrollments] se
inner join [dbo].[Students] s
	on se.StudentID = s.StudentID
inner join [dbo].[Courses] c
	on se.CourseID = c.CourseID ;
 

And the resulting actual plan:

以及最终的实际计划:

There is no access to the [dbo].[Courses] table introduced by the inner join due to foreign key addition. As a conclusion, foreign keys may reduce I/O, CPU and time for SELECT queries.

由于添加了外键,无法访问内部联接引入的[dbo]。[课程]表。 结论是,外键可以减少SELECT查询的I / O,CPU和时间。

Now, we will deactivate the foreign keys and insert data into tables then reactivate the foreign key. We will see whether this implies a different behavior or not and check that the foreign keys are untrusted.

现在,我们将停用外键并将数据插入表中,然后重新激活外键。 我们将查看这是否暗示其他行为,并检查外键是否不受信任。

 
Alter table [dbo].[StudentEnrollments] 
    nocheck constraint [FK_StudentEnrollments_Students];
Alter table [dbo].[StudentEnrollments] 
    nocheck constraint [FK_StudentEnrollments_Courses];
 

Are those foreign keys untrusted (using the query shown in Section 4).

那些外键不受信任吗(使用第4节中显示的查询)。

The query:

查询:

 
SELECT '[' + s.name + '].[' + o.name + '].[' + i.name + ']' AS keyname
FROM    sys.foreign_keys i
        INNER JOIN sys.objects o ON i.parent_object_id = o.object_id
        INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE   i.is_not_trusted = 1
        AND i.is_not_for_replication = 0
        AND i.is_disabled = 0
 

Its result:

其结果:

Conclusion: No, the foreign keys are not untrusted at the moment.

结论:不,外键目前并非不受信任。

Let’s populate the tables.

让我们填充表格。

 
SET NOCOUNT ON ;
DECLARE @RowCount INT;
DECLARE @CurrentSex VARCHAR(6);
DECLARE @RowIdStudent INT;
SET @RowCount = 0;
BEGIN TRANSACTION;
WHILE @RowCount < 21
BEGIN
	if(@RowCount %2 = 0)
    BEGIN
		SET @CurrentSex = 'Male';
    END;
	ELSE
    BEGIN
		 SET @CurrentSex = 'Female';
    END;
    insert into [dbo].[Students](FirstName,LastName,Sex)
    values (
        REPLICATE('a',@RowCount%6+1),
        REPLICATE('b',@RowCount%6+1),
        @CurrentSex
    );
 
    If(@RowCount % 10 = 0) 
    BEGIN
        Insert into [dbo].[Courses] (
			Title,CourseDescription
		)
		values(
			'Course : ' + CONVERT(VARCHAR(20),@RowCount),
			'Course from ' + CONVERT(VARCHAR(20),@RowCount)	
		);
	END;
    SET @RowCount = @RowCount + 1;
END;
COMMIT;
 
insert into StudentEnrollments (
    StudentID,CourseID
)
select StudentId, 1
from Students where StudentID % 7 = 0
union all 
select StudentId, 2
from Students where StudentID % 7 = 1;
 

Are the foreign keys untrusted now?

现在外键不受信任吗?

No, not yet as shown by the result of our simple detection query:

否,尚未如我们的简单检测查询结果所示:

So, let’s re-enable foreign keys.

因此,让我们重新启用外键。

 
Alter table [dbo].[StudentEnrollments] 
    check constraint [FK_StudentEnrollments_Students];
Alter table [dbo].[StudentEnrollments] 
    check constraint [FK_StudentEnrollments_Courses];
 

Are the foreign keys untrusted now?

现在外键不受信任吗?

Definitely, yes as shown by the result of our simple detection query:

肯定的,如我们的简单检测查询结果所示:

Did it change something in the query plan for our test query? Let’s run it again and check its actual execution plan and statistics.

它是否改变了我们测试查询的查询计划? 让我们再次运行它,并检查其实际执行计划和统计信息。

The actual query plan is the following one:

实际的查询计划如下:

While the statistics are:

虽然统计数据是:

Now let’s run our procedure [Administration].[GetUntrustedForeignKeys].

现在,让我们运行过程[Administration]。[GetUntrustedForeignKeys] 。

Here is the T-SQL code to run the procedure for the test database alone:

这是T-SQL代码,仅用于运行测试数据库的过程:

 
exec SAIDBA.[Administration].[GetUntrustedForeignKeys] 
     @DbName = 'TestFK';
 

Here is the result given by the procedure:

这是该过程给出的结果:

The DDL2Resolve value for the first untrusted foreign is:

第一个不受信任的外国人的DDL2Resolve值为:

 
USE [TestFK];
PRINT 'TestFK.dbo.StudentEnrollments(FK_StudentEnrollments_Courses)';
ALTER TABLE [dbo].[StudentEnrollments] 
    WITH CHECK CHECK CONSTRAINT [FK_StudentEnrollments_Courses];
 

Let’s run both commands as the number of untrusted foreign keys is limited.

让我们运行两个命令,因为不受信任的外键的数量受到限制。

Did we finally solve the problem? Let’s check that there is no untrusted foreign key anymore.

我们终于解决了这个问题吗? 让我们检查一下,不再有不受信任的外键。

The simple audit query:

简单的审计查询:

 
SELECT '[' + s.name + '].[' + o.name + '].[' + i.name + ']' AS keyname
FROM    sys.foreign_keys i
        INNER JOIN sys.objects o ON i.parent_object_id = o.object_id
        INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE   i.is_not_trusted = 1
        AND i.is_not_for_replication = 0
        AND i.is_disabled = 0
 

Its result:

其结果:

The test query:

测试查询:

 
set statistics io on
set statistics time on
select s.FirstName,s.LastName 
from [dbo].[StudentEnrollments] se
inner join [dbo].[Students] s
	on se.StudentID = s.StudentID
inner join [dbo].[Courses] c
	on se.CourseID = c.CourseID ;
 

Its actual plan:

其实际计划:

Its execution statistics:

其执行统计:

As the demo comes to its end, there is a final action to do: cleanup.

演示结束时,有最后一个动作要做:清理。

Database Cleanup Statements.

数据库清理语句。

 
Use [TestFK];
GO
/*
TRUNCATE TABLE Courses;
TRUNCATE TABLE Students;
TRUNCATE TABLE StudentEnrollments;
*/
IF(OBJECT_ID('[dbo].[StudentEnrollments]') IS NOT NULL)
BEGIN
    EXEC sp_executesql N'DROP TABLE [dbo].[StudentEnrollments]';
END;
GO
IF(OBJECT_ID('[dbo].[Students]') IS NOT NULL)
BEGIN
    EXEC sp_executesql N'DROP TABLE [dbo].[Students]';
END;
GO
 
IF(OBJECT_ID('[dbo].[Courses]') IS NOT NULL)
BEGIN
    EXEC sp_executesql N'DROP TABLE [dbo].[Courses]';
END;
GO
 
USE [master];
GO
 
IF(DB_ID('TestFK') IS NOT NULL)
BEGIN
    EXEC sp_executesql N'DROP DATABASE [TestFK]';
END;
GO
 

翻译自: https://www.sqlshack.com/managing-untrusted-foreign-keys/

mac 打不开 不受信任

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值