亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SQL Server中怎么實現約束增強

發布時間:2021-08-05 15:08:50 來源:億速云 閱讀:143 作者:Leah 欄目:數據庫

SQL Server中怎么實現約束增強,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

在許多情況下,對外鍵使用更復雜的邏輯表達式是非常有用的。 此外,在某些情況下能夠在索引視圖創建約束也將非常實用。 我將舉例說明,同時我希望針對此文的投票鏈接會盡快加上。當外鍵中需要更為復雜的邏輯表達式時 考慮下面的簡單常識: 您的設備的最大電流不能超過您插入到它的電路的最大電流。 假設下面的表存儲電路和設備數據:復制代碼 代碼如下: 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: 一個非常簡便的命令就可能實現這個業務規則: 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"發生沖突。 該沖突發生在數據庫"Test"的"data.Devices"表。 該語句被終止執行。 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. 可以看出,一個非常簡單而普通的業務規則實現起來也相當繁雜,因為數據庫引擎并不直接支持這種業務規則。 When you want to create constraints on indexed views 在索引視圖上創建約束 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: 盡管數據庫保證“您的設備的最大電流不能超過您插入到它的電路的最大電流”,但這還不夠。請看下列示例數據: 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: 數據庫中的數據表明可以插入一個以上的設備到電路,這沒有錯,可是當所有的設備都打開時,它們的最大電流之和會超過電路最大電流。為了加強這個業務規則,很自然的會創建一個索引視圖以使數據庫保證電流之和總是正確的。 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: 如果能在該索引視圖上創建一個約束,我將進行這樣的設置: 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. 實際上,我必須使用觸發器或者精心拼湊Check約束來實現。如果數據庫內置支持這種相當普遍的業務規則,那將會增加SQL Server的實用性 。

看完上述內容,你們掌握SQL Server中怎么實現約束增強的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

中牟县| 肃宁县| 桓台县| 简阳市| 平昌县| 株洲市| 富阳市| 曲麻莱县| 刚察县| 石狮市| 岫岩| 宁波市| 黔西县| 南宫市| 金川县| 锡林郭勒盟| 台州市| 县级市| 南康市| 惠州市| 察雅县| 祁连县| 塘沽区| 台南县| 平山县| 门头沟区| 库伦旗| 林西县| 武强县| 昌吉市| 新营市| 蕉岭县| 南康市| 霸州市| 延安市| 湖口县| 澳门| 海阳市| 吉林市| 青龙| 进贤县|