Notes
May 8, 2007
Deidré Foster

Text in this color is to be considered optional but important to remember
MySQL
Database ...
Some of the more popular types are ints, chars, bytes, blobs (for image
      files), varchars, strings, variable length s...
In the left-hand menu click on Downloads and chose the I Can Do It Myself
option. Click on the appropriate option for the ...
To open a database, for example information_schema and show the tables:

     use information_schema; <enter>
     show ta...
All the commands we typed at the command prompt can be written in a file and
saved to a .sql file and used as a script.

I...
Upcoming SlideShare
Loading in …5
×

Deidre Foster-Smith.doc

455 views
403 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
455
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Deidre Foster-Smith.doc

  1. 1. Notes May 8, 2007 Deidré Foster Text in this color is to be considered optional but important to remember MySQL Database Management System (DBMS) are used to create and manage databases and a database is nothing more than a data structure used to store data in a logical format. We will be using MySQL because it’s open source and free. Free being the operative word. There are other databases, for example: Microsoft SQL Server IBM’s DB2 – has a logical connection to Websphere application server Oracle – needs knowledgeable Database Administrator (DBA), used in a lot of enterprise applications PostGres SQL Syntax differs slightly with each DBMS. Data stored in tables also known as relations Records are the rows Fields are columns Name ID … … … Jeff 555 … … … Raj 111 Like all programming language DBMS’s have keywords. In MySQL key words are usually capitalized. The following examples of some of the syntax used in MySQL CREATE DATABASE <name_of_database>; If the database already exists it will throw an error saying that the db already exists USE <name_of_database>; CREATE TABLE <table name> (<field_name field_type options>, <field_name field_type options>, …) May 8, 2008 notes.doc 1
  2. 2. Some of the more popular types are ints, chars, bytes, blobs (for image files), varchars, strings, variable length strings , floats, times, timestamps, dates. Varchars and strings are all written in single quotes. INSERT INTO <database name> (<field_name1, field_name2>, …) VALUES (<field_name1_value, field_name2_value, …) UPDATE <database name> SET <field_name1> = <field_name1_value>, <field_name2> = <field_name2_value>… WHERE <record_to_change > = <value_of_record> Delete statements delete data in table not the table in table itself DELETE <field name> WHERE <filed name> = <field_value> SELECT * WHERE <field_name>=<field_value> <options> Which will go faster * or selected rows…neither To describe the attributes of a table DESC or DESCRIBE <table name> To delete a database DROP <database_name> An example of the above statements is shown below CREATE DATABASE myDB; USE myDB; CREATE TABLE student ( name VARCHAR(50) NOT NULL PRIMARY, ID INT(3) AUTO_INCREMENT); INSERT INTO student(name, ID) VALUES (‘Jeff’, 555); UPDATE student SET ID = 44, grade = B, WHERE name = ‘Jeff’; SELECT * FROM student WHERE name= ‘Jeff’ ORDER BY ID DESC LIMIT 20; Can check SELECT 1 to check if table is still running; DELETE student WHERE name = ‘Jeff’; DROP TABLE student; DROP DATABASE myDB; The DBMS can be downloaded at www.mysql.com. May 8, 2008 notes.doc 2
  3. 3. In the left-hand menu click on Downloads and chose the I Can Do It Myself option. Click on the appropriate option for the computer that you will be running the DBMS on. To set up the server at home you can chose the Windows essentials(x86) option and set it as a service but for in class installation we used the third option. Save the zip to the same folder as Apache and extract to the same folder. To ensure that all files extract to their respective folders, ensure that the options ‘All files/folders’ and ‘Use folder names’ are checked. To run MySQL service, click on the mysqld.exe (daemon process) in the bin directory. This should flash a command window which will disappear. To ensure that the service is actually run you can check the Task Manager for the mysqld.exe process. In a command prompt, change to the bin directory MySQL and type mysql –u root <enter> The root user has no password by default and is needed to create and alter all databases. Remember the syntax of MySQL requires that statements end with a ‘;’. To show the current databases mysql> show databases; Output +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) May 8, 2008 notes.doc 3
  4. 4. To open a database, for example information_schema and show the tables: use information_schema; <enter> show tables; <enter> Output +---------------------------------------+ | Tables_in_information_schema | +---------------------------------------+ | CHARACTER_SETS | | COLLATIONS | | COLLATION_CHARACTER_SET_APPLICABILITY | | COLUMNS | | COLUMN_PRIVILEGES | | KEY_COLUMN_USAGE | | PROFILING | | ROUTINES | | SCHEMATA | | SCHEMA_PRIVILEGES | | STATISTICS | | TABLES | | TABLE_CONSTRAINTS | | TABLE_PRIVILEGES | | TRIGGERS | | USER_PRIVILEGES | | VIEWS | +---------------------------------------+ 17 rows in set (0.00 sec) desc user_privileges; We can actually create the database we had outlined above. NB: -> denote that <enter> was pressed to get a new line; CREATE database student; USE student; CREATE TABLE user ( ->userID int (11) primarykey auto_increment ->fname varchar(50) not null, -> lname varchar(50) not null); Show tables; INSERT INTO user ( ->fname, lname) VALUES (‘Jeff’, ‘Miller’); INSERT INTO user ( ->fname, lname) VALUES (‘Homer’, ‘Simpson’); SELECT * from user fname=’Jeff’; DESC user; May 8, 2008 notes.doc 4
  5. 5. All the commands we typed at the command prompt can be written in a file and saved to a .sql file and used as a script. Inside the cs320 directory we created an example of a file used to run all the commands and called it create.sql DROP DATABASE IF EXISTS student; CREATE DATABSE student; USE student; CREATE TABLE user ( userID int (11) primary key auto_increment, fname varchar(50) not null, lname varchar(50) not null; We return to command prompt NB: < means piping a file into mysql > means piping data from mysql into a file mysql –u root < d:datacs320create.mysql This should take you back to the prompt. If you check the database the previous one we created would have been deleted and we should have a new one in its stead May 8, 2008 notes.doc 5

×