phpmyadmin Tutorial Please begin by logging into your Student Webspace. You will access the Student Webspace by logging into the Campus Common site. Go to the bottom of the page and click on the Go button under Student Webspace & Email. On the next page, click on the Student Webspace graphic on the right. Then click on the MySQL Management link. Creating a Database Next click on the Create new Database link: Page 1 of 9
You will see the following screen with your OLS user name, instead of mine. Enter a name for your database, and create a user name and a password. Click Create. You will see a screen similar to the following: You should copy and paste this information into a Word or text document. It is important when you later connect to your database through Dreamweaver. Click on the link to go back to the previous page. You will see your newly created database listed. Now click on the phpmyadmin link. Page 2 of 9
A new window will open like the following: Enter the password you created for the database and press Ok. When logged-in to phpmyadmin you will see a screen like the following: Page 3 of 9
Creating a Table The first thing to do is to create a table in your database. Click on the name of your database on the left side of the screen (mine is kk5370aii_test ). You will see a screen like the following. Under Create new table on database your database name type the name of your table and the number of fields it will contain. Remember that your fields are your columns and your records (the values you enter into the table) will be your records (or rows). When you are finished click the Go button. Creating Fields in Your Table It s now time to define each of your fields. Look at the image below. I have created five fields for my CD table. You need to enter a minimum of 4 fields. I entered the name of each field in the Field column. I selected a type for each field in the Type column, I entered a maximum allowed length in the Length column and I choose whether to allow the each field to be null or require it to be not null. You ll notice for the CD_ID field I selected type INT (Integer) and checked the A_I column to set it to auto increment. I did this because CD_ID is my (artificial) primary key and auto_increment is a convenient function offered by phpmyadmin so I don t have to type a value for each CD_ID. The first record will automatically start at 0 and each one will increase by 1. You must select type INT to use auto increment, and you would only use it for an artificial primary key. I also set the CD_ID field to be the primary key by selecting Primary under the Index column as seen below. You will probably have to scroll sideways to see this option. Page 4 of 9
For the title, artist, and length fields I selected the data type VARCHAR because the fields will be of varying length and they may contain characters and numbers. For the year field I selected the YEAR type. If you are entering an entire date, you would select the DATE type. You can find a great explanation of the many data types offered by MySQL here: http://dev.mysql.com/doc/refman/5.0/en/data-types.html You are only required to fill out the Field, Type, Length, and Null fields, and define a primary key. Be sure to select the InnoDB storage engine in the Storage Engine drop-down box just below your fields. When finished, click the Save button. You will see a screen confirming the addition of your table to the database, and showing the SQL query that was created for you. Page 5 of 9
Inserting Records It s now time to add some values (records) into your table. Click the Insert tab. You will see a screen like the one below. You need to enter a value for every field that you specified as not null. The others are optional. You should enter at least 5 records for your assignment. If you use auto increment on your primary key field, then don t enter a value for that field. SQL takes care of that for you! Enter the values, select Insert as new row and Go back to previous page, and then select Go. Row is just another word for record. If you want to insert more records in your database, you would select the Insert another new row option instead of Go back to previous page. Page 6 of 9
Viewing the Inserted Records When you are finished inserting records, click on the Browse tab to view all of the records that have been inserted into your table. Making Modifications If you need to make any modifications to your table (such as renaming it), click on the Operations tab and you will see a number of options for altering your table. Modifications can be made to your fields by clicking on the Structure tab, and your records can be modified by clicking on the Browse tab. Backing up Your Database Now you need to backup (export) your table so you can submit it to me as a.sql file. Click on the Export tab. Make sure that SQL is selected under the Export option. Check the Save as file box and enter a name for your.sql file. Your screen should look like the following: Page 7 of 9
Click the Go button. A pop-up will appear asking if you want to open or save the file. Select save and save it to a location you will remember. Open up your file in WordPad or another Word processor (it will appear very messy in Notepad). Your file should look something like this: phpmyadmin SQL Dump version 2.9.0.2 http://www.phpmyadmin.net Host: localhost Generation Time: Sep 26, 2008 at 01:35 AM Server version: 4.1.18 PHP Version: 4.4.4 Database: `jp159aii_tutorial` Page 8 of 9
Table structure for table `CD` CREATE TABLE `CD` ( `CD_ID` int(5) NOT NULL auto_increment, `Title` varchar(150) NOT NULL default '', `Artist` varchar(100) NOT NULL default '', `Length` varchar(5) default NULL, `Year` year(4) default NULL, PRIMARY KEY (`CD_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; Dumping data for table `CD` INSERT INTO `CD` VALUES (1, 'Madman Across the Water', 'Elton John', '60:56', 1973); INSERT INTO `CD` VALUES (2, 'Andrea', 'Andrea Bocelli', '75:35', 2000); Finishing the Assignment Post your.sql file and your reaction and summary of the tutorial you completed, in the Week 3 Assignment 2 discussion. Restoring Your Database If you need to restore your data, you would click on the SQL button directly beneath the phpmyadmin icon on the top left of the screen (not the SQL tab) and choose the Import Files tab, locate your. sql file, and click Go. Page 9 of 9