(MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of...

31
Databases (MariaDB/MySQL) CS401, Fall 2015

Transcript of (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of...

Page 1: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Databases (MariaDB/MySQL)CS401, Fall 2015

Page 2: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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)

Page 3: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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.

Page 4: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Database Software

● Oracle● Microsoft SQL Server● PostgreSQL● MySQL/MariaDB

○ We will use MariaDB in this course. Basically the same as MySQL.

Page 5: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Example Database

Page 6: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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.

Page 7: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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

Page 8: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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;

Page 9: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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)

Page 10: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

SQL Basics

Page 11: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

SQL Basics

Page 12: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Creating a tableCREATE TABLE name ( columnName type constraints, …

columnName type constraints);

DROP TABLE name;DROP TABLE IF EXISTS name;

Page 13: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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

);

Page 14: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Types (most common)

Page 15: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Column Constraints

Page 17: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Inspecting the database.● Show and describe tables after creating.

SHOW DATABASES;SHOW TABLES;DESCRIBE table;

Page 18: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

INSERTing data.INSERT INTO table VALUES(value1, value2, ...);

INSERT INTO table(col1, col2, ...) VALUES(value1, value2, ...);

Page 19: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

SELECTing data.SELECT column(s) FROM table;

SELECT * FROM table;

SELECT col1, col2 FROM table;

Page 20: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

SELECTing DISTINCT data.DISTINCT

SELECT DISTINCT column(s) FROM table;

SELECT DISTINCT gender FROM users;

Page 21: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Filtering data using WHERE clauseWHERE

SELECT column(s) FROM table WHERE condition(s);

WHERE SELECT

= > >= < <=<>BETWEEN min AND max

Page 22: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Multiple WHERE clauses: AND/ORAND OR

SELECT column(s)

FROM table

WHERE condition AND condition;

Page 23: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Wildcard Matching: LIKE / REGEXSELECT column(s) FROM tableWHERE column LIKE pattern;

SELECT column(s) FROM tableWHERE column REGEX expression;

Page 24: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Ordering Output: ORDER BYSELECT column(s) FROM tableWHERE column LIKE pattern ORDER BY column;

ORDER BY column DESC;

Page 25: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Limiting Output: LIMITSELECT column(s) FROM tableWHERE column LIKE patternORDER BY column LIMIT n;

Page 26: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Aggregate Data: AVG / COUNTSELECT AVG(column) FROM table;

SELECT COUNT(*) FROM table;

Page 27: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

UPDATEing Rows UPDATE table SET column_name = value, column_name = value … WHERE condition(s);

Page 28: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

DELETEing Rows DELETE FROM table WHERE condition(s);

DELETE FROM table; -- Will delete ALL rows.

Page 29: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

Multi-table Queries: JOIN●

● JOIN○

● ON

SELECT FROMJOIN ONWHERE

Page 30: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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

Page 31: (MariaDB/MySQL) Databasesmarissa/classes/401/...Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind

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!!