1.
To list available databases:
show databases;
CODE: Creating Databases
The general command for creating a database:
CREATE DATABASE <database_name>;
A specific example:
CREATE DATABASE soap_store;
CODE: Dropping and Using Databases
To drop a database:
DROP DATABASE <database-name>;
To use a database:
USE <database-name>;
CODE: Basic Datatypes Challenge: Tweet table
"Solution" to the Basic Datatypes Exercise
CODE: Creating Tables
Creating Tables:
1. CREATE TABLE cats (
2. name VARCHAR(50),
3. age INT
4. );
5.
6. CREATE TABLE dogs (
7. name VARCHAR(50),
8. breed VARCHAR(50),
9. age INT
10. );
CODE: How Do We Know It Worked?
SHOW tables;
SHOW COLUMNS FROM cats;
DESC cats;
CODE: Dropping Tables/ Delete
To drop a table:
DROP TABLE <table-name>;
To specifically drop the cats table:
DROP TABLE cats;
SOLUTION: Tables Basics Activity
Create the table:
1. CREATE TABLE pastries
2. (
3. name VARCHAR(50),
4. quantity INT
5. );
View tables:
SHOW TABLES;
View details of pastries table:
DESC pastries;
Delete the whole pastries table:
DROP TABLE pastries;
CODE: INSERT: The Basics
-- Re-create the cats table (I dropped it in a previous video)
1. CREATE TABLE cats (
2. name VARCHAR(50),
3. age INT
4. );
Insert a cat:
1. INSERT INTO cats (name, age)
2. VALUES ('Blue Steele', 5);
And another:
1. INSERT INTO cats (name, age)
2. VALUES ('Jenkins', 7);
CODE: A Quick Preview of SELECT
To view all rows in our table:
SELECT * FROM cats;
CODE: Multi-inserts
-- Single insert (switching order of name and age)
1. INSERT INTO cats (age, name)
2. VALUES
3. (2, 'Beth');
-- Multiple Insert:
1. INSERT INTO cats (name, age)
2. VALUES
3. ('Meatball', 5),
4. ('Turkey', 1),
5. ('Potato Face', 15);
SOLUTION: INSERT Exercise
-- INSERT Challenge Solution Code
1. CREATE TABLE people
2. (
3. first_name VARCHAR(20),
4. last_name VARCHAR(20),
5. age INT
6. );
1. INSERT INTO people(first_name, last_name, age)
2. VALUES ('Tina', 'Belcher', 13);
1. INSERT INTO people(age, last_name, first_name)
2. VALUES (42, 'Belcher', 'Bob');
1. INSERT INTO people(first_name, last_name, age)
2. VALUES
3. ('Linda', 'Belcher', 45),
4. ('Phillip', 'Frond', 38),
5. ('Calvin', 'Fischoeder', 70);
DROP TABLE people;
SELECT * FROM people;
SHOW TABLES;
CODE: Working With NOT NULL
Using NOT NULL:
1. CREATE TABLE cats2 (
2. name VARCHAR(100) NOT NULL,
3. age INT NOT NULL
4. );
CODE: Adding DEFAULT Values
Define a table with a DEFAULT name specified:
1. CREATE TABLE cats3 (
2. name VARCHAR(20) DEFAULT 'no name provided',
3. age INT DEFAULT 99
4. );
Notice the change when you describe the table:
DESC cats3;
Insert a cat without a name:
INSERT INTO cats3(age) VALUES(13);
Or a nameless, ageless cat:
INSERT INTO cats3() VALUES();
Combine NOT NULL and DEFAULT:
1. CREATE TABLE cats4 (
2. name VARCHAR(20) NOT NULL DEFAULT 'unnamed',
3. age INT NOT NULL DEFAULT 99
);
CODE: Introducing Primary Keys
-- One way of specifying a PRIMARY KEY
1. CREATE TABLE unique_cats (
2. cat_id INT PRIMARY KEY,
3. name VARCHAR(100) NOT NULL,
4. age INT NOT NULL
5. );
-- Another option:
1. CREATE TABLE unique_cats2 (
2. cat_id INT,
3. name VARCHAR(100) NOT NULL,
4. age INT NOT NULL,
5. PRIMARY KEY (cat_id)
);
CODE: Working With AUTO_INCREMENT
-- AUTO_INCREMENT
1. CREATE TABLE unique_cats3 (
2. cat_id INT AUTO_INCREMENT,
3. name VARCHAR(100) NOT NULL,
4. age INT NOT NULL,
5. PRIMARY KEY (cat_id)
6. );
SOLUTION: Insert Exercise
-- Defining employees table
1. CREATE TABLE employees (
2. id INT AUTO_INCREMENT,
3. first_name VARCHAR(255) NOT NULL,
4. last_name VARCHAR(255) NOT NULL,
5. middle_name VARCHAR(255),
6. age INT NOT NULL,
7. current_status VARCHAR(255) NOT NULL DEFAULT 'employed',
8. PRIMARY KEY(id)
9. );
-- Another way of defining the primary key:
1. CREATE TABLE employees (
2. id INT AUTO_INCREMENT PRIMARY KEY,
3. first_name VARCHAR(255) NOT NULL,
4. last_name VARCHAR(255) NOT NULL,
5. middle_name VARCHAR(255),
6. age INT NOT NULL,
7. current_status VARCHAR(255) NOT NULL DEFAULT 'employed'
8. );
-- A test INSERT:
1. INSERT INTO employees(first_name, last_name, age) VALUES
2. ('Dora', 'Smith', 58);