34
September 3, 2017 Sam Siewert CS317 File and Database Systems Lecture 2 – DBMS – DDL & DML Part-1

CS317 File and Database Systems - mercury.pr.erau.edu

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

September 3, 2017 Sam Siewert

CS317 File and Database Systems

Lecture 2 – DBMS – DDL & DML Part-1

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/

MySQL on PRClab – Verified? Required for all Exercises, So Make Sure it’s Working Basic MySQL Exploration on PRClab (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 – Analyze Volume Use by Files – Describe Advantages of DBMS over File Systems – Describe Advantages of File Systems over DBMS – Describe How Facebook Might Use both DBMS and File

Systems to Handle Structured and Un-structured Data

Sam Siewert 3

Exploring Sakila Tables: rental, film, customer, inventory rental: [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 out Customers that have rental

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

Sam Siewert 5

Some Example MySQL – Hands On Log in Which databases are available? Choose one Start working with it – Show tables in DB – Query tables – Add new rows to

tables (data instances) – Modify column entries

in rows

Sam Siewert 6

%mysql -u siewerts -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 473 Server 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 names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------------+ | Tables_in_sakila | +----------------------------+ | actor | | actor_info | … | staff | | staff_list | | store | +----------------------------+ 23 rows in set (0.00 sec) Mysql>

More Example MySQL – Hands On For 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 Query So Far, We are Using only 1 Table at Time Can we Query for Data from 2 Tables Correlated by a Field? Read Chapter 2 & 3 in Connolly-Begg

Sam Siewert 7

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>

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, PNG Video Files - MPEG Facebook Account User Facebook 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 8

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 9

Connolly-Begg Chapter 2

Discussion & Examples

Sam Siewert

10

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

Birth of DBMS & RDBMS CODASYL – 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 ODBMS NoSQL, Big Data, Structured/Unstructured - Current

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.

ANSI-SPARC ANSI-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 Solutions ERNIE CS Student Center Describes portion of database relevant to a user

– Conceptual Level – Community view of a database MySQL – 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 14

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

Pearson Education © 2014 15

Data Independence and the ANSI-SPARC Three-Level Architecture

(Connolly-Begg)

Pearson Education © 2014 16

MySQL – Example External View – PHPadmin Page for Sakila Conceptual 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 17

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>

Nice Learning Trick Use the PHPadmin page 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 Refman Example DBs - http://dev.mysql.com/doc/index-other.html

Sam Siewert 18

Alternative to MobaXterm and Putty for Command Line

Putty is Excellent for PRClab Basic Connection Cygwin – for personal equipment, provides Unix/Linux command shell for Windows – Emulation for PRClab Linux – Simpler than Virtual-Box + Linux – Better Cut and Paste (main reason I use) – https://www.cygwin.com/, download and run setup-x86.exe

Add OpenSSH to your Cygwin download

Sam Siewert 19

Database Languages Data 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 20

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

Sam Siewert 21

Database Languages

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 22

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 23

Data Model

Purpose – To represent data in an understandable way.

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

Pearson Education © 2014 24

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 25

Relational Data Model

Pearson Education © 2014 26

Network Data Model

Pearson Education © 2014 27

Hierarchical Data Model

Pearson Education © 2014 28

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 29

Functions of a DBMS

Data Storage, Retrieval, and Update. A User-Accessible Catalog. Transaction Support. Concurrency Control Services. Recovery Services.

Pearson Education © 2014 30

Functions of a DBMS

Authorization Services. Support for Data Communication. Integrity Services. Services to Promote Data Independence. Utility Services.

Pearson Education © 2014 31

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 32

Components of a DBMS

Pearson Education © 2014 33

Components of Database Manager

Pearson Education © 2014 34