forked from Ankit-Kum/DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS_11.sql
More file actions
253 lines (173 loc) · 6.04 KB
/
Copy pathDBMS_11.sql
File metadata and controls
253 lines (173 loc) · 6.04 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
# CREATE INDEX
/*
An index in MySQL is a
data structure (usually a B-tree) that speeds up retrieval of rows from a table.
-- MySQL supports indexes on InnoDB and MyISAM storage engines.
-- By default, PRIMARY KEY and UNIQUE constraints create indexes automatically.*/
-- you have a phone book that contains all the names and phone numbers of people in a city.
/*Let’s say you want to find Bob Cat’s phone number.
Knowing that the names are alphabetically ordered,
you first look for the page where the last name is Cat,
then you look for Bob and his phone number.
If the names in the phone book were not sorted alphabetically,
you would need to go through all the pages,
reading every name on it until you find Bob Cat.
*/
-- This is called sequential searching.
-- You go over all the entries until
-- you find the person with the phone number that you are looking for.
CREATE INDEX index_name
ON table_name (column_list)
-------------------------------
CREATE TABLE t(
c1 INT PRIMARY KEY,
c2 INT NOT NULL,
c3 INT NOT NULL,
c4 VARCHAR(10),
INDEX (c2,c3)
);
CREATE INDEX idx_c4 ON t(c4);
--------------------------------
-- By default, MySQL creates the B-Tree index if you don’t specify the index type.
# MySQL CREATE INDEX example
-- EXPLAIN SELECT
SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
jobTitle = 'Sales Rep';
-- We have 17 rows indicating that 17 employees whose job title is the Sales Rep.
-- To see how MySQL internally performed this query, you add the EXPLAIN clause at the beginning of the SELECT statement as follows:
-- As you can see, MySQL had to scan the whole table which consists of 23 rows to find the employees with the Sales Rep job title.
CREATE INDEX jobTitle
ON employees(jobTitle);
-- Execute the EXPLAIN statement again:
EXPLAIN SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
jobTitle = 'Sales Rep';
-- The output shows that MySQL just had to locate 17 rows from the jobTitle index as indicated in the key column without scanning the whole table.
--To list all indexes of a table,
SHOW INDEXES FROM employees;
# DROP INDEX statement
CREATE TABLE leads(
lead_id INT AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL,
information_source VARCHAR(255),
INDEX name(first_name,last_name),
UNIQUE email(email),
PRIMARY KEY(lead_id)
);
DROP INDEX name ON leads;
# DROP PRIMARY KEY index
CREATE TABLE t(
pk INT PRIMARY KEY,
c VARCHAR(10)
);
DROP INDEX `PRIMARY` ON t;
/*
Benefits of Indexes
Faster Query Performance
Improves the speed of SELECT queries with WHERE, JOIN, ORDER BY, GROUP BY.
Efficient Searching
Allows quick lookups instead of scanning the whole table.
Sorting Optimization
Helps avoid costly sorting operations since data can be retrieved in index order.
Uniqueness Enforcement
Unique indexes ensure no duplicate values (e.g., primary key, unique constraints).
Better JOIN Performance
Speeds up table joins by indexing foreign keys.
Downside: More storage space is needed, and INSERT, UPDATE, DELETE operations become slightly slower because indexes must also be updated.
*/
/*
Types of Indexes
Primary Index
Created automatically with the primary key. Ensures uniqueness.
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100)
);
Unique Index
Ensures that all values in the indexed column are different.
CREATE UNIQUE INDEX idx_email ON table_name(email);
Composite Index
Index on multiple columns. Useful for queries filtering by multiple conditions.
CREATE INDEX idx_name_age ON table_name(name, age);
Full-Text Index
Special index type for searching text (words/phrases) in large text fields.
CREATE FULLTEXT INDEX idx_content ON articles(content);
CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200),
body TEXT,
FULLTEXT (title, body) -- create FULLTEXT index
) ENGINE=InnoDB;
INSERT INTO articles (title, body) VALUES
('MySQL FullText Search', 'MySQL supports full-text search to find relevant words.'),
('Indexing in Databases', 'Indexes help speed up SELECT queries in SQL.'),
('Learning Python', 'Python is a great programming language for data science and AI.'),
('Database Optimization', 'Using indexes wisely can optimize database performance.');
*/
# composite index example
CREATE INDEX name
ON employees(lastName, firstName);
SELECT
firstName,
lastName,
email
FROM
employees
WHERE
lastName = 'Patterson';
EXPLAIN SELECT
firstName,
lastName,
email
FROM
employees
WHERE
lastName = 'Patterson';
-- clustered index
-- Data is physically stored in the order of the index.
-- Only one clustered index per table (usually on the primary key).
-- descending index
-- A descending index is an index that stores key values in the descending order.
CREATE TABLE t(
a INT NOT NULL,
b INT NOT NULL,
INDEX a_asc_b_desc (a ASC, b DESC)
);
------------------------------------
## Temporary Tables in MySQL
-- A temporary table in MySQL is session-specific and automatically dropped when the session ends or connection closes.
CREATE TEMPORARY TABLE temp_sales (
id INT,
product_name VARCHAR(100),
total_sales DECIMAL(10,2)
);
INSERT INTO temp_sales VALUES (1, 'Laptop', 1200.50);
SELECT * FROM temp_sales;
DROP TEMPORARY TABLE temp_sales;
/*
Scope: Only visible to the current session.
Name Reuse: You can have a temporary table with the same name as a permanent one (MySQL uses the temp version inside that session).
Performance: Useful for breaking down complex queries or caching intermediate results.
*/
----------------------------------
## ACID Properties in MySQL
ACID stands for:
Atomicity
Consistency
Isolation
Durability
These properties guarantee that database transactions are processed reliably, even in cases of crashes, errors, or multiple users accessing data at the same time.
MySQL’s InnoDB storage engine fully supports ACID.