forked from Ankit-Kum/DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS_12.sql
More file actions
377 lines (242 loc) · 12.1 KB
/
Copy pathDBMS_12.sql
File metadata and controls
377 lines (242 loc) · 12.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Transaction Control Language (TCL)
-- COMMIT
Permanently saves changes made in the current transaction.
COMMIT;
-- ROLLBACK
Undoes all changes in the current transaction (back to the last START TRANSACTION).
ROLLBACK;
-- SAVEPOINT
Sets a "checkpoint" in the transaction.
SAVEPOINT sp1;
-- ROLLBACK TO SAVEPOINT
Reverts back to a specific savepoint.
RELEASE SAVEPOINT sp1;
-- DCL Commands (GRANT/REVOKE/GRANT OPTION)
# The GRANT command is used to give specific privileges to a user or role on a particular dat
abase object (e.g., a table, view, or stored procedure).
-- GRANT → Gives privileges to a user.
-- REVOKE → Removes privileges from a user.
GRANT privilege_name [, privilege_name ...]
ON object_name
TO user_name [, user_name ...]
[WITH GRANT OPTION];
privilege_name: The specific action you want to allow. Common privileges include:
SELECT: Allows a user to read data from an object.
INSERT: Allows a user to add new data.
UPDATE: Allows a user to modify existing data.
DELETE: Allows a user to delete data.
ALL: Grants all available privileges.
EXECUTE: Allows a user to execute a stored procedure or function.
object_name: The name of the database object to which you are granting the privileges.
user_name: The user or role to whom you are granting the privileges.
WITH GRANT OPTION: This is a crucial clause. If you include WITH GRANT OPTION, the user you are granting privileges to can, in turn, grant those same privileges to other users.
-- created a user
CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'testpassword';
CREATE DATABASE test_database;
USE test_database;
CREATE TABLE MyTable(data VARCHAR(255));
GRANT SELECT ON test_database.MyTable TO 'test_user'@'localhost';
SHOW GRANTS FOR 'test_user'@'localhost';
GRANT SELECT, UPDATE ON
TABLE sample TO test_user;
-- multiple user
GRANT SELECT, INSERT, UPDATE ON
TABLE sample TO test_user1, test_user2, test_user3;
-- Database Level Privileges
GRANT SELECT, INSERT, UPDATE
ON test.* TO 'test_user'@'localhost';
-- Column Level Privileges
CREATE TABLE Employee (
ID INT, Name VARCHAR(15), Phone INT, SAL INT);
/*Following query grants SELECT privilege to the user named 'test_user'@'localhost' on the ID column and INSERT and UPDATE privileges on the columns Name and Phone of the Employee table*/
GRANT SELECT (ID), INSERT (Name, Phone)
ON Employee TO 'test_user'@'localhost';
GRANT SELECT, INSERT ON mydb.* TO 'user1'@'localhost';
GRANT SELECT, INSERT, UPDATE ON mydb.* TO 'alice'@'localhost';
-- REVOKE
Takes back privileges from a user.
REVOKE INSERT ON mydb.* FROM 'user1'@'localhost';
REVOKE INSERT, UPDATE ON mydb.* FROM 'alice'@'localhost';
# with GRANT OPTION
Allows a user to pass on privileges to others.
GRANT SELECT ON mydb.* TO 'bob'@'localhost' WITH GRANT OPTION;
-- Bob can now grant SELECT to another user.
-- Global level → Applies to all databases.
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';
-- Database level → Applies to all tables in a database.
GRANT ALL PRIVILEGES ON sales.* TO 'manager'@'localhost';
--Table level → Specific table.
GRANT SELECT, INSERT ON sales.orders TO 'staff'@'localhost';
-- Column level → Specific columns in a table.
GRANT SELECT(order_id, customer_id) ON sales.orders TO 'auditor'@'localhost';
/*
WITH GRANT OPTION is a clause used with the GRANT
command in SQL that gives a user or role the authority
to pass on the privileges they've received to other users.
It's a key part of Data Control Language (DCL)
and is used to delegate a user's permission management.
*/
-- A database administrator (DBA) wants to give a team lead (lead_dev) the power to manage access to the projects table for their team.
GRANT SELECT, INSERT ON projects TO lead_dev WITH GRANT OPTION;
-- lead_dev can now SELECT and INSERT data into the projects table.
-- Crucially, they can also grant these same SELECT and INSERT privileges to other users.
# lead_dev then wants to give a junior developer (junior_dev) the ability to view and add project data.
-- This command is run by lead_dev, not the DBA
GRANT SELECT, INSERT ON projects TO junior_dev;
--junior_dev can now SELECT and INSERT data.
-- However, because lead_dev did not include WITH GRANT OPTION in their command, junior_dev cannot pass these privileges on to anyone else.
REVOKE SELECT, INSERT ON projects FROM lead_dev;
## ACID Properties
CREATE TABLE accounts (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
balance DECIMAL(10,2) CHECK (balance >= 0) -- ensures Consistency
) ENGINE=InnoDB; -- InnoDB is required for ACID
INSERT INTO accounts (name, balance) VALUES
('Alice', 1000.00),
('Bob', 500.00);
/*
ACID ensures reliable processing of database transactions.
A – Atomicity
Definition: A transaction must be all or nothing.
Either every step completes successfully,
or the whole transaction is rolled back.
Example:
Imagine transferring $500 from Alice’s bank account to Bob’s.
Step 1: Deduct $500 from Alice.
Step 2: Add $500 to Bob.
If Step 1 succeeds but Step 2 fails (say, system crash), the
transaction should roll back, so Alice doesn’t lose money
without Bob receiving it.
*/
START TRANSACTION;
-- Step 1: Deduct $200 from Alice
UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice';
-- Step 2: Add $200 to Bob
UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob';
-- Step 3: Commit transaction
COMMIT;
-- 👉 If both succeed → COMMIT
-- 👉 If any fails (e.g., negative balance, error) → ROLLBACK
[-- If Step 2 fails (e.g., system crash), Step 1 is rolled back automatically → Atomicity.
-- Step 3: Try to add $300 to Bob (but introduce an error)
UPDATE accounts SET balance = balance + 300 WHERE name = 'Bobby';
-- ❌ This will fail because 'Bobby' doesn’t exist
-- Transaction is now in an error state
-- Step 4: Rollback everything
ROLLBACK;
-- Step 5: Check balances (Alice’s money should be restored)
SELECT * FROM accounts;]
/*
C – Consistency
Definition: A transaction must take the database from one valid state to
another valid state, preserving rules, constraints, and data integrity.
Example:
Suppose Alice has $1000. If the bank rule is that account balance can’t go negative,
then transferring $1500 must fail.
This ensures the database remains in a consistent state
(no negative balance).
*/
SELECT SUM(balance) AS total_balance FROM accounts;
/*
Before transfer → 1500
After transfer → 1500
→ Total money in system remains same → Consistency.
Also, the CHECK (balance >= 0) prevents overdrafts.
*/
/*
I – Isolation
Definition: Multiple transactions running at
the same time must not interfere with each other.
Each should behave as if it’s the only one executing.
Example:
Alice transfers $500 to Bob.
At the same time, Bob checks his balance.
Bob should either see the balance before the
transfer or after the transfer,
but not a halfway state where Alice’s money
is deducted but not yet credited to Bob.
*/
--Session A:
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice';
-- (not committed yet)
--Session B (while A is open):
SELECT * FROM accounts WHERE name = 'Alice';
/*
At REPEATABLE READ (default): Session B still sees Alice’s old balance until Session A commits.
This prevents dirty reads → Isolation.
*/
/*
D – Durability
Definition: Once a transaction is committed,
the data should be permanently saved,
even if the system crashes right after.
Example:
If Alice transfers $500 to Bob and the bank confirms
the transfer, the change must remain in the database—even
if there’s a sudden power failure or system restart.
START TRANSACTION;
UPDATE accounts SET balance = balance + 200 WHERE id = 2;
COMMIT;
Run a transaction and COMMIT;.
Simulate crash (e.g., kill MySQL process). (Even if MySQL crashes right now, Bob’s balance increase is permanently saved because of redo logs in InnoDB.)
Restart MySQL.
Data is still there because InnoDB Redo Logs replay committed changes.
*/
Real-Time Scenario (Banking Transaction)
Alice sends $500 to Bob.
Atomicity: Either both debit and credit happen, or neither.
Consistency: Total money in the system remains the same.
Isolation: Other people checking balances don’t see partial updates.
Durability: Once transfer is done, it remains forever in the database.
# Schema
The schema defines the logical design of the database. its the unchanging framework that defines how data is organized.
It is the static, unchanging structure that dictates how data is organized.
Think of it as the rules and framework for the database.
Tables: The names of the tables in the database.
Columns: The names of the columns within each table.
Data types: The type of data each column can hold (e.g., INTEGER, VARCHAR, DATE).
Relationships: The relationships between different tables, often defined by primary and foreign keys.
Constraints: Rules that govern the data, such as NOT NULL, UNIQUE, and CHECK constraints.
# Instance
The instance is the actual content of the database at a specific point in time.
It is the dynamic data that lives within the structure defined by the schema.
Think of it as the current state or snapshot of the database.
The instance includes all the rows and values in the databases tables.
Example: Using the students schema from above, an instance would be the actual data in the table:
| student_id | name | major |
| 1 | Alice | Computer Science |
| 2 | Bob | Physics |
| 3 | Charlie | Biology |
This instance is what changes every time a student is added,
deleted, or has their information updated.
These changes are made using Data Manipulation Language (DML) commands
like INSERT, UPDATE, and DELETE. The database can have many different instances over time,
but it only has one schema.
# MySQL Storage Engines (InnoDB, MyISAM and others),
InnoDB
InnoDB is the default and most widely used storage engine for MySQL since version 5.5.
Its a transactional engine, making it suitable for high-concurrency, high-traffic applications
that require data integrity.
Key Features:
/*
Transactions (ACID Compliance): Guarantees that database operations are reliable and processed as a single, atomic unit. This is crucial for applications like financial systems.
Row-Level Locking: Locks individual rows instead of entire tables during writes. This significantly increases concurrency and performance, as multiple users can write to different rows in the same table simultaneously.
Foreign Key Support: Enforces referential integrity between tables, ensuring that relationships between data remain consistent.
Crash Recovery: Uses a transaction log to automatically recover the database to a consistent state after a crash or power failure.
*/
MyISAM
MyISAM was the default storage engine before MySQL 5.5.
It is non-transactional and designed for read-heavy applications
where speed is more important than transactional integrity.
Key Features:
/*
Table-Level Locking: Locks the entire table during write operations. This is a major drawback for high-concurrency environments, as it can cause a "bottleneck" where users have to wait for the whole table to be unlocked.
No Transactions: Lacks ACID compliance, meaning that data integrity is not guaranteed in the event of a crash.
Fast Read Operations: MyISAM is often faster for read-only operations because of its simpler structure and lack of transactional overhead.
Full-Text Search: Provides built-in support for full-text indexing, making it a good choice for search-heavy applications.
*/
Memory: Stores all data in RAM. Its extremely fast for read operations but loses all data when the MySQL server is restarted. Best for temporary, session-specific, or read-only caches.
CSV: Stores tables as plain text CSV files. Data can be manipulated from outside the MySQL server using spreadsheet software. Its useful for importing or exporting data but lacks indexing and is not suitable for complex queries.
Archive: Optimized for high-speed insertion of data and compression. Its used for storing historical data that is rarely accessed, as it does not support UPDATE or DELETE operations.