Skip to content

Commit f797afc

Browse files
John McGaugheyJohn McGaughey
John McGaughey
authored and
John McGaughey
committed
added files for maintaining and transactions
1 parent 10147db commit f797afc

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

maintaining-sql-eddb.sql

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- DELETE STATEMENT
2+
DELETE
3+
FROM Student
4+
WHERE SAT <=900;
5+
6+
-- UPDATE statement
7+
update Student set
8+
GPA = GPA - 0.1
9+
where MajorId = (select id from Major where Description like '%Math%');
10+
11+
update Student set
12+
GPA = GPA + 0.1
13+
where majorid = (select id from Major where Description like '%Math%');
14+
15+
select *
16+
from Student
17+
where majorid = (select id from Major where Description like '%Math%');
18+
19+
-- INSERT statement
20+
INSERT Student -- primary cant be listed if sql generates it
21+
(Firstname, Lastname, StateCode, SAT, GPA, MajorId) -- SAT/MajorId can be null
22+
VALUES
23+
('John', 'McGaughey', 'KY', '1250', '2.8', '7'),
24+
('Justin', 'McGaughey', 'KY', '1000', '3.2', null);
25+
26+
DELETE from Student
27+
Where Lastname like ('%McGaughey%');
28+
29+
select Firstname as 'First Name', Lastname as 'Last Name', StateCode as 'State', sat, gpa, Description as 'Major'
30+
from Student s
31+
join major m
32+
on s.MajorId = m.Id
33+
where Lastname like ('%McG%');
34+
35+
UPDATE Student set
36+
MajorId = (select id from Major where Description like ('%Education%'))
37+
where Lastname like ('%McG%');
38+
39+
-- Create Major Underwater Basket Weaving
40+
insert Major
41+
(Code, Description, MinSAT)
42+
VALUES
43+
('BSKT', 'Underwater Basket Weaving', 12);
44+
45+
Select * from Major
46+
order by Description;
47+
48+
-- remove major Finance, put all students in Finance into the Chad Major Underwater Basket Weaving
49+
update Student set
50+
MajorId = (select id from Major where code = 'BSKT')
51+
where MajorId = (select id from Major where Description like ('%General Studies%'));
52+
53+
-- check all students now underwater
54+
select Firstname as 'First Name', Lastname as 'Last Name', StateCode as 'State', sat, gpa, Description as 'Major'
55+
from Student s
56+
join major m
57+
on s.MajorId = m.Id
58+
WHERE s.MajorId = (select id from Major where Description like ('%Under%'));
59+
60+
-- delete major general studies
61+
delete major
62+
where ID = (select id from Major where Description = 'General studies')
63+
64+
select * from major;

practice-query-EdDb.sql

+17
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,20 @@ select statecode as 'State', COUNT(*) as 'Number of Students'
112112
select *
113113
from Student
114114
where GPA = (select MAX(gpa) from Student);
115+
116+
-- CONCAT combines multiple tables into 1 string
117+
select CONCAT(Lastname,', ', Firstname) as 'name', GPA, SAT
118+
from Student
119+
order by Lastname;
120+
121+
-- FORMAT can change the format of a table to show currency
122+
-- IN SALESDB
123+
select name, FORMAT(sales, 'C')
124+
from customers;
125+
126+
-- ISNULL - will change null to a specified string
127+
select CONCAT(s.firstname, ' ', s.Lastname) as 'Name', ISNULL(Description, 'Undeclared') as 'Major'
128+
from Student s
129+
left join Major m
130+
on s.MajorId = m.Id
131+
order by s.Lastname;

transactions-sql-eddb.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- TRANSACTIONS
2+
begin transaction;
3+
4+
update major set
5+
Description = 'To be deleted'
6+
where id = 8;
7+
8+
select * from Major;
9+
10+
rollback; -- this will revert the changes to description

0 commit comments

Comments
 (0)