Using Relational Databases and SQL

Download Report

Transcript Using Relational Databases and SQL

CS 122:
Using Relational Databases and SQL
Tarik Booker
Department of Computer Science
California State University, Los Angeles
Prologue: (A Little History…)
A Little History…(2)
A Little History…(3)
Lord Byron
(1788 – 1824)
+
+
Charles Babbage
(1791 – 1871)
=
Ada (Byron) Lovelace
(1815 – 1852)
The Difference Engine
(Designed but never built)
A Little History (4)…
Using Relational Databases and SQL
Part I
Databases
Database Definition
Data (information) base (foundation)
Database - a structured collection of persistent
data.
Structured: organized according to a set of rules. In
this case, organized according to a database model.
Persistent: stored in permanent storage, not just RAM.
If you shut down the application or the power goes
off, the data is not lost.
Database Definition
Many definitions are like this one:
A collection of data, typically modelling the activities of
one or more related organizations (Ramkrishnan and
Gherke, Database Management Systems.)
 Keep in mind, databases don’t always model anything
in particular. Database designers don’t always know
what the data will be used for.
What is a Database?
Structured using a database model
No database model, no database!
Often, not always, used to model organizational
activities
Examples:
Companies
Stores
Universities
Database Skills
Database skills are foundational in CS
The great majority of modern applications use
databases to store information
You will put these skills together with your OOP
programming skills a little later if you are an
undergraduate, very soon if you are a grad student
As a working software engineer, you will
probably use the skills you learn in this class
every day
Database Skills
Some applications you are familiar with that rely
heavily on large databases:
Wikipedia
GET
Amazon.com
ITunes
Others?
Old Databases
Physical/Logical Separation
Before DBMS, users had to implement this
functionality themselves!
Traverse pointers at the physical level to
extract data from a database
Far easier to abstract at the physical level!
Write a program at the logical level instead
Database Models
The Two Levels of a Database Model
Physical Level (how data is stored)
The things we don’t have to worry about
Logical Level (how data is organized)
The things we do care about
The Basic Models
Hierarchical Model (IBM’s IMS) represented data as
a tree
Network Model (CODASYL)
Relational Model (ALPHA, SEQUEL)
Hierarchical Database Model
Hierarchical Database Model
Example of a query to retrieve info:
for book in (get_children("Programming/J.Smith”))
print book.field("Title"), book.field("Publisher")
Mostly superseded by relational model
Has an afterlife with XML
XML Data
<CATALOG>
<CD>
<TITLE>Is this it?</TITLE>
<ARTIST>The Strokes</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Atlantic</COMPANY>
<PRICE>7.99</PRICE>
<YEAR>1995</YEAR>
</CD>
<CD>
<TITLE>Black Angel</TITLE>
<ARTIST>Savage Rose</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>Mega</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1995</YEAR>
</CD>
</CATALOG>
Network Model
Built on hierarchical model but allows multiple parents
and multiple children
Relational Model
Proposed by Edgar F. Codd (circa 1969)
Database is a collection of tables (relations)
Relational comes from ‘Relational
Algebra/Calculus’ and not from ‘Relationships’
Relational model is based on extensive
mathematical theory, which we will not cover in
this class
Dominant database model
Oracle was the first to aggressively market a
commercial relational database product
Dr. Edgar F(rank) Codd
MA Mathematics, MA Chemistry
MS and PhD in Communication Sciences
ACM Turing Award (1981)
Tables
Artists = Table (Relation)
ArtistID, City, Region, ... = Columns (Attributes)
Each row is called a Record (Tuple)
Using Relational Databases and SQL
Part II
Database Management Systems
Database Management Systems
(DBMS)
A DBMS handles these functions:
Data definition: Defining new data structures for a database,
removing data structures from the database, modifying the
structure of existing data.
Update: Inserting, modifying, and deleting data.
Retrieval: Obtaining information either for end-user queries
and reports or for processing by applications.
Administration: Registering and monitoring users,
enforcing data security, monitoring performance,
maintaining data integrity, dealing with concurrency control,
and recovering information if the system fails.
Source: Wikipedia
Database Management Systems
(DBMS)
Some common relational DBMSs:
MySQL, PostgreSQL (free, open source)
Oracle, MS SQL Server (commercial)
Database Schemas
The definition of the database, where you define
Tables
Relationships
Constraints
Stored Functions and Procedures
Views
Indexes
Schemas are typically represented by a schema
diagram; see the Lyric diagram linked from the
course page
Database Management Systems
(DBMS)
You can have multiple databases, each with a
single schema
A separate database for each application
Toystore (First database)
Bookstore (Second database)
Furniture Store (Third database)
Etc.
You can also have a single database, with
multiple schemas
Using Relational Databases and SQL
Part III
Query Languages
Query Languages
Query: question
Query Language = A computer language used to
extract data from a database
Data Sublanguage = A computer language used to
extract and manipulate database data
SEQUEL/SQL (1974) developed at IBM
Query Languages
Data Sublanguage Alpha (Codd’s original query
language)
Data Sublanguage SEQUEL (SQL)
SQL
“Structured Query Language”
A non-procedural, domain-specific language (not like
Java, C or C++)
An open ANSI and ISO standard
Supported by most major DBMS
Some variations in implementations
Used by programmers, managers, and database
administrators
SQL
SQL is “nonprocedural” or “declarative”
Procedural languages, like Java or C, require
programmers to implement an algorithm (“a series of
instructions that will solve a problem in a finite
amount of time”) to accomplish each task
Nonprocedural / declarative languages, like SQL,
require the programmer to describe *what data* s/he
wants. The platform (in this case, DBMS) determines
how to produce the data
This is an important distinction, but as we will see, it
is not as clear-cut for SQL as it is for, say, HTML.
SQL Functions
View information from relational databases
Single and multiple table selections
Calculation and analysis
Manipulate information in relational databases
Insert and delete records
Update records
Create relational databases
Create databases, tables, constraints, ...
Nonstandard Features
• SQL is an open standard, but developers of DBMSs often
add additional features that are not part of the standard
• Differentiate their products from competitors
• Vendor lock-in
• What happens when you want to switch to a different DBMS?
• Is it a good idea to use features like this?
Using Relational Databases and SQL
Part IV
Lyric Database Discussion
Primary Keys
Primary key is used to uniquely identify every
record in a table
Must be a field or combination of fields with
unique values
What would happen if we needed to identify
individuals in the university DB and tried to do this
using first name? Last name? Both? Height? DOB?
If more than one field is required, we have a
composite primary key
The Lyric Database
Database for a web-based company that provides
services to artists and the studios that they work
for
Before we start extracting data from a database,
we must understand the database completely first
Primary Key Example
What is the primary key of the Studios table?
What is the primary key of the
XRefArtistsMembers table? (hint: it may require
more than one field to make up a primary key!)
Using Relational Databases and SQL
Part V
MySQL
MySQL
For coursework, we will use MySQL, which you
must install on a USB drive.
– Bring a USB drive to the next class meeting!
You may also install it on your own laptop, but
note that you will have to use the lab computers
for the midterm and final exam, so be sure you
can run it from a USB drive before the midterm.
Downloading MySQL, Part I
Go to CS122 web page and follow the links to MySQL site
Get MySQL Community Server
mysql-5.5.x has the MySQL database client and server
programs
Get the .zip files (not the MSIs) for your OS (Windows vs.
OSX) and processor (32 vs 64 bit).
The files are labelled in a way that may confuse you into
downloading the source code, which you don’t need. Be careful
to get the binaries instead. MySQL 5.5.8-win32, for example, is
132 MB. The 27 MB file is the source code.
Downloading MySQL, Part II
Extract the zip files; you will have two directories
You may also want to use the MySQL Workbench, which is a
GUI tool for working with MySQL. However, Workbench only
works with the 32 bit version and is buggy in any case. Please
don’t ask me to help you with it until at least week 3, after
everyone is working smoothly with the main MySQL software.
mysql-workbench-gpl-5.2.x.... is the MySQL GUI Tools
Using MySQL In Windows
This process should only be slightly different in OSX
Open up a Windows command line console
Use the cd command to navigate to the
mysql-5.x.xx-xx/bin directory
• If you add this directory to your PATH, you won’t have to
navigate there every time. However, you *won’t* be able to add
anything to the PATH on the lab computers.
Type in the following to start the database server:
start mysqld
Then type in the following to start the database client:
mysql –u root
Some MySQL Commands
Once MySQL has started and you see the mysql
prompt:
At mysql> prompt type in: show databases;
At mysql> prompt type in: create database lyric;
At mysql> prompt type in: use lyric;
At mysql> prompt type in: show tables;
You shouldn’t see any yet
Adding Data to a Database
Now that the database is selected, let's load a
database script
Download lyric.sql from the course webpage
At mysql> prompt type in: source [path] lyric.sql;
Where [path] stands for the path to the location where you
saved lyric.sql.
If you put lyric.sql in mysql’s bin directory, all you will
have to type is source lyric.sql
You should see a bunch of messages like this:
Query OK, 1 rows affected (0.01 sec).
Verify that the database is set up
To check whether everything has worked correctly, type
SELECT * FROM Salespeople;
The output should look like this:
+---------+-----------+----------+----------+--------+------------+
| SalesID | FirstName | LastName | Initials | Base | Supervisor |
+---------+-----------+----------+----------+--------+------------+
|
1 | Bob
| Bentley | bbb
| 100.00 |
4|
|
2 | Lisa
| Williams | lmw
| 300.00 |
4|
|
3 | Clint
| Sanchez | cls
| 100.00 |
1|
|
4 | Scott
| Bull
| NULL | NULL |
| sjb
+---------+-----------+----------+----------+--------+------------+
4 rows in set (0.39 sec)
Using MySQL in the lab
If you will be using your own laptop
in the lab, bring it to the next class
meeting
If you will be using MySQL on a lab
computer, *bring a USB drive to the
lab* on Wednesday