Databases: MySQL Introduction - Free Download PDF

1m ago
1.59 MB
56 Pages

Databases: MySQLIntroduction

Databases: MySQL introductionHow to Use this User GuideThis handbook accompanies the taught sessions for the course. Each sectioncontains a brief overview of a topic for your reference and then one or moreexercises.Exercises are arranged as follows: A title and brief overview of the tasks to be carried out; A numbered set of tasks, together with a brief description of each; A numbered set of detailed steps that will achieve each task.Some exercises, particularly those within the same section, assume that you havecompleted earlier exercises. Your teacher will direct you to the location of files thatare needed for the exercises. If you have any problems with the text or the exercises,please ask the teacher or one of the demonstrators for help.This book includes plenty of exercise activities – more than can usually becompleted during the hands-on sessions of the course. You should select some totry during the course, while the teacher and demonstrator(s) are around to guideyou. Later, you may attend follow-up sessions at IT Learning Programme (ITLP)called Computer8, where you can continue work on the exercises, with somesupport from IT teachers. Other exercises are for you to try on your own, as areminder or an extension of the work done during the course.Text ConventionsA number of conventions are used to help you to be clear about what you need todo in each step of a task. In general, the word press indicates you need to press a key on thekeyboard. Click, choose or select refer to using the mouse and clickingon items on the screen. If you have more than one mouse button, clickusually refers to the left button unless stated otherwise. Names of keys on the keyboard, for example the Enter (or Return) key areshown like this ENTER. Multiple key names linked by a (for example, CTRL Z) indicate that thefirst key should be held down while the remaining keys are pressed; all keyscan then be released together. Words and commands typed in by the user are shown like this. Labels and titles on the screen are shown l i k e t his . Drop-down menu options are indicated by the name of the optionsseparated by a vertical bar, for example Fil e Pri n t . In this example youneed to select the option P ri n t from the Fi l e menu or tab. To do this, clickwhen the mouse pointer is on the Fil e menu or tab name; move the pointerto Pr i n t ; when Pr i n t is highlighted, click the mouse button again. A button to be clicked will look l i k e t hi s . The names of software packages are identified like this, and the names offiles to be used l i k e t hi s .IT Learning Programme2

Databases: MySQL introductionSoftware UsedXAMPPFiles UsedRevision InformationVersionDateAuthorChanges made1.01.1Sep 2013Mar 2014Mohammad YaqubMohammad YaqubCreation of the textUpdate some text andexamples1.2May 2014Mohammad Yaqub1.3Sep 2014Mohammad Yaqub1.4Mar 2015Mohammad Yaqub1.51.6Jun 2015Nov 2015Mohammad YaqubMohammad YaqubFurther revision to text andexercisesMajor revision for thewhole bookMinor revision for someexercisesMinor revision to the slidesMinor revision to the slidesCopyrightThis document is made available under a Creative Commons AttributionNonCommercial-ShareAlike CC BY-NC-SA licence by Mohammad Yaqub whoasserts his right to be identified as the author.Note that some images used in the document and presentations are copyright oftheir owners and may be subject to different copyright conditions. Where possiblethis has been noted in the text. If an error in attribution/copyright has been made,please contact the author who will be pleased to make the necessary corrections.Screenshots are copyright of the respective software suppliers.AcknowledgementMost of the syntax in this book was adopted from Learning Programme

Databases: MySQL introductionContents1 Introduction . 71.1. What is a Database? . 71.2. What is SQL? . 81.3. MySQL . 82 Installation Guide to use MySQL . 92.1. XAMPP . 92.2. phpMyAdmin . 92.3. How to Complete the Exercises . 103 Setting up MySQL . 123.1. Creating/Editing/Dropping SQL users .123.2. Creating/Dropping Databases .134 Creating Tables . 154.1. Data Types . 154.2. Primary Key .164.3. Linking Tables via Primary – Foreign Keys .215 Manipulating Data in Tables. 225.1. INSERT Statement . 225.2. UPDATE Statement . 235.3. DELETE Statement. 246 Queries . 266.1. SELECT Statement . 266.2. Where Clause . 276.3. Comparisons and Conditions . 287 Advanced Queries . 317.1. Sorting Data – ORDER BY Clause .317.2. Querying Multiple Tables .317.3. Pseudonyms for Table or Column Names . 327.4. Subquery (inner SELECT) . 337.5. The IN Operator . 337.6. Basic String Comparison Functions . 347.7. The BETWEEN Operator . 358 Importing and Exporting . 378.1. Migration from/to MySQL Database only . 37IT Learning Programme4

Databases: MySQL introduction9 What is Next? . 38Explore phpMyAdmin . 38Read a book or tutorials about MySQL or SQL in general. 385IT Learning Programme

Databases: MySQL introductionExercisesExercise 1 Create MySQL users. 13Exercise 2 Create MySQL database . 14Exercise 3 Create MySQL table .17Exercise 4 Create the other 3 MySQL tables using the importfacility.19Exercise 5 Create Foreign Key constraints . 21Exercise 6 Insert data to tables . 23Exercise 7 Update data in a table . 24Exercise 8 Delete data from a table . 25Exercise 9 Querying data from a table . 27Exercise 10 Querying specific records . 28Exercise 11 Querying data – using conditions . 30Exercise 12 Retrieving sorted records . 31Exercise 13 Querying multiple tables . 32Exercise 14 Querying multiple tables: use primary-foreign keysrelationship 33Exercise 15 Querying multiple tables: use primary-foreign keysrelationship 33Exercise 16 Querying multiple tables: use IN operator . 34Exercise 17 The use of LIKE and NOT LIKE . 35Exercise 18 The use of STRCMP() . 35Exercise 19 Querying data – BETWEEN operator. 36IT Learning Programme6

Databases: MySQL introduction1IntroductionThe Structured Query Language (SQL) is the language of databases. SQL was, is,and will stay for the foreseeable future the database language for relationaldatabase servers such as IBM DB2, Microsoft SQL Server, MySQL, Oracle,Progress, Sybase Adaptive Server, and dozens of others.SQL supports a small but very powerful set of statements for manipulating,managing, and protecting data stored in a database. This power has resulted in itstremendous popularity. Almost every database server supports SQL or a dialect ofthe language. Currently, SQL products are available for every kind of computer,from a small handheld computer to a large server, and for every operating system,including Microsoft Windows, Mac and many UNIX variations.1.1. What is a Database?A database is a structured collection of data that is used by the application systemsof some given enterprise, and that is managed by a database management system.For the purpose of this course, think of a database as a collection of tables whichare connected to each other. IT Learning Programme (ITLP) in the University ofOxford offers a course on how to design a database. This course is a pre-requisiteto this course. However, if you did not attend the database designing course, pleaseread the following paragraphs.As we mentioned, a database is a collection of tables. Each table is similar to aspreadsheet table in which each row is called a record and each column is called afield. For example, if we need to create a table that contains students’ information,we might have the following fieldsSt IDSt NameSt DateOfBirthSt EmailData can be entered to this table so you can get the following tableSt ID4521545287486525142052201St NameJohn SmithAlison GreenThomas LiSusan BaileyWill KingSt /1997St [email protected] this table contains students’ information, it does not contain eachstudent’s grades. This is fine because the grades have to appear in a different tableto reduce data redundancy. This is called database normalisation. The gradestable might look likeGrade IDSt IDCourse ID7Grade ValueCommentsIT Learning Programme

Databases: MySQL introductionNotice how the Grades table is linked to the Students table via St ID whichappears in both tables. The field St ID in the Students table is acting as theprimary key which is a unique id to identify each record in the table. The fieldSt ID in the Grades table is called the foreign key and it links to a primary key ina different table. You might have noticed that there is a field called Course ID inthe Grades table which is another foreign key to identify a grade’s course. Thismeans that there must be another table that contains data for different courses.Form the previous simple example you should now have an idea of what we meanby a database. It is important to understand the following concepts: database,table, record, field, primary key, foreign key and data normalisation. Next sectionswill build on this and focus on SQL and how to use it to build a complete databaseusing MySQL.1.2. What is SQL?Structured Query Language (SQL) is a relational database language which allowsyou to create, delete, access and manipulate databases. The following is a list of themain operations that can be formulated with SQL: creating new databases deleting a database creating new tables in a database deleting tables from a database creating and removing users (database access control) executing queries against a databaseoretrieving data from a databaseoinserting records in a databaseoupdating records in a databaseodeleting records from a database creating stored procedures in a database setting permissions on tables and procedures creating relationships between tables1.3. MySQLMySQL is a Relational Database Management System (“RDBMS”). It is used bymost modern websites and web-based services as a convenient and fast-accessstorage and retrieval solution for large volumes of data. A simple example of itemswhich might be stored in a MySQL database would be a site-registered user’s namewith associated password (encrypted for security), the user registration date, andnumber of times visited, etc.MySQL can also be accessed using many tools. It can be easily communicated withvia PHP (PHP Hypertext Preprocessor), a scripting language whose primary focusis to manipulate HTML for a webpage on the server before it is delivered to aclient’s machine. A user can submit queries to a database via PHP, allowinginsertion, retrieval and manipulation of information into/from the database.IT Learning Programme8

Databases: MySQL introduction2 Installation Guide to use MySQLMySQL can be downloaded from There arealso several MySQL management tools which can be downloaded and installed toallow the manipulation of MySQL. These tools mainly provide an interface tooperate on MySQL. Many of these tools are free and provide an easy configurationof MySQL with PHP, e.g., XAMPP, WampServer, AMPPS . Another free MySQLmanagement syst

via PHP (PHP Hypertext Preprocessor), a scripting language whose primary focus is to manipulate HTML for a webpage on the server before it is delivered to a client’s machine. A user can submit queries to a database via PHP, allowing insertion, retrieval and manipulation of information into/from the database. Databases: MySQL introduction 9 IT Learning Programme 2 Installation Guide to use ...