sql – 是否可以向外键添加逻辑约束?

前端之家收集整理的这篇文章主要介绍了sql – 是否可以向外键添加逻辑约束?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个表,我添加了一个外键约束. Kewl – 效果很好.现在,是否有可能进一步限制父表中某些数据的关系?

基本上,我在父表中有动物,而子表希望只包含父数据为……嗯哺乳动物的数据.

例如.

Animals
^^^^^^^
AnimalId INT PK NOT NULL IDENTITY
AnimalType TINYINT NOT NULL -- 1: Mammal,2:Reptile,etc..
Name

Mammals
^^^^^^^
AnimalId INT PK FK NOT NULL
NumberOfMammaryGlads TINYINT NOT NULL

所以,我希望确保AnimalId只能是Animals.AnimalType = 1类型

这可能吗??

我不想让某人尝试在儿童表中插入一些针对爬行动物的信息……

干杯:)

编辑:

我以为我必须使用Check Constraint(从我的前两个答案下面确认 – 干杯!),但我不知道如何(例如,sql语法引用Animals表).

更新:

亚历克斯有一个非常好的帖子(下面),基准一些建议…一个非常好的阅读!

解决方法

我运行了一个小基准测试 – 在这种情况下,使用UDF的方法运行速度差了近100倍.

cpu时间中FK的开销= 375 ms – 297 ms = 78 ms

cpu时间中UDF的开销= 7750 ms – 297 ms = 7453 ms

这是sql代码……

– 设置128K行的辅助表数字:

CREATE TABLE dbo.Numbers(n INT NOT NULL PRIMARY KEY)
GO
DECLARE @i INT;
SET @i = 1;
INSERT INTO dbo.Numbers(n) SELECT 1;
WHILE @i<128000 BEGIN
  INSERT INTO dbo.Numbers(n)
    SELECT n + @i FROM dbo.Numbers;
  SET @i = @i * 2;
END;
GO

– 表格

CREATE TABLE dbo.Animals
(AnimalId INT NOT NULL IDENTITY PRIMARY KEY,AnimalType TINYINT NOT NULL,-- 1: Mammal,etc..
Name VARCHAR(30))
GO
ALTER TABLE dbo.Animals
ADD CONSTRAINT UNQ_Animals UNIQUE(AnimalId,AnimalType)
GO
CREATE FUNCTION dbo.GetAnimalType(@AnimalId INT)
RETURNS TINYINT
AS
BEGIN
DECLARE @ret TINYINT;
SELECT @ret = AnimalType FROM dbo.Animals
  WHERE AnimalId = @AnimalId;
RETURN @ret;
END
GO
CREATE TABLE dbo.Mammals
(AnimalId INT NOT NULL PRIMARY KEY,SomeOtherStuff VARCHAR(10),CONSTRAINT Chk_AnimalType_Mammal CHECK(dbo.GetAnimalType(AnimalId)=1)
);
GO

—使用UDF填充:

INSERT INTO dbo.Animals
  (AnimalType,Name)
SELECT 1,'some name' FROM dbo.Numbers;
GO
SET STATISTICS IO ON
SET STATISTICS TIME ON
GO
INSERT INTO dbo.Mammals
(AnimalId,SomeOtherStuff)
SELECT n,'some info' FROM dbo.Numbers;

结果是:

sql Server parse and compile time: 
cpu time = 0 ms,elapsed time = 2 ms.
Table 'Mammals'. Scan count 0,logical reads 272135,physical reads 0,read-ahead reads 0,lob logical reads 0,lob physical reads 0,lob read-ahead reads 0.
Table 'Numbers'. Scan count 1,logical reads 441,lob read-ahead reads 0.

sql Server Execution Times:
    cpu time = 7750 ms,elapsed time = 7830 ms.

(131072 row(s) affected)

—填充FK:

CREATE TABLE dbo.Mammals2
(AnimalId INT NOT NULL PRIMARY KEY,CONSTRAINT Chk_Mammals2_AnimalType_Mammal CHECK(AnimalType=1),CONSTRAINT FK_Mammals_Animals FOREIGN KEY(AnimalId,AnimalType)
  REFERENCES dbo.Animals(AnimalId,AnimalType)
);

INSERT INTO dbo.Mammals2
(AnimalId,AnimalType,1,'some info' FROM dbo.Numbers;

结果是:

sql Server parse and compile time: 
   cpu time = 93 ms,elapsed time = 100 ms.
Table 'Animals'. Scan count 1,logical reads 132,lob read-ahead reads 0.
Table 'Mammals2'. Scan count 0,logical reads 275381,lob read-ahead reads 0.

sql Server Execution Times:
   cpu time = 375 ms,elapsed time = 383 ms.

– 没有任何完整性的填充:

CREATE TABLE dbo.Mammals3
(AnimalId INT NOT NULL PRIMARY KEY,SomeOtherStuff VARCHAR(10)
);
INSERT INTO dbo.Mammals3
(AnimalId,'some info' FROM dbo.Numbers;

结果是:
sql Server解析和编译时间:
cpu时间= 1 ms,经过时间= 1 ms.

sql Server Execution Times:
   cpu time = 0 ms,elapsed time = 66 ms.
Table 'Mammals3'. Scan count 0,lob read-ahead reads 0.

sql Server Execution Times:
   cpu time = 297 ms,elapsed time = 303 ms.

(131072 row(s) affected)

cpu时间中FK的开销= 375 ms – 297 ms = 78 mscpu时间中UDF的开销= 7750 ms – 297 ms = 7453 ms

原文链接:https://www.f2er.com/mssql/76548.html

猜你在找的MsSQL相关文章