You are on page 1of 5

Introducing MySQL with MySQLCC

Topics Covered This set of exercises cover the following topics 1. Starting MySQLCC 2. Connecting to the local MySQL server 3. Opening a database 4. Opening a table 5. Executing SQL queries 6. Adding rows to a table using the SQL INSERT statement 7. Removing rows from a table using the SQL DELETE FROM statement 8. Altering rows in a table using the SQL UPDATE statement 9. Creating a new table using the MySQLCC GUI 10. Backing and restoring a database 1. STARTING THINGS UP 1.1 Start MySQL Control Center (MySQLCC) Most of today's exercises will be done using the GUI client for MySQL called MySQL Control Center. It is installed in all labs in Block 16. In 16G26/7 it can be located on the START menu but on C floor you have to launch it using Windows Explorer: 1. 2. 3. 4. 5. 6. 7. 8. Click on START in the bottom left corner Click on Windows Explorer to start Windows Explorer In the left-hand panel of "Windows Explorer", click on My Computer Click on Local Disk (C:) If in either 16C28 or 16C29 locate the folder Program Files and click on it Locate the folder mysqlcc and click on it In the right-hand panel locate mysqlcc.exe and double click on it. The MySQLCC window should open an look a bit like this

1.3 Connecting to the local server If there is not an entry Databases in the MySQL Servers panel on the left-hand side (see image) then you have to open a connection to the MySQL server running on the machine in front of you 1. in the panel MySQL Servers right-click on localmysql 2. click on the option Connect 1.4 Opening a database 1. double-click on Databases in the MySQL Servers panel 2. this will produce a list of the databases that you have access to (probably two) 3. right-click on localdatabase 4. select the option Connect 5. an entry labeled Tables should appear 2. EXPLORING A DATABASE

2.1 Opening the 'cars' table 1. 2. Double-click on the entry Tables under the database that you opened in part 1. You should have a single table called cars Double click on the table A Query Window should open listing the contents of the table

3. QUERIES 3.1 My First Query First we have open the SQL pane so we can enter arbitrary SQL 1. 2. In the Query window click on the menu View Select the option SQL Pane

A pane should now open below the tool bar. In the panel there should be the query select * from cars - this is the query that produced the list in the table below it. 1. 2. 3. Change the query to select make from cars Click on the Query menu Select the Execute option

How did this affect the table displayed? This is an example of projection. Can you spot the Execute button on the toolbar? 3.2 More SQL Queries Try the following queries and describe what each of them do
(a) (b) (c) (d) (e) (f) (g) (h) select select select select select select select select mot, year, price, make from cars * from cars where price > 2000 * from cars order by price * from cars where price > 2000 and year < 1998 distinct make from cars count(make) cars count(distinct make) from cars * from cars order by price

3.2 Writing SQL Queries Can you work out the SQL for the following queries ? (a) list all cars that have an mot (b) list all cars sorted by year (c) list all cars that cost more than 1500 but do not have an MOT (d) display how many different years are in the table 4. ALTERING THE DATA IN A DATABASE You will now briefly learn how to make some alterations to the data in the database adding rows to a table (INSERT) changing values in a table (UPDATE) removing rows from a table (DELETE) 4.2 Adding records 1. In the Query section of the Query Window enter the following SQL to create the library table INSERT INTO cars (Make, Year, Price, MOT) VALUES ('Skoda Oktavia','2002','8500','Yes'); 2. If you open the cars table you should now have a new row present

Now add the following records using SQL insert statements: Make Ford Ka2 Vauxhall Astra Fiat Punto Year 1999 2000 1998 Cost 4300 6500 3500 MOT Yes Yes Yes

2.3 Updating records The UPDATE statement used to alter the values in existing records. Most will feature a WHERE clause so it only affects a single targeted record. First we will correct the spelling of Oktavia to Octavia UPDATE cars

SET Make = "Skoda Octavia" WHERE Make = "Skoda Oktavia" No write SQL to devalue all cars from 1999 by 100 pounds. 2.5 Deleting records WARNING Once you have deleted a record you cannot get it back! Delete records with extreme caution To remove a record you use a DELETE statement. Like the UPDATE it can feature a WHERE clause to only delete one record or a selected subset records. For example to remove all cars that from 2000 DELETE FROM cars WHERE Year = 2000 5 CREATING TABLES 5.1 Create a table 1. In the Console Manager window right click on Tables 2. Select the option New Table The Creating Table window should open. This has a table that allows us to specify for each field the name of the field (Field Name) the type of value the field will hold (Data Type) whether a field is allowed to be empty (Allow NULL) Below the table we can specify the size of the field (Length) and the common automatic value (Default)

5.2 Adding fields 1. Click in the empty cell below Fieldname 2. Type in DoctorNo 3. Click on the Data Type drop-down box 4. Select Int

Now add the field ActorName which can hold a varcharof size 30 5.3 Primary key Now we will indicate which field is the primary key 1. Click on the DoctorNo field to select it 2. Click on the Table menu 3. Select the Add Primary Key option A little golden key symbol should appear next to the fieldname If the primary key was a composite key (ie made up from 2 or more field values) then you would repeat this for each field in the primary key. 5.4 Save the table Finally we will save this table 1. 2. Click on the File menu Select the option Save A dialog box should open prompting you for a name for the table 3. Enter Doctors 4. Press OK 5. Close the Creating Table window The computer will ask if you wish to save changes say no Note: there is problem altering a table once you have saved it you will not be able to save any changes you make! Your only solution is to DROP the whole table and create it again from scratch. 5.5 Fill the table with data Now open the table by double clicking on it in the Console Manage window. Fill the table with some data DoctorNo 1 2 3 4 5 6 7 Actor Hartnell, William Troughten, Patrick Pertwee, Jon Baker, Tom Davison, Peter Baker, Colin McCoy, Sylverster

6. BACKING UP AND RESTORING MySQL is set-up to store databases on the C: drive. Before you logout you will have to back-up your database to your P: drive and when you return you will have to restore it. 6.1 Backing-up 1. Hold down the WINDOWS key and press R 2. In the Run dialog box type in command and press RETURN 3. Into the command.com window type in the following c:\mysql\bin\mysqldump u student1 p localdatabase > p:\studentdata.sql This will create a back-up of your database (essentially a file of SQL statements that can recreate it) on you P: drive with the name studentdata.sql. Open the file using Notepad to view the SQL. 6.2 Restoring The same as backing-up except type in the following command c:\mysql\bin\mysql -u student1 -p localdatabase < p:\studentdata.sql

You might also like