Databases (MariaDB/MySQL)CS401, Fall 2015
Database Basics● Relational Database
○ Method of structuring data as tables associated to each other by shared attributes.
● Tables (kind of like a Java class) have○ rows (records or tuples) (kind of like an object)
○ columns (attributes or fields of records) (kind of like an instance variable)
Database Basics● Use Structured Query Language (SQL) to
define, manage, and search data.● Why do we use them?
○ Fast compared to files.○ Scale well for large data sets.○ Built-in failure recovery.○ Concurrency (multi-user) features.
Database Software
● Oracle● Microsoft SQL Server● PostgreSQL● MySQL/MariaDB
○ We will use MariaDB in this course. Basically the same as MySQL.
Example Database
Website Concept
● Imagine we have an online discussion board with a diverse user group.
● When a user signs up, they will provide a username, password, email address, name, age, and species.
● Users can leave posts. We want to keep track of who posts what.
users
id username password email first_name last_name age gender
123 snoopy snoopypass [email protected]
Snoopy Brown 7 M
222 blackwidow nataliapass [email protected] Natalia Romanova 30 F
223 mulan mulanpass [email protected] Mulan Hua 18 F
posts
id user_id message posted
6653 123 Woof woof! 2015-04-14 01:23:21
6654 223 I’ll never make my father happy :(
2015-04-16 12:18:01
6653 123 woof woof woof!! 2015-04-16 12:22:19 This is better, but for simplicity in our example, we don’t do this. Why is it better than having it in users?
species
id species
2276 human
2277 dog
2278 rabbit
users_species
user_id species_id
123 2277
222 2276
223 2276
Setting up your database on WebDev
● SSH to onyx, then SSH to webdev.● Login to MariaDB.
mysql -u yourlogin -p● You can change your password if you wish.
set password for ‘yourlogin’@’localhost’ = PASSWORD(‘sup3rs3cr3t’);
● Specify which database you want to useuse yourlogin;
Running Your First Query
● To see your current set of tables, runshow tables;
● You have no tables, so this should returnempty set (0.0 sec)
SQL Basics
SQL Basics
●
●
●
Creating a tableCREATE TABLE name ( columnName type constraints, …
columnName type constraints);
DROP TABLE name;DROP TABLE IF EXISTS name;
Creating a tableDROP TABLE IF EXISTS users;CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,username VARCHAR(50) NOT NULL UNIQUE,email VARCHAR(100) NOT NULL,password VARCHAR(128) NOT NULL,first_name VARCHAR(50) NOT NULL,last_name VARCHAR(50) NOT NULL,age INT NOT NULL
);
Types (most common)
Column Constraints
Let’s do it.● Create the table as shown in create_tables.
sql● Follow tutorial as shown in
sql_tutorial.sql● Show and describe tables after creating.
SHOW TABLES;DESCRIBE users;
Inspecting the database.● Show and describe tables after creating.
SHOW DATABASES;SHOW TABLES;DESCRIBE table;
INSERTing data.INSERT INTO table VALUES(value1, value2, ...);
INSERT INTO table(col1, col2, ...) VALUES(value1, value2, ...);
SELECTing data.SELECT column(s) FROM table;
SELECT * FROM table;
SELECT col1, col2 FROM table;
SELECTing DISTINCT data.DISTINCT
SELECT DISTINCT column(s) FROM table;
SELECT DISTINCT gender FROM users;
Filtering data using WHERE clauseWHERE
SELECT column(s) FROM table WHERE condition(s);
WHERE SELECT
= > >= < <=<>BETWEEN min AND max
Multiple WHERE clauses: AND/ORAND OR
SELECT column(s)
FROM table
WHERE condition AND condition;
Wildcard Matching: LIKE / REGEXSELECT column(s) FROM tableWHERE column LIKE pattern;
SELECT column(s) FROM tableWHERE column REGEX expression;
Ordering Output: ORDER BYSELECT column(s) FROM tableWHERE column LIKE pattern ORDER BY column;
ORDER BY column DESC;
Limiting Output: LIMITSELECT column(s) FROM tableWHERE column LIKE patternORDER BY column LIMIT n;
Aggregate Data: AVG / COUNTSELECT AVG(column) FROM table;
SELECT COUNT(*) FROM table;
UPDATEing Rows UPDATE table SET column_name = value, column_name = value … WHERE condition(s);
DELETEing Rows DELETE FROM table WHERE condition(s);
DELETE FROM table; -- Will delete ALL rows.
Multi-table Queries: JOIN●
● JOIN○
● ON
SELECT FROMJOIN ONWHERE
Multi-table Queries: JOINSELECTFROM JOIN ON
users
id username password email ...
123 snoopy snoopypass [email protected] ...
222 blackwidow nataliapass [email protected] ...
223 mulan mulanpass [email protected] ...
posts
id user_id message posted
6653 123 Woof woof! 2015-04-14 01:23:21
6654 223 I’ll never make my father happy :(
2015-04-16 12:18:01
6653 123 woof woof woof!! 2015-04-16 12:22:19
Multi-table Queries: JOINSELECTFROM JOIN ON
users
id username password email ...
123 snoopy snoopypass [email protected] ...
222 blackwidow nataliapass [email protected] ...
223 mulan mulanpass [email protected] ...
posts
id user_id message posted
6653 123 Woof woof! 2015-04-14 01:23:21
6654 223 I’ll never make my father happy :(
2015-04-16 12:18:01
6653 123 woof woof woof!! 2015-04-16 12:22:19
users JOIN posts
username message
snoopy Woof woof!
mulan I’ll never make my father happy :(
snoopy woof woof woof!!
Top Related