This repository was archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicDatabaseCode.txt
More file actions
526 lines (319 loc) · 9.42 KB
/
basicDatabaseCode.txt
File metadata and controls
526 lines (319 loc) · 9.42 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// To start CMD DB in terminal
cd c:\xampp\mysql\bin
mysql.exe -u root
C:\xampp\mysql\bin\mysql.exe -u root -p
// To show databases
show databses;
// To create database
CREATE DATABASE db_name;
// To select database
USE database_name;
// To delete database
drop database db_name;
// CREATE TABLE
CREATE TABLE table_name (
P_ID int,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
// To view table from a database
show tables;
// Show a table
SELECT * FROM table_name;
// Description of a table
DESCRIBE table_name;
// Query code of describe table
show create table table_name;
// Delete a table
DROP TABLE table_name;
// Adding a column or attribute in a table
ALTER TABLE table_name
ADD Email VARCHAR (255);
// Insert values or record in a table
INSERT INTO persons
VALUES(003, 'Mahmud', 'Nishat', 'Narinda', 'Dhaka', 'nishatrhythm@gmail.com');
// Insert values in specific column
INSERT INTO persons(P_ID, LastName, FirstName)
VALUES(001, 'Hasan', 'Rizwan');
// Delete a column or attribute
ALTER TABLE persons
DROP COLUMN Email;
// Update cell value
UPDATE persons
SET P_ID = 1
WHERE P_ID = 0;
// Add PRIMARY KEY (can't be NULL)
create table Product(
name char(30) Primary key,
category varchar(255),
price int Primary Key
);
OR
create table Product(
name char(30),
category varchar(255),
price int,
Primary Key(name, category)
);
// Set primary key after table creation
ALTER TABLE table_name
MODIFY P_ID int PRIMARY KEY;
OR,
ALTER TABLE table_name
ADD PRIMARY KEY(P_ID);
// DELETE OR DROP PRIMARY KEY
alter table student
drop primary key;
// NOT NULL Field
create table notnullfield(
PersonID int NOT NULL,
FirstName varchar(100) NOT NULL,
LastName varchar(100) Not null,
Age int
);
// NOT NULL Field After Table Creation
Alter Table notnullfield
MODIFY Age int NOT NULL;
// INSERT NULL Values in a table, NO ERROR
insert into notnullfield(PersonID, FirstName, LastName, Age)
Values(203, '', '', 30);
// INSERT NULL Values in a table, ERROR!
insert into notnullfield(PersonID, FirstName, LastName, Age)
Values(203, NULL, NULL, 30);
// UNIQUE Constraint (accept NULL values)
create table uniquefield(
P_ID int UNIQUE,
LastName varchar(255) Not Null,
City varchar(255)
);
OR,
create table uniquefield(
P_ID int,
FirstName varchar(255) Not Null,
LastName varchar(255) Not Null,
City varchar(255),
CONSTRAINT uc_ID UNIQUE (P_ID)
);
// UNIQUE Constraint after table creation
alter table uniquefield
add UNIQUE (Email);
// CHANGE / UPDATE column name
alter table student
change column old_heading
new_heading int NOT NULL;
// AUTO Increment
create table animals(
id int not null AUTO_INCREMENT,
name char(30) not null,
primary key (id)
);
// Multiple value insert for same column
insert into animals(name)
values('Cat'), ('Whale');
// Reset AUTO Increment
alter table animals auto_increment = 1000;
//SQL Check Constraints
create table persons(
P_ID int not null Check(P_ID>0),
LastName varchar(255) not null,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
// DEFAULT VALUE while table creation
create table users(
userID int Not null,
salary decimal(18,2) Default 5000.00,
country varchar(255) Default 'Bangladesh',
);
// DEFAULT VALUE after table creation
Alter table users
Add JoinDate DATETIME DEFAULT CURRENT_TIMESTAMP();
// DELETE or DROP a DEFAULT Value
Alter Table users
Alter country Drop Default;
// CASECADE FOREIGN KEY
create table customer(
CustomerID INT(11) NOT NULL PRIMARY KEY,
Name Varchar(30) Not Null,
City Varchar(30)
);
create table orders(
OrderID int NOT NULL PRIMARY KEY,
productName varchar (100) NOT NULL,
orderCustomerID int NOT NULL,
FOREIGN KEY(orderCustomerID) REFERENCES customer(CustomerID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
insert into customer value(1001, 'Nishat', 'Dhaka');
insert into customer value(1002, 'Rizwan', 'Mymensingh');
insert into customer value(1003, 'Safwan', 'Mymensingh');
insert into orders value(1, 'Mobile', 1001);
insert into orders value(2, 'Fan', 1001);
insert into orders value(3, 'Fan', 1002);
insert into orders value(4, 'Light', 1002);
// SELECT specific column from table
select productName, orderCustomerID from orders;
// SELECT specific column from table with no repetition
select distinct productName from orders;
// SELECT COMPLEX / Others
SELECT * FROM Persons
WHERE LastName='Svendson'
AND (FirstName='Tove' OR FirstName='Ola');
select loan_number,amount from table_name
where branch_name='Perryridge' and amount>=1000; and amount<=1400
where branch_name='Perryridge' and amount between 1000 and 1400
select loan_number, branch_name, amount from loan
where branch_name IN / NOT IN('Round Hill','Perryridge');
//NOT EQUAL
!= <>
// DELETE Row or DELETE Record from a table
Delete from table_name
where Name = 'Nishat';
// SQL JOIN
create table suppliers(
supplier_id int,
supplier_name char(255),
primary key(supplier_id)
);
create table orders(
order_id int,
supplier_id int,
order_date date,
primary key(order_id)
);
insert into suppliers values(10000,'IBM');
insert into suppliers values(10001,'Hewlett Packard');
insert into suppliers values(10002,'Microsoft');
insert into suppliers values(10003,'NVIDIA');
insert into orders values(5000,10000,now());
insert into orders values(5001,10001,curdate());
insert into orders values(5003,10004,'2020-05-1');
// INNER Join
SELECT *
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
// CROSS Join
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_id, orders.order_date
FROM suppliers
CROSS JOIN orders;
// Natural Join
SELECT * FROM suppliers
NATURAL JOIN orders;
// Left Outer Join
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
SELECT *
FROM suppliers as S
LEFT JOIN orders as O
ON S.supplier_id = O.supplier_id;
//Right Outer Join
SELECT orders.order_id,orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
SELECT *
FROM suppliers as S
RIGHT JOIN orders as O
ON S.supplier_id = O.supplier_id;
// Full Outer Join
SELECT *
FROM suppliers as S
LEFT JOIN orders as O
ON S.supplier_id = O.supplier_id
UNION
SELECT *
FROM suppliers as S
RIGHT JOIN orders as O
ON S.supplier_id = O.supplier_id;
// Self Join
select F.EmployeeID, F.LastName, S.EmployeeID, S.LastName, F.Country
from Employee F, Employee S
where F.Country = S.Country and F.EmployeeID < S.EmployeeID
Order by F.EmployeeID, S.EmployeeID;
select B.branch_name, B2.branch_name, B.branch_city
from branch as B, branch as B2
where B.branch_city=B2.branch_city and B.branch_name < B2.branch_name
order by B.branch_name
// SQL AVG() Average
Select avg(orderPrice) from orders;
select avg(assets) as AVGASSET from branch;
select branch_city, avg(assets) as AVGASSET from branch
GROUP By branch_city;
// SQL COUNT() Count
SELECT COUNT(*) AS NumberOfOrders FROM Orders;
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Nilsen';
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders;
// SQL MAX() and MIN() Maximum and Minimum
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders;
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders;
// SQL SUM() Summation
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders;
// SQL GROUP BY
select branch_name, avg(balance) from account
group by branch_name;
select branch_name, count(distinct customer_name) from depositor, account
where depositor.account_no=account.account_no
group by branch_name;
// SQL HAVING
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000;
select student_id, avg(marks) from student_result
group by student_id
having avg(marks)>50;
// SQL ORDER BY Ascending (ASC) / Descending (DESC)
SELECT * FROM Persons
ORDER BY LastName DESC;
select * from student
order by dept_no asc,
student_name desc;
// SQL TOP
SELECT TOP 2 * FROM Persons;
SELECT TOP 50 PERCENT * FROM Persons;
SELECT * FROM loan
ORDER BY amount DESC
LIMIT 2, 4;
// SQL LIKE and STRING and Wildcards
SELECT * FROM Persons WHERE City LIKE 's%'; //starts with s
SELECT * FROM Persons WHERE City LIKE '%s'; //endss with s
SELECT * FROM Persons WHERE City LIKE or NOT LIKE '%tav%';
SELECT * FROM Persons WHERE City LIKE 'sa%';
SELECT * FROM Persons WHERE City LIKE 'sa%';
SELECT * FROM Persons WHERE FirstName LIKE '_la';
SELECT * FROM Persons WHERE LastName LIKE 'S_end_on';
select customer_name from customer where customer_name like '_____'; //exactly FIVE characters
// SQL IN
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen');
// SQL UNION or INTERSECT or MINUS
select customer_name from depositor
UNION or UNION ALL or INTERSECT or INTERSECT ALL or MINUS or MINUS ALL
select customer_name from borrower;
// SQL TRUNCATE, removes the complete data without removing its structure
TRUNCATE TABLE table_name;
// SQL View
CREATE VIEW minimumPriceView AS
SELECT items_name FROM items
WHERE items_cost<10000;
SHOW FULL TABLES
WHERE table_type='VIEW';
SELECT * FROM minimumPriceView;
// SQL Index
SELECT items_name from items
WHERE items_cost=2000;
EXPLAIN SELECT items_name from items
WHERE items_cost=2000;
CREATE INDEX indexCost
ON items (items_cost);
SHOW INDEXES FROM table_name;
DROP INDEX indexName
ON tableName;