Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M"...

32
Database Software James Brucker

Transcript of Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M"...

Page 1: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Database Software

James Brucker

Page 2: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Free Relational Databases

MySQL - one of the most popular

the "M" in "LAMP" and "XAMP" stacks

"free" for non-commercial use only

many useful tools: MySQL Administrator (Windows or Linux)

MySQL Workbench & Query Browser

phpMyAdmin

Owned by Oracle

http://www.mysql.com

SOLD

Page 3: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Free Relational Databases

PostgreSQL

Based on Berkeley Postgres

Open Source, can be used in commercial apps without a licence

Reputation as very stable and reliable

Included with Linux distros

Has some O-O features

http://www.postgresql.org

Page 4: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Client - Server Databases

Database Server is a separate process running on a host.

Clients can run on any machine. Many programs may be clients using a standard API.

Server (mysqld)"mysql" utility

Java appusing JDBC

Excel client

Server sideClient side

server controls access to database

Page 5: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Why the "d" in mysqld

The MySQL server is named "mysqld". Other programs ending in "d":

ftpd - ftp server httpd - HTTP server sshd - Secure Shell server

Why "d"?

Server (mysqld)

database

Page 6: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Lightweight & Embedded Databases

"Light" - they don't consume much cpu or memory.

"Embedded" - database manager is included (embedded) into your application.

Called "embedded mode".

MyApp

Connectionderby.jar

include the database software (jar) with your application.

Software in JAR manages database as

plain files. No server process.

Page 7: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Hypersonic SQL

HSQLDB - lightweight, fast database written in Java

database can be stored in memory or on disk.

embed in Java app - no separate server

don't need to install database server or disk-based database

can also run in client-server mode

useful for development and "demo" systems

http://hsqldb.org

Page 8: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Derby

Derby - lightweight, pure Java database

formerly "Cloudscape", donated to Apache foundation

only 1 user can connect to database at a time

embed in Java applications - no separate server

similar to HSQLDB

can also run in client-server mode

included with JavaEE as "Java DB"

http://db.apache.org/derby

Page 9: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

SQLite

World's most widely distributed database

written in C

very small: 350KB binary

used on Android

3rd party JDBC drivers:

http://code.google.com/p/sqlite-jdbc/

http://www.ch-werner.de/javasqlite/

http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC

Page 10: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Berkeley DB

Berkeley DB - sleepycat.com (RIP) libraries for embedded database using the OS's file system.

No db manager, No network access, No query language.

used as data tier for LDAP, sendmail, and many other apps

very small and fast -- faster than any relational DB w/ manager

C and pure Java version

language bindings for C++, Perl, Python, Ruby, and more

bought by Oracle in 2006: http://www.oracle.com/database/berkeley-db/index.html

still Open Source under the "Sleepycat Public License" and "Sleepycat Commercial License",

not required to distribute the source code with your app.

SOLD

Page 11: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

"Community Edition" Databases

IBM DB2 Express-C - relational DB with XML support free edition of IBM DB2 good documentation and learning tools:

http://www.ibm.com/university http://www.ibm.com/db2/express

Oracle 11g Express Edition (XE) leading market share among commercial databases XE is easier to administer than full Oracle http://www.oracle.com

Page 12: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Commercial Databases

Databases ranked by 2006 revenue (million US$).Source: Gartner Research (www.gartner.com)

0

1000

2000

3000

4000

5000

6000

7000

8000

Oracle IBM Microsoft Teradata Sybase Other

Page 13: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Install and Admin MySQL

Page 14: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Getting MySQL

http://dev.mysql.com/downloads/

Server and client

Query Browser, Admin Tool

Java, ODBC, .Net, PHP

Many platforms:

Windows, Linux, Solaris, Mac OS-X

Page 15: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

MySQL Software

Server and Client

Download MySQL "Community Edition" from www.mysql.com

Ubuntu can install using package manager or Synaptic

GUI Tools

mysql-gui-tools-5.2r6-platform

mysql-query-browser-1.1.17-win.msi

Connectors

Connector/J Java JDBC

For CPE, download from http://se.cpe.ku.ac.th/download/mysql

Page 16: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

How to Administer MySQL

To manage a MySQL server, you need an administrator account (root) and administration tool:

mysqladmin - command line tool (included)

MySQL Administrator - part of MySQL Workbench

Other Tools:

phpMyAdmin - Web-based admin tool, open source

Webmin - another Web-based admin tool, for Linux

Page 17: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

MySQL Administrator

Connection Dialog

Main Window, "Catalogs" view

Easy to use GUI interface.

Page 18: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

mysqladmin and mysql

Useful command line tools. Change MySQL administrator password. Create new database. Import data / export data. Backup a database. Modify privilege tables.

cmd> mysqladminUsage: mysqladmin [OPTIONS] command command....Where command is a one or more of: create databasename Create a new database drop databasename Delete a database and all its tables flush-tables Flush all tables password new-password Change old password to new-password reload Reload grant tables shutdown Take server down status Gives a short status message from server version Get version info from server

Page 19: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Create a database

cmd> mysql -h hostname -u root -pPassword: ********mysql> create database Students;Query OK, 1 row affected mysql> use Students;Database changedmysql> show tables;Empty set

Page 20: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Create a table in batch mode

DROP TABLE IF EXISTS 'Students';CREATE TABLE 'Students' ( `ID` int(11) PRIMARY KEY NOT NULL auto_increment, `Name` varchar(60) NOT NULL DEFAULT '', `Telephone` char(12), `Birthday` date) DEFAULT CHARSET=utf8;

It is easier and more repeatable to put SQL commands in a text file and process the file using a MySQL client.

(1) create a text file ("student-schema.sql") containing commands:

(2) use mysql command tool to source the text file:

cmd> mysql -u root -p

mysql> use Student;

mysql> source student-schema.sql;

Page 21: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Another Batch Example

-- create table for student data-- use the 'UTF8' character set for Thai names-- Jim Brucker, Jan 2006USE test;-- this will discard any existing data!!!DROP TABLE IF EXISTS students;

CREATE TABLE students ( id CHAR(8) PRIMARY KEY NOT NULL,prefix VARCHAR(24) NOT NULL DEFAULT '',firstname VARCHAR(40) NOT NULL DEFAULT '', lastname VARCHAR(40) NOT NULL DEFAULT '',enfirstname VARCHAR(40) NOT NULL DEFAULT '',enlastname VARCHAR(40) NOT NULL DEFAULT ''

) DEFAULT CHARSET=utf8;

Page 22: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Adding Data to Tables

1. Text file containing SQL "INSERT" commands.

2. Comma-delimited (CSV) file. Can be created using Excel.

Page 23: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Comma Separated Values files

cmd> mysql -h hostname -u root -p

Password: ********

mysql> LOAD DATA INFILE '/path/filename' INTO TABLE tbl_name FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' ;

Query OK, 499 rows affected

"James","Brucker","[email protected]",1234

"George","Bush","[email protected]",1111

"Santa","","[email protected]",001

Comma Separated Values (CSV) is a common interchange format for text data. Used by Excel, Yahoo AddressBook, ... many apps.

Page 24: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

MySQL Users

What identifies a user?

"student" on local machine may not be the same person as "student" on pirates.com

even if "student" is the same, you may want to assign different privileges to local and network logins.

MySQL identifies users as: username@hostname

student@localhost

student@% (any host)

[email protected]

Page 25: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Creating a User

Easy way: use MySQLAdmin or phpMyAdmin.

Command line (MySQL 5.0):

mysql> CREATE USER 'user1'@'localhost' IDENTIFIED BY 'secret';

mysql> CREATE USER 'user1'@'%.ku.ac.th' IDENTIFIED BY 'secret2';

mysql> CREATE USER 'user1'@'%' IDENTIFIED BY 'hackme';

Page 26: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Managing Users and Permissions

Give "guest" permission to view the World database:

sql> GRANT SELECT ON world.* TO guest;

Allow "student" to insert/edit records in the City table:

sql> GRANT INSERT,UPDATE ON world.City TO student;

All "student" to modify the population field of existing countries (but not add new countries):

sql> GRANT UPDATE(population) on world.Country TO student;

Deny all privileges to everything to "hacker":

sql> REVOKE ALL on *.* TO Hacker;

Page 27: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

GRANT / REVOKE Syntax

privilege:SELECT, INSERT, UPDATE, DELETE, REFERENCES,CREATE, ALTER, DROP, INDEX,CREATE_VIEW, SHOW_VIEW

with_option

GRANT OPTIONMAX_USER_CONNECTIONS countMAX_CONNECTIONS_PER_HOUR countMAX_QUERIES_PER_HOUR count

GRANT privilege[(column_list)] [, ... ]

ON { table_name | * | *.* | db_name.* }

TO user [IDENTIFIED BY [PASSWORD] 'password']

[, ... ]

[WITH with_option ... ]

Page 28: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

GRANT / REVOKE Example

student can query, insert, update, and delete records in the world database, but he can't change the database schema or indexing, can't grant privileges to others.

This command also creates a student user with password secret.

GRANT select,insert,update,delete ON world.*

TO student@'%' IDENTIFIED BY 'secret' ;

Page 29: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

GRANT / REVOKE Example

Create an admin user for the "wiki" database so that you can create tables, indices, etc.

Access allowed only on local machine, not over network.

Typical way of setting database permissions for a web application.

GRANT ALL ON wiki.*

TO 'wikiadmin'@'localhost'

IDENTIFIED BY 'secret' ;

Page 30: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Privileges you can GRANT and REVOKE

Type of Operation Statement

View table data SELECT

Add rows to a table INSERT

Modify data in a table UPDATE

Delete rows DELETE

Reference a table from another REFERENCES

Drop tables DROP

Create or Alter tables CREATE, ALTER

Index a table by an expression INDEX

All privileges ALL

Page 31: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Exercise

Create a user named "hacker".

Give hacker permission to view data in world.Country and world.City, but not world.CountryLanguage.

Give hacker permission to view, insert, update, and delete rows in all tables in the test database.

Page 32: Database Software James Brucker. Free Relational Databases MySQL - one of the most popular the "M" in "LAMP" and "XAMP" stacks "free" for non-commercial.

Resources

MySQL http://dev.mysql.com/tech-resources/articles/dotnet/

Learning SQL http://www.w3schools.com/sql/

nice tutorial and command reference