-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.CRUD Commands.sql
146 lines (87 loc) · 2.72 KB
/
3.CRUD Commands.sql
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
/* CRUD COMMANDS
C=Create,
R=Rename,
U=Update,
D=delete,*/
show databases;
use my_cat;
/*Preparing Our Data
Let's drop the existing cats table:*/
DROP TABLE cats;
/*Recreate a new cats table:*/
CREATE TABLE cats
(
cat_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
breed VARCHAR(100),
age INT,
PRIMARY KEY (cat_id)
);
DESC cats;
/*And finally insert some new cats:*/
INSERT INTO cats(name, breed, age)
VALUES ('Ringo', 'Tabby', 4),
('Cindy', 'Maine Coon', 10),
('Dumbledore', 'Maine Coon', 11),
('Egg', 'Persian', 4),
('Misty', 'Tabby', 13),
('George Michael', 'Ragdoll', 9),
('Jackson', 'Sphynx', 7);
/*Various Simple SELECT statements:*/
SELECT * FROM cats;
SELECT name FROM cats;
SELECT age FROM cats;
SELECT cat_id FROM cats;
SELECT name, age FROM cats;
SELECT cat_id, name, age FROM cats;
SELECT age, breed, name, cat_id FROM cats;
SELECT cat_id, name, age, breed FROM cats;
/*Introduction to WHERE*/
/*Select by age:*/
SELECT * FROM cats WHERE age=4;
/*Select by name:*/
SELECT * FROM cats WHERE name='Egg';
/*Notice how it deals with case:*/
SELECT * FROM cats WHERE name='egG';
/*CODE: Select Challenges Solution*/
SELECT cat_id FROM cats;
SELECT name, breed FROM cats;
SELECT name, age FROM cats WHERE breed='Tabby';
SELECT cat_id, age FROM cats WHERE cat_id=age;
SELECT * FROM cats WHERE cat_id=age;
/*CODE: Introduction to Aliases*/
SELECT cat_id AS id, name FROM cats;
SELECT name AS 'cat name', breed AS 'kitty breed' FROM cats;
DESC cats;
/*CODE: Updating Data
Change tabby cats to shorthair:*/
UPDATE cats SET breed='Shorthair' WHERE breed='Tabby';
/*Another update:*/
UPDATE cats SET age=14 WHERE name='Misty';
/*CODE: Update Challenges Solution*/
SELECT * FROM cats WHERE name='Jackson';
UPDATE cats SET name='Jack' WHERE name='Jackson';
SELECT * FROM cats WHERE name='Jackson';
SELECT * FROM cats WHERE name='Jack';
SELECT * FROM cats WHERE name='Ringo';
UPDATE cats SET breed='British Shorthair' WHERE name='Ringo';
SELECT * FROM cats WHERE name='Ringo';
SELECT * FROM cats;
SELECT * FROM cats WHERE breed='Maine Coon';
UPDATE cats SET age=12 WHERE breed='Maine Coon';
SELECT * FROM cats WHERE breed='Maine Coon';
/*CODE: DELETING DATA*/
DELETE FROM cats WHERE name='Egg';
SELECT * FROM cats;
SELECT * FROM cats WHERE name='egg';
DELETE FROM cats WHERE name='egg';
SELECT * FROM cats;
DELETE FROM cats;
/*CODE: DELETE Challenges Solution*/
SELECT * FROM cats WHERE age=4;
DELETE FROM cats WHERE age=4;
SELECT * FROM cats WHERE age=4;
SELECT * FROM cats;
SELECT * FROM cats WHERE cat_id=age;
DELETE FROM cats WHERE cat_id=age;
DELETE FROM cats;