Download Getting Started with Databases

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Data model wikipedia , lookup

Information privacy law wikipedia , lookup

Serializability wikipedia , lookup

SQL wikipedia , lookup

Expense and cost recovery system (ECRS) wikipedia , lookup

Microsoft Access wikipedia , lookup

Data vault modeling wikipedia , lookup

Business intelligence wikipedia , lookup

PL/SQL wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SAP IQ wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Concurrency control wikipedia , lookup

Database wikipedia , lookup

Versant Object Database wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
CIT 3353 -- Fall 2006
www.clt.astate.edu/jseydel/mis3353
Website Development &
Management
Getting Started with Databases
Instructor: John Seydel, Ph.D.
Student Objectives
Upon completion of this class meeting, you
should be able to:

Interact directly with MySQL to




Start the database server
Manage users
Create databases
Work with database tables





Create
Insert rows
Display information
Summarize basic database concepts
Write basic PHP scripts that connect to MySQL
databases and display database info
Data Management Fundamentals
Review of the logical organization of data




Field: characteristic of the entity involved (column)
Record: collection of fields for a specific entity (row)
File: collection of records for a given entity type (table)
Database: collection of related files
Typical data management operations



Sort
Query
Update
Purpose: raw data  information (reports)
Typically done via some interface


Direct interaction with DBMS
Applications program calls to DBMS
 Desktop applications
 Web applications (via PHP)
Review:
The Installation Process for MySQL
Generally follows procedure given in Meloni textbook
Download mysql-4.0.24-win.zip from course
Handouts page (or from MySQL.com) into Downloads
directory
Uncompress into a default temporary directory and
then open that directory
Double-click on SETUP.EXE and accept all defaults as
the installation wizard runs
Test the installation




Run c:\mysql\bin\winmysqladmin.exe
Provide a username and password you’ll remember (generally,
these won’t be used again, ever)
Note the stoplight now on taskbar at bottom right
MySQL’s database server now starts upon bootup
Now, some followup . . .
Now:
Completing the MySQL Setup
Note the following clarifications, as well as corrections to errors in
the textbook
MySQL executables are located in c:\mysql\bin (not
c:\mysqlbin)
Initial startup for the MySQL database server

Use the WinMySQLAdmin interface (not simply c:\mysql\bin)
 Starts the database server for the first time
 Creates a userID and password (typically not used again)

From then on MySQL will start automatically at boot time
You must create a database user (in DOS window) before working
with any databases (see Chapter 11, pp. 180-181)
mysql –u root -p
USE mysql;
GRANT ALL ON *.* TO jojobeans@localhost IDENTIFIED BY “psw”;
To ensure things are working, create a database
CREATE DATABASE testDB;
When done with MySQL
QUIT;
(do not need to run mysqladmin)
How MySQL Stores the Data
Each table (i.e., relation) involves three files:



TableName.frm
TableName.MYD (where the actual data are)
TableName.MYI
The files for all the tables belonging to a given
database are stored in a directory with the same
name as the database
All database directories are stored in the data
directory within c:\mysql
Note: upon completion of the MySQL installation
process, there are two databases

mysql
 Stores system info (users, permissions, etc.)
 Do not mess with this

test (this is initially empty, and anyone can access it without
login)
Working with MySQL
Get started
mysql -u jojobeans –p
USE testDB;
SHOW TABLES;
Using tables
CREATE TABLE tblTest
(testID int, testNote text);
EXPLAIN tblTest;
SELECT * FROM tblTest;
INSERT INTO tblTest
VALUES (123,’First entry’);
INSERT INTO tblTest
VALUES (456,’Second entry’);
SELECT * FROM tblTest;
QUIT;
Using PHP to Interface with MySQL
Making sure MySQL works with PHP


In php.ini: uncomment extension=php_mysql.dll
Copy c:\php\ext\php_mysql.dll into c:\windows New
Always starts with the creation of a connection
Requires opening a database
Generally involves generating a recordset (aka, result
set)
Processing records typically involves some sort of
looping construct


One repetition per record
Must be controlled by some sort of counter: foreach(),
while(), and for()
Makes use of several important functions . . .
PHP Functions: Processing Basic
Queries
mysql_connect()


Arguments (string): server, userID, password
Returns a string (connection information)
mysql_select_db()


Arguments (string): database to be used, connection
Returns a boolean value
mysql_query()


Arguments (string): SQL query, connection
Returns a recordset (an array of data)
mysql_num_rows()


Argument (array): recordset
Returns an integer (number of records)
mysql_fetch_array()


Arguments (string): recordset
Returns a row from the recordset
Demonstration: Listing FKAuto’s
Inventory
The database:


fkauto
One table at present: UsedCars
Write a script to access the data and create a
web page displaying a bullet list with make,
model, year, mileage, and price for each car
What We’ve Seen
New PHP functions





mysql_connect()
mysql_select_db()
mysql_query()
mysql_num_rows()
mysql_fetch_array()
A little SQL


Used directly with MySQL
Incorporated into PHP script
The for() construct
How to use PHP to connect to MySQL, open a database,
create a recordset, and process that recordset
How/where MySQL files are stored
The FKAuto Database Files
For use on your own computer:


Download these
Available in the Documents directory (not
public_html) of the cit3353 account on SuSE1
 Recall the password?
 All three files for the UsedCars database are in a
directory named fkauto
For use on SuSE1: don’t worry about where
they are, as they are available to your scripts


However, the “owner” is user cit3353
Thus use the appropriate values for user and
password