CRUD
Create
How to create a database and a table in the database?
Create a database called catmanic so all crazy cat datasets can go into this database
Use this database and create a table called cats in the catmanic database
Show the table called cats
Describe our new table cats
Read
How do we retrieve and search data?
USE SELECT * FROM cats; The star means give me all columns!
SELECT Expression: what columns do you want? SELECT name FROM cats; Only selects names.
You can reverse the order of the variables or select any number of variables you want.
EXAMPLES
WHERE: The WHERE clause searches for specific data. We need WHERE for updating and
deleting things because we usually want to update and delete things specific.
EXAMPLES
Note that EGG and Egg are both ok and they both give us the same answer! Not case sensitive!
SELECT CHALLENGES
Select the cat_id column from the cats table.
Select the columns name and breed from the cats table
Select the cat breed Tabby
Select the cat that has the its cat_id equals to its age. Then select all information about these
cats based on the cat_id = age criterion.
Aliases
They are used to make column names more readable.
SELECT column_name AS alias_name
FROM table_name
Give cat_id a new name or alias called id
Give name and breed new names
There is nothing changed in our field.
Update
How do we alter existing data?
EXMAPLES
Updating the breed Tabby to Shorthair
Updating Mistys age
Here Mistys age has been changed to 14.
UPDATE CHANLLEGE
Change Jacksons name to Jack
Change Ringos breed to British Shorthair from Shorthair
Update both Maine Coons ages to 12
Delete
How to delete things?
The current table we have
Before deleting, lets select the kitty named Egg.
Now Egg is happily adopted! Notice that the cat_id associated with Egg is gone and cat_ids still
remain the same.
DELETE CHANLLEGE
Delete all 4-year-old cats, but first select the cats who are 4. Then make sure it is deleted from
the original table by using SELECT * FROM cats.
Delete cats whose age is the same as their cat_id, but first select the cat whose age is the same
as their cat_id. Then make sure it is deleted from the original table by SELECT*FROM cats.
Delete all cats!!!