Chapter 9: MySQL for Server-Side Data Storage

Similar documents
SQL Interview Questions

8) A top-to-bottom relationship among the items in a database is established by a

How to design a database

Today Learning outcomes LO2

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

CSC Web Programming. Introduction to SQL

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

Database: Introduction

Unit 1 - Chapter 4,5

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

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

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

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

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

Normalization in DBMS

Chapter 3 Introduction to relational databases and MySQL

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

CGS 3066: Spring 2017 SQL Reference

Module 3 MySQL Database. Database Management System

Data about data is database Select correct option: True False Partially True None of the Above

Simple Quesries in SQL & Table Creation and Data Manipulation

Normalization Rule. First Normal Form (1NF) Normalization rule are divided into following normal form. 1. First Normal Form. 2. Second Normal Form

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

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

SQL stands for Structured Query Language. SQL is the lingua franca

6.830 Lecture PS1 Due Next Time (Tuesday!) Lab 1 Out end of week start early!

DC62 Database management system JUNE 2013

Learning outcomes. On successful completion of this unit you will: 1. Understand data models and database technologies.

MySQL for Developers with Developer Techniques Accelerated

MySQL by Examples for Beginners

T-SQL Training: T-SQL for SQL Server for Developers

Chapter 1 SQL and Data

Test Bank for A Guide to SQL 9th Edition by Pratt

Introduction to relational databases and MySQL

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

E-R diagrams and database schemas. Functional dependencies. Definition (tuple, attribute, value). A tuple has the form

MySQL Installation Guide (OS X)

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

How to use SQL to create a database

Using MySQL on the Winthrop Linux Systems

Steps in normalisation. Steps in normalisation 7/15/2014

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

The appendix contains information about the Classic Models database. Place your answers on the examination paper and any additional paper used.

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

MySQL and MariaDB. March, Introduction 3

Get Table Schema In Sql Server 2008 To Add Column If Not Exists >>>CLICK HERE<<<

Relational databases and SQL

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Distributed Database Systems By Syed Bakhtawar Shah Abid Lecturer in Computer Science

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Applied Databases. Sebastian Maneth. Lecture 5 ER Model, Normal Forms. University of Edinburgh - January 30 th, 2017

Basic SQL. Basic SQL. Basic SQL

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

DATABASE MANAGEMENT SYSTEM SHORT QUESTIONS. QUESTION 1: What is database?

CS143: Relational Model

MySQL Introduction. By Prof. B.A.Khivsara

Relational Database Language

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

Exact Numeric Data Types

Chapter-14 SQL COMMANDS

Operating systems fundamentals - B07

MTA Database Administrator Fundamentals Course

DATABASES SQL INFOTEK SOLUTIONS TEAM

Database Management Systems by Hanh Pham GOALS

Database Management System Prof. Partha Pratim Das Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

SQL: Data Definition Language

Database Systems. phpmyadmin Tutorial

Database Management Systems,

Lesson 13 Transcript: User-Defined Functions

1. The process of determining the particular tables and columns that will comprise a database is known as database design.

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

5. Single-row function

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 6 Normalization of Database Tables

Product: DQ Order Manager Release Notes

Advanced SQL Tribal Data Workshop Joe Nowinski

EXAM Microsoft Database Fundamentals. Buy Full Product.

Lecture 5. Monday, September 15, 2014

Lecture 19 Query Processing Part 1

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

Test Bank For A Guide To Mysql 1st Edition By Pratt And Last

ch02 True/False Indicate whether the statement is true or false.

Link download full of Solution Manual:

Creating the Data Layer

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

SSE 3200 Mysql lab. Introduction. Getting started

Lecture 1. Monday, August 25, 2014

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

SQL DATA DEFINITION LANGUAGE

Provider: MySQLAB Web page:

Chapter 3. Introducation to Relational Database 9/2/2014 CSC4341 1

IS 263 Database Concepts

Chapter 1 An introduction to relational databases and SQL

Database Foundations. 3-9 Validating Data Using Normalization. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Data and Knowledge Management Dr. Rick Jerz

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

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

Transcription:

Chapter 9: MySQL for Server-Side Data Storage

General Notes on the Slides for This Chapter In many slides you will see webbook as a database name. That was the orginal name of our database. For this second edition our website used a new name: webbook2e (but the original name continues to be used in these slides). Your database name can be whatever you want it to be, with appropriate substitutions wherever the database name is used.

Overview and Objectives History and a high-level view of the relational database model Some high-level goals for designing your own database Important aspects of the preferred structure of a relational database: normalization table keys possible relationships between tables functional dependencies between record attributes Online resources to help you set up a database The phpmyadmin interface to a MySQL database system Setting up a MySQL database for our Nature s Source website Using the Structured Query Language (SQL) to manipulate a database Command-line interface to MySQL Importing and exporting tables and databases to and from MySQL

Relational Databases Early databases used somewhat unstructured hierarchical and network models. In 1970, E. F. Codd from IBM introduced the relational database model. Data in the relational model is represented as mathematical relations. Data is manipulated using relational calculus. A mathematical basis for the relational database model makes it possible to minimize data redundancy and verify data integrity.

Database Design Goals All data necessary for smooth operation needs to be recorded with appropriate frequency. Data integrity must be maintained when adding, updating, and deleting information. The stored data should facilitate easy retrieval using a language called SQL (Structured Query Language). A good database should: occupy minimal storage have lightning-fast response times to all queries be easily modifiable be extensible when the needs of the business change

Avoiding Anomalies The insertion anomaly We are entering a new record. Not all fields of that record can be filled in. Why not? Because some of the required data is missing. The update anomaly Information is updated in one place in the database. The same information is not updated in some other database location. Elimination or minimization of redundant data can alleviate this problem, and also help to ensure more logical and efficient storage. The deletion anomaly One piece of information is deleted from the database. This forces the deletion of something else that should not be deleted.

Database Normalization A database is said to be in a particular normal form if the relationships between the attributes of the entities represented by the records in the tables of the database are rigorously defined in a certain way. Originally, in 1970, Codd specified three normal forms: First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) Additional normal forms Boyce-Codd Normal Form (BCNF) 4NF and 5NF 6NF for temporal databases

First Normal Form (1NF) Each row of a table must have the same number of columns. That is, the record for each item must have the same number of attributes. Also, each attribute value must contain a single piece of information. That is, each attribute value must be atomic.

1NF

Database Keys Primary key: Must be unique for every possible record in the table Must always have a value that will never change Foreign key: Suppose we have a primary key column in Table A. This column may also appear in Table B. This establishes a relationship between those two tables. In Table B the primary key from Table A is called a foreign key. Compound (or composite) primary key: The concatenation of the attribute values in two (or more) columns can be used as a (compound) primary key.

Functional Dependencies and 2NF The second and third normal forms deal with the relationship between key and non-key attributes in a table. This relationship is called functional dependency. Functional dependency requirement for 2NF: Each non-key attribute in a table must be information about the primary key. In fact, it must be about the whole primary key. And, finally, about nothing but the primary key.

Functional Dependency and 2NF

Data Model for an e-commerce Web Site Based on Models from DataBaseAnswers.com Figure 9.1 graphics/ch09/dbschema.jpg.

Frequently Used MySQL Data Types INTEGER (or INT), a numeric data type for storing 32-bit integer values DOUBLE, a numeric data type for storing 64-bit floating point values (values containing a decimal point, i.e., real numbers ) VARCHAR(M), an efficient data type for storing variable-length strings up to 65, 535 characters TINYTEXT, another data type for storing strings of length not exceeding 255 characters TEXT, for storing large amounts of text (up to 65,535 characters) DATE, for storing date values

Specifying the Name and Size of the Customers Table Using phpmyadmin Figure 9.2 graphics/ch09/displaycreatetablecustomers.jpg.

Specifying the Attributes for the Customers Table Figure 9.3 graphics/ch09/displayspecifyattributecustomers.jpg.

Result of Creating the (empty) Customers Table Figure 9.4 graphics/ch09/displayresultcreatetablecustomers.jpg.

Categories of SQL Commands Commands for data definition CREATE ALTER DROP Command for data retrieval SELECT, to query the database and retrieve information Commands for data manipulation INSERT LOAD UPDATE DELETE TRUNCATE Commands for data transaction (not discussed here) Commands for data control (not discussed here)

The CREATE Command CREATE DATABASE nameofdatabase; Example: CREATE DATABASE webbook; CREATE TABLE table_name ( column_name column_type colum_constraints, ); Example follows

SQL Command for Creating Our Customers Table Figure 9.5 graphics/ch09/createcustomers.sql.

Altering the Customers Table Figure 9.6 graphics/ch09/displaychangeattributeproperty.jpg.

Example of the ALTER Command ALTER TABLE 'Customers' ADD 'gender' VARCHAR(1) NOT NULL AFTER 'customer_last_name'

Examples of the DROP Command To drop an attribute: ALTER TABLE 'temp' DROP 'dummy1'; To drop an entire table: DROP TABLE 'temp';

Dropping an Attribute from a Table Figure 9.9 graphics/ch09/displaydropdummy1.jpg.

Dropping a Table from a Database Figure 9.10 graphics/ch09/displaydroptemp.jpg.

General Format of the INSERT Command INSERT INTO table_name (comma-separated list of attributes) VALUES (comma-separated list of values for attribute 1), (comma-separated list of values for attribute 2), (comma-separated list of values for attribute 3),... (comma-separated list of values for last attribute)

Inserting Records into a Table Using phpmyadmin Figure 9.11 graphics/ch09/displayinsertrecords.jpg.

Result of Inserting Records into a Table Using phpmyadmin Figure 9.12 graphics/ch09/displayresultinsertrecords.jpg.

SQL Command for Inserting Two Records into a Table INSERT INTO 'webbook'.'ref_invoice_status' ( 'invoice_status_code', 'invoice_status_description' ) VALUES ( 'IS', 'Issued' ), ( 'PD', 'Paid' );

Partial View of a csv File Containing Product Records Figure 9.14 graphics/ch09/products870truncated.csv.

Inserting Multiple Records into a Table Using phpmyadmin Figure 9.15 graphics/ch09/displayinsertmanyrecords.jpg.

Result of Inserting Multiple Records into a Table Using phpmyadmin Figure 9.16 graphics/ch09/displayresultinsertmanyrecords.jpg.

Partial View of a csv File Containing Customer Records Figure 9.17 graphics/ch09/customers10000truncated.csv.

Loading Multiple Customer Records from a csv File Using phpmyadmin Figure 9.18 graphics/ch09/displayloadfile.jpg.

Result of Loading Multiple Customer Records from a csv File Figure 9.19 graphics/ch09/displayresultloadfile.jpg.

SQL Command for Loading Multiple Customer Records Figure 9.20 graphics/ch09/loadfile.sql.

General Format of the LOAD Command Figure 9.18 graphics/ch09/displayloadfile.jpg.

Syntax of the UPDATE Command UPDATE 'table_name' SET column1_name=expression [column2_name=expression2,...] [WHERE where_expression] [LIMIT n]

Selecting Records from a Table Using phpmyadmin Figure 9.21 graphics/ch09/displayselectall.jpg.

Selecting Records from a Table Using SQL Figure 9.22 graphics/ch09/selectall.sql.

Deleting a Record from a Table Using phpmyadmin Figure 9.23 graphics/ch09/displaydeleterecord.jpg.

Syntax of the DELETE Command DELETE FROM table_name [WHERE where_condition] [ORDER BY...] [LIMIT row_count]

Truncating the Customers Table Using phpmyadmin Figure 9.24 graphics/ch09/displaytruncatetable.jpg.

A First Look at the Command-line Interface for MySQL (1 of 3) ok > mysql -u webbook -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 28603 Server version: 5.0.75-0ubuntu10.2 (Ubuntu) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use webbook; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed

A First Look at the Command-line Interface for MySQL (2 of 3) mysql> show tables; +------------------------+ Tables_in_webbook +------------------------+ Customers Invoices Order_Items Orders Payments Products Ref_Invoice_Status Ref_Order_Item_Status Ref_Order_Status Ref_Product_Categories Shipment_items Shipments +------------------------+ 12 rows in set (0.00 sec)

A First Look at the Command-line Interface for MySQL (3 of 3) mysql> select * from Ref_Invoice_Status; +---------------------+----------------------------+ invoice_status_code invoice_status_description +---------------------+----------------------------+ IS Issued PD Paid +---------------------+----------------------------+ 2 rows in set (0.01 sec) mysql> quit Bye webbook >

Keyword Modifiers Commonly Used with a SELECT Command FROM, which is followed by a comma-separated list of the names of the tables from which the data are to be taken. WHERE, which is followed by a commaseparated list of the conditions that specify which rows are of interest for the retrieval. GROUP BY, which is followed by information indicating how the data in rows with related values are to be combined. ORDER BY, which is used to identify which columns are used to sort the retrieved data. LIMIT, which specifies a range of records for which the data are to be retrieved.

Use of the COUNT() Function in SQL Figure 9.26 graphics/ch09/countsql.txt.

Use of the SUM() Function in SQL Figure 9.27 graphics/ch09/sumsql.txt.

An SQL Query Illustrating Conditional Retrieval Figure 9.28 graphics/ch09/conditionsql.txt.

Ordering Retrieved Records Using SQL Figure 9.29 graphics/ch09/ordersql.txt.

Retrieving Data from Multiple Tables Using a Join in SQL Figure 9.30 graphics/ch09/multitablesql.txt.

Creating a Copy of a Portion of the Customers Table Using SQL Figure 9.31 graphics/ch09/copytablesql.txt.

Exporting a Database Using phpmyadmin Figure 9.32 graphics/ch09/displayexport.jpg.

Result of Exporting a Database Using phpmyadmin Figure 9.33 graphics/ch09/displayresultexport.jpg.

Exporting a Database Using mysqldump Figure 9.34 graphics/ch09/mysqldump.txt.

Exported Database in an SQL File

Importing a Database Using the Command-Line Interface to MySQL Figure 9.36 graphics/ch09/importsql.txt (Part 1).

Importing a Database Using the Command-Line Interface to MySQL (confirming the import) (1 of 2) Figure 9.37 graphics/ch09/importsql.txt (Part 2).

Importing a Database Using the Command-Line Interface to MySQL (confirming the import) (2 of 2) Figure 9.37 graphics/ch09/importsql.txt (Part 2).

Importing a Database Using phpmyadmin Figure 9.38 graphics/ch09/displayimport.jpg.

Result of Importing a Database Using phpmyadmin (failed attempt) Figure 9.39 graphics/ch09/displayresultimport.jpg.