To list available databases:
show databases;
________________________________________________________
The general command for creating a database:
CREATE DATABASE <database_name>;
A specific example:
CREATE DATABASE soap_store;
_______________________________________________________________
To drop a database:
DROP DATABASE <database-name>;
Example:
DROP DATABASE soap_store;
_________________________________________________
To use a database:
USE <database-name>;
Example:
USE DATABASE soap_store;
_____________________________________________________
To show the current database we are working on:
SELECT database();
_____________________________________________________________
Databases have lots of tables.
An attempt at wikipedia schema
https://en.wikipedia.org/wiki/Database_schema#/media/File:MediaW
iki_1.28.0_database_schema.svg
An attempt at simplified facebook schema
https://upload.wikimedia.org/wikipedia/commons/9/95/Metamodel_of
_Facebook.jpg
Some SQL Datatypes
Docs - https://dev.mysql.com/doc/refman/8.4/en/data-types.html
Creating Tables:
CREATE TABLE cats (
name VARCHAR(50),
age INT
);
CREATE TABLE dogs (
name VARCHAR(50),
breed VARCHAR(50),
age INT
);
______________________________________________________________
Showing Tables
SHOW tables;
SHOW COLUMNS FROM cats;
DESCRIBE cats;
DESC cats;
______________________________________________________________
To drop a table:
DROP TABLE <table-name>;
To specifically drop the cats table:
DROP TABLE cats;
______________________________________________________________
Activity
Create a pastries table. It should include two columns: name and
quantity. Name is 50 characters max. Then delete the table.
Solution
Create the table:
CREATE TABLE pastries
(
name VARCHAR(50),
quantity INT
);
View tables:
SHOW TABLES;
View details of pastries table:
DESC pastries;
Delete the whole pastries table:
DROP TABLE pastries;
_______________________________________________________________
-- or # can be used for single line comments.
Multi-line comments are enclosed in /* and */.