MySQL
description
Transcript of MySQL
Information Systems Today (©2006 Prentice Hall)
MySQLMySQL
1CS3754 Class Note #8, 2013
1. Is an open-source relational database management system
2. Is fast and stable3. Is available as free software and as commercial
software4. Supports the majority of features considered important
by the database community5. Is a great tool for learning about databases
Information Systems Today (©2006 Prentice Hall)
Installing MySQLInstalling MySQL
You can download MySQL from You can download MySQL from
www.mysql.com/down loadswww.mysql.com/down loads You can run MySQL on Linux on the You can run MySQL on Linux on the
computer system of our department computer system of our department by executing:by executing:
mysql –h stretch –u username -pmysql –h stretch –u username -p
2CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Introduction to MySQL Introduction to MySQL MonitorMonitor
After logged in you can see what After logged in you can see what databases exist on the system by: databases exist on the system by: show database;show database;
You can select a database from the You can select a database from the listed database names by:listed database names by:use use databasenamedatabasename;;
After selecting a database, you can After selecting a database, you can see what table are in it by:see what table are in it by:show tables;show tables;
3CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Introduction to MySQL Introduction to MySQL MonitorMonitor
You can get information on a particular table You can get information on a particular table by:by:
describe describe tablenametablename;; You can log out of the monitor by: \qYou can log out of the monitor by: \q You can get a help by: \hYou can get a help by: \h You can execute a file of commandsYou can execute a file of commands
When logged into the monitor by : Source When logged into the monitor by : Source filenamefilename When not logged into the monitorWhen not logged into the monitor by: by:
mysql –u username –p < filenamemysql –u username –p < filename
/* -u means username –p means login with password /* -u means username –p means login with password
4CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Creating Databases and Creating Databases and TablesTables
Creating a database byCreating a database bycreate database create database databasenamedatabasename;;
Creating a table byCreating a table bycreate table create table tablename (table tablename (table definition) [type = table_type]definition) [type = table_type];;
Ex: create table deptEx: create table dept(deptID int not null auto_increment (deptID int not null auto_increment
primary key,primary key,Name varchar(30)) type=InnoDBName varchar(30)) type=InnoDB
5CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Dropping Databases and Dropping Databases and TablesTables
Drop a database byDrop a database by
drop database drop database databasenamedatabasename;; Drop a table byDrop a table by
drop table drop table tablename;tablename;
6CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Inserting, Deleting, and Inserting, Deleting, and Updating DataUpdating Data
Deleting by: Deleting by: delete from deptdelete from dept Inserting by: Inserting by:
insert into dept valuesinsert into dept values(42, ‘Finance’ ),(42, ‘Finance’ ),(128, ‘Research and Development’ );(128, ‘Research and Development’ );
Update by:Update by:
update employeeupdate employeeset job = ‘DBA’ set job = ‘DBA’ where employeeID = “6651’;where employeeID = “6651’;
7CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Uploading data with Load Uploading data with Load Data InfileData Infile
The Load Data Infile command allows The Load Data Infile command allows you to bulk insert data from a text file you to bulk insert data from a text file into a single table without having to into a single table without having to write Insert statements.write Insert statements.
Example: Example:
load data local infile ‘dept_infile.txt’ load data local infile ‘dept_infile.txt’
into table dept;into table dept;
/* Local means the data file is on the /* Local means the data file is on the client machine not on the server. */client machine not on the server. */
8CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Querying MySQLQuerying MySQL
Select columns from tables:Select columns from tables:
select * from dept;select * from dept;
select name, employeeID from employee;select name, employeeID from employee; Using joins to run queries over multiple tables:Using joins to run queries over multiple tables:
select employee.name as EmployeeName, dept.name as select employee.name as EmployeeName, dept.name as deptNamedeptName
from employee, deptfrom employee, dept
where employee.deptID = dept.deptID;where employee.deptID = dept.deptID;
9CS3754 Class Note #8, 2013
Information Systems Today (©2006 Prentice Hall)
Table Types in MySQLTable Types in MySQL ISAM, having replaced by MyISAMISAM, having replaced by MyISAM MyISAM as the default typeMyISAM as the default type
Offer very fast but not transaction-safe storageOffer very fast but not transaction-safe storage InnoDBInnoDB
Offer very fast and transaction-safe storageOffer very fast and transaction-safe storage Row-level lockingRow-level locking Supports for foreign keysSupports for foreign keys Tables are portable from systems to systemsTables are portable from systems to systems
BerkeleyDB (BDB) BerkeleyDB (BDB) Offer transaction-safe storage but slightly worse performance Offer transaction-safe storage but slightly worse performance
than InnoDBthan InnoDB MERGEMERGE
Used to treat multiple MyISAM tables as a single tableUsed to treat multiple MyISAM tables as a single table HEAPHEAP
Stored only in memory and need to be limited in sizeStored only in memory and need to be limited in size10CS3754 Class Note #8, 2013