Repository navigation
Expand file tree
/
Copy pathConstraints.sql
More file actions
92 lines (67 loc) · 2.51 KB
/
Copy pathConstraints.sql
File metadata and controls
92 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
USE [70-761]
-- ADD Unique Constraint on Email Address
UPDATE Person SET Email = 'tonystark@gmail.com' WHERE PersonID = 2
ALTER TABLE Person
ADD CONSTRAINT unq_Person_Email UNIQUE (Email)
BEGIN TRAN
--Looks for Unique Key Constraint
SELECT * FROM sys.indexes
WHERE OBJECT_NAME(object_id) = 'Person'
--Drops Constraint
ALTER TABLE Person
DROP CONSTRAINT unq_Person_Email
SELECT * FROM sys.indexes
WHERE OBJECT_NAME(object_id) = 'Person'
ROLLBACK TRAN
------------------------- Default ---------------------------------
BEGIN TRAN
--ALTER TABLE TRANSACTIONS
--DROP CONSTRAINT defDateOfEntry
--ALTER TABLE Transactions
--DROP COLUMN DateOfEntry
ALTER TABLE Transactions
ADD DateOfEntry datetime
ALTER TABLE Transactions
ADD CONSTRAINT defDateOfEntry DEFAULT GETDATE() FOR DateOfEntry
INSERT INTO Transactions(Amount,TransactionDate,PersonID)
VALUES (562,'2015-06-23',23)
SELECT * FROM Transactions WHERE PersonID = 23
ROLLBACK TRAN
-------------------- Check ----------------------------
BEGIN TRAN
--Add Check Constraint to Person Table
ALTER TABLE Person
ADD CONSTRAINT chk_Person_Email CHECK (Email LIKE '%@%')
--Test Constraint with Error Information
BEGIN TRY
INSERT INTO Person VALUES ('Louie David',27,'0978434187','louiedavid hotmail.com',DEFAULT,40)
END TRY
BEGIN CATCH
SELECT 'There was no @!' AS ErrorMsg
END CATCH
--Test Constraint with Correction Information
BEGIN TRY
INSERT INTO Person VALUES ('Louie David',27,'0978434187','louiedavid@hotmail.com',DEFAULT,40)
SELECT * FROM Person WHERE Age = 27
END TRY
BEGIN CATCH
SELECT 'There was no @!' AS ErrorMsg
END CATCH
ROLLBACK TRAN
--------------- Foreign Key --------------------
--Delete Foreign Key if Exists
IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'fk_Person_Dept' )
ALTER TABLE Person
DROP CONSTRAINT fk_Person_Dept
--Create Foreign Key on Person Table
ALTER TABLE Person
ADD CONSTRAINT fk_Person_Dept FOREIGN KEY (Department) REFERENCES Departments(DeptID)
--Delete Foreign Key if Exists
IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'fk_Transaction_Person' )
ALTER TABLE Person
DROP CONSTRAINT fk_Transaction_Person
--Create Foreign Key on Transaction Table
ALTER TABLE Transactions WITH NOCHECK
ADD CONSTRAINT fk_Transaction_Person FOREIGN KEY (PersonID) REFERENCES Person(PersonID)
--Create a Unique Clustered Index on Transaction Table
CREATE UNIQUE CLUSTERED INDEX inx_Transactions ON dbo.Transactions(Amount,[TransactionDate],PersonID)