Using MySQL - Introducing SQL

This is the first in a series of MySQL workshops designed to introduce MySQL and SQL statements to a novice developer.

What is MySQL and why are we using it?
MySQL is a powerful Relational Database Management System (RDBMS) which we will use to learn the basic principles of database and data manipulation using Structured Query Language (SQL) statements. SQL is a database language that is used to retrieve, insert, delete and update stored data. This is achieved by constructing conditional statements that conform to a specific syntax (i.e. the strict order required of elements for a statement to work). Although it is assumed that most people reading this know what a database and SQL are (if not necessarily how to use them), there follows a little recap that it does no harm ;-)

How does MySQL work?
MySQL is a database server program and as such is installed on one machine, but can 'serve' the database to a variety of locations. To explain look at the following diagram.

The MySQL Server is installed on a Server and can be accessed directly via various client interfaces, which send SQL statements to the server and then display the results to a user. Some of these are: A Local Client - a program on the same machine as the server. An example of this is the command line MySQL client software we will be using in the rest of the MySQL workshops (although there are other programs including graphical interfaces). A Scripting Language - can pass SQL queries to the server and display the result.

A Remote Client - a programme on a different machine that can connect to the server and run SQL statements. You can also use two more indirect methods. Remote Login - You may be able to connect to the Server Machine to run one of its local clients. Web Browser - you can use a web browser and scripts that someone has written (we're going to use this method for the rest of the workshop).

A bit about SQL
Structured Query Langauge is cross between a math-like language and an English-like language that allows us to ask a database questions or tell it do do things. There is a structure to this language: it uses English phrases to define an action, but uses math-like symbols to make comparisons. For example:
SELECT * FROM table;

Where 'SELECT', 'FROM' and 'table' are in English, but '*' is a symbol that means all. It is important to learn SQL as it is common to almost all database programs and was developed specifically as a language used to retrieve, add and manipulate data in databases. You will find it not only here in MySQL, but underlying MS Access, MS SQL Server, and in every web-based database application. While it may seem confusing at first it is almost like telling a story or asking a question once you become comfortable with the syntax.

A Bit About Database Structure
Databases can be as complicated as you wish to make them... so lets start with simple and work out way up from there. A database can have many TABLEs holding data. Imagine a simple table of car information:
CarID 1094 1095 1096 1097 Manufacturer Subaru Suzuki Toyota Volkswagen Year 91 95 97 95 Car Legacy Vitatra Corolla Golf3 Model 2000 1600 1300 1600 AirCon FALSE FALSE FALSE FALSE CDMulti FALSE FALSE FALSE FALSE

If you look at the blue Cell we call this a 'FIELD' and it has a value of 'Suzuki'. This FIELD exists in the COLUMN named 'Manufacturer'. The 'Model' COLUMN is green in this example. All the FIELDs in the 'Model' COLUMN contain the same type of data (i.e. the model of the car). Whereas a ROW (in this case red) contains a series of FIELDs, one in each COLUMN, together comprising a record about one car. This record represents the real world uniqueness of each thing we are recording (in this case a car) and thus is given a unique number (in database language the 'Primary Key') with which to identify it. In our simple table each unique number is stored as a FIELD in the 'carID' COLUMN.

Creating a First SQL Statement

As we have yet to create a database it would be difficult to construct some simple SQL statements to explain the above without first getting involved in some MySQL server administration. However as we saw above there are many ways to interact with a database and thus I have created a database and a 'cars' table filled with car info and provided a web browser interface to accept SQL statements and return the results for you to experiment with. So open the web interface in a new browser window and switch between the two or print this out and work from it. The first SQL statement we will look at is the SELECT statement. The basic SELECT statement has the following syntax.
SELECT columns FROM table [WHERE (condition)]

The WHERE condition is in square brackets as it is optional. So using our 'cars' table we can start issuing commands and you should see the actual data being displayed. Note: As an SQL statement can span many lines of a script or when typing it in at a command line (this virtual workshop will 'format' the SQL statements over multiple lines to avoid overlapping and to aid readability). When using MySQL the statement is only deemed 'complete' when a semi-colon is typed at the end to signify that you have finished constructing your statement. So ensure you include a semi-colon....... To display all the data from the 'cars' table we would issue the following command (where '*' is the wildcard symbol for all).

FROM cars;

The result should be a large amount of data displayed (due to the quantity it may take a few moments to display). This is not very useful, but we can begin to restrict the output by including WHERE conditions. For example to display only the records that contain the data '95-98' in the 'Year' field, try the following command:

FROM cars

WHERE (Year = '95-98');

There are a couple of items of good practice that make life easier at this point. The first is that our conditions (the bit after WHERE) should be enclosed by brackets. This 'forces' the condition to be evaluated and is needed when you have nested conditions in complex queries, so you should to get into the habit of doing this from the beginning. Secondly, it is likely that you will at some point wish to display data from different tables using the same query - making it a good idea to also get into the habit of using a the full TABLE.COLUMN reference (as different tables sometimes have COLUMNs of the same name). For example if we use another select statement, perhaps all records that have 'Volvo' as 'Manufacturer', we are explicit that we mean the Manufacturer COLUMN in the cars TABLE.


FROM cars

WHERE (cars.Manufacturer = 'Volvo');

As hinted at above, conditions can be combined to achieve better filtering of results, the simplist being to use the 'AND' operator

FROM cars

WHERE ((cars.Year = '95-98')

AND (cars.Manufacturer = 'Volvo'));

This last statement should produce only one result and you can begin to see how using conditions can be useful in finding individual records.

A Tiny Bit about Operators
Operators are another tool that you can use within your SQL statement to refine your search for specific records.

FROM cars

WHERE ((cars.Year = '95-98')

AND (cars.Manufacturer = 'Volvo'));

The above statement uses the 'AND' operator (it can also be expressed as '&&') to combine two conditions. Both conditions have to be met in order for the record to be displayed. We can also use the 'OR' operator (can be expressed as '||' ) to ask for a record to be displayed if either condition is met.

FROM cars

WHERE ((cars.Year = '95-98')

OR (cars.Manufacturer = 'Volvo'));

The final operator we'll discuss here is the 'NOT' operator ('!' in case you were wondering), which is a bit more complicated. Rather than joining conditions together it becomes part of the condition, turning a

positive into a negative. The following statement retrieves all records that do notcontain 'Volvo' as 'Manufacturer'.

FROM cars

WHERE (cars.Manufacturer != 'Volvo');

As the 'NOT' operator has become part of the condition it can be used with another operator to combine positive and negative conditions. For example to retrieve the records that contain the data '95-98' in the 'Year' field but do not contain 'Volvo' as 'Manufacturer' enter the following.

FROM cars

WHERE ((cars.Year = '95-98')

AND (cars.Manufacturer != 'Volvo'));

There are also other operators, but they will be discussed in a later part of the MySQL Virtual Workshop series.

Restricting Columns
Before leaving our initial encounter with the SELECT statement we need to address one final component. In all the examples we have used so far the wildcard '*' has been used to retrieve all the COLUMNs. While this may be okay for a table that only has 7 COLUMNs, it would not work quite so well for a table with 20 COLUMNs. Thus it may be desirable to sometimes restrict which COLUMNs are returned. If we look again at the seven fields that make up the 'cars' table


| CarID | Manufacturer | Year

| Car

| Model

| AirCon | CDMulti |


We may only be interested in seeing the 'Manufacturer','Year','Car' and 'Model' fields and thus we would construct a statement like so:
SELECT cars.Manufacturer, cars.Year, cars.Car, cars.Model

FROM cars

WHERE (cars.Year = '95-98');

Which, as you can see, asks for only certain columns to be returned - each field separated by a comma. That's the end of this introductory Virtual Workshop - in the next part we will look at data and structures. If you want you can also attempt the following open-ended mini exercise.

Mini Exercise
Try creating your own combinations of conditions and operators to retrieve data, for more practice with the SELECT statement.

Using MySQL - Data and Structures Workshop Requirements You should have completed the first workshop in this series. You should also have access to the MySQL command line client software. You can either ask your systems adminstrator for access to this client, alternatively you could install a local copy of MySQL see Setting Up a Server for details. You should also have full permissions on a database (or the ability to create a new one), again check with your administrator if necessary. Finally you should have printed out and filled in the Data Collection Form........ from the experience of others it IS easier than doing this 'on the fly'. Important information to understand before beginning. A common problem when learning how to the MySQL Command Line Client is getting the syntax exactly right. There are several conventions which are used throughout the MySQL virtual workshops when displaying commands that you will have to type. $ - This preceding a command signifies that the command should be typed at the command line of your system. The '$' should not be included in any statement you type. mysql> - This preceding a command signifies the command should be typed at the MySQL client Command Line Interface. Again ignore this and only type what follows. There are also several other command prompts that you may encounter within the mysql client. -> This is a continuation line allowing you to enter a long statement over several lines. A statement is never executed until you issue a semi colon at the end of it. You may at some point require to type either an apostrophe (') or a double quote (") as part of your data entry. However as these are used as part of the SQL syntax the statement would fail. This is signified by either: '> MySQL is asking you for another apostrophe Or

"> MySQL is asking you for another double quote. To successsfully execute a statement containing either of these characters they must be entered in pairs. e.g. Keith's should be typed with two single apostrophes Keith''s and "Hello" should be typed""Hello"" with two sets of double quotes. The final convention that we use in this series is the 'Syntactic Example'. As each new element of SQL is introduced, the syntax will be given before an actual example to try. The syntactic command will contain generic references with angular brackets that will not work if typed. eg: SELECT <field> FROM <table> This means that the <field> and <table> parts of the statement should contain the actual names of a FIELD and TABLE. Connecting To Your Database In order to issue MySQL data manipulation statements, we must specify a username, a password and a database to use. This information will have been provided by your systems adminsistrator. If you are running MySQL yourself, then skip ahead to the section on Creating a Database in Part 6 - then come back to this point. Either way you should now have in your possession a database name, a valid password for that database and a password. For the purposes of these workshops the following example values will be used. Database Name Valid MySQL Username Password vworksDB vworks mypass

In order to log into MySQL correctly we must pass information to the MySQL client program when we start it. This is done with the following commands and syntax. $ mysql -u <mysql_username> -p <password> <database name> The name of the program (mysql) is followed by the User flag (-u), the Password (-p) and finally the name of the database. So to start our MySQL session you could enter (but don't just yet): $ mysql -u vworks -p mypass vworksDB However is it a REALLY bad idea to ever enter a password anywhere in plain text, so if we omit the password but still leave the password flag (-p), you will be prompted for it (again do not try this yet). $ mysql -u vworks -p vworksDB

Enter password: You could also omit the name of the database as well (wait 'til I say so). $ mysql -u vworks -p Enter password: ...and once logged in elect to 'use' a specific database. You could choose to do this if your user has privileges on different databases. mysql> use vworksDB Database changed ...but we wont do that either. Okay, you can now log in to the database using the second example above. $ mysql -u vworks -p examples Enter password: You should now be looking at the MySQL Command Line Interface that is awaiting any statement you wish to type. mysql> In the previous MySQL Virtual Workshop we looked at issuing SQL SELECT statements containing conditions to retrieve data from a table. The natural progression is to start constructing other statements which INSERT, UPDATE and DELETE records from the table. However, before we attempt this we need to create a table in our database. Creating a Table Datatypes Creating a table requires you to have an understanding of MySQL datatypes (similar if not identical to other database datatypes) so that you can define the fields in the table. The datatypes below are some of the basic datatypes and are meant as a simple introduction. If you are planning a production database you should investigate the datatypes thoroughly. Datatype INT VARCHAR(n) Description Numeric entry Example: id INT

Text string of characters up to n with a maximim of name VARCHAR(20)

255 characters CHAR(n) Text string with specific number (n) of characters. address CHAR(30) If the number of characters is less than 'n' then is padded by spaces (spaces are removed when data is retrieved). 255 Maximum. Holds between 255 - 65535 characters The date stored in the format YYYY-MM-DD The time stored in the format HH:MM:SS philosophy TEXT dob DATE tob TIME


The syntax for defining a field and datatype (as used in the examples) is 'fieldname datatype'. So lets get started with making a database table. You should have completed the Data Collection form so we will create a table and begin to enter some of the data into your database. CREATE TABLE...... The syntax for creating a table is as follows: mysql> CREATE TABLE <table_name> ( field1 datatype, field2 datatype, etc...... ); When split over multiple lines within the MySQL command line client the continuation indictor will be used. mysql> CREATE TABLE <table_name> ( -> field1 datatype, -> field2 datatype, -> etc......); REMINDER: the command statement will not be executed until you use a semi-colon. Thus for the command to create a table based on the information in the data collection form you should enter the following (omitting the 'No. of Tracks' which we will add later):

mysql> CREATE TABLE cds ( -> cdID INT(3) auto_increment primary key, -> artist VARCHAR(20), -> title VARCHAR(30), -> year INT(4), -> label VARCHAR(20), -> bought DATE );

Query OK, 0 rows affected (0.00 sec) When entering these commands over several lines it is possible you may make a mistake and would want to cancel the command. This can be done by typing the '\c' command: mysql> CREATE TABLE cds ( -> cdID IN(3) -> \c mysql> You can check that you have created the table correctly by issuing a 'describe' command. mysql> describe cds; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+----------------+ | cdID | int(3) | | PRI | NULL | auto_increment | | NULL | | NULL | | NULL | | NULL | | NULL | | | | | |

| artist | varchar(20) | YES | | title | varchar(30) | YES | | year | int(4) | YES |

| label | varchar(20) | YES | | bought | date | YES |

+--------+-------------+------+-----+---------+----------------+ 6 rows in set (0.01 sec) You will have noticed that when we created the first 'cdID' field we added a few other instuctions. These were 'auto_increment' and 'primary key'. These are used in the context of relational databases to identify the unique COLUMN in the TABLE (primary key) and to ensure that there cannot be duplicate numbers in the COLUMN by automatically numbering this FIELD (auto_increment). Now that the table has been created we are ready to start inserting data. INSERTing data The SQL statement to INSERT data is again fairly straight forward once you get used to the syntax. The basic syntax for the INSERT statement is as follows. INSERT INTO <table_name> VALUES ( value1, value2, etc...... ) Which will work fine if you are inserting data into every field in a row. However if you only want to insert data into certain fields (columns) you will need to specify only these fields as part of the statement otherwise the number of data items will not match the number of available fields and MySQL will return an error. INSERT INTO <table_name> ( column1, column2, .... ) VALUES ( value1, value2, ....


To illustrate this point we will try to insert the first example from the Data Collection form into the 'cds' table. mysql> INSERT INTO cds -> VALUES ( -> 'jamiroquai', -> 'A Funk Odyssey', -> '2001', -> 'Sony Soho2', -> '2001-09-13'); ERROR 1136: Column count doesn't match value count at row 1 As you can see we get an error telling us that the number of values we have tried to insert (5) does that match the number of columns that there are (6). This is because we have omitted the 'cdID' field which is used as the unique indentifer and thus gets generated automatically. In order for this to work we we would have to specify which 5 fields we want to insert our values into. mysql> INSERT INTO cds ( -> artist, title, year, label, bought -> ) VALUES ( -> 'jamiroquai', -> 'A Funk Odyssey', -> '2001', -> 'Sony Soho2', -> '2001-09-01'); Query OK, 1 row affected (0.02 sec) Although MySQL tells us that this has worked, we can check for ourselves by issuing a SELECT query. mysql> SELECT * FROM cds;

+------+------------+----------------+------+------------+------------+ | cdID | artist | title | year | label | bought |

+------+------------+----------------+------+------------+------------+ | 1 | jamiroquai | A Funk Odyssey | 2001 | Sony Soho2 | 2001-09-01 | +------+------------+----------------+------+------------+------------+ 1 row in set (0.00 sec) Also notice that the 'cdID' field has a value of '1' that has been generated automatically by the 'auto_increment' property. Although we could enter all our data by explicitly declaring the fields then the values, there is another way we could do it. Our first attempt at the INSERT SQL statement failed because we were trying to add 5 values to a database that has 6 fields (columns) and we couldn't specify a 6th value for the 'cdID' field as that value is an automatically generated number. What we can do in the statement is acknowledge that a field exists, but admit we don't know what the value will be. This is done by substituting a question mark for a real value. If we enter the statement again.

mysql> INSERT INTO cds VALUES ( -> '?', -> 'jamiroquai', -> 'A Funk Odyssey', -> '2001', -> 'Sony Soho2', -> '2001-09-01'); Query OK, 1 row affected (0.00 sec) This time the INSERT statement succeeds. Another SELECT statement confirms this. mysql> SELECT * FROM cds; +------+------------+----------------+------+------------+------------+ | cdID | artist | title | year | label | bought |


| 1 | jamiroquai | A Funk Odyssey | 2001 | Sony Soho2 | 2001-09-01 | | 2 | jamiroquai | A Funk Odyssey | 2001 | Sony Soho2 | 2001-09-01 | +------+------------+----------------+------+------------+------------+ 1 row in set (0.00 sec) Now we have the same entry twice, which is okay as we will learn how to delete records in the next Virtual Workshop. Mini Exercise You should now enter the second example and all of your own data into the 'cds' TABLE using the INSERT statement. Try alternating between explicitly declaring the fields/values and offering the '?' as the 6th field when inserting the data. Also revist the SELECT command used in Part One, querying the data you have just entered. TOP TIP: The 'Up Arrow' key allows you to retrieve previously typed statements, which speeds up the process of entering the data enormously

Sign up to vote on this title
UsefulNot useful