Database Server
Dog Walker
Database
Soap Shop
Database
Practice
Database
News Site
Database
Dog Walker Database
Soap Shop Database
Dogs
Soaps
Users
Users
Payments
Payments
show databases;
CREATE DATABASE <name>;
CREATE DATABASE soap_store;
CREATE DATABASE DogApp;
CREATE DATABASE My App;
DROP DATABASE <name>;
DROP DATABASE soap_store;
Now What?
USE <database name>;
SELECT database();
"a collection of related data held in a structured format within a database"
Boring Wikipedia Definition
The Cat's Table!
Name | Breed | Age |
---|---|---|
Blue | Scottish Fold | 1 |
Rocket | Persian | 3 |
Monty | Tabby | 10 |
Sam | Munchkin | 5 |
The Cats Table!
Name | Breed | Age |
---|
Columns (headers)
Name | Breed | Age |
---|---|---|
Blue | Scottish Fold | 1 |
Rocket | Persian | 3 |
Monty | Tabby | 10 |
Sam | Munchkin | 5 |
Rows (the actual data)
Databases are made up of lots of tables.
Sometimes it gets crazy.
Name | Breed | Age |
---|---|---|
Blue | Scottish Fold | 1 |
Rocket | Persian | 3 |
Monty | Tabby | ten |
Sam | Munchkin | I am yung cat |
The Importance of Data Types
Name | Breed | Age |
---|---|---|
Blue | Scottish Fold | 1 |
Rocket | Persian | 3 |
Monty | Tabby | ten |
Sam | Munchkin | I am yung cat |
Calculate "cat age"
Age * 7
1 * 7 = 7
3 * 7 = 21
'ten' * 7 = ???
'I am yung cat' * 7 =
😡
(AKA Bad)
Name | Breed | Age |
---|---|---|
Blue | Scottish Fold | 1 |
Rocket | Persian | 3 |
Monty | Tabby | ten |
Sam | Munchkin | I am yung cat |
Must Be
Text
Must Be
Text
Must Be
Number
Numeric Types
String Types
Date Types
😳
Numeric Types
String Types
with a max value of 4294967295
12
0
-9999
3145677
42
Between 1 and 255 characters
'coffee!!'
'aAbbb akljsd'
'-9999'
'L'
'The quick brown fox jumps over the lazy brown dog'
Name | Breed | Age |
---|---|---|
Blue | Scottish Fold | 1 |
Rocket | Persian | 3 |
Monty | Tabby | ten |
Sam | Munchkin | I am yung cat |
Must Be
Text
Must Be
Text
Must Be
Number
varchar(100)
varchar(100)
int
At a minimum the columns must include:
Make sure to specify correct MySQL datatypes!
username | content | favorites |
---|---|---|
'coolguy' | 'my first tweet!' | 1 |
guitar_queen | 'I love music :) ' | 10 |
'lonely_heart' | 'still looking 4 love' | 0 |
Username (max 15 chars)
Favorites
varchar(15)
varchar(140)
int
Content (max 140 chars)
Finally!
CREATE TABLE tablename
(
column_name data_type,
column_name data_type
);
CREATE TABLE cats
(
name VARCHAR(100),
age INT
);
SHOW TABLES;
SHOW COLUMNS FROM <tablename>;
DESC <tablename>;
Or...
DROP TABLE <tablename>;
Creating Your Own Table!
INSERT INTO cats(name, age)
VALUES ('Jetson', 7);
INSERT INTO cats(name, age) VALUES ("Jetson", 7);
INSERT INTO cats
(NAME,
age)
VALUES ("jetson",
7);
INSERT INTO cats(name, age)
VALUES ("Jetson", 7);
INSERT INTO cats(age, name)
VALUES (12, 'Victoria');
I come from the future, with a new SQL command you won't learn until the next section.
SELECT * FROM cats;
INSERT INTO cats(name, age)
VALUES ('Charlie', 10)
,('Sadie', 3)
,('Lazy Bear', 1);
first_name | last_name | age |
---|---|---|
'Tina' | 'Belcher' | 13 |
first_name | last_name | age |
---|---|---|
'Bob' | 'Belcher' | 42 |
first_name | last_name | age |
---|---|---|
'Linda' | 'Belcher' | 45 |
'Phillip' | 'Frond' | 38 |
'Calvin' | 'Fischoeder' | 70 |
INSERT INTO cats(name, age)
VALUES ('This cat is named
Charlie which is also a
human name. In fact I know
a couple of Charlies. Fun Fact',
10)
Query OK, 1 row affected, 1 warning (0.01 sec)
SHOW WARNINGS;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | varchar(5) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
INSERT INTO cats(name)
VALUES ('Alabama');
Who names their cat 'Alabama'?
INSERT INTO cats()
VALUES ();
CREATE TABLE cats2
(
name VARCHAR(100) NOT NULL,
age INT NOT NULL
);
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(100) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | varchar(5) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
CREATE TABLE cats3
(
name VARCHAR(100) DEFAULT 'unnamed',
age INT DEFAULT 99
);
CREATE TABLE cats4
(
name VARCHAR(100) NOT NULL DEFAULT 'unnamed',
age INT NOT NULL DEFAULT 99
);
Isn't This Redundant?
We can still manually set things to NULL if we don't specify NOT NULL
INSERT INTO cats3(name, age)
VALUES(NULL, 3);
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | varchar(5) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
Name | Breed | Age |
---|---|---|
Monty | Tabby | 10 |
Monty | Tabby | 10 |
Monty | Tabby | 10 |
Monty | Tabby | 10 |
Name | Breed | Age | CatID |
---|---|---|---|
Monty | Tabby | 10 | 1 |
Monty | Tabby | 10 | 2 |
Monty | Tabby | 10 | 3 |
Monty | Tabby | 10 | 4 |
CREATE TABLE unique_cats (cat_id INT NOT NULL
,name VARCHAR(100)
,age INT
,PRIMARY KEY (cat_id));
CREATE TABLE unique_cats2 (cat_id INT NOT NULL AUTO_INCREMENT
,name VARCHAR(100)
,age INT
,PRIMARY KEY (cat_id));
Define an Employees table, with the following fields:
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
last_name VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
middle_name VARCHAR(255),
age INTEGER NOT NULL,
current_status VARCHAR(100) NOT NULL DEFAULT 'employed',
PRIMARY KEY (id)
);
THE SOLUTION
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
last_name VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
middle_name VARCHAR(255),
age INTEGER NOT NULL,
current_status VARCHAR(100) NOT NULL DEFAULT 'employed'
);
THE SOLUTION
(with a slight difference)