### CREATING DATABASES
CREATEDATABASE zoo; #Make a new databse, obviously lol
SHOWDATABASES;
USE zoo;
SHOWTABLES;
DESCRIBE animal; #info about a specified table.
DROPDATABASE zoo; #To delete a database.
DROPTABLE animal; #To delete a table.
### CREATING TABLES
CREATETABLEanimal (
id INTNOTNULL,
name VARCHAR(64),
species VARCHAR(64),
age INT,
habitat_id INT,
FOREIGNKEY (habitat_id) REFERENCEShabitat(id)
);
### MODIFIYING TABLES
# Use the `ALTER TABLE` to modify the table structure. Not the data itself
# `ALTER TABLE` with `ADD` adds stuff to the table
ALTERTABLE animal ADD scientific_name VARCHAR(100);
ALTERTABLE animal ADDPRIMARYKEY(scientific_name);
ALTERTABLE animal ADDFOREIGNKEY(scientific_name)
REFERENCESscarydatabase(scientific_name);
# `ALTER TABLE` with `DROP` removes Stuff from Tables.
ALTERTABLE animal DROP age;
ALTERTABLE animal DROPPRIMARYKEY;
ALTERTABLE animal DROPFOREIGNKEY;
### INSERTING DATA
INSERTINTOanimal(id,name,species,age,habitat_id) VALUES (1, 'Dog', 'tikkcutekollo', 2, 001);
INSERTINTO animal VALUES (2, 'Cat', 'cutekollo', 10, 001);
INSERTINTO animal SET id=3,
name='cockroach',
species='uglykollo',
age=1,
habitat_id=001;
### UPDATING DATA
UPDATE animal SET species='cutekollo', age =4WHERE habitat_id =001;
### DELETING DATA
DELETEFROM animal WHERE id =1;
TRUNCATE TABLE animal;
### QUERYING DATA aka SELECTING DATA aka RETRIVING DATA
#An example of a single-table query
SELECT name,species FROM animal
WHERE id !=3GROUPBY species
HAVINGAVG(age) >3ORDERBYAVG(age) DESC;
# Use `DISTINCT` to display only the different values among duplicates
SELECTDISTINCT name FROM animal;
## Conditions
SELECT*FROM animal WHERE species='uglykollo'AND age >9;
SELECT*FROM animal WHERE species='cutekollo'OR habitat_id=001;
SELECT*FROM animal WHERE id NOTIN (1,2); #`IN` is equal to multiple OR conditions
SELECT*FROM animal WHERE age BETWEEN13AND18; #to get teenage animals in the zoo XD
SELECT*FROM animal WHERE name LIKE'S%';
SELECT*FROM animal WHERE name LIKE'%s';
SELECT*FROM animal WHERE name LIKE'K___';
SELECT*FROM animal WHERE name NOTLIKE'San%';