38
September 4, 2018 Sam Siewert CS317 File and Database Systems Lecture 2 DBMS DDL & DML Part - 1

CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

September 4, 2018 Sam Siewert

CS317File and Database Systems

Lecture 2 – DBMS – DDL & DMLPart-1

Page 2: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

MySQL on Linux (LAMP) Skills

DBMS DDL & DML – Part-1(Definition of and Manipulation of Data)

Sam Siewert

2

http://dilbert.com/strips/comic/2010-08-02/ - UFR=? Uniform Financial Report

Page 3: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

MySQL on PRClab1 – Verified?Required for all Exercises, So Make Sure it’s WorkingBasic MySQL Exploration on PRClab1 (and File system)

Goals for Assignment #1– Test Access and MySQL Account– Learn Basic SQL Commands and Explore DB– Learn Basic Linux File System Commands and Explore (“ls”, “lsof”, – Analyze Volume Use by Files (e.g. “df -h”, “du”, “ls -lS -R”, etc.)– Describe Advantages of DBMS over File Systems– Describe Advantages of File Systems over DBMS– Understand How Facebook Might Use both DBMS and File Systems to Handle

Structured and Un-structured Data

Sam Siewert 3

DBs and tables - InnoDB, MyISAM, ISAMDefault is InnoDB per table

Page 4: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Exploring Sakila - AdminerTables: rental, film, customer, inventoryrental: [customer_id], [inventory_id] (where return_date is NULL)-> customer: customer_id-> inventory: inventory_id [film_id]

Film: film_id (where INTERVAL film.rental_duration DAY < CURRENT_DATE())

Sam Siewert 4

All rentals still outCustomers that have rental

Page 5: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Adminer - Sakila exploration

Sam Siewert 5

Page 6: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

myPHPAdmin (similar to Adminer)Useful for insert and delete DB maintenanceCut and paste SQL into command lineSQL editing help minimalRecommend Adminer while learning SQLSeems slower than Adminer was and a little less friendly

Sam Siewert 6

Describe rental ->

Page 7: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Exploring film table on command lineFirst describe the table columns (attributes, domain, integrity, insert features) - shown as rows

Query for data “tuples” or “rows” for all fields

Sam Siewert 7

Page 8: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

More command line explorationAll fields from film table is more than we want, so use a “projection” to select specific columns

1000 films, so use “where” clause (implicit join) to match tuples with attribute data ranges and “limit” to first 5

Sam Siewert 8

Page 9: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Oracle’s Answer – “Flims Rented Out”Requires an INNER JOIN (We have not studied yet)Where rental.return_date is NULLWhere rental_date + film.rental_duration < TODAY

Sam Siewert 9

Page 10: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Before using JOIN, try WHEREAttempt to list late movies meeting specific criteria with WHERE clauses (implicit join)

select customer.last_name,address.phone,film.title, rental_datefrom rental,customer,address,film,inventorywhere rental.customer_id = customer.customer_idand customer.address_id = address.address_idand rental.inventory_id = inventory.inventory_idand inventory.film_id = film.film_idand rental_date < current_date() limit 5;

Sam Siewert 10

Page 11: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Some Example MySQL – Hands OnLog inWhich databases are available?Choose oneStart working with it– Show tables in DB– Query tables– Add new rows to

tables (data instances)– Modify column entries

in rows

Sam Siewert 11

%mysql -u siewerts -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 473Server version: 5.5.38 MySQL Community Server (GPL) by Remi

mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || budinadb |…| sakila |…|| vismdb || whismansdb |+--------------------+21 rows in set (0.00 sec)

mysql> use sakila;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Database changedmysql> show tables;+----------------------------+| Tables_in_sakila |+----------------------------+| actor || actor_info |…| staff || staff_list || store |+----------------------------+23 rows in set (0.00 sec)

Mysql>

Page 12: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

More Example MySQL – Hands OnFor Sakila DB (selected on previous slide)– Show Columns Used in a Table (Schema)– Show Rows in the Table (Data)

Limit or Filter the Results Returned from the QuerySo Far, We are Using only 1 Table at TimeCan we Query for Data from 2 Tables Correlated by a Field?Read Chapter 2 & 3 in Connolly-Begg

Sam Siewert 12

mysql> show columns from city;+-------------+----------------------+------+-----+-------------------+-----------------------------+| Field | Type | Null | Key | Default | Extra |+-------------+----------------------+------+-----+-------------------+-----------------------------+| city_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment || city | varchar(50) | NO | | NULL | || country_id | smallint(5) unsigned | NO | MUL | NULL | || last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |+-------------+----------------------+------+-----+-------------------+-----------------------------+4 rows in set (0.00 sec)

mysql> SELECT * FROM city limit 3;+---------+--------------------+------------+---------------------+| city_id | city | country_id | last_update |+---------+--------------------+------------+---------------------+| 1 | A Corua (La Corua) | 87 | 2006-02-15 04:45:25 || 2 | Abha | 82 | 2006-02-15 04:45:25 || 3 | Abu Dhabi | 101 | 2006-02-15 04:45:25 |+---------+--------------------+------------+---------------------+3 rows in set (0.00 sec)

mysql>

Page 13: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

For Discussion…DBMS vs. File System (read DMBS Chapter #1) – Come to Class Prepared to Discuss– Why is DBMS Better then File System– Why do we Use File Systems still?– What is NoSQL? (Not in Book, http://en.wikipedia.org/wiki/Nosql)– Structured vs. Unstructured Data? – For Discussion

Image Files – JPG, PNGVideo Files - MPEGFacebook Account UserFacebook User Friends and Likes

Assignment #1 Discussion– I will Post Every Other Wednesday, We’ll Discuss, Due Following

Week on Friday– Late Assignments – 10% Penalty for Monday Turn-in, After Monday,

only with Instructor Permission

Sam Siewert 13

Page 14: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

For Discussion…Coordinating Structured and Un-structured Data– Databases– Files (Images, Digital Video, Documents)– E.g. http://mercury.pr.erau.edu/~siewerts/extra/images/– http://mercury.pr.erau.edu/~siewerts/extra/video/– http://mercury.pr.erau.edu/~siewerts/extra/documents/– http://mercury.pr.erau.edu/~siewerts/extra/code/

Assignment #1 – Due this Friday– Late Assignments – 10% Penalty for Monday Turn-in, After

Monday, only with Instructor Permission

Read DBMS Chapters 2 & 3

Sam Siewert 14

Page 15: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Connolly-Begg Chapter 2

Discussion & Examples

Sam Siewert

15

Page 16: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Database Management System (DBMS)

A software system that enables users to define, create, maintain, and control access to the database.

(Database) application program: a computer program that interacts with database by issuing an appropriate request (SQL statement) to the DBMS.

Be Clear on Data Manipulation Language and Query Language (Chapter 2) – DML has larger scope than SQL and includes DBMS User Management, Access Control, Import/Export, … More than just Query

SQL In Depth – Chapters 6 to 9, Appendix I

Pearson Education © 2014

Page 17: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Birth of DBMS & RDBMSCODASYL – Conference on Data Systems Languages (1959 – 1980’s)– COBOL – Common Business Oriented Language– http://special.lib.umn.edu/findaid/xml/cbi00011.xml

1970 – E.F. Codd Paper (on Canvas and Web Page), http://www.seas.upenn.edu/~zives/03f/cis550/codd.pdf– Formalization of earlier work– Landmark paper (we will read in Assignment #2)– Relational Calculus– Codd’s Theorem – Relational Algebra (imperative) and Relational

Calculus (logical)

Modern RDBMS and ODBMSNoSQL, Big Data, Structured/Unstructured - Current

Page 18: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Three Tier DBMS Architecture –Connolly-Begg (ANSI-SPARC)

Pearson Education © 2014

1. All users should be able to access same data.

2. A user’s view is immune to changes made in other views.

3. Users should not need to know physical database storage details. Different from LAMP 4-Tier / Connector 3-Tier

(Week-1, slide #18) - Abstracted 3-Tier, 4-Tier

Page 19: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

ANSI-SPARCANSI-SPARC (ANSI Standards Planning and Requirements Committee) – 1975

Logical Design– External Level – User’s View, relevant to each user (student,

faculty, records and registration, …)ERNIE Campus SolutionsERNIE CS Student CenterDescribes portion of database relevant to a user

– Conceptual Level – Community view of a databaseMySQL – show databases; show tables;Describes the data stored in a database and realtionships

Internal Level and Physical Design– Describes how data is stored

Sam Siewert 19

Page 20: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Differences between Three Levels of ANSI-SPARC Architecture – Connolly-Begg

Pearson Education © 2014 20

Page 21: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Data Independence and the ANSI-SPARC Three-Level Architecture

(Connolly-Begg)

Pearson Education © 2014 21

Page 22: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

MySQL – ExampleExternal View – PHPadmin Page for SakilaConceptual Schema for Sakila– Columns in Tables in DB– http://dev.mysql.com/doc/sakila/en/sakila-structure.html

Internal View – MyISAM, InnoDB (E.g. world DB)

Sam Siewert 22

mysql> show columns from city;+-------------+----------------------+------+-----+-------------------+-----------------------------+| Field | Type | Null | Key | Default | Extra |+-------------+----------------------+------+-----+-------------------+-----------------------------+| city_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment || city | varchar(50) | NO | | NULL | || country_id | smallint(5) unsigned | NO | MUL | NULL | || last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |+-------------+----------------------+------+-----+-------------------+-----------------------------+4 rows in set (0.00 sec)

mysql> SELECT * FROM city limit 3;+---------+--------------------+------------+---------------------+| city_id | city | country_id | last_update |+---------+--------------------+------------+---------------------+| 1 | A Corua (La Corua) | 87 | 2006-02-15 04:45:25 || 2 | Abha | 82 | 2006-02-15 04:45:25 || 3 | Abu Dhabi | 101 | 2006-02-15 04:45:25 |+---------+--------------------+------------+---------------------+3 rows in set (0.00 sec)

mysql>

Page 23: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Nice Learning TrickUse PHPadmin - http://prclab1.erau.edu/phpmyadmin/Browse a DB with Point and Click– Note the constructed MySQL command for “Select data” or “Show

structure”, Copy and Paste– Play with variants on command line

(http://dev.mysql.com/doc/refman/5.0/en/retrieving-data.html )

Great way to teach yourself MySQL along with RefmanExample DBs - http://dev.mysql.com/doc/index-other.html

Sam Siewert 23

Page 24: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Database LanguagesData Definition Language (DDL)

– Allows the DBA or user to describe and name entities, attributes, and relationships required for the application

– plus any associated integrity and security constraints.

MySQL – Create Table Full DDL Syntax (Logical & Physical)– http://dev.mysql.com/doc/refman/5.6/en/create-table.html– DDL in MySQL, both Logical and Physical Specification

Physical DDL Specifications– Storage Engine Selection– InnoDB - http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html– MyISAM - http://dev.mysql.com/doc/refman/5.6/en/myisam-storage-engine.html– Memory - http://dev.mysql.com/doc/refman/5.6/en/memory-storage-engine.html

MySQL – Create Database and Table with Logical Focus– http://dev.mysql.com/doc/refman/5.7/en/database-use.html

Pearson Education © 2014 24

Page 25: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Database and Table CreationFocus on Logical (simple table creation for logical schema)

Sam Siewert 25

Page 26: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Database Languages - Connelly & Begg

Data Manipulation Language (DML)– Provides basic data manipulation operations on data held in

the database.

Procedural DML – allows user to tell system exactly how to manipulate data.

Non-Procedural DML – allows user to state what data is needed rather than how it is to

be retrieved.

Fourth Generation Languages (4GLs)

Pearson Education © 2014 26

Page 27: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Data Model

Integrated collection of concepts for describing data, relationships between data, and constraints on the data in an organization.

Data Model comprises:– a structural part;– a manipulative part;– possibly a set of integrity rules.

Pearson Education © 2014 27

Page 28: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Data Model

Purpose– To represent data in an understandable way.

Categories of data models include:– Object-based– Record-based– Physical.

Pearson Education © 2014 28

Page 29: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Data Models

Object-Based Data Models– Entity-Relationship– Semantic– Functional– Object-Oriented.

Record-Based Data Models– Relational Data Model– Network Data Model– Hierarchical Data Model

Physical Data Models

Pearson Education © 2014 29

Page 30: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Relational Data Model

Pearson Education © 2014 30

Page 31: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Network Data Model

Pearson Education © 2014 31

Page 32: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Hierarchical Data Model

Pearson Education © 2014 32

Page 33: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Conceptual Modeling

Conceptual schema is the core of a system supporting all user views.Should be complete and accurate representation of an organization’s data requirements.

Conceptual modeling is process of developing a model of information use that is independent of implementation details.Result is a conceptual data model.

Pearson Education © 2014 33

Page 34: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Functions of a DBMS

Data Storage, Retrieval, and Update.

A User-Accessible Catalog.

Transaction Support.

Concurrency Control Services.

Recovery Services.

Pearson Education © 2014 34

Page 35: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Functions of a DBMS

Authorization Services.

Support for Data Communication.

Integrity Services.

Services to Promote Data Independence.

Utility Services.

Pearson Education © 2014 35

Page 36: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

System Catalog

Repository of information (metadata) describing the data in the database.One of the fundamental components of DBMS.Typically stores:– names, types, and sizes of data items;– constraints on the data;– names of authorized users;– data items accessible by a user and the type of

access;– usage statistics.

Pearson Education © 2014 36

Page 37: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Components of a DBMS

Pearson Education © 2014 37

Page 38: CS317 File and Database Systemsmercury.pr.erau.edu/~siewerts/cs317/documents/... · MySQL on PRClab1 – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL

Components of Database Manager

Pearson Education © 2014 38