Last active
January 21, 2025 12:15
-
-
Save ritacse/5a8470f422d098e80b13d56653d9cfa4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE SECTION( | |
c# int FOREIGN KEY REFERENCES Course(c#), | |
se# int, | |
emp# int FOREIGN KEY REFERENCES Professor(EMP#), | |
class_time time, | |
controlNumber int DEFAULT 20, | |
CONSTRAINT pk_SectionID PRIMARY KEY (c#, se#), | |
-----Adding Conditional constraint here --- | |
CONSTRAINT chk_controlNumber CHECK ( | |
controlNumber between 40 AND 60 and (c# < 3000 or c# > 5000) or | |
(c# between 3000 and 5000 and controlNumber <= 40)) | |
----Remove constraint | |
ALTER TABLE TableName | |
DROP CONSTRAINT constraintName; | |
---- Add UNIQUE constraint | |
ALTER TABLE Persons | |
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName); | |
--//Example: | |
ALTER TABLE PurchaseOrderChild_UnlockHistory | |
ADD CONSTRAINT UK_PurOrderChild_UnlockHistory UNIQUE ( | |
[PurOrderMasterID] ASC, [PurReqMasterID] ASC, | |
[ItemID] ASC, [Version] ASC | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- Conditional constraint using FUNCTION | |
CREATE FUNCTION CheckActiveCount( | |
@Id INT | |
) RETURNS INT | |
AS | |
BEGIN | |
DECLARE @ret INT; | |
SELECT @ret = COUNT(*) FROM CheckConstraint WHERE Id = @Id AND RecordStatus = 1; | |
RETURN @ret; | |
END; | |
GO | |
--- Add Constraint using function | |
ALTER TABLE CheckConstraint | |
ADD CONSTRAINT CheckActiveCountConstraint CHECK (NOT (dbo.CheckActiveCount(Id) > 1 AND RecordStatus = 1)); | |
----Remove constraint | |
ALTER TABLE TableName | |
DROP CONSTRAINT constraintName; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment