COM427 MySQL Worksheet 1 1. enter following URL in browser http://localhost/~labuser/phpmyadmin 2. Login: labuser Password: macimd15 3. There are existing database areas that can be used (e.g. Test and Labuser). Create a new database area called Uni : Type Uni in the Create New Database box and press Create (creates Uni as a database area) 4. Download the database file university.sql from the S:drive of the windows server (or from Module Webpage) To get the S:drive of the Windows Server - Click on the Go option in Finder, then select the Connect to Server option - enter smb://scmserver.scmnet.local/common in the Server Address Box and click Connect - scmserver becomes available in Finder : Navigate to S:/Young/COM427 Examine university.sql using an editor - you will see - CREATE TABLE statements to create for the 3 tables - INSERT statements adding the records - ALTER TABLE statements at the end for adding the referential integrity Select database Upload the database file Select import filename Click on the Import Tab (see diagram above) – if the database is not already loaded Click Browse and find university.sql then press Go (database should load) [Remember to delete the database area at end of practical session – See end of worksheet] Click to move up to database level 5. Examine the database tables tablenames SQL tab Click on a tablename to view the table details 6. Click on Structure Tab to see the structure of a table (e.g. datatypes) Click on each tablename in turn & examine with the Browse & Structure Tabs UNIVERSITY DATABASE Relational Schema STUDENT(Studentnum, FirstName, Surname, Age, Email, CourseName) TAKES(ModuleCode, Studentnum, CWMark, ExamMark, ModuleMark) MODULE(ModuleCode, ModuleName, HoursPerWeek, TotalWeeks) Table Structure in MySQL Field StudentNum FirstName Surname Age Email CourseName MODULE TAKES STUDENT Type Char Char Char Tinyint Char Char Length 9 15 20 30 30 Field ModuleCode StudentNum CWMark ExamMark ModuleMark Type Char Char Tinyint Tinyint Tinyint Length 6 9 Field ModuleCode ModuleName HoursPerWeek TotalWeeks Char: Character/text data type Length gives maximum number of Characters allowed TinyInt : Integer (whole number) up to 99 (approx) Also Smallint (up to 9999) , Mediumint (up to 999999), Int (Up to 999999999) & Bigint Type Char Char Tinyint Tinyint Length 6 30 RUNNING RETRIEVAL QUERIES 1. Click on Database:labuser at the top of the screen & use the SQL tab. Enter the SQL query & press Go. If your query shows an error examine the error message, change the query & rerun. Note that you must use the exact names of columns & tablenames and put the exact value of text values (char data types) in inverted commas. 2. Get all records in the module table (query will run if written on one line instead – also not case sensitive except for text column values) SELECT * FROM MODULE; equivalent to SELECT ModuleCode, ModuleName, HoursPerWeek, TotalWeeks FROM MODULE; 3. Get the module names where the hours per week are 5. SELECT ModuleName FROM MODULE WHERE HoursPerWeek = 5; 4. Get the full names of students aged above 18 who are on the BSc ICT course SELECT Firstname, Surname FROM STUDENT WHERE Age > 18 AND CourseName = "BScICT"; EXERCISE - Now try some yourself (Note that MySQL does not store queries permanently and the SQL statements should be copied into a file (e.g. TextWrangler, Word etc) a. Get the surnames and emails of all students on the BScCS course. b. Get the module codes of students who got more than 50 in their module mark. c. Get the student numbers of students on the module coded COM147 whose coursework mark is below 60. d. Get the firstnames and emails for students aged above 18 and below 21. Aggregate Functions AVG( )-average; SUM( ) – total; MAX( ) – highest; MIN( ) – lowest; COUNT( ) – count/how many (counts the number of rows in the result table) 5. Get the average age of all students SELECT AVG(Age) FROM STUDENT; 6. How many modules are there? SELECT COUNT(ModuleCode) FROM MODULE; equivalent to SELECT COUNT(*) FROM MODULE; 7. How many students are on module COM147 SELECT COUNT(Studentnum) FROM TAKES WHERE ModuleCode = "COM147"; EXERCISE e. How many modules does student number B00234567 take? f. Get the total number of hours per week taught in all modules. g. What is the average module mark for the module coded COM179. Saving Your Database Your database may be lost when you logout (although you still have the original load file) To keep a copy of your university database (useful to know if changes are made or if you create a new database yourself) 1. Go to the top level of the database (Click on the Database:labuser link) 2. Click the Export Tab (Note that all four tables should be listed) 3. Go to the bottom of the screen to the Save As File section 4. Change File Name Template to University1 (Export Format set by default as SQL) 5. Press Go Then Save to your own work area or a memory stick Now delete the database area in phpmyadmin before leaving - Click on localhost (127.0.0.1) at top of screen - Click on Databases Tab - Click on box beside Uni database then click Drop Icon (Pressing OK when Warning Checkbox appears)