校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > SQL Server約束增強(qiáng)的兩點建議

SQL Server約束增強(qiáng)的兩點建議

熱門標(biāo)簽:分享百度地圖標(biāo)注多個位置 知名電銷機(jī)器人價格 長沙智能外呼系統(tǒng) 菏澤語音電銷機(jī)器人加盟公司 電銷機(jī)器人公司 需要哪些牌照 湖南電腦外呼系統(tǒng)平臺 外呼系統(tǒng)改進(jìn) 地圖標(biāo)注牌 廣東防封卡外呼系統(tǒng)原理是什么
在許多情況下,對外鍵使用更復(fù)雜的邏輯表達(dá)式是非常有用的。 此外,在某些情況下能夠在索引視圖創(chuàng)建約束也將非常實用。 我將舉例說明,同時我希望針對此文的投票鏈接會盡快加上。
當(dāng)外鍵中需要更為復(fù)雜的邏輯表達(dá)式時
考慮下面的簡單常識: 您的設(shè)備的最大電流不能超過您插入到它的電路的最大電流。 假設(shè)下面的表存儲電路和設(shè)備數(shù)據(jù):
復(fù)制代碼 代碼如下:

CREATE TABLE Data.Curcuits(CurcuitID INT NOT NULL
CONSTRAINT PK_Curcuits PRIMARY KEY,
MaximumCurrent INT NOT NULL,
Description VARCHAR(100) NOT NULL);
GO
INSERT INTO Data.Curcuits(CurcuitID,
MaximumCurrent,
Description)
SELECT 1, 25, 'Deck and Garage';
GO
CREATE TABLE Data.Devices(DeviceID INT NOT NULL
CONSTRAINT PK_Devices PRIMARY KEY,
CurcuitID INT NULL,
MaximumCurrent INT NOT NULL,
Description VARCHAR(100) NOT NULL,
CONSTRAINT FK_Devices_Curcuits FOREIGN KEY(CurcuitID)
REFERENCES Data.Curcuits(CurcuitID)
);
GO

It would be very convenient to issue a simple command and implement this business rule:
一個非常簡便的命令就可能實現(xiàn)這個業(yè)務(wù)規(guī)則:
ALTER TABLE Data.Devices ADD CONSTRAINT FK_Devices_Curcuits
FOREIGN KEY(CurcuitID, MaximumCurrent)
REFERENCES Data.Curcuits(CurcuitID, MaximumCurrent)
MATCH ON((Data.Devices.CurcuitID = Data.Curcuits.CurcuitID) AND
(Data.Devices.MaximumCurrent = Data.Curcuits.MaximumCurrent));
However, it is not supported, so I need to use a workaround, one more column and three constraints instead of one, as follows:
然而,該語句并不被支持,所以必須采用其他辦法——多增加一列約束,使用3個而不是1個約束,如下所示:
ALTER TABLE Data.Curcuits
ADD CONSTRAINT UNQ_Curcuits UNIQUE(CurcuitID, MaximumCurrent);
GO
ALTER TABLE Data.Devices ADD CurcuitMaximumCurrent INT NULL;
GO
ALTER TABLE Data.Devices DROP CONSTRAINT FK_Devices_Curcuits;
GO
ALTER TABLE Data.Devices ADD CONSTRAINT FK_Devices_Curcuits
FOREIGN KEY(CurcuitID, CurcuitMaximumCurrent)
REFERENCES Data.Curcuits(CurcuitID, MaximumCurrent)
ON UPDATE CASCADE;
GO
ALTER TABLE Data.Devices
ADD CONSTRAINT CHK_Devices_SufficientCurcuitMaximumCurrent
CHECK(CurcuitMaximumCurrent >= MaximumCurrent);
GO
You can verify that the constraints work:
你可以驗證該約束有效:
INSERT INTO Data.Devices(DeviceID,
CurcuitID,
MaximumCurrent,
CurcuitMaximumCurrent,
Description)
SELECT 1, 1, 50, 25, 'Electric car charger'
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the CHECK constraint "CHK_Devices_SufficientCurcuitMaximumCurrent". The conflict occurred in database "Test", table "data.Devices".
The statement has been terminated.
INSERT 語句和CHECK約束"CHK_Devices_SufficientCurcuitMaximumCurrent"發(fā)生沖突。 該沖突發(fā)生在數(shù)據(jù)庫"Test"的"data.Devices"表。
該語句被終止執(zhí)行。
As you have seen, the implementation of a very simple and very common business rule is quite involved, because such business rules are not directly supported by the database engine.
可以看出,一個非常簡單而普通的業(yè)務(wù)規(guī)則實現(xiàn)起來也相當(dāng)繁雜,因為數(shù)據(jù)庫引擎并不直接支持這種業(yè)務(wù)規(guī)則。
When you want to create constraints on indexed views
在索引視圖上創(chuàng)建約束
Even when your database guarantees that “the maximum current of your device cannot exceed the maximum current of the circuit you plug it into”, it is not good enough. Consider the following sample data:
盡管數(shù)據(jù)庫保證“您的設(shè)備的最大電流不能超過您插入到它的電路的最大電流”,但這還不夠。請看下列示例數(shù)據(jù):
INSERT INTO Data.Devices(DeviceID,
CurcuitID,
MaximumCurrent,
CurcuitMaximumCurrent,
Description)
SELECT 2, 1, 15, 25, 'ShopVac';
INSERT INTO Data.Devices(DeviceID,
CurcuitID,
MaximumCurrent,
CurcuitMaximumCurrent,
Description)
SELECT 3, 1, 15, 25, 'Miter Saw';
The database structure allows to plug more than one device into a circuit, which is correct, but if you turn both devices on, their combined maximum current exceeds the circuit's maximum current. To enforce this business rule, it would be natural to create an indexed view, so that the database guarantees that the totals are always correct:
數(shù)據(jù)庫中的數(shù)據(jù)表明可以插入一個以上的設(shè)備到電路,這沒有錯,可是當(dāng)所有的設(shè)備都打開時,它們的最大電流之和會超過電路最大電流。為了加強(qiáng)這個業(yè)務(wù)規(guī)則,很自然的會創(chuàng)建一個索引視圖以使數(shù)據(jù)庫保證電流之和總是正確的。
CREATE VIEW Data.TotalMaximumCurrentPerCircuit WITH SCHEMABINDING
AS
SELECT d.CurcuitID,
c.MaximumCurrent AS CircuitMaximumCurrent,
SUM(d.MaximumCurrent) AS TotalMaximumCurrent,
COUNT_BIG(*) AS NumDevices
FROM Data.Devices d JOIN Data.Curcuits c ON d.CurcuitID = c.CurcuitID
GROUP BY d.CurcuitID, c.MaximumCurrent;
GO
CREATE UNIQUE CLUSTERED INDEX Data_TotalMaximumCurrentPerCircuit
ON Data.TotalMaximumCurrentPerCircuit(CurcuitID);
GO
If I could create a check constraint on that indexed view, I would be all set:
如果能在該索引視圖上創(chuàng)建一個約束,我將進(jìn)行這樣的設(shè)置:
ALTER VIEW Data.TotalMaximumCurrentPerCircuit
ADD CONSTRAINT CHK_TotalMaximumCurrentPerCircuit_ValidCurcuit
CHECK(TotalMaximumCurrent = CircuitMaximumCurrent)
Instead, I need to use triggers or rather contrived kludges. A built in native support for such quite common business rules would increase the usefulness of SQL Server.
實際上,我必須使用觸發(fā)器或者精心拼湊Check約束來實現(xiàn)。如果數(shù)據(jù)庫內(nèi)置支持這種相當(dāng)普遍的業(yè)務(wù)規(guī)則,那將會增加SQL Server的實用性 。
您可能感興趣的文章:
  • mysql創(chuàng)建Bitmap_Join_Indexes中的約束與索引
  • Mysql中返回一個數(shù)據(jù)庫的所有表名,列名數(shù)據(jù)類型備注
  • sql語句查詢數(shù)據(jù)庫中的表名/列名/主鍵/自動增長值實例
  • sqlserver 修改列名及表名的sql語句
  • MySQL學(xué)習(xí)筆記4:完整性約束限制字段
  • sql腳本查詢數(shù)據(jù)庫表,數(shù)據(jù),結(jié)構(gòu),約束等操作的方法
  • MySQL 添加、修改、刪除表的列及約束等表的定義
  • 使用sql語句創(chuàng)建和刪除約束示例代碼
  • MySQL導(dǎo)出所有Index和約束的方法
  • SQLSERVER實現(xiàn)更改表名,更改列名,更改約束代碼

標(biāo)簽:美容院 泉州 福建 西寧 呼和浩特 珠海 天水 商洛

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQL Server約束增強(qiáng)的兩點建議》,本文關(guān)鍵詞  SQL,Server,約束,增,強(qiáng)的,兩點,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL Server約束增強(qiáng)的兩點建議》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQL Server約束增強(qiáng)的兩點建議的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 闻喜县| 赤壁市| 平乐县| 三江| 望谟县| 深水埗区| 平安县| 夏邑县| 阿克| 康平县| 温宿县| 郸城县| 宣汉县| 陕西省| 安义县| 钦州市| 社会| 正镶白旗| 浦东新区| 舒兰市| 宜川县| 彭山县| 贵溪市| 乐山市| 祁连县| 读书| 电白县| 谢通门县| 大英县| 武山县| 洛扎县| 白山市| 乡宁县| 星座| 黄骅市| 安西县| 平定县| 隆子县| 宜城市| 河东区| 濉溪县|