-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Constraints.sql
More file actions
239 lines (186 loc) · 9.72 KB
/
SQL_Constraints.sql
File metadata and controls
239 lines (186 loc) · 9.72 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
-- SQL Constraints
/*
SQL constaints are set of rules that restricts the type of data that can be inserted into a table.
They ensure data integrity and enforce certain conditions on the data stored in the database.
Constraints can be applied to the tables or columns and can be defined at the time of table creation or added afterward.
*/
-- Here are the different types of SQL constraints:
/*
1. UNIQUE
2. NOT NULL
3. PRIMARY KEY
4. FOREIGN KEY
5. IDENTITY
6. CHECK
7. DEFAULT
*/
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 1. UNIQUE:
/*
• Ensures that all values in a column are unique/distinct (no duplicate).
• A unique key constraint can be applied to multiple columns in a table.
*/
USE SQL_Server_DB
CREATE TABLE Employee_unique (
Emp_ID INT UNIQUE,
Emp_Name VARCHAR(100),
Emp_Salary INT
)
INSERT INTO Employee_unique (Emp_ID, Emp_Name, Emp_Salary)
VALUES (1, 'John Doe', 50000)
INSERT INTO Employee_unique (Emp_ID, Emp_Name, Emp_Salary)
VALUES (1, 'Jane Smith', 55000) --error: Violation of UNIQUE KEY constraint (duplicate value Emp_ID)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 2. NOT NULL:
/*
• Ensures that column cannot have a NULL value.
• A not null constraints can be applied for multiple columns in a table.
*/
CREATE TABLE Employee_notnull (
Emp_ID int UNIQUE,
Emp_Name VARCHAR(100) NOT NULL,
Emp_Email VARCHAR(100) NOT NULL
)
INSERT INTO Employee_notnull (Emp_ID, Emp_Name, Emp_Email)
VALUES (1, 'John Doe', 'john.doe@gmail.com')
INSERT INTO Employee_notnull (Emp_ID, Emp_Name, Emp_Email)
VALUES (2, null, 'jane.smith@gmail.com') --error: Violation of NOT NULL constraint (can not insert null value)
INSERT INTO Employee_notnull (Emp_ID, Emp_Name, Emp_Email)
VALUES (3, 'Emily Johnson', null) --error: Violation of NOT NULL constraint (can not insert null value)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 3. PRIMARY KEY:
/*
• Uniquely identifies each record in a table.
• A primary key combines the properties of UNIQUE and NOT NULL constraints.
• A table can have only one primary key.
• Composite primary key can consist of one or more columns.
*/
-- single column primary key
CREATE TABLE Employee_primary (
Emp_ID INT PRIMARY KEY,
Emp_Name VARCHAR(100) NOT NULL,
Emp_Email VARCHAR(100) UNIQUE
)
INSERT INTO Employee_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (1, 'John Doe', 'john.doe@gmail.com')
INSERT INTO Employee_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (1, 'Jane Smith', 'jane.smith@gmail.com') --error: Violation of PRIMARY KEY constraint (duplicate value Emp_ID)
INSERT INTO Employee_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (null, 'Emily Johnson', 'emily.johnson@gmail.com') --error: Violation of PRIMARY KEY constraint (can not insert null value)
INSERT INTO Employee_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES
(2, 'Robert Brown', 'robert.brown@gmail.com'),
(3, 'David Williams', 'david.williams@gmail.com')
-- composite primry key
CREATE TABLE Employee_composite_primary (
Emp_ID INT,
Emp_Name VARCHAR(100),
Emp_Email VARCHAR(100),
PRIMARY KEY(Emp_Name, Emp_Email)
)
INSERT INTO Employee_composite_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (1, 'John Doe', 'john.doe@gmail.com')
INSERT INTO Employee_composite_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (2, 'Jane Smith', null) --error: Violation of PRIMARY KEY constraint
INSERT INTO Employee_composite_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (3, 'Jane Smith', 'jane.smith@gmail.com') --no-error because both Emp_Name and Emp_Email should not be repeated
INSERT INTO Employee_composite_primary (Emp_ID, Emp_Name, Emp_Email)
VALUES (4, 'Jane Smith', 'jane.smith@gmail.com') --error: Violation of Composite ORIMARY KEY constaint (two records are same)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 4. FOREIGN KEY:
/*
• Foreign key creates a relationship between two tables.
• The value in the foreign key column(s) must match values in the primary key or unique column(s) of the referenced table.
• Foreign key enforces referential integrity meaning it prevents inconsistent data or orphan records.
• A table can have multiple foreign keys, each linking to different tables.
*/
CREATE TABLE Department_foreign (
Dept_ID INT,
Dept_Name VARCHAR(100),
Emp_ID INT FOREIGN KEY REFERENCES Employee_primary (Emp_ID)
)
INSERT INTO Department_foreign (Dept_ID, Dept_Name, Emp_ID)
VALUES (11, 'HR', 1)
INSERT INTO Department_foreign (Dept_ID, Dept_Name, Emp_ID)
VALUES
(12, 'Marketing', 2),
(11, 'HR', 3)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 5. INDENTITY:
/*
• The IDENTITY constraint automatically generates a unique numeric value for each new row inserted into a table.
• This is typically used for primary keys where a unique, sequential identifier is required.
• You cannot have multiple identity columns in a table because only one column serves as the uniqui identifier (often a primary key).
*/
CREATE TABLE Employee_identity (
Emp_ID INT PRIMARY KEY IDENTITY,
Emp_Name VARCHAR(100),
Emp_Email VARCHAR(100)
)
INSERT INTO Employee_identity (Emp_Name, Emp_Email)
VALUES
('John Doe', 'john.doe@gmail.com'),
('Jane Smith', 'jane.smith@hmail.com'),
('Emily Johnson', 'emily.johnson@gmail.com')
SELECT * FROM Employee_identity
-- with seed and increment value
CREATE TABLE Employee_identity_seedincrement (
Emp_ID INT PRIMARY KEY IDENTITY(10, 1), -- 10 is seed (starting) and 1 is increment value (step)
Emp_Name VARCHAR(100),
Emp_Email VARCHAR(100)
)
INSERT INTO Employee_identity_seedincrement (Emp_Name, Emp_Email)
VALUES
('John Doe', 'john.doe@gmail.com'),
('Jane Smith', 'jane.smith@hmail.com'),
('Emily Johnson', 'emily.johnson@gmail.com')
SELECT * FROM Employee_identity_seedincrement
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 6. CHECK:
/*
• Ensures that the value in a column satisfies a specific condition or expression.
• It's a way to enforce data integrity by restricting the values tha can be inserted or updated in a table.
• You can multiple check constraints in a table.
*/
-- single check constraint
CREATE TABLE Employee_check (
Emp_ID INT PRIMARY KEY IDENTITY,
Emp_Name VARCHAR(100),
Emp_Email VARCHAR(100),
Emp_Age INT CHECK (Emp_Age > 21)
)
INSERT INTO Employee_check (Emp_Name, Emp_Email, Emp_Age)
VALUES ('John Doe', 'john.doe@gmail.com', 23)
INSERT INTO Employee_check (Emp_Name, Emp_Email, Emp_Age)
VALUES ('Jane Smith', 'jane.smith@gmail.com', 18) --error: INSERT statement conflicted with the CHECK constraint
-- multiple check constraint
CREATE TABLE Employee_check_multiple (
Emp_ID INT PRIMARY KEY IDENTITY,
Emp_Name VARCHAR(100),
Emp_Email VARCHAR(100),
Emp_Age INT CHECK (Emp_Age > 18),
Emp_Salary INT CHECK (Emp_Salary < 30000)
)
INSERT INTO Employee_check_multiple (Emp_Name, Emp_Email, Emp_Age, Emp_Salary)
VALUES ('John Doe', 'john.doe@gmail.com', 23, 25000)
INSERT INTO Employee_check_multiple (Emp_Name, Emp_Email, Emp_Age, Emp_Salary)
VALUES ('Jane Smith', 'jane.smith@gmail.com', 17, 37000) --error: INSERT statement conflicted with the CHECK constraint
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 7. DEFAULT:
/*
• Default constraint provide default value for a column when no value is specified during an INSERT operation.
• If a value for the column is not provided, the column will automatically take the default value specified by the constraint.
• You can multiple default constraint in a table.
*/
CREATE TABLE Employee_default (
Emp_ID INT PRIMARY KEY IDENTITY,
Emp_Name VARCHAR(100),
Emp_Age INT DEFAULT 21,
Emp_Department VARCHAR(100) DEFAULT 'IT'
)
INSERT INTO Employee_default (Emp_Name, Emp_Age, Emp_Department)
VALUES ('John Doe', 23, 'HR')
INSERT INTO Employee_default (Emp_Name)
VALUES ('Jane Smith') --no-error: it takes default value
SELECT * FROM Employee_default
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------