Upload
louise-james
View
230
Download
0
Tags:
Embed Size (px)
Citation preview
Database Software
Brief Description
James Brucker
Free Relational Databases
MySQL - one of the most popular
runs on almost any OS
"free" for non-commercial use only
widely used in OpenSource software - "LAMP" apps
many useful tools: MySQL Administrator (Windows or Linux)
MySQL Query Browser
phpMyAdmin
MySQL now owned by Sun Microsystems
http://www.mysql.com
Free Relational Databases
PostgreSQL - another popular database
Based on Berkeley Postgres
Open Source, can be used in commercial apps without a licence
Reputation as very stable and reliable
Included with Linux, widely used in OpenSource
Has some O-O features
http://www.postgresql.org
Free Lightweight Databases
HSQLDB - lightweight, fast database written in Java
store database in memory or on disk.
can embed in Java application - no separate server
don't need to install a database server or disk-based database
can also run in server mode and access via JDBC
very useful for development, test, and "demo" systems
http://hsqldb.org
Free Lightweight Databases
Derby - lightweight, pure Java database
formerly "Cloudscape", donated to Apache foundation
very small: 2MB for database engine and JDBC driver
only 1 user can connect at a time
can embed in Java applications - no separate server
similar to HSQLDB
included with Sun JavaEE as "Java DB"
http://db.apache.org/derby
Database without a manager
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 Open Source applications.
very small and fast -- faster than any relational DB with manager for random queries and updates.
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", which does not require distributing the source code with your app.
Berkeley DB has both a C and pure Java version
language bindings for: C++, Perl, Python, Ruby, many others
Community Edition Databases
IBM DB2 Express-C - relational DB with XML support DB2 is commercial, community edition is free much documentation on IBM academic web site
http://www.ibm.com/university http://www.ibm.com/db2/express
Oracle 11g Express Edition (XE) leading market share among commercial databases XE is simpler to administer than full Oracle http://www.oracle.com
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
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
MySQL Software
Server and Client
mysql-essential-5.x.y-win32.msi
mysql-standard-5.x.y-linux-i686-glib23.rpm
GUI Tools
mysql-gui-tools-5.0r6-platform
mysql-query-browser-1.1.17-win.msi
Connectors
Connector/J Java JDBC
Connector/ODBC Windows ODBC
Connector/Net Microsoft .Net
For CPE Lab, can download from http://se.cpe.ku.ac.th/download/mysql
How to Administer MySQL
To manage a database server, you need an administrator account and administration tool
mysqladmin - command line tool
MySQL Administrator - GUI tool from MySQL.com
phpMyAdmin - Web-based admin tool, open source
Webmin - another Web-based admin tool, for Linux
MySQL Administrator
Connection Window
Main Window, "Catalogs" view
phpMyAdmin
http://www.phpmyadmin.net
mysqladmin and mysql
Useful command line tools. Change MySQL administrator password. Create new database. Import data / export data. 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
Create database and tables
cmd> mysql -h hostname -u root -pPassword: ********mysql> create database Students;Query OK, 1 row affected mysql> use Students;Database changedmysql> show tables;Empty setmysql> CREATE TABLE student (
firstname VARCHAR(20) NOT NULL, lastname VARCHAR(30) NOT NULL, id CHAR(8) NOT NULL, birthday DATE, PRIMARY KEY(id));
Query OK, 0 rows affected
Batch mode table/database creation
DROP TABLE IF EXISTS 'City';CREATE TABLE 'City' ( `ID` int(11) NOT NULL auto_increment, `Name` char(35) NOT NULL DEFAULT '', `CountryCode` char(3) NOT NULL DEFAULT '', `District` char(20) NOT NULL DEFAULT '', `Population` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;
It is easier and more repeatable to put SQL commands in a file and process the file using a MySQL client. For example:
cmd> mysql -u root -p < filename.sql
Password: ********
or while using mysql:
mysql> source filename.sql
Data in file:
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) 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 '',PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Adding Data to Tables
Using Comma Separated Text 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.
MySQL Users
What identifies a user?
DBMS accepts connection over a network.
"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.
MySQL identifies users as:
username@hostname
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';
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;
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 ... ]
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' ;
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' ;
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
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.
Resources
MySQL http://dev.mysql.com/tech-resources/articles/dotnet/
Learning SQL http://www.w3schools.com/sql/
nice tutorial and command reference