-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy path05.CRUD Commands.sql
70 lines (55 loc) · 1.22 KB
/
05.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
/*
data preparation
*/
DROP TABLE cats;
CREATE TABLE cats
(
cat_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
breed VARCHAR(100),
age INT,
PRIMARY KEY (cat_id)
);
DESC 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);
#--------------------------#
#id only
SELECT cat_id FROM cats;
#name,breed columns
SELECT name,breed
FROM cats;
#just Tabby breed cats only
SELECT name,age
FROM cats
WHERE breed LIKE 'Tabby';
#cat id same as age
SELECT cat_id,age
FROM cats
WHERE cat_id = age;
#Change Jackson's name to "Jack"
UPDATE cats
SET name = 'Jack'
WHERE name ='Jackson';
#Change Ringo's breed to "British Shorthair"
UPDATE cats
SET breed = 'British Shorthair'
WHERE name = 'Ringo';
#Update both Maine Coons' ages to be 12
UPDATE cats
SET age = 12
WHERE breed = 'Maine Coon';
#DELETE all 4 year old cats
DELETE FROM cats
WHERE age = 4;
#DELETE cats whose age is the same as their cat_id
DELETE FROM cats
WHERE cat_id = age;
#DELETE all cats
DELETE FROM cats;