Top Banner
1 - CSE 330 – Creative Programming and Rapid Prototyping Module 3 – MySQL Database Module 3 Contains 2 components – Individual Assignment – Group Assignment BOTH are due on Wed, July 6th Read the WIKI before attempting the lab 1 2 - CSE 330 – Creative Programming and Rapid Prototyping Database Management System A database is simply a collection of data. In a relational database, data is organized into tables. Database Management System (DBMS) is software to maintain and utilize the collections of data (Oracle, DB2, MySQL) Student_ID Name Major Grade 101 Shannon CSE A 102 Mike CHEM A 103 Wang BIO A 2
17

Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var

Jun 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 11 - CSE 330 – Creative Programming and Rapid Prototyping

Module 3 – MySQL Database

• Module 3 Contains 2 components– Individual Assignment– Group Assignment

• BOTH are due on Wed, July 6th

• Read the WIKI before attempting the lab

1

Extensible Networking Platform 22 - CSE 330 – Creative Programming and Rapid Prototyping

Database Management System• A database is simply a collection of data. In a relational

database, data is organized into tables.

• Database Management System (DBMS) is software to maintain and utilize the collections of data (Oracle, DB2, MySQL)

Student_ID Name Major Grade101 Shannon CSE A

102 Mike CHEM A

103 Wang BIO A

… … …

2

Page 2: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 33 - CSE 330 – Creative Programming and Rapid Prototyping

Cells, Rows, Tables and Databases

• Cell -- a single (scalar) value

12134

3

Extensible Networking Platform 44 - CSE 330 – Creative Programming and Rapid Prototyping

Cells, Rows, Tables and Databases

• Row -- a group of scalar values representing a single instance of an object or event

12135 1310391314 Letter: July 23,1842

4

Page 3: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 55 - CSE 330 – Creative Programming and Rapid Prototyping

Cells, Rows, Tables and Databases

• Table -- a series of rows describing separate objects or events

ID METSID LABEL12134 1090313313 Letter: November 18, 183812135 1310391314 Letter: July 23,184212136 1313020414 Waterloo at Sunset

5

Extensible Networking Platform 66 - CSE 330 – Creative Programming and Rapid Prototyping

Cells, Rows, Tables and Databases

• Database -- a collection of related tables describing various facets of a group of objects or events

OBJECTS CLINKS COLSID METSID ID

METSID COLID NAMELABEL URL

ABSTRACT

6

Page 4: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 77 - CSE 330 – Creative Programming and Rapid Prototyping

Relationships in Databases

• Databases are great for storing different types of data and managing relationships between them

• When designing a DB it is important to understand the what types of relationships you need to create– These relationships are defined through referential

integrity (keys and/or constraints)

• There are a few common types of DB relationships we will look at– One-to-One– One-to-Many– Many-to-Many

7

Extensible Networking Platform 88 - CSE 330 – Creative Programming and Rapid Prototyping

Relations -- One to One

Table1 Table2Record RecordRecord RecordRecord RecordRecord RecordRecord RecordRecord RecordRecord Record

• Example: Table containing Social Security Number has a one-to-one relationship to table of Wash U Student IDs

8

Page 5: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 99 - CSE 330 – Creative Programming and Rapid Prototyping

One to Many

Table1 Table2Record RecordRecord RecordRecord RecordRecord RecordRecord RecordRecord RecordRecord Record

• Example: Table of Academic Advisors has a one to many relationship with a table containing students at WashU

9

Extensible Networking Platform 1010 - CSE 330 – Creative Programming and Rapid Prototyping

Many to Many

Table1 Table2Record RecordRecord RecordRecord RecordRecord RecordRecord RecordRecord RecordRecord Record

• Example: Table of courses taught in the Fall semester has a many to many relationship with a table containing students at WashU

10

Page 6: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 1111 - CSE 330 – Creative Programming and Rapid Prototyping

Which DB to use? Why MySQL?

• Free SQL (Structured Query Language) database server– licensed with the GNU General public license

http://www.gnu.org/

• MySQL is a relational database management system (RDBMS)

• MySQL is Open Source Software• Officially pronounced “my Ess Que Ell”

11

Extensible Networking Platform 1212 - CSE 330 – Creative Programming and Rapid Prototyping

Basic MySQL Operations

• Create table• Insert records• Load data• Retrieve records• Update records• Delete records• Modify table• Join table• Drop table• Optimize table• Count, Like, Order by, Group by• More advanced ones (sub-queries, stored procedures, triggers, views …)

12

Page 7: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 1313 - CSE 330 – Creative Programming and Rapid Prototyping

How MySQL stores data (by default)

• A MySQL server can store several databases

• Databases are stored as directories – Default is at /usr/local/mysql/var/

• Tables are stored as files inside each database (directory)

13

Extensible Networking Platform 1414 - CSE 330 – Creative Programming and Rapid Prototyping

Login

• mysql –h hostname –u username –p [password]

• Example% mysql -u username -pEnter password: passwordWelcome to the MySQL monitor. Commands end with ; or \g. Your

MySQL connection id is 23 to server version: 3.23.41.

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

14

Page 8: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 1515 - CSE 330 – Creative Programming and Rapid Prototyping

Create User and Database

•mysql>create user ‘test1’@’localhost’ identified by ‘mysecretpass’;

– Create a new database user test1

•mysql>grant all on *.* to test1@’localhost’ with grant option;

– Gives administrative privileges to user test1• It is common to restrict users to a particular database

with limited access, which we are NOT doing here

15

Extensible Networking Platform 1616 - CSE 330 – Creative Programming and Rapid Prototyping

What are the current databases at the server?mysql> show databases;+--------------+| Database |+--------------+ | mysql | mysql is a database (stores users’ password …) used by system.| test |+--------------+Create a database (make a directory) whose name is MyDBmysql> create database MyDB;Select database to usemysql> use MyDB;Database changedWhat tables are currently stored in the MyDB database?mysql> show tables;Empty set (0.00 sec)

Create Database

16

Page 9: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 1717 - CSE 330 – Creative Programming and Rapid Prototyping

• CREATE TABLE Table_Name (column_specifications)• Example

mysql> CREATE TABLE student-> (-> student_ID INT UNSIGNED NOT NULL,-> name VARCHAR(20) NOT NULL,-> major VARCHAR(50),-> email VARCHAR(50)-> );Query OK, 0 rows affected (0.00 sec)

Student_ID Name Major Email

Create Table

17

Extensible Networking Platform 1818 - CSE 330 – Creative Programming and Rapid Prototyping

Domain Types in SQL

• Similar to data types in classical programming languages

Type DescriptionCHAR(n) Fixed length character string, with specified length nVARCHAR(n) Variable length character string, with specified

maximum length nINTEGER Integer (a machine-dependent finite subset of the

integers)SMALLINT(n) A small integer (a finite subset of INTEGER)FLOAT(M,D) Floating point number, with total number of digits M

and number of digits following the decimal point DDOUBLE(M,D) Double-precision floating point number

18

Page 10: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 1919 - CSE 330 – Creative Programming and Rapid Prototyping

mysql> show tables;+--------------------+| Tables_in_MyDB |+--------------------+| student |+--------------------+1 row in set (0.00 sec)mysql> describe student;+---------------+----------------------+------+------+----------+--------+| Field | Type | Null | Key | Default | Extra |+---------------+----------------------+-------+-----+-----------+-------+| student_ID | int(10) unsigned | | | 0 | || name | varchar(20) | | | | || major | varchar(50) |YES | | NULL | || email | varchar(50) | YES | | NULL | |+---------------+----------------------+-------+------+----------+-------+4 rows in set (0.00 sec)

Display Table Structure

19

Extensible Networking Platform 2020 - CSE 330 – Creative Programming and Rapid Prototyping

Demo

20

Page 11: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 2121 - CSE 330 – Creative Programming and Rapid Prototyping

Primary and Foreign Keys

• One mechanism to enforce “referential integrity” is through primary and foreign keys

• Primary Keys are used in MySQL as unique identifiers for each row in a table

– Consider a database of students• The student ID could serve as a primary key

• A foreign key is a field in a table which is also the primary key of another table

– Known as referential integrity– Consider a WashU Database with tables for students, courses and enrollment

• What if the student tries to enroll in a course that does not exist?

21

Extensible Networking Platform 2222 - CSE 330 – Creative Programming and Rapid Prototyping

• ALTER TABLE table_name Operationsmysql> alter table student add primary key (student_ID);Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0

mysql> describe student;+---------------+--------------------- +-------+------+----------+-------+| Field | Type | Null | Key | Default | Extra |+---------------+----------------------+-------+------+----------+-------+| student_ID | int(10) unsigned | | PRI | 0 | || name | varchar(20) | | | | || major | varchar(10) | YES | | NULL | || email | varchar(50) | YES | | NULL | |+---------------+----------------------+-------+------+-----------+-------+4 rows in set (0.00 sec)

Modify Table Structure

22

Page 12: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 2323 - CSE 330 – Creative Programming and Rapid Prototyping

• INSERT INTO table_name SET col_name1=value1, col_name2=value2, col_name3=value3, …

• Example

mysql> INSERT INTO student SET student_ID=101, name='Shannon', major=’CSE', email=‘[email protected]’;

Query OK, 1 row affected (0.00 sec)

Student_ID Name Major Email101 Shannon CSE shannon@

yahoo.com

Insert Record

23

Extensible Networking Platform 2424 - CSE 330 – Creative Programming and Rapid Prototyping

• SELECT what_columnsFROM table or tablesWHERE condition

• Example

mysql> SELECT major, email FROM studentWHERE name='Shannon';

+-------+------------------------+| major| email |+-------+------------------------+| CSE | [email protected]|+-------+------------------------+1 row in set (0.00 sec)

mysql> SELECT * FROM student;

Student_ID Name Major Email

101 Shannon CSE [email protected]

102 Mike CHEM [email protected]

103 Wang BIO [email protected]

… … …

Retrieve Record

24

Page 13: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 2525 - CSE 330 – Creative Programming and Rapid Prototyping

• UPDATE table_nameSET which columns to changeWHERE condition

• Examplemysql> UPDATE student SET email=‘[email protected]’ WHERE name='Shannon';Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT * FROM student WHERE name=‘Shannon’;+------------+---------------+--------+-------------------------+| name | student_ID | major | email |+------------+---------------+--------+-------------------------+| Shannon | 101 | CSE | [email protected] |+------------+---------------+--------+-------------------------+1 row in set (0.00 sec)

Update Record

25

Extensible Networking Platform 2626 - CSE 330 – Creative Programming and Rapid Prototyping

• DELETE FROM table_name WHERE condition• Examplemysql> DELETE FROM student WHERE name='Shannon';Query OK, 1 row affected (0.00 sec)

Mysql> DELETE FROM student;Will delete ALL student records!

Delete Record

26

Page 14: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 2727 - CSE 330 – Creative Programming and Rapid Prototyping

More complex queries

• Queries are often interested in data from multiple tables

• INNER JOIN (or just JOIN)

• LEFT JOIN

• RIGHT JOIN

• The wiki has some great examples of using the JOIN operator

27

Extensible Networking Platform 2828 - CSE 330 – Creative Programming and Rapid Prototyping

phpMyAdmin

28

Page 15: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 2929 - CSE 330 – Creative Programming and Rapid Prototyping

PHP and MYSQL

• We will use PHP to communicate with our mySQLdatabase

• MySQL Improved Prepared Statements provide a clean way to issue queries

• Refer to the wiki for additional syntax examples

29

Extensible Networking Platform 3030 - CSE 330 – Creative Programming and Rapid Prototyping

MySQL and PHP Demo

30

Page 16: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 3131 - CSE 330 – Creative Programming and Rapid Prototyping

Create database with user/password

• Never store passwords in a database• Instead store password hash

– More importantly store a salted hash.

31

Extensible Networking Platform 3232 - CSE 330 – Creative Programming and Rapid Prototyping

User accounts and passwords

• The group portion of this module requires user accounts and passwords – Never store passwords as plain text in a database

– Instead use a salted password hash• A password hash is a function that takes a password and

maps it to a fixed size bit string

– Hashed passwords are also fixed length, so perfect for a char (not varchar) data type

32

Page 17: Module 3 –MySQL Databasetodd/cse330/cse330_lecture3.pdf · •A MySQL server can store several databases •Databases are stored as directories –Default is at /usr/local/mysql/var/

Extensible Networking Platform 3333 - CSE 330 – Creative Programming and Rapid Prototyping

Hashing

• The MD5 Message Digest Algorithm is a widely used hash function for security applications

• I could store my password as an MD5 hash in the mySQL…– But these passwords are trivially reversed

• So we add additional information to the string (called salt) to make it harder to accomplish

• Hash(Password + Random characters)

• So can PHP help me with this?– Fortunately PHP has a method called

password_hash which takes of most of work• Older versions of PHP used a less insecure function called

crypt– See the wiki for more information

33