The Basics of CRUD
Create
Read
Update
Delete
This Should Be Review
INSERT INTO
INSERT INTO cats(name, age)
VALUES('Taco', 14);
Create
Read
Update
Delete
Let's start with a clean slate...
DROP TABLE cats;
CREATE TABLE cats
(
cat_id INT AUTO_INCREMENT,
name VARCHAR(100),
breed VARCHAR(100),
age INT,
PRIMARY KEY (cat_id)
);
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);
How do we retrieve and search data?
SELECT * FROM cats;
Remember This?
SELECT * FROM cats;
"Give Me All Columns"
SELECT name FROM cats;
SELECT age FROM cats;
SELECT name, age FROM cats;
Not just to SELECT...
SELECT * FROM cats WHERE age=4;
SELECT * FROM cats WHERE name='Egg';
Rapid Fire Exercises
+--------+
| cat_id |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+--------+
Write the SQL that selects the following:
+----------------+------------+
| name | breed |
+----------------+------------+
| Ringo | Tabby |
| Cindy | Maine Coon |
| Dumbledore | Maine Coon |
| Egg | Persian |
| Misty | Tabby |
| George Michael | Ragdoll |
| Jackson | Sphynx |
+----------------+------------+
Write the SQL that selects the following:
+-------+------+
| name | age |
+-------+------+
| Ringo | 4 |
| Misty | 13 |
+-------+------+
Write the SQL that selects the following:
(Just the Tabby cats)
+--------+------+
| cat_id | age |
+--------+------+
| 4 | 4 |
| 7 | 7 |
+--------+------+
Write the SQL that selects the following:
cat_id is same as age
SELECT cat_id AS id, name FROM cats;
+----+----------------+
| id | name |
+----+----------------+
| 1 | Ringo |
| 2 | Cindy |
| 3 | Dumbledore |
| 4 | Egg |
| 5 | Misty |
| 6 | George Michael |
| 7 | Jackson |
+----+----------------+
How do we alter existing data?
UPDATE cats SET breed='Shorthair'
WHERE breed='Tabby';
UPDATE cats SET age=14
WHERE name='Misty';
Thumb
Try SELECTing before you UPDATE
Relatively Painless UPDATE exercises
Change Jackson's name to "Jack"
Change Ringo's breed to "British Shorthair"
Update both Maine Coons' ages to be 12
Time to learn to...delete things
DELETE FROM cats WHERE name='Egg';
DELETE FROM cats;
Always run SELECT first to double check...
Get Some Practice With DELETE
Super
CRUD
Exercise
The Annual Closet Inventory
shirt_id | article | color | shirt_size | last_worn |
---|---|---|---|---|
1 | t-shirt | white | S | 10 |
2 | t-shirt | green | S | 200 |
3 | polo shirt | black | M | 10 |
4 | tank top | blue | S | 50 |
5 | t-shirt | pink | S | 0 |
6 | polo shirt | red | M | 5 |
7 | tank top | white | S | 200 |
8 | tank top | blue | M | 15 |
Primary Key
('t-shirt', 'white', 'S', 10),
('t-shirt', 'green', 'S', 200),
('polo shirt', 'black', 'M', 10),
('tank top', 'blue', 'S', 50),
('t-shirt', 'pink', 'S', 0),
('polo shirt', 'red', 'M', 5),
('tank top', 'white', 'S', 200),
('tank top', 'blue', 'M', 15)
With a single line
Purple polo shirt, size M last worn 50 days ago
But Only Print Out Article and Color
Print Out Everything But shirt_id
Change their size to L
Change last_worn to 0
Change size to 'XS' and color to 'off white'
Last worn 200 days ago
Your tastes have changed...
Catastrophe!
Catastrophe Again!
CREATE TABLE shirts
(
shirt_id INT NOT NULL AUTO_INCREMENT,
article VARCHAR(100),
color VARCHAR(100),
shirt_size VARCHAR(100),
last_worn INT,
PRIMARY KEY(shirt_id)
);