Module 3 MySQL Database. Database Management System

Similar documents
SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Introduction to relational databases and MySQL

Chapter 3 Introduction to relational databases and MySQL

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Basic SQL. Basic SQL. Basic SQL

CSC 453 Database Technologies. Tanu Malik DePaul University

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Using MySQL on the Winthrop Linux Systems

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

CS143: Relational Model

MySQL Creating a Database Lecture 3

Database Management Systems by Hanh Pham GOALS

Networks and Web for Health Informatics (HINF 6220)

Unit 27 Web Server Scripting Extended Diploma in ICT

SQL Commands & Mongo DB New Syllabus

CSC 3300 Homework 3 Security & Languages

Provider: MySQLAB Web page:

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Chapter 9: MySQL for Server-Side Data Storage

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

Downloaded from

Simple Quesries in SQL & Table Creation and Data Manipulation

The Relational Model of Data (ii)

General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Reminder: our Mini-U db

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Lecture 1. Monday, August 25, 2014

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 6 st, 2017

SQL DATA DEFINITION LANGUAGE

SQL Data Definition and Data Manipulation Languages (DDL and DML)

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Database Systems. phpmyadmin Tutorial

CGS 3066: Spring 2017 SQL Reference

Relational databases and SQL

Oracle Login Max Length Table Name 11g Column Varchar2

A practical introduction to database design

Chapter-14 SQL COMMANDS

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

CSCB20 Week 3. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

MySQL Introduction. By Prof. B.A.Khivsara

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Chapter 9: Working with MySQL

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Top 20 Design Tips

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Operating systems fundamentals - B07

SQL Introduction. CS 377: Database Systems

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Database Systems Fundamentals

Lab # 1. Introduction to Oracle

user specifies what is wanted, not how to find it

SQL functions fit into two broad categories: Data definition language Data manipulation language

Course Outline and Objectives: Database Programming with SQL

CS634 Architecture of Database Systems Spring Elizabeth (Betty) O Neil University of Massachusetts at Boston

Full file at

Chapter 3: Introduction to SQL

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

A Flat file database. Problems with a flat file database (data redundancy)

MySQL. A practical introduction to database design

MySQL: Access Via PHP

The SQL data-definition language (DDL) allows defining :

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Using Relational Databases for Digital Research

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

itexamdump 최고이자최신인 IT 인증시험덤프 일년무료업데이트서비스제공

SQL DATA DEFINITION LANGUAGE

Previously everyone in the class used the mysql account: Username: csci340user Password: csci340pass

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 1 st, 2016

Referential integrity

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Introduction to SQL Server 2005/2008 and Transact SQL

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Introduction to Computer Science and Business

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

Lab # 4. Data Definition Language (DDL)

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

SSE 3200 Mysql lab. Introduction. Getting started

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

UBC Graduate Information System (GIS)

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

COMP283-Lecture 6 Applied Database Management

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Working with Databases and Java

The Relational Model. Suan Lee

Transcription:

Module 3 MySQL Database Module 3 Contains 2 components Individual Assignment Group Assignment BOTH are due on Mon, Feb 19th Read the WIKI before attempting the lab Extensible Networking Platform 1 1 - 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. Student_ID Name Major Grade 101 Shannon CSE A 102 Mike CHEM A 103 Wang BIO A Database Management System (DBMS) is software to maintain and utilize the collections of data (Oracle, DB2, MySQL) Extensible Networking Platform 2 2 - CSE 330 Creative Programming and Rapid Prototyping

Cells, Rows, Tables and Databases Cell -- a single (scalar) value 12134 Extensible Networking Platform 3 3 - 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 Extensible Networking Platform 4 4 - CSE 330 Creative Programming and Rapid Prototyping

Cells, Rows, Tables and Databases Table -- a series of rows describing separate objects or events ID METSID LABEL 12134 1090313313 Letter: November 18, 1838 12135 1310391314 Letter: July 23,1842 12136 1313020414 Waterloo at Sunset Extensible Networking Platform 5 5 - 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 COLS ID METSID ID METSID COLID NAME LABEL URL ABSTRACT Extensible Networking Platform 6 6 - 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 Extensible Networking Platform 7 7 - CSE 330 Creative Programming and Rapid Prototyping Relations -- One to One Table1 Table2 Example: Table containing Social Security Number has a one-to-one relationship to table of Wash U Student IDs Extensible Networking Platform 8 8 - CSE 330 Creative Programming and Rapid Prototyping

One to Many Table1 Table2 Example: Table of Academic Advisors has a one to many relationship with a table containing students at WashU Extensible Networking Platform 9 9 - CSE 330 Creative Programming and Rapid Prototyping Many to Many Table1 Table2 Example: Table of courses taught in the Fall semester has a many to many relationship with a table containing students at WashU Extensible Networking Platform 10 10 - 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 Extensible Networking Platform 11 11 - 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 ) Extensible Networking Platform 12 12 - 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) Extensible Networking Platform 13 13 - CSE 330 Creative Programming and Rapid Prototyping Login mysql h hostname u username p [password] Example % mysql -u username -p Enter password: password Welcome 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> Extensible Networking Platform 14 14 - 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 Extensible Networking Platform 15 15 - CSE 330 Creative Programming and Rapid Prototyping Create Database 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 MyDB mysql> create database MyDB; Select database to use mysql> use MyDB; Database changed What tables are currently stored in the MyDB database? mysql> show tables; Empty set (0.00 sec) Extensible Networking Platform 16 16 - CSE 330 Creative Programming and Rapid Prototyping

Create Table 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 Extensible Networking Platform 17 17 - CSE 330 Creative Programming and Rapid Prototyping Domain Types in SQL Type CHAR(n) VARCHAR(n) INTEGER SMALLINT(n) FLOAT(M,D) DOUBLE(M,D) Description Fixed length character string, with specified length n Variable length character string, with specified maximum length n Integer (a machine-dependent finite subset of the integers) A small integer (a finite subset of INTEGER) Floating point number, with total number of digits M and number of digits following the decimal point D Double-precision floating point number Similar to data types in classical programming languages Extensible Networking Platform 18 18 - CSE 330 Creative Programming and Rapid Prototyping

Display Table Structure 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) Extensible Networking Platform 19 19 - CSE 330 Creative Programming and Rapid Prototyping Demo Extensible Networking Platform 20 20 - 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? Extensible Networking Platform 21 21 - CSE 330 Creative Programming and Rapid Prototyping Modify Table Structure ALTER TABLE table_name Operations mysql> alter table student add primary key (student_id); Query OK, 0 rows affected (0.00 sec) s: 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) Extensible Networking Platform 22 22 - CSE 330 Creative Programming and Rapid Prototyping

Insert 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= shannon@yahoo.com ; Query OK, 1 row affected (0.00 sec) Student_ID Name Major Email 101 Shannon CSE shannon@ yahoo.com Extensible Networking Platform 23 23 - CSE 330 Creative Programming and Rapid Prototyping Retrieve SELECT what_columns FROM table or tables WHERE condition Example mysql> SELECT major, email FROM student WHERE name='shannon'; +-------+------------------------+ major email +-------+------------------------+ CSE shannon@yahoo.com +-------+------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM student; Student_ID Name Major Email 101 Shannon CSE shannon@ yahoo.com 102 Mike CHEM mike@ gmail.com 103 Wang BIO wang@ wustl.edu Extensible Networking Platform 24 24 - CSE 330 Creative Programming and Rapid Prototyping

Update UPDATE table_name SET which columns to change WHERE condition Example mysql> UPDATE student SET email= shannon@wustl.edu WHERE name='shannon'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM student WHERE name= Shannon ; +------------+---------------+--------+-------------------------+ name student_id major email +------------+---------------+--------+-------------------------+ Shannon 101 CSE shannon@wustl.edu +------------+---------------+--------+-------------------------+ 1 row in set (0.00 sec) Extensible Networking Platform 25 25 - CSE 330 Creative Programming and Rapid Prototyping Delete DELETE FROM table_name WHERE condition Example mysql> DELETE FROM student WHERE name='shannon'; Query OK, 1 row affected (0.00 sec) Mysql> DELETE FROM student; Will delete ALL student records! Extensible Networking Platform 26 26 - 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 Extensible Networking Platform 27 27 - CSE 330 Creative Programming and Rapid Prototyping phpmyadmin Extensible Networking Platform 28 28 - CSE 330 Creative Programming and Rapid Prototyping

PHP and MYSQL We will use PHP to communicate with our mysql database MySQL Improved Prepared Statements provide a clean way to issue queries Refer to the wiki for additional syntax examples Extensible Networking Platform 29 29 - CSE 330 Creative Programming and Rapid Prototyping MySQL and PHP Demo Extensible Networking Platform 30 30 - 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. Extensible Networking Platform 31 31 - 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 Extensible Networking Platform 32 32 - 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 Extensible Networking Platform 33 33 - CSE 330 Creative Programming and Rapid Prototyping