navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

MySQL Essential Training
Richard Downs

MySQL Essential Training

with Bill Weinman

 


MySQL is by far the most popular database management system for small- to medium-sized web projects. In MySQL Essential Training, instructor Bill Weinman provides clear, concise instruction in the tools needed to create and maintain a database. Bill explores the different data types MySQL supports, and the functions it provides, enabling the user to get the most out of them. He even shows the MySQL PHP interfaces that can be used to create web applications. Exercise files accompany the course.
Topics include:
  • Installing and configuring MySQL
  • Creating and using databases
  • Using numeric, string, and full-text search functions
  • Using the command line interface to access MySQL's powerful security model
  • Inserting and retrieving data
  • Choosing a storage engine

show more

author
Bill Weinman
subject
Developer, Databases
software
MySQL
level
Beginner
duration
2h 46m
released
Aug 04, 2009

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



1. Introducing MySQL
Welcome
00:00(Music playing.)
00:03Hi! I'm Bill Weinman and welcome to MySQL Essential Training.
00:07In this course, I aim to give you the knowledge to perform the basic functions
00:11that will allow you to use MySQL effectively, and to take advantage of its
00:15powerful and unique features.
00:17After I walk you through the installation of the program, we get right into
00:20creating and using your own databases.
00:23We'll look at the different data types MySQL supports and the functions that are
00:27provided to get the most out of them.
00:29We'll take a good look at the command-line interface, and how to use it to
00:33access MySQL's powerful security model.
00:36I'll show you the MySQL PHP interfaces to help you create web applications.
00:41Whatever the application, calendar, email, Websites, bookkeeping, sales,
00:46inventory, even the lowly To Do list, it's all about the data, and that usually
00:51mean some sort of a database.
00:53This course will give you the tools to create and maintain the database you need.
00:57Now let's get started with MySQL Essential Training.
Collapse this transcript
What is MySQL?
00:00As we talk about MySQL, it's important to understand what MySQL is and what it isn't.
00:06MySQL is not an application.
00:08It is a database management system.
00:10It's not going to manage your database for you without another application to support it.
00:14MySQL is a database management system consisting of a database server, which
00:19manages the data and performs the basic CRUD operations.
00:22That's Create, Read, Update, and Delete.
00:26It's a set of client libraries.
00:28These are the interfaces for applications to connect with the database server.
00:32It includes a full command line client, a full-featured client capable of
00:37performing all the operations necessary to manage and operate the database server.
00:41MySQL is a full-featured database management system.
00:45It has all the components you need to incorporate it into a
00:48full-featured database application that will scale well, and serve your needs for the long-term.
Collapse this transcript
Important terminology
00:00MySQL is a technical topic.
00:02Whenever you're dealing with a technical topic, it's a good idea to agree on the
00:06terminology involved in the discussion.
00:08I'll cover some of the more general terms here in this movie, and I'll cover
00:11more specific terms as they come up.
00:13Also at the end of each movie you'll find a slight recapping terms covered in that movie.
00:18DBMS is Database Management System.
00:21This is the software package that manages the data in the database. MySQL is a DBMS.
00:28The DBMS stores, retrieves, searches, updates, and deletes data in the database.
00:33It also typically controls access to the data.
00:36The DBMS may be used directly by users, or may be used programatically by
00:40another computer program that requires the use of a database.
00:44A storage engine is the part of the DBMS that stores and accesses the data.
00:50The storage engine includes mechanisms for indexing, searching, and selectively
00:54locking portions of the data in the database.
00:57Different storage engines may be optimized for different types and purposes of data.
01:03SQL, Structured Query Language is a language for interacting with a DBMS.
01:08SQL is by far, the most popular language for this purpose.
01:12SQL is used by both users and programs to interface with the DBMS.
01:17A database is the store of data, usually stored on disc, but may be stored in
01:21memory or somewhere else.
01:23It's often stored in a system of files and the file system, sometimes in a single file.
01:28The term is used to refer generically to the place where the data lives.
01:33So the file system itself is actually a form of the database.
01:35A database is typically segregated into tables.
01:40Each table has its own configuration, and usually carries its own class of data,
01:45like a country table or an album table.
01:49Tables are typically organized in rows and columns.
01:52A row usually represents all the information in a given record of data.
01:57For a given country in the country table, a single row would have the name,
02:00continent, region, etcetera.
02:02For the single country, or for an album, the album table, the title artist and
02:07label for a given album.
02:08A column represents all like items across multiple rows.
02:13The names of all the countries would be in the name column of the country
02:16table, or the artist for all the albums would be in the artist column of the album table.
02:22Columns are sometimes called fields, a more general term that applies also to
02:26non-relational databases.
02:27Likewise, rows are sometimes called records.
02:30An individual item of data is sometimes called a cell.
02:34A cell is one place in a table that can hold one value.
02:38It is one column in one row.
02:41These are the major terms that will be used during this course.
02:43More specific terms would be covered in individual lessons as they come up along the way.
02:48Also at the end of each movie, you'll find a slight recapping of terms
02:51covered in that movie.
Collapse this transcript
Using the exercise files
00:00So, let's talk a little bit about the Exercise Files folder that comes with his course.
00:04There are three items in the Exercise Files folder, and they are SID and CRUD and SQL.
00:10In the SQL folder, you'll find the databases that we're using in the examples,
00:14the album database, the test database and the world database.
00:17We'll import these into MYSQL in one of the lessons using the command line client.
00:22The SID and CRUD applications, we'll install later.
00:25But let's just take a quick look at them.
00:27The SID application is how we'll be demonstrating the exercises, type the SQL in
00:32here, press Go and it executes and it gives you the results down here.
00:37It gives you the information that comes back from MYSQL including how long it
00:40took and how many rows were returned.
00:43And the CRUD application is a simple Create, Read, Update, Delete, which is
00:48what stands for CRUD.
00:49And it's an Album database using a couple of joint tables, and it's got Albums and Tracks.
00:54Here is the Albums and if we look in one of the albums, we'll see the Tracks
00:59and really that's it.
01:00The CRUD application will be used for explaining how to write code in PHP that
01:05uses MYSQL and the SID application of course, will be used to demonstrate the
01:10various examples throughout the course.
01:13So that's really it.
01:14There is nothing vital in the Exercise Files.
01:17If you don't have access to the Exercise Files, that's fine.
01:19You can still follow along in the command line client in MYSQL.
01:22Then really you should be able to do all of the examples and all of the course
01:27using your own data and using the MYSQL command line client.
01:30That's it, those are the Exercise Files, enjoy and let's get started with the course now.
Collapse this transcript
2. Installing the Test Environment
Installation overview
00:00Let's take a look at the process of installing MYSQL.
00:03When you're installing your database management system, the first thing you want
00:06to think about is how will this installation be used?
00:09It will be used in a testing environment, where the priority to use on
00:12flexibility and power, and your activities are basically going to be concerned
00:16with the learning about the environment that you're going to be installing.
00:19Or it will be used in a production environment.
00:21Where it will be concerned with security and flexibility and reliability.
00:26And you also want to think about what applications will you be using with the database?
00:31Most applications that use a database will include their own instructions, for
00:34how it gets installed and will often includes scripts for installing the
00:37database itself or for installing the data.
00:40There are two major options when installing a Database Management System,
00:43in particular MYSQL.
00:45One is to install it as a part of a package and this is how we'll be doing in this course.
00:50In this case, we'll be installing MYSQL as part of the package called XAMPP,
00:56which includes Apache, it includes MYSQL, it includes PHP and Perl.
01:01It's cross-platform, it's simple to use and you can download it
01:04from apachefriends.org.
01:07The other option is to install MYSQL as a standalone package.
01:10In other words, you have your Web Server already installed separately, you have
01:14your languages installed separately, and you're going to be installing the
01:17database separately.
01:19If you choose to go this route, you can either download your MYSQL from the
01:23Download URL here on the screen, or you can buy one of their support packages.
01:28When you look for support, you either have the support from MYSQL because you
01:31bought it, or you can look at their forums, or my personal favorite to go to
01:34Google and type in my question and find the many other smart people who have run
01:38into the same issue and have already resolved it.
01:41If you're installing MYSQL standalone, you may also want to install their GUI
01:44tools, which are available at this URL.
01:47For the purposes of the course, we're going to be using the Command-Line
01:50client because it accesses all of the power of MYSQL and all of the features
01:54in MYSQL and I'm going to be covering some of those features that are not part
01:57of their GUI tools.
01:59You're also going to need a Text Editor to edit some of the configuration files
02:03and it's very important to realize that a text editor is not a word processor.
02:07Do not use Microsoft Word or any of the other word processors that are
02:10available out here.
02:11The Text Editor, when you edit the text and save it in a file, it saves only the text.
02:17A word processor saves other what we call meta-information about the formatting
02:21and the fonts, and the layout and the things like this, which do not work with
02:25the configuration files.
02:26So you'll need to have a plain text editor and I have a couple of
02:30recommendations here for you.
02:31On the Mac, I'll use BBEdit and I recommend it highly.
02:34I do a lot of text editing on my Mac, and I find BBEdit to be a very powerful,
02:39flexible and easy to use tool.
02:41Text Wrangler is also by the Bare Bones Company who makes BBEdit.
02:45It's a smaller free version of BBEdit and that'll work just fine for all
02:49purposes here in this course.
02:51On the PC, I'll use Notepad++.
02:54I have used other text editors on the PC, some of which I paid good money for
02:58and I end up using Notepad++, which is free, highly recommended.
03:03It's a good powerful, easy to use, easy to install Text Editor for the PC.
03:08You're also going to need a way to access the Command-Line client in MYSQL
03:12and on the PC this facility is built-in with the cmd.exe application included with Windows.
03:20This is how you access the Command-Line on the PC.
03:23On the Mac, you can use either the Terminal application that's included with OS
03:27X and it's heart of the operating system.
03:30Or you can use iTerm, which is the one that I use.
03:33It's also free from iterm.sourceforge.net.
03:36It's much more powerful, easier to use, more flexible and in my opinion it just works better.
03:42So that's the overview of installing MYSQL on both the Mac and the PC.
03:47And now we'll get into the actual installation.
Collapse this transcript
Installing XAMPP on Windows
00:00So let's go ahead and install MySQL on a PC running Windows Vista.
00:06We're going to be using the XAMPP for Windows package, and this is the webpage
00:10where you can download XAMPP for Windows.
00:12The URL is up here in the location bar.
00:14The XAMPP for Windows package includes all of the pieces that we're going to be
00:18using plus a lot more.
00:20Apache web server, MySQL latest version, the latest version of PHP and a lot
00:26of other pieces that we won't be using here, but you may find useful at another time.
00:30Go ahead and download the Installer by clicking on this link.
00:33I've already downloaded it and saved it to the desktop.
00:38We have it right here.
00:40Before we actually run the Installer and install XAMPP, we need to make sure
00:44that there is an important setting in Windows is set correctly.
00:50Windows Vista has a feature called UAC, which we can access in User Accounts.
00:59Windows Vista has a feature called User Accounts Control, which needs to be off.
01:04The User Account Control prevents programs from gaining administration level
01:08access without user intervention.
01:10XAMPP requires administration level access.
01:13User Account Control was turned on by default in some early versions of Vista.
01:17It is turned off by default in current versions.
01:20We just want to make sure that it is actually off.
01:22We can see here that's it's not checked.
01:25So this feature is off in this installation.
01:29So you want to check that and make sure that it's off.
01:32It needs to be off in order to run XAMPP.
01:38Close the Control Panel and go ahead and install XAMPP now. Select a language.
01:44Notice this alert that User Control is deactivated and that's what you expect. So that's correct.
01:52Go ahead and press OK.
01:53It's really important.
01:54XAMPP won't run without it.
01:55Next, the destination folder C:\xampp is okay.
02:01That's an unusual place to install an application, but this isn't a
02:04normal application.
02:05This actually is a system level service.
02:07So that's a common way to do that.
02:09We'll go ahead and click Next.
02:11These are the checkboxes.
02:12Exactly how they need to be.
02:14You want a desktop icon.
02:16You want it in the Start menu.
02:18You do not want anything installed as a service.
02:20We're not running this in a production environment, and really if your running a
02:24production environment you probably don't want to run a package like this.
02:27Go ahead and click Install, and now it's installing.
02:33XAMPP is installed. Click Finish.
02:38Now, we want to start the XAMPP Control Panel.
02:40So we'll go ahead and click Yes and this will start the XAMPP Control Panel.
02:44You can see we have an icon for it on the desktop.
02:46We'll start MySQL first and then start Apache.
02:51You don't need to start anything else.
02:54Close the Control Panel.
02:56Notice that it stays down here in our Notification Area so that we can launch
03:00it again we need to.
03:03Now let's just bring up Firefox and make sure that it's well working. Type localhost.
03:12Localhost is a special address that means on my computer. There it is.
03:17So the Apache web server is working and PHP is working.
03:21We can click on Status here.
03:24These green lights are perfect.
03:26These red lights are just right, because we're not running SMTP. We're not running FTP.
03:31We don't need it for our purposes.
03:33And XAMPP is now installed.
03:35There's one more piece of configuration that we need to do.
03:40We run our command line on Windows and type in mysql.
03:44This will not work.
03:47The reason for that is that the MySQL command line client is not in
03:52the execution path.
03:53So we need to edit the execution path on Windows and then this will work.
03:58So we'll go ahead and exit our command line, and bring up the Control Panel again.
04:04What the execution path is the path of directories that Windows looks in, in
04:09order to execute an executable program from the command line.
04:14So go ahead and edit it.
04:15We'll click on System and Maintenance, click on System, and Advanced system
04:21settings, and down here Environment Variables.
04:23The execution path is set in environment variable called Path, and there it is.
04:29We'll click on Edit.
04:31This is a list of directories or folders in Windows that are separated by semi-colons.
04:38So I want to put one at the end of the list.
04:40Semincolon, c:\xampp\mysql\bin, and click OK, click OK, and click OK. There we have it.
04:56We should be able to run MySQL now from command line.
05:02And there we have it.
05:03Now MySQL is installed, XAMPP is installed, and it's all tested and working.
Collapse this transcript
Installing XAMPP on a Mac
00:00So now let's go ahead and install on MySQL on a Mac.
00:04We're using the XAMPP for Mac OS X package, which includes a number of useful things.
00:08This package includes Apache, MySQL 5.1.33, the latest versions of PHP and Perl
00:15and a number of other useful things that you might use in another time.
00:17So we'll simply click on the download link here.
00:21I've already downloaded this to my desktop.
00:24We'll go ahead and click on the DMG package here, which is how you install stuff on a Mac.
00:33Like many Mac installers we simple drag the package into the Applications folder
00:38and it copies thousands of things for us.
00:42Now we can unmount the package by right-clicking or Ctrl-clicking either here or
00:48on the desktop icon.
00:50Now XAMPP is actually installed.
00:54Let me go ahead and open the Applications folder.
00:57You see the XAMPP Control Panel right here.
01:03I'm going to drag that into the dock. There it is.
01:09Close that. Now I'll go ahead and run the Control Panel, get the standard warning about
01:16downloading stuff from the Internet.
01:17Now I'm going to go ahead and start MySQL, I type my password.
01:24We'll also start Apache and put that back in the dock and close this.
01:32So now XAMPP is installed and running and it's tested.
01:35Bring up Firefox and type localhost into the location bar.
01:42There is XAMPP running our local computer, so the Apache sever is working.
01:48Then if I click on English, you can see the PHP running because I'm running PHP URL.
01:54Status, everything is activated and working.
01:57So I'm going to go ahead and close Firefox.
02:03We have one more bit of configuration that we need to do here in order to run
02:07the MySQL command line client.
02:09We need to set the execution path. I'll show you why.
02:14Type MySQL.
02:16We'll see it says command not found.
02:19That's because it is installed, but the path to the MySQL executable file is not
02:26set in the operating system.
02:29So OS X is based on UNIX, so this is done in a .profile file in your home directory.
02:36On this computer there is no .profile file.
02:41You may already have one.
02:42You may have set it up yourself, something else that you installed may have set
02:46one and your path may already be getting set to something other than default.
02:51In my case it is set to the default and I'm going to show you how to look at that.
02:55You type echo $PATH and you can see the path, and this is the standard path that
03:01MAC OS X and most UNIX operating system come with.
03:05You notice there are a number of folders or directories as they are called in
03:08UNIX separated by a colon.
03:10In each case you have a usr/bin, you have bin:, usr/sbin:, etcetera.
03:16Each of these is separated by a colon.
03:18It's not a semi-colon. It's a colon.
03:20And you don't need one at the end of the list.
03:23So we're going to adding one more directory to the end of the list with another
03:28colon, and here's how we do it.
03:30The file that we want to edit and in this case it doesn't exist, we're going to
03:34be creating it, is called .profile.
03:36So I'm going to type bbedit, which is the name of my editor, and .profile, like that.
03:43I can't simply open BBedit and go to the File menu and select Open, and open
03:48that file, because dot files don't show up in those listings.
03:52So you need to do this from command line.
03:54So when I press Enter, it will launch BBedit and because the .profile file is
03:59not there already, it will launch it with a blank file. Here we go.
04:05It says untitled text, because there's not already a file with that name.
04:10So we're going to go ahead and we're going to put in a command that's going to
04:13assign a new directory to the end of the path, and it looks like this.
04:34So the path is assigned an environment variable.
04:37The environment variable is called PATH.
04:40This is the way you add something to the end of an environment variable.
04:43Type the name of the environment variable with a $ sign, that's PATH=$PATH and
04:49then whatever you're going to add after it.
04:51In this case, it's a colon followed by the path to the MySQL binary executable
04:57which is /Applications/XAMPP/xamppfiles/bin.
05:03Then you need to export the path.
05:05You can simply say export PATH.
05:07Occasionally you'll see this written in a different way where the code export
05:10will be before the line above it.
05:13Either way if you already have a .profile file, and it already has an assignment
05:17like this in it, simply add the path to the end of that line with a colon,
05:23:Applications, blah, blah, blah like that.
05:26So now we're going to go ahead and save this.
05:28I'm going to choose Save As.
05:31It's going in my home directory billw and it's going to be called .profile. Like that.
05:40No when I say Save, I get this warning that Names that begin with a dot are
05:44reserved for the system.
05:45I go ahead and say okay, I Use dot, and now it's saved.
05:49So I'll quit BBEdit.
05:51I want to quit my terminal program, because the next time I start it, it'll
05:58start a new shell and it will read variable correctly.
06:02Now I should be able to run MySQL. There it is.
06:07This is the MySQL command line client and we'll be using this later in the lesson. That's it.
06:12XAMPP is installed, MySQL is installed and working.
Collapse this transcript
3. Configuring MySQL
Using the MySQL Command Line Interface on Windows
00:00So now we're going to talk about the MySQL command line client from the Windows
00:04side and I want to show you how to invoke the MySQL command line, because we're
00:09going the to be using it for the next few lessons.
00:11Now the reason we're using the command line client instead of any graphic tools
00:17is because the command line client that comes with my MySQL is able to access
00:21all of the features of MySQL in all the variations with all other options.
00:26There is not a graphical tool that allows you to access all of the options of
00:32all of the features of MySQL.
00:33So we're going to be using the command line client to be able to tap into its
00:37power and to show you how to administer MySQL from the command line.
00:41In order to do this, we need to run the Windows cmd.exe application.
00:48Cmd.exe has one particular quirk that we are going to need to get around and
00:52that is once you invoke it, it starts up in the directory where cmd.exe lives
00:57rather than your home directory or a directory where you might want to use it.
01:01So I want to show you a way to get around that.
01:03I'll cmd in this search field here and that brings it up and I'll right-click on
01:10cmd and select Copy.
01:12Right-click on the desktop, say Paste Shortcut and this puts a shortcut on the
01:17desktop to the cmd application.
01:20Now if I double-click on this now, you'll see that it starts up in
01:23Windows\System32 directory, which is not at all where we want it to be, and it's
01:27actually a very dangerous place to be.
01:29So we don't wan it to do that.
01:31I'm going to type exit here.
01:32Instead, we're going to tell it to start in a different directory.
01:36So I'll right-click on it and select the Properties and then this starting field
01:42is what we want to change.
01:43You see it has the System32 directory.
01:46I'm going to put the directory where the SQL files are because we're going to
01:49need that at some point soon.
01:51So type in C:\Users\William Weinman and start typing my name and then I'm going
01:58to use my arrow keys so that we don't get any typos.
02:01Down arrow gets my name and then \ Desktop\Exercise Files\, I can't see the end
02:13of that so I'm going to type SQL, there we go.
02:18Say OK and now this command processor will start in the directory that we want it to be.
02:24There is the directory where all my SQL files are.
02:26I'll just type dir and there they are.
02:29If I type mysql at this point, now we're in the MySQL command line client, we
02:36can type our SQL statements here just type show databases.
02:41Because in its default configuration, there is not a whole lot else we can do.
02:44But that shows that it is working.
02:46Type exit and exit from this now.
02:49When we want to run the MySQL command line client in Windows just double-click
02:52on this shortcut and type mysql and there we are.
02:58Just one more quick trick here about the command line shortcut.
03:02You'll notice when we bring up this window, it's 80 characters wide and 24
03:05characters high and those numbers actually have some historical significance,
03:09but it's been so long ago that it's become hysterical significance instead of
03:12historical and we want to change it.
03:14The problem is that when you just drag the corner of this window, in Windows, it
03:18doesn't actually change the dimensions of the window.
03:22It just changes the dimensions of the screen part of it and that doesn't work.
03:26So what you really need to do is you need to change it in the properties of the
03:29shortcut, which makes no sense whatsoever, which is why I'm showing you this
03:32trick, because it's hard to figure out on your own.
03:35So I'm going to right-click on the shortcut, which brings up little context menu
03:39and select Properties and select Layout and this is where you change it.
03:44You see it's kind of buried in there and I'm going to make it 100X50 and you
03:46have to change it in both places, which also makes lots of sense.
03:52You'll notice that this little preview shows me how much of the screen
03:55real estate is taken up.
03:56Your screen is probably bigger than mine.
03:58Mine is this size, because we are making a movie that you can watch on your desktop.
04:02Your screen is probably bigger.
04:03So play with these numbers and have it take up most of the screen real
04:06estate like that, because that's going to come in handy as we use this
04:10window in the later lessons.
04:12So I'm going to go ahead and show you what that looks like.
04:14You just double-click on it and there it is.
04:16You've now resized this window.
04:18Wasn't that intuitive?
Collapse this transcript
Using the MySQL Command Line Interface on a Mac
00:00We're going to be using the MySQL command line client in the next few lessons.
00:05So let's take a look at how to run this MySQL command line client on the Mac.
00:10We'll start by bringing up our terminal application.
00:12In this case I'm using iTerm, but you can use Terminal that comes with OS X.
00:18At the command line, I simply type mysql and that brings up the MySQL command line client.
00:24There are other options that we'll be using sometimes and we'll talk about those
00:28when we get to them, but that's really it.
00:30Once you're in the MySQL command line client, you can type your SQL right here.
00:34Like for instance, I'll just type show databases.
00:38You always need to use a semi-colon at the end of your SQL commands in the MySQL
00:44command line client.
00:46There we have the databases that are available at this time in the MySQL command
00:51line client and that's really it to exit the client you type exit and then you
00:55exit again to exit the command line shell, and there you have it.
00:59That's how you run the MySQL command line client on the Mac.
Collapse this transcript
Setting up the root user
00:00Now we're going to set up the root user in the MySQL.
00:03Users in MySQL are different than users in your operating system.
00:07You may login to your computer using a particular user name and password, but
00:11MySQL requires a separate login for its purposes with a separate user name and a password.
00:15MySQL has a special super user called root that has privileges for all actions
00:20including creating other user.
00:22This is a necessary starting point to set up a system.
00:26By default, the root user is installed without a password.
00:30So anyone would access to the system would be able to create and destroy users and data.
00:35If your installation is being set up on a private system on a private network
00:39only for the purpose of testing and debugging, this may be okay.
00:42But for production system, you must correct this before proceeding.
00:46To make matters just a little more confusing, MySQL is often installed with
00:50more than one root user.
00:52The reason for this is also confusing.
00:54It has to do with how MySQL maintains its access privilege system.
00:58But the point is important.
00:59There are usually two root users and there may be more and you'll have to
01:03update all of them.
01:06So let's start up the command line MySQL client.
01:11Double-click on the shortcut that we created earlier and type mysql -u root and
01:19what this does is it starts the MySQL command line client with the root user and
01:24this allows you to change things like the user database.
01:28MySQL keeps all of its user credentials in a database called MySQL and a table called user.
01:37So we're going to use SQL to update the user table in the MySQL database and set
01:43the password in all the rows where the user is root.
01:47So here is the SQL to do that. UPDATE mysql.user.
01:54That means the user table in the MySQL database.
01:58SET Password, that's the password column in the user table in the MySQL database
02:08= PASSWORD, and this is the password function which encrypts a password for the
02:14purpose of storing it in this table.
02:17We're going to put our password in quotes here.
02:19I'm just going to call it foo.bar.
02:21It's a simple to remember, temporary password for the purpose of this lesson.
02:26Please don't make your root password to that.
02:28Come up with something unique for your installation WHERE User=root.
02:36That will update this table only for the rows where the user column is equal to root.
02:46So how ever many root users are in this system, and I happen to know that
02:49there are two here.
02:50We enter a semi-colon and press Enter.
02:54We'll see that this has affected two rows and it changed two values and two rows.
03:00Now that we're updated the user table and inserted these passwords, we need to
03:06get MySQL to reread it's database so that it can apply these changes to the
03:14system that's being used and we do this with the special FLUSH command.
03:18FLUSH PRIVILEGES and what this does is it tells MySQL to reread these tables
03:26so that it will update the passwords and memory and be able to start using them right now.
03:30So press Enter and we'll exit and start up again.
03:35If I type mysql -u root, you'll see that we can't get in because we're not using
03:40the password now and so I put a -p at the end and this tells it to ask for a
03:45password and we'll type in the password foo.bar, and there we are.
03:50So we've updated the password for the root users and we've tested it and we know
03:55that it works and that's how we set up the root user in MySQL.
Collapse this transcript
Setting up a regular user
00:00Now we are going to talk about setting up regular users in MySQL.
00:03Remember users in MySQL are different than users in your operating system.
00:08You may login to your computer using a particular username and password.
00:12MySQL requires a separate login for its purposes with a separate username and password.
00:17MySQL uses a complex and flexible access privilege system.
00:22It's possible to grant very specific privileges to particular users on
00:26individual databases and on individual systems or across the system.
00:31Because the privilege system is so powerful, it's important that you setup users
00:35carefully so that they have the privileges that they need to do their job, but
00:39you are still able to prevent costly abuse and accidents.
00:43Keep in mind the users are not necessarily people.
00:46They can be programs or other processes as well.
00:49I think of them as roles so that I may have a web user that's for web based
00:53applications like the SID application or an admin account that I'll use from my
00:58day-to-day administrative work.
01:00Users are distinguished by both username and host name.
01:03So a user at local host is different than user at some domain.com.
01:08So let's go ahead and start up the MySQL command line client, double-click on
01:13the shortcut that we created earlier and type mysql -u root -p, so that will
01:20use the root user and it will ask for the password which we setup in the last lesson.
01:24It's foo.bar.
01:26Now we are in the MySQL command line client and we are as the root users, so we
01:32can do dangerous things.
01:34Now this is probably last time we are going to use the root user, because one of
01:37the things we'll do here is setup the admin user, which we'll use for
01:41administrative work in the future.
01:43I want to go ahead and create two users.
01:45One of them for web work and one for administrative work.
01:50CREATE USER web@localhost;
01:54and CREATE USER admin@localhost;
02:00Now by using the @localhost syntax, that means that these users are restricted
02:05to operating from the local host.
02:08If they connect into this computer from another computer either on the local
02:11network or out on the Internet, this login will not work.
02:15You will need a separate user account for outside of this computer.
02:19Now the users have been created, but they have no privileges so you can't use them.
02:23We need to create privileges for these users.
02:26Once a user is created, you grant privileges using the Grant statement.
02:31For the web user, you need all the basic data permissions plus, permissions
02:35necessary for managing a database.
02:37So this is everything except the administrative purposes.
02:40Now these privileges get specified individually.
02:43So there is a bit of typing here.
02:45Let's go ahead and type the command.
02:47GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, FILE, INDEX, ALTER, CREATE
03:01TEMPORARY TABLES, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE,
03:17EXECUTE, ON *.* TO web@localhost;
03:26So these are a number of privileges listed individually each one separated by a
03:31comma and a space and these privileges are granted on *.* which means that the
03:37user can use these privileges on all the tables in all the databases to
03:42web@localhost which grants these privileges to the web user on local host that
03:47we setup a view minutes ago.
03:49So I'll hit Enter, and we'll go ahead now and grant privileges to the admin user.
03:54Now this is a bit easier because the admin user gets all privileges and there
03:59is shortcut for that. GRANT ALL ON *.*.
04:02That grants all privileges on all tables in all databases to admin@localhost
04:10with Grant option and that means that not only our all of these privileges
04:15granted to this user, but this user also has the privilege of granting
04:19privileges to other users.
04:21Go ahead and hit Enter and now we'll issue the FLUSH PRIVILEGES command.
04:26Now remember what this does is it tells the MySQL database management system to
04:35go ahead and read the privileges from the database again so these privileges
04:39can be used right away.
04:42Now we'll go ahead and assign passwords to these two users and we do that in the
04:48user table the same way that we did it for the root user.
04:51UPDATE mysql.user SET PASSWORD = PASSWORD <' foo.baz'> WHERE user = 'web ' ; and
05:15we'll do the same thing for the admin.
05:19I'm just going to hit the Up arrow and go ahead and admin.
05:24You wouldn't do this in the real world, but for our purposes for demonstration,
05:28I'm just setting the password to the same for both of them and there we go.
05:32Now I need to FLUSH PRIVILEGES again.
05:38We FLUSH PRIVILEGES and now let's go ahead and test them.
05:47Try the web user first and you see it needs a password. So we'll -p.
05:51I like to test things without the password just to make sure.
05:55Let me go ahead and type the password, foo.baz, and there we are. We are in.
06:02If I say show databases so we can see that it's working and it has
06:05authorization to the system.
06:07Go ahead and exit and try the admin user.
06:11Again, it doesn't work without the password, -p.
06:19Type the password and there we are. Show databases.
06:24We can see that that works.
06:25All right, so that's how we setup the users and you would go through that same
06:29process for any other users that you may have.
Collapse this transcript
Importing databases on Windows
00:00Sometimes you may need to import some data into a database as a starting point.
00:05For example, when you are installing an application that uses MySQL, like the
00:08example applications provided with this course.
00:11Here is how you do that.
00:12Let's go ahead and start up the command line client.
00:16We'll double-click on the shortcut that we created earlier and you'll notice
00:21that this drops us into the SQL directory in the Exercises Folder.
00:26If I type dir here, it will give us a listing of this directory.
00:30We'll see that there is three files, album-mysql.sql, test-mysql.sql,
00:38and world-mysql.sql.
00:40What these are, are files with SQL commands that create and populate databases,
00:48album database, test database, and world database respectively.
00:52The way these are used is by piping them into the MySQL command line
00:56client using a special pipe character that will read the input to a
01:01program from another source.
01:03In this case, a file rather than from the keyboard.
01:06So let's see how this works.
01:08So you type in the command line here, mysql -u admin -p, so that invokes the
01:23MySQL command line client with the admin user and asks for a password.
01:28Then you see the left angel bracket is the pipe character that reads input from
01:33a file instead of the keyboard and then the name of the file album-mysql.sql.
01:40So when I hit the Enter key, this will invoke the MySQL program with the admin user.
01:47It will ask me for the password and then it will read all of the SQL commands
01:51from this file and use them instead of the keyboard and it will just give me
01:55back the command prompt here.
01:56I am going to type the password and there we are.
02:03It has now created the album database and populated that database with the
02:07data from that file.
02:08Let's go ahead and do the other two and then we'll go into MySQL command line
02:12client and look at what it did.
02:14I am going to go ahead and hit the Up arrow and then edit the command line so I
02:18don't have to type the whole thing again. Here we go.
02:27Type the password and do the same thing with the world database.
02:42Now we have imported all three of these databases into our MySQL.
02:46I'm going to go ahead and run MySQL and we'll take a look at what we have.
02:58First, we'll use the test database. use test;
03:03What this does is it tells MySQL that the commands that I'm going to give it
03:06from here on forward are relative to the test database and I'll take a look at
03:13the tables with the show tables command, show tables, semicolon.
03:18Let's take a look at what's in item, select * from item;
03:23Here we have four items in our item table in the test database.
03:30So we have imported this data from the test-mysql.sql file.
03:36Let's take a look at the album database. use album;
03:41Remember the semicolon terminates a command in SQL. show tables;
03:47I have two tables in the album database, album and track.
03:52The album table has information that's relative to an album like the artist and what have you.
03:59And the track table has the data that's relative to the track.
04:02So let's take a look at the album table first.
04:05select * from album;
04:06Here are the albums in our album table.
04:12This is actually the database that's used for the CRUD application that we'll see later.
04:16You will see that there are a number of albums.
04:19It looks like seven albums in that table and the tracks will list each of the
04:23tracks for all of those albums.
04:25select * from track;
04:29and that scrolls on by.
04:30That's got all of those tracks in it.
04:33Now also take a look at the world database. use world;
04:37Now the world database has bigger tables.
04:41Show tables, city, country, language and you will notice select count * from city;
04:53There are 4,000 rows in the city table.
04:59select count * from country and there are likewise 239 rows in the country database.
05:08So we have successfully imported data for three different databases using the
05:13command line very easy, very little typing and now we have gone in and we've
05:17confirmed that the data is there.
05:19So now you know how to import data.
05:22This is a common way for the data to be distributed within an application in
05:27SQL files like that.
05:29So this is how you import those SQL files into MySQL from the command line.
Collapse this transcript
Importing databases on a Mac
00:00Sometimes you may need to import some data into a database as a starting point,
00:04for example, when you are installing an application that uses MySQL like the
00:08example applications provided with this course.
00:11Here is how we do that on a Mac.
00:12I am going to start up my terminal application.
00:16In this case, I'm using iTerm.
00:17You can use the Terminal application that comes with Mac OS X if you prefer.
00:22Now before I invoke the MySQL Command-Line client, I'm going to need to
00:26change directories into the directory that has the SQL files that we are
00:30going to be importing.
00:31So we use the cd command for that.
00:34cd stands for change directory.
00:36And then, you type the directory path from where you are.
00:39So in this case, type Desktop and you will notice that I just typed the first
00:43couple of letters and then I pressed the Tab key, which completes the typing.
00:48So I don't have to type it myself and the computer figures it out from what I
00:52have started with, and it spells it correctly and everything.
00:55So for example, after Desktop, I want the Exercise Files directory, so I type
01:02Ex and that's enough, and I press the Tab key and it goes ahead and completes that for me.
01:06You will notice also, I'm going to bring the mouse cursor over here to point.
01:09You'll see this Backslash before the space between the word Exercise and Files.
01:14That's actually necessary.
01:15That tells the operating system that that's a literal space and it's not a space
01:20separating two commands.
01:22Then after Exercise Files, I want the SQL folder inside the Exercise Files
01:27folder, and I press Enter, and now we are at the correct folder.
01:33So I'm going to type another UNIX command here.
01:35It's called pwd, which stands for print working directory.
01:39This will give us the directory path where we are, and we can confirm that where
01:42we want to be and that's correct.
01:45Users/billw/Desktop/Exercise Files/SQL.
01:48That's the path to the directory where we want to be.
01:51I am gong to type another UNIX command, which is ls, which is for listing the
01:56files in a directory.
01:57We can see we have three files in this directory, album-mysql.sql,
02:02test-mysql.sql, and world-mysql.sql.
02:10These are the files that contain the SQL commands for creating and populating
02:16the databases that we want to import into our installation.
02:20We do this by using what's called a pipe.
02:23What we are going to do is we are going to invoke the MySQL command-line
02:27program with a pipe.
02:29What a pipe does is it reads commands into a program from something other than a
02:34keyboard and we are going to use the pipe that reads from a file.
02:37This is really a lot more simple than it sounds.
02:39So let's take a look at how this works.
02:42I am going to type now, mysql -u admin -p.
02:50So that MySQL will ask for the password and then the pipe character and I'm
02:56going to use the test database first, and I can use command completion again.
03:00I can just type the first few letters and press the Tab key and the computer
03:05figures out from those first few letters what it is that I meant to type and it
03:09completes it for me.
03:10And so that's the test-mysql.sql file.
03:14So what this command does is it invokes the MySQL command-line program with the
03:18user admin, it will ask for the password, and then it will read the data from
03:23the file as if I was typing that into the interactive program.
03:28And then it will finish. So here we go.
03:32So there we have it.
03:33That command completed.
03:34We'll take a look, in a minute, inside the MySQL command-line application to see
03:41the data that we have imported.
03:42But let's go ahead first and import these other databases first.
03:47So we have the same command, all we need to do is to change the file name.
03:51So I'm just going to press the Up arrow on my keyboard, and that will bring up
03:55the command that I last entered.
03:57Let me use the Backspace key here, and type the album database.
04:02al should be enough, I press Tab, and it completes and I type in the password
04:09and finally the world database.
04:12We'll do the same thing.
04:14Press the Up arrow and Backspace and type world, and Tab, and type the password,
04:26and there we have it.
04:27Now we have imported the three databases into the MySQL database application.
04:34Now let's go into the command-line application and take a look at the data.
04:39mysql -u admin -p foo.baz and there we are.
04:47We are in the MySQL command line application, and we can take a look at our deal.
04:53Let's start with the test data.
04:54We'll say use test;
04:56Remember we always have to press the semicolon at the end of our SQL inside the
05:01command-line application.
05:03Use test means to use the test database for the command that follow and let's take a look.
05:11We'll show tables;.
05:14We have there tables.
05:16Let's take a look at some of the data in one of those tables, select * from
05:20item;, and that will show all the data in the item, table.
05:26There it is and let's use the album database.
05:34The album database is what's used in the CRUD application that we'll be taking a look at later.
05:38So tables.
05:40Here is the tables in the album database.
05:43Let's take a look at the data inside the album table, there it is.
05:53And finally, use world.
05:56The world database is huge.
05:58It's got a lot of data in it, show tables.
06:01For example, if you take a look at the City table,
06:06Well, there we are, we have successfully imported three databases into our MySQL
06:20installation, and we have looked and seen that the data got there safely.
06:24Now we can just exit and type exit again to get out of our terminal app and we are done.
Collapse this transcript
4. Using the SQL Interactive Demonstrator (SID)
Using SID
00:00I have created a simple interface for showing examples and for use by those of
00:04you who need a simple way of typing in some SQL and viewing the results.
00:08I call this app SID for SQL Interactive Demonstrator.
00:12You don't need to use it.
00:13It's included in the Exercise Files and it's a great way to follow along, as
00:17I'll be demonstrating the examples in MySQL using SID.
00:20SID requires MySQL, PHP, and a web server, typically Apache, which are all
00:26available for Windows, Mac OS X, Linux and most other operating systems.
00:30I suggest the XAMPP package for Windows and OS X because it's easy to install
00:35and it includes all the necessary components.
00:37Most Linux applications already have the required packages already installed.
00:42So this is what SID is.
00:43You select your database over here that you are going to be operating on, and
00:46you type sql in right here.
00:52You don't need to put a semicolon at the end of the sql in SID, and you just hit
00:56Go, and there is the results.
01:00Like I said it's easy to use, and that was the reason that I designed and built
01:03this is to make it easy and quick to demonstrate and try out SQL statements.
01:09I'll be using this to demonstrate the SQL throughout the remainder of this
01:12course and the lessons that immediately follow this one show you how to install
01:17SID on Windows and Mac OS X.
Collapse this transcript
Installing SID on Windows
00:00Now that we have imported the databases, let's go ahead and install the SID application.
00:05SID stands for SQL Interactive Demonstrator, and it's a program that I created
00:11for the purpose of demonstrating SQL on these courses here on Lynda.com.
00:16We'll be using the SID application for a lot of the remainder of this course
00:20to demonstrate the various features that are specific to the MySQL Database Management system.
00:25You will find SID in the Exercise Files folder.
00:28So, we'll go ahead and open that up.
00:32SID is in that folder.
00:34They are in the SID folder inside of it.
00:36I'll bring this down a little bit.
00:37What we are going to do is we are going to copy this whole SID folder into the
00:42htdocs folder under the xampp directory.
00:46So, go ahead and find xampp.
00:48It's under Computer and Local C Disk and xampp.
00:52You remember we installed it there.
00:53If I open this up, you will see there is a folder called htdocs.
00:57This is where the files go that are used for the Apache Web Server.
01:03So, we simply drag this folder, and I'm going to hold down the Ctrl key which
01:06makes it Copy instead of moving, so that we are installing a Copy of it, and we
01:10have the original still available there.
01:13I'm going to drag that into the htdocs folder, so it says Copy to htdocs and
01:18I'll let go, and there it is.
01:20Now, we'll go into this SID folder that we just copied over and you see there is
01:26a file that are called sid.
01:27When I hover the mouse over it, it says Type is PHP Script.
01:31It's actually called sid.php and Windows, by default, hides the file name
01:37extensions of files.
01:39You can turn this back on in an option in Windows and make it show them, but we
01:43don't need to do that.
01:44We are just going to edit the file.
01:45I'm going to right-click on it with my mouse, right-click with the mouse and
01:49select Edit with Notepad ++.
01:51When we install Notepad ++, it put this context menu for the explorer.
01:57So, I'll press on that and it opens up.
02:00So, the reason we are editing this file is to enter the correct username and
02:04password for accessing the MySQL database.
02:08So, here we have towards the top of the program and whenever you have to edit
02:11a program for this, it's usually going to be towards the top, some place easy to find.
02:14Here we have the place where you enter the user name and password for MySQL.
02:19And in this case it's easy to see because it says MYSQLUSER and MYSQLPASS
02:24and sometimes there will be comments that say edit this here or instructions will come with it.
02:29In this case, this is where we do it.
02:31So, we have Web already as the user and that's the username that we want, so we
02:35don't need to change that.
02:37And over here in PASS I'm going to put my cursor between these two quote marks
02:40and I'm going to type the password that we entered in the MySQL Command-Line
02:44client when we set up the web at local host user, I'll put in foo.baz.
02:48Now we'll notice that it doesn't say web at local host.
02:52It just says web, the out local host apart means that it will be connecting
02:57with the username web from the local host and so, this just needs to say web right here.
03:03So, we are actually done editing this file so we'll go ahead and save it.
03:07I'll save and we'll close out text editor and we'll close this Explorer window
03:16and go ahead and start up the web browser, Firefox, and go ahead and type in
03:21here locahost/SID/sid.php, which is the name of the application.
03:31Press Enter and this brings up the SID application.
03:35There it is SQL Interactive Demonstrator.
03:39Now, how this works is you select a database and we have the choice of the
03:42three databases that we imported already, and you type the SQL in this box over here and press Go.
03:48So, let's go ahead and try it out, we'll select the test database and we'll type
03:53in some SQL, SELECT * FROM item and go ahead and press Go.
04:01The semicolon is not necessary in this context, go ahead and press Go and there
04:06we have the contents of the item table in the test database, so it works.
04:12We've successfully imported the databases and we successfully installed SID.
04:17That's pretty easy to do.
Collapse this transcript
Installing SID on a Mac
00:00Now that we have imported our databases, let's go ahead and install the SID application.
00:05SID stands for SQL Interactive Demonstrator.
00:08It's a program that I created for these SQL courses here on Lynda.com for
00:12demonstrating SQL commands, and we'll be using this application for most of the
00:17rest of the this course.
00:19The SID application is in the Exercise Files folder in the SID folder within
00:26there and we are going to be installing it in the Apache web tree in the XAMPP
00:32Applications folder.
00:33So go down here in Applications and find xampp and you see this htdocs folder.
00:42It's actually a shortcut that's where you put the files.
00:45It will show up in the Apache web tree.
00:47So, files that you want to be available to Apache for displaying on a web
00:51page, you put in there.
00:52So, all we need to do is to drag, this SID folder and I'm going to hold down
00:57the Option key, so that it copies instead of moving and drop it in the htdocs folder over here.
01:05And then we are going to need to come into the htdocs folder and go into SID and
01:10we are going to want to edit the sid.php file, so that we can enter the username
01:16and password so that SID is able to access the database.
01:21So, I'm going to right-click or you can Ctrl-click if you have a single button
01:25mouse and Open With and I'm going to select my text editor which in this case
01:31is BBEdit, you can use whatever text editor you are using, you should be able
01:35to access it this way.
01:36I am going to click on that, and that will open the sid.php file and over here
01:44in the first few lines is where you find the username and password for accessing MySQL.
01:50Username is already correct, it's web, and we want to put in the password that
01:53we entered in our database, and that was foo.baz for the web at local host user.
02:00Now, you don't need to put web at local host over here in the USER, you just put
02:04web and local host is the name of the host that is accessing from.
02:08So, this is the way that it needs to be, we'll go ahead and Save and close
02:15BBEdit and we can close these folders here and go ahead and run our web browser
02:27and we should be able to access it.
02:30I'm going to type localhost/SID/sid.php and there is SID and the way that
02:42SID works is you select a database and here is the databases that we already imported.
02:46You type in your SQL over here and press Go. So, let's try it.
02:51Test database and we'll type SELECT * FROM item, just like we tested it in
02:59the command line client and there is the data.
03:02We have got 4 rows.
03:03It tells you how quick it happened and there's the rows of data, so that's it.
03:09We have installed SID successfully and we are ready to go with the rest of the course.
Collapse this transcript
5. Creating a Database
Creating a database
00:00In MySQL's model, as with most relational database management systems, tables
00:04are contained within a database.
00:06So before you can create a table, you must create a database.
00:10MySQL has a simple statement for creating a database.
00:16The Create Database statement creates a database and you follow that with the
00:20name of the database you want to create.
00:23Press Go and it's done.
00:24You can use Show Databasea to see the databases that are available.
00:33Here we see the sales database.
00:35It's right in there that we just created.
00:37Then you can use DROP Database to delete a database that already exists.
00:42This is very dangerous, you don't want to do this with the database that you need.
00:46It's not recoverable.
00:47It doesn't put it a trashcan someplace. It erases it.
00:56Drop Database sales deletes the sales database, the entire thing with all of its
01:01tables and all of its data.
01:03It doesn't ask you for confirmation.
01:05Drop Database sales deletes the sales database.
01:07So now if we do Show DatabaseS again, you'll see that the sales database is now gone.
01:16So that's how you create and destroy a database in MySQL.
Collapse this transcript
Creating a table
00:00As a database contains tables, a table contains all the rows and columns for
00:04a given set of data.
00:05A table definition contains all the details of a data set.
00:09Because of this, the CREATE TABLE statement is a little more complex than the
00:13CREATE DATABASE statement.
00:15The CREATE TABLE statement is how you define a table including its columns,
00:19storage options, auto- increments, keys and much more.
00:23Here's an example of a CREATE TABLE statement from the test database.
00:26It's included with this course.
00:28So this is the CREATE TABLE statement and the name of the table is after the
01:18CREATE TABLE keywords.
01:19You'll notice I named it test_customer, because there is already a customer
01:23in the test database.
01:25The id column, name, address, city, state and zip, these are the columns.
01:30The details of these various data types and options, we're going to cover
01:35some of those later.
01:36But let's just briefly take a look at how this works.
01:39The id column is integer and has these options:
01:43NOT NULL, AUTO_INCREMENT, PRIMARY KEY.
01:46The AUTO_INCREMENT option is actually very useful and I'm going to just tell you
01:49a little bit about that right here.
01:51It's common for each row that's inserted.
01:53MySQL will automatically place the next incremental value in this column.
01:57It's very useful for id rows.
01:59The name is a variable number of characters between 0 and 255 characters.
02:05Address, city are the same, state is a fix number of characters, two characters,
02:09zip is up to ten characters.
02:11We'll go ahead and hit Go.
02:13That will create the table.
02:15You notice it didn't work because we haven't selected a database yet.
02:18A table is contained within a database, so we need to select a database in order
02:23to create the table inside of that database.
02:25I'll go ahead and press Go, and there we go.
02:28The table got created and now we can go and show tables and we'll see that the
02:35table is in our list of tables.
02:38There it is, test_customer.
02:39Just like SHOW DATABASES shows you a list of databases.
02:43SHOW TABLES shows a list of tables from within a given database.
02:48When I'm done with that table, I can use DROP TABLE, just like the DROP DATABASE
02:53will delete a database.
02:55DROP TABLE test_customer will delete the table and all the data within it.
03:00Again, this does not put it in a trashcan someplace where you can recover it.
03:03This is destructive.
03:05When you drop a table, you lose all the data in that table.
03:10So now the table is gone, we'll go ahead and show tables and you'll see that the
03:17table is no longer in our list there.
03:20So that's the CREATE TABLE statement.
03:22It's generally a good idea to know how this works, even if you're not going to
03:25be creating your table definitions yourself.
Collapse this transcript
6. MySQL Data Types
What are data types?
00:00Let's take a moment and talk about data types in general before we get into the
00:03specifics of how MySQL represents data with its data types.
00:08Data types are important because they're how the database stores and
00:12represents its data.
00:13If you understand the data types, you'll have a much easier time working with the data.
00:17MySQL has data types for numeric data, for representing numbers, string types
00:23for storing words and text, large storage types for storing documents and files,
00:29data and time types for storing date and time values, bit values for storing
00:35logical values in flags and enumeration types for mnemonic values.
00:40MySQL has a rich set of data types.
00:43This chapter covers the most common and useful types.
00:46If you're interested in knowing the full breadth and depth of what MySQL has
00:50available, please see the documentation on the MySQL website.
Collapse this transcript
Numeric types
00:00There are three major kinds of numeric types, integer types, floating point
00:04types and fixed point types.
00:07Integer types are for storing whole numbers, i.e., numbers that don't have a
00:11decimal point or a fractional part, numbers like 1, 2, 3 and 4.
00:16Floating point types are for storing real numbers, numbers like 3.14159 or 147.39.
00:24Use floating point storage for applications where the range of mathematical
00:28results is more important in the accuracy of a calculation.
00:32Fixed point types are for storing decimal numbers that have a fixed precision.
00:36This is a special type that is useful in situations like financial
00:40applications, where arithmetic functions must return accurate results rounded
00:45to a specific number of digits.
00:47The distinction between floating point and fixed point types is very important.
00:51So let's take a look at an example.
00:53In the test database, you'll find a table called numerics.
00:56I'll type DESCRIBE numerics and that will give us the structure of the table.
01:05It has an id field and it has four numeric fields, two decimal and two floating point.
01:12The decimal types are da and db for decimal a and decimal b and the floating
01:18point columns are fa and fb for floating point a and floating point b.
01:24There's one row of data in the table and you'll see it has four values, 0.10 and
01:360.20 in decimal, and 0.1 and 0.2 in floating point.
01:41The decimal values have the decimal point in a fixed place and a fixed number of
01:45digits after the decimal point.
01:47The floating point numbers do not have a fixed number of places after the decimal point.
01:52So they just show 0.1 and 0.2.
01:54Now if we take the decimal values, da and db, and add them together, say da+db,
02:04you'll see we get the expected result, da is 0.10, db is 0.20 and da+db is 0.30.
02:11Now if we test this for equality with .3, in other words, we're testing the
02:16result of da+db against a literal value of .3, we should get a true answer.
02:23True would be 1 and false would be 0.
02:26So we expect to get a 1 here.
02:28So this is what we expected, da+db = .3 is true.
02:35Now let's try the same little experiment with the floating point values.
02:39Take fa and fb, which are 0.1 and 0.2, and we'll take the sum of fa and fb,
02:46which is 0.3, and we'll test it against .3 and see if that comes out as true.
02:53We notice in this case it is false, 0 means false.
02:57We might say well, if fa+fb is not equal to .3, what is it equal to?
03:03Let's just take out the = .3 part here and see what fa+fb is.
03:11That's not really what we expected, is it?
03:14It's 0.3, which is accurate to about, I don't know, 6 or 7 decimal places, and
03:21then a bunch of junk.
03:22This is not equal to 0.3, and that's the problem.
03:27Floating point numbers in all computer systems, not just in SQL, but in all
03:32computer systems, floating point numbers sacrifice accuracy for what's called precision.
03:38In the mathematical rule, precision mean something different than what you might
03:41think it means in your average colloquial speech.
03:44In the mathematical world, precision refers to the number of digits of accuracy.
03:49So it's accurate to 6 or so decimal places, but beyond that, it is not accurate anymore.
03:55Floating point numbers sacrifice accuracy for what they call precision.
04:01Decimal number systems in computers are specifically designed to limit the
04:07precision for accuracy.
04:09In other words, they will always be accurate to the number of decimal places
04:12that you defined them to be, that you'll use specific rules for doing rounding.
04:17This works best and was designed specifically for monetary applications, for
04:22applications where money is involved.
04:25If you're a designer and you know the difference between JPEG and GIF files,
04:28think of this like the difference between JPEG and GIF.
04:31JPEG files are great for photographic data, but they sacrifice the precision of the colors.
04:36They give you the access to millions of colors, but they sacrifice the accuracy
04:41of those colors for the amount of colors that you get.
04:44GIF files on the other hand, great for flat artwork, where you've got a
04:47limited number of colors, because they will very accurately represent up to
04:51255 colors and no more.
04:54That's the difference between floating point and decimal.
04:57Fixed point types are like the GIFs.
05:00They have a limited amount of precision and they are very, very accurate.
05:04Floating point types are like the JPEGs.
05:06They're able to represent a wide range of numbers and they sacrifice a bit
05:11of accuracy for that.
05:13This distinction is very important and the point is, of course, if you're doing
05:16monetary applications, like banking or accounting or anything like that, make
05:20sure that you use the fixed point types, in this case, decimal.
Collapse this transcript
String types
00:00String Types are for storing strings of data, often text.
00:05Fixed string types are for storing text that's always the same size like two
00:10letter state code or something like that.
00:13Variable Length string types are for storing strings of values that might have
00:18different sizes, like a name or an address.
00:23Character string types are for storing textual data.
00:26And I pronounce CHAR, some people say CHAR.
00:30CHAR is the fixed length type and VARCHAR is the Variable length type.
00:37BINARY types are for non-textual data.
00:40BINARY is the fixed length type and VARBINARY is the variable length type.
00:46This is the Create Table statement for the Album table in the album database.
00:53You'll see we have an Integer type for the id, and we have VARCHAR for
00:58title, artist and label.
01:00In parenthesis, you put the maximum length of the variable character, field.
01:06So, we have 255 for all of these and then we have a DATE type that we'll
01:10talk about these later.
01:11So, this is an example of this in use and this is usually how you'll see it used.
01:16Everyone once in a while, you'll see CHAR for fixed length ones, very, very
01:19rarely, you see the BINARY type or the VARBINARY type.
01:23So, String Types are for storing strings of data, usually text.
Collapse this transcript
Large storage types
00:00Blob and Text are Large Objects Storage Types, Binary and Text respectively.
00:06Blob stands for Binary Large Object.
00:08It's used for storing data that is not text.
00:11There are four types of Blobs in MYSQL.
00:14TinyBlob, for storage up to 256 bytes, Blob for storage up to 64K bytes,
00:22MediumBlob for storage up to 16M bytes and LongBlob are for storage up to 4G bytes.
00:29And these all are for non-textual data.
00:32For textual data, we have the Text types, and likewise, they come in four sizes.
00:38TinyText for up to 256 bytes, Text for up to 65535 bytes which is 64K,
00:48MediumText for up to 16M bytes and LongText for up to 4G bytes.
00:54The Blob types are useful for storing binary objects like images.
00:58The Text types are useful for storing text objects like articles or web pages.
01:03The Text types can be used with MYSQL's full-text searches.
Collapse this transcript
Date and time types
00:00The DATE and TIME types are useful for storing date and time related
00:04information, it may include the DATE type, which is good for dates only, and
00:09dates in the range of year 1000 to the year 9999.
00:12And the TIME type which is good for times and time related values, not just time of day.
00:19Give or Take for times in the range of +/- 840 hours.
00:23And the DATETIME type for date and time combinations.
00:28The TIMESTAMP type is useful for event logging and it's automatic.
00:33So, every time you insert a row, it automatically gets updated with the time and
00:38date that the row was inserted.
00:40Let's take a look at some examples.
00:42We'll go ahead and create a table in the test database.
00:45This will create a table called datetest, with a DATETIME type for the date, and
01:03a TIMESTAMP type for a timestamp called stamp.
01:06We'll go ahead and insert some data into it.
01:25This will INSERT INTO the table datetest, the column date with the value
01:312009-05-04, so that's May 4th, 2009, a 3:31 in the afternoon.
01:37Now, let's take a look at what we've done.
01:54So, this goes ahead and it shows our two columns here Date and Stamp.
01:58And it calculates the difference between these two values using the DATEDIFF
02:03function that we'll cover later. There we go.
02:08They are 16 days apart.
02:10The DATEDIFF function gives the result in number of days.
02:13So, that's just a little example of how you can use these values and how they work.
02:18The DATE and TIME types are useful for handling date and time related
02:22information and storing that in your database.
Collapse this transcript
Bit type
00:00The BIT type in MySQL is used for storing bit fields.
00:04So let's describe that way of example.
00:06We'll create a table in our test database.
00:12We call it bittest.
00:16We'll give it two columns b1 and b2.
00:18b1 is a bit field with 8 bits and b2 is a bit field with 10 bits.
00:32I'll go ahead and create this table and insert some data into it.
00:56What this does is it inserts into the table bittest, values for two columns b1 andb2.
01:01And the values are four On bits followed by four Off bits, and Off On Off Off On.
01:09The b here before the quoted value indicates that it's a binary value.
01:15And so it'll be just be ones and zeros.
01:16I'll go ahead and insert that row, and there we go.
01:20Now, let's take a look at it.
01:22Now we're not going to expect this to be displayable data, because we
01:29entered binary data.
01:30So what we'll see will be odd and there we go.
01:33We have this odd little character there, and it couldn't figure out a way
01:36to display b2 at all.
01:38And so, we insert a binary data, how can we new look at it as binary data.
01:43Well, one way to do that is to tell MySQL that this is a number and you do that
01:49by adding zero to it.
01:51Any kind of math, will tell it that it's a number.
01:55So, you can just add zero to tell it that it's number without modifying the value.
02:03And there you see we've got 240 and 9.
02:05But we entered binary values.
02:07Can we see them as binary values?
02:09And in fact, we can.
02:11We can use the BIN function to see them as binary values, and there we have the
02:21values that we entered.
02:22You'll notice that the leading zero is gone from b2, so there is ten bits
02:26available there and we've really only used four of them.
02:29The rest of them are zeros, which is significant, but it doesn't need to display them.
02:34There are other number systems that are used for binary values, and just briefly
02:37I'm going to show them to you.
02:38So there is BIN, I'm just going to show them with b1.
02:42There is Octal, which is base 8.
02:44There is Hexadecimal, which is base 16.
02:54And so there is those values in Binary, Octal and Hexadecimal, that's all the same value.
03:00So that's the bit field.
03:01It's useful for storing flags.
03:04If you want to store the state of a number of things in a very small space, it's
03:07really useful for that.
03:09You won't actually see it that often.
03:11It's something that might be used by programmer types, but you're not going
03:14to see it a lot in web work or in most of the database applications that you run across.
03:18And that's the bit field.
Collapse this transcript
Boolean values
00:00MySQL does not actually have a Boolean type.
00:04It uses integers for Boolean values.
00:07So, if you declare a column in your table, as type BOOL, it will actually
00:11store it as TINYINT.
00:13The word BOOL is an alias for the word TINYINT.
00:17True is represented by an integer 1.
00:20False is represented by an integer 0.
00:23And in fact, the BIT type is an excellent way to store Boolean values.
00:29Let's take a brief look at what integer values look like in MySQL.
00:34We can do this by simply testing for equality. If we ask is 5=5?
00:42We should get 1 for True.
00:44Yes, 5 is equal to 5.
00:46And try something that we know is not equal. Is 5=7?
00:49No, 5 is not equal to 7, so we get a 0.
00:54So we can see that 1 is True and 0 is False.
00:58And that's really all you need to know about Boolean values in MySQL.
01:02You can store them in a bit field or you can store them in an INT,
01:05and 1 is True and 0 is False.
Collapse this transcript
Enumeration types
00:00There are two different enumeration types in MySQL, enum and set and they
00:05work very differently.
00:07So, let's look at a couple of examples so that you can see how it is that they work.
00:12I am going to create a table in the test database to show you enum.
00:31This will create a table called enumtest with one column color of type ENUM with
00:38three possible values, red, blue and green.
00:42Go ahead and create the table, and now let's insert a value.
00:57This will insert 1 row into the enumtest table, in the test database with the
01:02value red in the color column.
01:04There we go and let's take a look.
01:14And there we have the one column with the value red, let's insert a couple more
01:33and then another one for green.
01:34And so, what we have now is we have 3 rows, one is red, one is blue and one is green.
01:51The value of this is that, these different values are actually stored in just 1 byte each.
01:58The ENUM type is actually just an integer and it stores a number called an index.
02:03And when you define it, you have defined three possible values red, blue and
02:08green and so it will store a one if it's red and a two if it's blue and a
02:13three if it's green.
02:14So it takes up very little space, if you have a limited number of possible
02:18values for a column and this is a great way to be able to still see them
02:24mnemonically, to be able to still see red, blue and green and to not have to
02:27translate it in your code.
02:29That one is a red, because the database engine will do that for you and it does
02:32it very, very efficiently.
02:34So, this is a very useful column type.
02:37I want to show you one other little quirky aspect of the ENUM type.
02:50If I try to put a value in here that is not defined in the declaration, it's not
02:56really going to know what to do with it.
02:58It can't store a 1 or a 2 or a 3.
03:01It can't store a 4 because it doesn't have it defined, and it won't know what
03:04that is when it tries to read it.
03:06So, what it does is it just silently stores nothing, and it's not even a null value.
03:13It's just empty.
03:16So, when we go and look at this, we only see three values.
03:25There's actually a fourth row down here and just because HTML, doesn't display
03:30an empty table cell there, we are not seeing it but we if we do count, we'll see
03:34that there are 4 rows, there are 4 rows.
03:42So it has stored an empty value there for the unknown color.
03:46So, this is just an important aspect to understand about how ENUMS work.
03:50It will silently fail effectively.
03:52I would have preferred that it gave an error and didn't allow us to insert a
03:56value that's not in the enumeration list, when we declared it.
03:59But this is what it does, and it's important to know that that's how it operates.
04:03Now, let's look at the SET column type.
04:06We'll create another test table.
04:24This creates a table called settest.
04:26We have one column attrib, which is a SET type and it has three possible
04:32values, bold, italic and underline, and these are attributes that can be
04:37applied in combinations.
04:39And so what SET does is it allows you to have multiple values in one column, in one row.
04:47So, one value can be bold or it can be italic or it can be bold and italic or it
04:53can be italic and underline.
04:55It can be any combination of these three values, and the way that MySQL does
05:00this is it stores them in a bit field.
05:02This is really just another way to use a bit field with mnemonic values and
05:07it's very, very flexible and powerful and it allows you to use your bit fields
05:12and not have to do a lot of computation to figure out which bits are set and
05:15which bits are not set.
05:16Instead you get these lovely mnemonic values for the bits.
05:20So, let's go ahead and create this table and let's assign some values to it.
05:26I'm going to go ahead and do several of these.
05:39I'm just going to Cut and Paste, so that we can -- let's say we'll do bold and
05:49italic and we'll do another one with say bold and underline, and another one
06:04with say underline and italic and let's do one more with underline and bold.
06:15We did bold and underline before, let's do underline and bold, so we can see how that works.
06:20The truth is that MySQL will always display them in the order that they were defined.
06:25So, for underline, italic, it will say italic, underline and underline, bold it
06:30will say bold, underline, when we read them back.
06:33Let's go ahead and store this.
06:34We should have 5 rows in our table and take a look. There we go.
06:45We have bold; bold, italic;
06:47bold, underline;
06:48italic, underline and bold, underline.
06:50You see underline is always at the end because that's the way that we defined it.
06:54If we go and look at our declaration DESCRIBE settest, we see that that's the
07:02order that they are defined, set, bold, italic and underline, in that order so
07:06it will always read them back in that order.
07:08So that's the SET type.
07:10SET is very useful for flags.
07:12It makes the bit field usable by somebody who is not even terribly technical and
07:17those are the two enumeration types that are available in MySQL.
07:20There is ENUM for one value per row and they are stored as indexes in an integer
07:27field and then there is settest for multiple combinations of possible values and
07:33they are stored in a bit field.
Collapse this transcript
7. MySQL Functions
MySQL functions
00:00In SQL functions are used to provide derived values, values that are derived
00:05from other values, and from other data.
00:07For example, this is a SELECT statement from the world database, selecting a
00:21COUNT of all the rows from the Country table.
00:23This is a syntax that's familiar to most people who have ever seen any SQL, this
00:28is one of the most common functions used in SQL and it's an aggregate function
00:33that basically counts all the rows from a select statement.
00:37So, COUNT is the function and the asterisk is the argument.
00:42The general syntax of a function call is the name of the function and an open
00:48parentheses and then the argument to the function or more than one argument if
00:53there are, and they are separated by commas, and the close parentheses and this
00:58gives you the result of the function call, rather than the raw data.
01:02So, in this case COUNT with the asterisk, means to count all the rows from the table.
01:09So, this gives you the count of all the rows from the country and we put
01:12another table name here, you will see we get a different number, 4079 rows in the city table.
01:19So, in a nutshell, that's how a function works.
01:22Let's go ahead and look at some specific functions that are available in MySQL.
Collapse this transcript
String functions
00:00MySQL has a number of useful String functions and we're going to look at some of them here.
00:04But first let's talk about what a String is?
00:13This is a Literal String, SELECT 'Hello, World'.
00:17So Hello World is a literal string.
00:19You can see it's a number of characters enclosed in single quote marks and
00:22that's how you do a string in SQL.
00:25Of course, there is also strings that are stored in the database.
00:27So, this will select the title column, from the Album table in the album database.
00:41There we go.
00:41These are strings also and string functions applied to these as well.
00:46Let's go back to our literal string, and there it is Hello World.
00:56And let's say that these two words were coming from different sources.
01:01So, we'll use a function called CONCAT, to concatenate two literal strings.
01:09So, remember our function syntax, we have the name of the function, the
01:15parenthesis and inside the parenthesis we have arguments.
01:19In this case, there is two arguments separated by a comma. We run this.
01:22It will concatenate Hello with World and run them together. There we go.
01:28CONCAT ('Hello', 'World') comes out.
01:30HelloWorld run together without the comma and the space, because the comma and
01:34the space in this case are simply separating the arguments to the function and
01:39they're not actually part of the string.
01:41If we want the comma and the space, we could add that as a separate string
01:46inside here and now, we have three strings.
01:51We have Hello, we have comma, space and we have World.
01:56It's a little hard to look at because we're also using a comma and a space
02:00to separate the arguments and when we run it, we'll see we get the result
02:04that we're looking for. Hello, World.
02:07Like I said, that's a little hard to look at.
02:09MySQL has another function called Concatenate with Separator, which is spelled
02:14CONCAT_WS and you give the separators the first arguments, so I'm going to give
02:19a colon as the separator and then I use a comma to separate the arguments.
02:26So, I'm going to take out this comma, space string and now we have three arguments.
02:31We've the first argument is the Separator that we're going to use and the second
02:35argument is the literal sting, Hello, the third argument is the literal string,
02:38World and so what this will do is it will use this separator, to separate all
02:43the arguments after it.
02:44So, we have Hello:World and if we were to have more arguments, those will all
02:57get separated with colons as well.
03:00I'll show you a practical example of this little bit later in the lesson, but
03:03right now, I want to talk about LPAD and RPAD.
03:07In order to do that, we're going to close our browser, we're going to open up
03:10the command line client, because spaces are hard to see in a web browser.
03:15Web browsers tend to fold numbers of spaces all into one space.
03:18So, it's hard to illustrate something that actually uses the spaces in a web browser.
03:23So, go ahead and run the MySQL command line client and we'll take a look at
03:33the Title column from the Album Database again, and Use Album, SELECT title FROM album.
03:44Remember, you have to use a semi-colon to terminate all of your SQL Commands in
03:48the command line client.
03:50And there we have the Title column from the Album table in the album database.
03:56Now, what if we wanted all of these to be Right Flush.
03:59This is something we can use LPAD for.
04:12SELECT LPAD title, 30, FROM album;
04:19and what this does is it calls the LPAD function.
04:23This first argument is what the string is.
04:26So, in this case it's the Title column from the album database.
04:30The second argument is the number of characters that you want to have in the
04:34final string, and you will see what that looks like in a moment and the third
04:38argument is what you are padding it with to make the string that long.
04:42And then we have our SQL from Album telling it what table to take the column
04:47from and this is what we get.
04:49We get a title padded out to 30 characters.
04:53So all these strings are exactly 30 characters long and if the data wasn't long
04:57enough, it pads the left-hand side.
04:59That's what's LPAD with spaces.
05:02If we use RPAD instead, then we get them padded out on the right-hand side to 30 characters.
05:18It's good to know also what happens when the padding is less than the length
05:23of the string and what happens is that the string will be truncated to the
05:26length that you specify.
05:27So if I say SELECT LPAD title 15 FROM album, then we get strings that are
05:44exactly 15 characters long and they are truncated at the end.
05:48If the string is short enough, in the case of Apostrophe or Kind of Blue, or
05:53Birds of Fire then, that's fine.
05:55If they are too long, in case of Two Men with the Blues then it's truncated to 15 characters.
06:01If they are too short they're padded on the left-hand side.
06:03This is what you'd expect if your strings are longer than the amount of the padding.
06:09So, this is really useful for reports that are going to be printed in a
06:13fixed-width typeface, which reports typically were until just the last few years
06:18and in a lot of places they still are.
06:20So, that's the LPAD and RPAD functions.
06:23And we'll go ahead and Exit the command line client and go back to the web browser.
06:27And I want to show you a practical example of how these are used.
06:35This is an example from the CRUD application and we'll actually see it in place
06:40in the CRUD application later on in the course.
07:01So, here is an SQL statement that's going to show us a few columns from the
07:04Track table in the Album database.
07:10There we have it, id, album_id, title, track_number, and duration.
07:14You'll notice the duration is in number of seconds.
07:17That's the length of the track in the number of seconds.
07:18Now we see, we don't want to display it that way and I decided to store it that
07:22way in the database for certain reasons.
07:24But I want to display it as minutes and seconds.
07:28Now, there is a function built into MySQL that will display it as hours, minutes
07:32and seconds, which is not what I want, and that looks like this.
07:41So you have hours, minutes, and seconds and few tracks are long enough for
07:45that, and when they are I'm happy to display 90 minutes instead of 1 hour and 30 minutes.
07:51And so what I really wanted was to display this in minutes and seconds, and so I
07:55came up with a way to do it using CONCAT_WS and LPAD and that looks like this.
08:03Go ahead and indent it on another line, so that it's obvious and visible.
08:08You'll remember from the SQL course that whitespace is ignored in SQL and it's
08:16all just folded into one space and so this allows us to indent the code, so that
08:20it looks readable on page.
08:22So, here we have a rather long expression and it uses two functions, CONCAT_WS
08:45and LPAD and when you're looking at a long expression like this, it's best to
08:49start in the inside when you read it.
08:51This makes it easier to read.
08:52So if we look at this part of the expression here, we see we have an LPAD
08:56function, with duration MOD 60 and we'll be explaining that in the section
09:01on Numeric functions but in a nutshell that does a division and gives you the remainder.
09:06So, we have duration MOD 60 as the string and 2 is the length of the string and
09:13the literal 0 is what to pad it with.
09:17And so what this will do is it will take this duration MOD 60 and if it's less
09:21than two characters long, it will pad it on the left-hand side with zeros and
09:25that gives you things like this here, this duration is 3 minutes and 5 seconds
09:30and you'll see that it display as just 3 or 5 and we're all done with this.
09:35And then we have CONCAT_WS, which has three arguments.
09:39It has the separator.
09:42It has the duration DIV 60 which is the number of minutes and this LPAD
09:48expression for the number of seconds.
09:50So it takes those two values and it concatenates them together with a colon,
09:54which is exactly what we want.
09:56So, that's how this line works.
09:58When we press Go here, we'll see.
10:00We now get the duration in minutes and seconds.
10:02So we have 5 minutes here, down here, we have one that's 13 minutes long.
10:06We'll notice that it shows the whole 13.
10:08It also shows this 06, with a leading 0.
10:12We have that in a few places there.
10:14So it's padding the left-hand side of that value with a 0, so we have a leading
10:190 on the seconds, we don't have a leading 0 on the minutes which is exactly how
10:23I think it should be displayed and so that's why I have created it this way.
10:27So, there is a practical example of these String functions.
10:31Of course MySQL has many String functions available, most of them are not as
10:36commonly used as the concatenation and the padding and they're all documented on
10:40the MySQL website, if you're interested in them.
Collapse this transcript
Numeric functions
00:00MySQL has a number of useful numeric functions and we are going to talk
00:03about those in a moment.
00:05But first, I want to talk about numbers a little bit.
00:11SELECT 320, so 320 here is a literal number and when I press Go, you see the result is 320.
00:17I can also get numbers from a database.
00:27So this will select the duration column from the track table in the album database.
00:32So there we have a bunch of numbers and those come from a database.
00:37So for our demonstrations here we'll be using mostly literal numbers and we'll
00:42also use some numbers from database.
00:44So let's start with this 320 number.
00:49And let's say I want to know how many minutes there are in 320 seconds.
00:53I can just divide by 60.
00:57I see there is 5 minutes in change.
00:58Now, what if I'm not interested in the change, I just want to know the whole minutes.
01:04So I can use the DIV instead of the Slash operator.
01:09This is actually a function call, 320 DIV 60 and that will give me 5.
01:15So there is 5 minutes and we are not worried about the change here.
01:19If we are worried about the change, we can just say MOD instead of DIV and that
01:22will give us the modulus which is the remainder of a division and that will tell
01:27us that there is 20 seconds after the 5 minutes are taken away.
01:32And this is really useful.
01:33An example from the CRUD application, do something like this.
01:59And this will give us all of those durations from the track table in the album
02:03database formatted as minutes and seconds.
02:07So if we look at this and when we nest functions like this, we want to look from
02:10the inside out, we use LPAD to format this function.
02:15So this is the function here, and duration, MOD 60 that gives us the remainder.
02:19It gives us the number of seconds after the minutes are taken away.
02:21I want to format that to be 2 characters long and padded with 0s on the left and
02:26so the LPAD function does that and that's from here to there.
02:31And that is one argument to this CONCAT_WS and we are using the colon as a
02:38separator, CONCAT_WS is covered in our string functions section and we are using
02:43the duration DIV 60 which is the number of minutes and the padded number of
02:47seconds, padded on the left with 0s, FROM track.
02:51So this will give us the minutes and the seconds and the seconds will be padded
02:55with 0s on the left, separated by a colon.
02:59And there we have it.
03:01So 320 seconds is 5 minutes and 20 seconds, 5:44, 4:56 and you will see that the
03:09numbers that have a number of seconds less than 10 are padded on the left with a
03:130, this one and there is another one.
03:16And we'll see that the numbers for the minutes are not padded and yet when there
03:20is more than 10 minutes, it shows that way.
03:27So this is a useful example and this is actually how it's done in the
03:31CRUD application we'll see that later on when we look at the code of the CRUD application.
03:36There are other useful numeric functions that you will see commonly used in
03:38MySQL and let's pick another literal number to use for this example. Select 125.
03:46That number is in base 10 which is commonly how we use numbers in our day-to-day
03:50life but let's say that we are a computer geek and we want to see it in base 16,
03:54you can say CONVERT 125, 10, 16.
04:03Base 16 is also called hexadecimal and so we are converting from base 10, which
04:08is also called decimal into base 16, and the number in base 10 is 125, so it
04:14will give us the result in hexadecimal.
04:17You can do the same thing, converting to base 2 and you can convert the
04:23other way around as well.
04:33There are some shortcuts for some of the more common conversions.
04:36If you want to see, 125 in HEX, you can just say HEX (125) or if you want to see
04:46it in binary, you can say BIN, that's base 2 and if you want to see it in octal,
04:53there is a shortcut for that which is OCT, which is base 8.
04:59So those are the conversion functions.
05:01Another common numeric function that you will see is the CRC32 function and this
05:13does what's called a Cyclic Redundancy Check.
05:15It's a form of a check sum.
05:17It will come up with a unique number that represents this string and it's CRC32,
05:22which is a common form of CRC, a common form of hashing.
05:26There is different words that are used to describe it and you will see it a
05:30lot and then there is the number and it comes up in decimal by default and if
05:33you want it in text, which is also common, you will see this very often, this construction here.
05:38All right and you will also commonly see it, used from that table.
05:46Say SELECT title and the CRC from the track table.
05:52And even though it's just a 32 bit number, you will see these numbers are almost
06:01entirely unique for most lists of less than a few hundred thousand of things.
06:06So it's often used for hashing and for indexing into data structures.
06:10So you will see that now and then.
06:12There is also a complete set of trigonometric functions and we are just going
06:16to look at a couple of them because I wouldn't want to get into too much heavy math here.
06:21Like for instance, DEGREES from radians, PI is half a circle and radians.
06:28So we have that PI function inside as the argument to DEGREES function and this
06:32will give us PI and DEGREES which will come out to 180 degrees or if you want
06:37radians from degrees, you can say RADIANS of 180.
06:41And that will give you a 180 RADIANS, which is more digits of PI than I have memorized.
06:50There is of course many more trigonometry functions.
06:52You have got sines and cosines and tans and things like that.
06:57There is the common formatting function, you will see this one a lot.
07:07And what this will do is it will format this large number with two decimal
07:11places and it will put commas in between the digits and the number.
07:16So there we have easy to read number and you will see this function, we used a
07:20lot, and it's internationalized.
07:22So if you are using a different locale in your installation of MySQL, it will
07:26use periods instead of the commas and commas instead of the periods if you are in Europe.
07:30Or however it's set up on your computer in your
07:33internationalization, localization.
07:36Another common function that you will see is power, spelled POW.
07:43So this will give us 16 squared or if we want a larger power, 16 raised to the 8.
07:53It's a big number and the Random function is also very common.
07:58It gives you a random number and every time you call it, you get a
08:02different random number.
08:04Numbers are all less than one.
08:07And this is actually very, very useful.
08:10For example, you can select let's say I want a random list of countries from
08:29the world database.
08:30This is a very common way to get that, so this will give us random records from a database.
08:36Every time I click on it, I get 10 different random countries.
08:41And if we want, you can seed the random number generator, this is interesting
08:47and it will give you the same random list every time because it's starting from
08:50the same place in its randomization.
08:52I guess a different number.
08:55It will give you a different...
08:56But each time I click at it, it's the same.
09:00So those are the most common numeric functions that you will see in MySQL, of
09:02course there are a lot more of them.
09:05If you go to the MySQL website, you can find a complete list but these are the
09:09ones that you will see the most common and that you will use the most commonly.
Collapse this transcript
Date and time functions
00:00MySQL has a number of useful time and date related functions.
00:04Most common of which is the NOW function.
00:09The NOW function shows you what the time and date is right now. There it is.
00:16That's the time and date that I'm recording this, in the booth of Lynda.com.
00:20You can also just get the date part of that, using the CURDATE function for
00:25current date, there is just the date part of when it is right now and you can
00:31get the time part with CURTIME, for current time.
00:38You can get the current time and date in the UTC Time Zone, which is the
00:43Universal Time Zone, also called Greenwich Mean Time.
00:49Using UTC TIMESTAMP, UTC_TIMESTAMP.
00:54There is the current date and time in the UTC time zone.
01:00So I can say NOW, UTC_TIMESTAMP and see both of them together.
01:06And if I want to know the difference between them, I can say NOW -
01:13UTC_TIMESTAMP showing that I can do math, I can do simple arithmetic anyway
01:20directly on time values.
01:23Now, it gives me the difference in this internal time format.
01:26If I want it formatted for display, I can say TIME and use the TIME function
01:33wrapped all the way around that expression and it will give me just the time,
01:39seven hours difference between here and UTC.
01:43If you want to know the number of days between two dates, you can do something
01:48like this, DATEDIFF, NOW, there's what we were looking for.
02:02And we can see that that's a seven days difference, like we expected.
02:06And finally a common function that you will see used a lot is DATE_FORMAT.
02:24What DATE_FORMAT does is it formats a date for being readable and it uses these
02:29various tokens for specifying how the date is going to be formatted and these
02:34are all explained to the MySQL documentation.
02:37But in short, these are common ones.
02:39Capital W is the for the day of the week, so we see Thursday and that comes
02:44from the capital W. Capital D is for the day of the month with the English ordinal there.
02:52If I do that in a lower case d, you will see I'll get something different.
02:57It's just the number like that.
03:00The capital M is for the month spelled out, capital Y is for the four digit year
03:06and capital T is for the time formatted thusly in a common way.
03:11So that's DATE_FORMAT and you will see that a lot.
03:15So those are the date and time related functions in MySQL.
03:17Of course, like all of the other categories, there is a lot more functions
03:21available and if you look on the MySQL website, you can find a complete list.
Collapse this transcript
Time zones in MySQL
00:00So let's take a look at how MySQL handles timestamps.
00:03I'm going to bring up the command line client for this and we're going to create
00:11a table in the Test database.
00:13So we'll use Test, remember the semi- colon because this the command line client.
00:40So this creates a table with three columns ID, Name, and Stamp.
00:45ID is an auto-increment column, Name is a string, and Stamp is a timestamp column.
00:52So timestamps would be inserted automatically in that column.
00:55Let's go ahead and insert a row.
01:10So we've inserted a row into our table and we set a value for the Name column,
01:15Fred Flintstone, and we did not set values for Stamp and ID because those areautomatically inserted.
01:20Let's take a look.
01:26So there's our row.
01:27We have the ID of 1, the name Fred Flintstone, and the timestamp of the
01:32time when I inserted it.
01:34Now, you notice that it displays this time in my local time zone, which is Pacific time.
01:39So that's the same time as the time in my watch.
01:42It's stored internally in the universal time zone, which is Greenwich Mean Time.
01:48It's the universal time zone.
01:50So in order to display the time in my local time zone, it needs to know what
01:55that time zone is and that's done with a system variable called Time Zone.
02:03These two @ signs are to tell me that I'm looking at a system variable and
02:09time zone is right there.
02:11That's set to System, which means that it's using the time zone of my system.
02:16Now, this is normal and this is the way that MySQL is normally setup.
02:20So it's not actually storing the time zone internally.
02:24It's using the time zone from the host system.
02:27And then it does its calculations based on that.
02:30MySQL is capable of using multiple time zones in a session.
02:34And if you have an application that needs that, then this can be very useful.
02:39You can actually change the time zone for a given session and then change it back.
02:44This is useful especially for web applications where your users can set their
02:49time zones and save them.
02:50Unfortunately, this feature only works if the time zone tables are installed.
02:54Now, I'm going to go ahead and set the time zone to Eastern time, and as you
02:59follow along, this may not work for you.
03:01Now, if you get an error when you type that, that means that your time zone
03:13tables are not installed.
03:15There is another lesson in this chapter to tell you how to install them.
03:19So if this is important to you, then you can pause here and go and install the
03:23time zone tables, and come back, and follow along.
03:26Now, I've set my time zone to Eastern time, which is three hours later
03:30than Pacific time.
03:31So when I query my table, we can see that the time is now three hours later
03:40than it was before.
03:42We can set it back by setting it to US Pacific or we can simply set it to System.
03:49Now, I recommend if you're using this feature in production then at the
03:58beginning of a session you set the time zone, and then at the end of the
04:01session, you always set it back to system.
04:03So that when a different client connects to that connection, the time zone is
04:07set in a known place.
04:10Now, we query again.
04:11You'll see that the time is back to what it was, in Pacific time.
04:18Now, you'll notice a couple of little quirks here.
04:21One is that when the time is set to a different time zone.
04:24It's not always exactly the same time.
04:27I've noticed this too, and I'm not sure exactly why this is.
04:30I've tried to find out, and I've not found an answer.
04:32If I do find an answer, I'll post it on my website at bw.org.
04:36But it's just something worth noting, if you are setting your time zones, it's
04:39going to be consistent, and that's what really matters.
04:43So this is how MySQL handles time zones, and it stores its timestamps in
04:49universal time, which makes it convenient for it to do the calculations when it reads it.
04:53So that way, if different people are reading the same value in different time
04:57zones, it will always display it in their local time zone and the time will be correct.
05:01So that's how MySQL handles time zones, and if you need to install your time
05:05zone tables, be sure to take a look at the lessons for installing your time zone
05:09tables on Mac and on Linux.
Collapse this transcript
Installing time zone support in MySQL in Windows
00:00For those of you who need to install the time zone support files in MySQL, this
00:06is a lesson on how to install those files in the Windows operating system.
00:11First, let's take a look at MySQL and make sure that we need to install the files.
00:15So I'm going to start up the MySQL command line client.
00:23Let's start by taking a look at the system variable that tells us the time
00:28zone of this process.
00:34So this is a simple select statement that's looking at a system variable and you
00:37can tell it's got the two at signs and time_zone;
00:43and that value is SYSTEM.
00:45This is the normal value.
00:47You'll see this whether or not you need to install these files.
00:50Now I'm going to try to change the value.
00:52So I say SET time_zone = 'US/Eastern', to make it a string literal and
01:11semicolon, and I get this error.
01:13This error says unknown or incorrect time zone.
01:15This is because MySQL does not recognize the name of the time zone that I've
01:20entered, and it doesn't recognize that name because it's missing its time zone files.
01:25So we're going to go ahead and install them now.
01:27If you don't get an error here, then you can check the time zone variable and
01:32see if it's been changed.
01:37Now, in this case, it has not been changed, because we need the files.
01:41After we install the files, we'd go ahead and do this again, and see that it's been changed.
01:45I'm going to exit the command line client, and exit the command line window.
01:50Now, I'm going to come down here and run the XAMPP Control Panel and stop MySQL.
01:57You must stop MySQL before installing these files, so I press Stop there.
02:01I'm waiting for it to stop. There we go.
02:05See down here it says Stopped.
02:07I'll minimize that.
02:10I'll bring up my web browser.
02:12Now I've already navigated to this page.
02:14This is the URL, dev.mysql.com/downloads/timezones.html,
02:20and this is where you find the files that you need to download.
02:24Now, there is two links here, one for an older version and one for a newer version.
02:28We're going to download the newer version.
02:30I'm going to save this file.
02:41I have my Firefox set to save files on the desktop.
02:44I'll go ahead and close this and close Firefox.
02:51There is the time zone ZIP file.
02:53I'll go ahead, and Extract All, to my desktop.
02:57I'll just say Extract, and there is this directory, and here is the files.
03:02So these are the time zone files that I'm going to install.
03:06These are actually database files.
03:08This is what database files look like in MySQL, in the default format.
03:13So now we're going to come over here, I have my local C disc up.
03:16We're going to navigate to XAMPP folder, and the MySQL folder, and then
03:22Data folder, and then inside this these are the different databases that we
03:25have installed here.
03:27I'm going to go to the MySQL database, which is where the system files are kept,
03:30and you see here there are no time zone files.
03:34Now, you may see time zone files and still have this where it's not working,
03:38and that's okay, you can replace those files, because those are default files
03:43that just have a minimal amount of information in them, but they don't have the full database.
03:47See here where the name database is 106 K, the transition database is 600 K for each file.
03:53So these are the files that we want.
03:54I'm going to go ahead and select all of these, and copy these over.
03:58I'm going to move them over, and that's okay, because we have them archived in
04:03the ZIP file already. We'll close that.
04:06We'll start the database up again, Start MySQL, there it says Running.
04:12We'll go ahead and open the command line client.
04:16We'll take a look at that variable.
04:20Now, the value hasn't changed.
04:26It still says SYSTEM, but I'm going to go ahead and set it to something
04:28else, and it worked.
04:38Now, when I take a look at the variable, you'll see that it's been changed, and
04:45we're now in the Eastern time zone.
04:48If I use the test database, I've still got my time zone test table.
04:57I see that value is the Eastern value, and just to make sure, I'll go ahead and
05:01set my variable back to the default.
05:07SYSTEM is the default like that, and select from the database again.
05:17This is working as expected.
05:18I'm now in my default time zone, which is the US Pacific, and when I had it set
05:23to US Eastern it gave me that value.
05:25We've successfully installed and tested the time zone database in MySQL on
05:29the Windows operating system.
Collapse this transcript
Installing time zone support in MySQL on a Mac
00:00Okay, so let's install the Time Zone Database in MySQL on the Mackintosh side.
00:05The first thing we want to do is to confirm that we actually need to do this.
00:09So let's open our terminal and start up the MySQL command-line interface.
00:13So, we'll start by taking a look at the time zone variable and this says
00:31SELECT @@time_zone.
00:33The double @ sign means it's a system variable, and don't forget to put in the
00:36semicolon because this is the command-line interface.
00:39We see that it's set to system, which is as it should be.
00:43So, let's try to change it.
00:44So, I'm setting time zone to US/Eastern which is a value that I know should be
00:57there for time zone databases intact, and we get unknown or incorrect time zone.
01:02So, we know that we have a problem.
01:04We know that the timezone database is not as it should be.
01:08Let's just take a look and see if the database exists and is empty, or it
01:13doesn't exist at all.
01:14And really, either way, we are going to need to replace it but let's just take a look.
01:26So the database is named time_zone_name and we are just going to take a count from it.
01:30It should be several hundred entries, although sometimes it comes installed with
01:35no entries, empty, not existing at all, or just a few entries.
01:39And in any of these cases, we'll need to update it.
01:41I need to select the MySQL database, use MySQL and then we'll do that command again.
01:48I'm just pressing the Up arrow a couple of times to get that, and there we have it.
01:52We have a database installed but it's empty.
01:54And this is common and it just means that we need to go ahead and install the
01:58time zone databases.
01:59So let's exit from here and exit our terminal.
02:02Bring up the web browser and go ahead and download the file.
02:06So we are going to http://dev. mysql.com/downloads/timezones.html.
02:17All right, so here is the MySQL page where you can download the time zone
02:24description tables and you will see there is two links down here.
02:27One of them says preferred.
02:29So, I'm going to click on that one.
02:30I will go ahead and save the file.
02:33I have my Firefox set to save files on my Desktop.
02:37Go ahead and quit Firefox and there is the file.
02:40It's a zip file, which means that on a Mac I can just double-click on it and it
02:44will expand the zip file into this nice handy folder, and I'll go ahead and
02:48double-click on the folder and there is the time zone files.
02:51These are what database files look like in MySQL.
02:55So, now we want to go and install these in MySQL.
02:57Now, this is going to be a two-step process.
03:00The first step is to find the place where they go, and this is in
03:03Applications/XAMPP/xamppfiles/var/ mysql and this is where the MySQL databases
03:16are, and you will see we have world, test, mysql.
03:20The mysql database is where MySQL's system tables are and you will see, go down
03:26here, we'll see there is the time zone files, all right.
03:28So, I'm just going to take these files here and select them all.
03:33I pressed Command+A to select them all.
03:36I'm going to drag those over.
03:40This copy dialog comes up and it says that the files already exist.
03:43So, I'm going to Apply to all and Replace. Oops!
03:48So, before I copy these files over, I have to stop MySQL.
03:55Bring up the XAMPP Controls and press Stop.
03:57MySQL is not running and now I can simply drag these files over.
04:05And I get this Copy dialog that says that the files already exist.
04:11So Apply to all and press Replace.
04:14And there we have it.
04:16Files have been copied.
04:18Unfortunately, the way they come from the factory, these files have the wrong
04:22permissions and they will not work.
04:23So, we are going to need to do a little bit of surgery.
04:26It looks a little bit complicated.
04:28It's not that hard if you just follow along, and you only need to do it once.
04:32So, there is the good news.
04:34First, we are going to use the cd command.
04:36I brought up the terminal and we are using the command-line now in the UNIX side
04:40of OS X. So, I'm using the cd command, which changes directory, and I'm going to
04:45cd to this directory over here where all these time zone files are.
04:50So that's in Applications.
04:52I just need to type a few letters and I can press the Tab key, and it will
04:56complete that for me.
04:57And the XAMPP, press the X and press Tab, xamppfiles/var/mysql/mysql.
05:07So, that's the cd command and I press Enter, and now I'm in this directory where
05:11those time zone files are.
05:12I can say ls time*, like that, and I can see those files.
05:17So I need to use the chmod command.
05:21It's a unixism for changing the permissions on the files.
05:26I want to see what the permissions are.
05:27I can use ls -l and I see that these permissions over here are not
05:35sufficient for what we want.
05:36So, we are going to say chmod 775 time*.
05:43So, that will change all of these files to the permissions 775.
05:48Essentially, it means that more of these bits will be turned on and we would
05:51be able to use them.
05:53Press Enter, and we notice that the icon shifted over here when I did that.
05:57And now if I just use my Up arrow a couple of time and get this ls command,
06:02press Enter, we'll see that now a lot more of these bits are turned on and these
06:06files are now actually usable by MySQL.
06:10So, we can just type exit and we can close this.
06:14And then we can close this and then we can turn On MySQL again, almost there.
06:20That's running and let's go and test it.
06:25So, we'll start up the MySQL command- line client, and I'm going to use mysql and
06:38SELECT COUNT(*) FROM time_zone_name;
06:44and there we have it, 546 rows. This should work.
06:49Let's go ahead and test it. use test;
06:52SELECT * FROM tztest;
06:57There we have it with our local time zone, and I'm going to change my time zone.
07:03SET time_zone = US/Eastern;. Oh!
07:11Look at that. It worked.
07:12Now, I'll just Up arrow a couple of times and do my SELECT and look at that.
07:17The time zone is different.
07:18Now, this time stamp is stored in the database in universal time.
07:23So, when I change my local time, it's able to translate that into a
07:27different time zone.
07:28When I change my local time zone back, SET time_zone = SYSTEM;.
07:37Now, I select from the tztest and we'll see that the time zone has changed back.
07:41Our time zone support is now working.
07:43We have successfully undated the time zone database for MySQL on the Mac.
Collapse this transcript
Aggregate functions
00:00MySQL has a few unique aggregate functions or aggregate function usages.
00:05Most of the aggregate functions that are available in MySQL are also available
00:09on other platforms and are actually part of the standard.
00:12So we are going to focus in this lesson on those differences of MySQL.
00:15If you are interested in knowing more about the rest of the aggregate
00:19functions, take a look on my MySQL Essential Training course here on Lynda.com
00:23for a more complete reference.
00:30The most common aggregate function is the Count function, I'm sure you've
00:33seen it many times.
00:35I'm going to go ahead here and select the world database, and what we'll have
00:38here is a count of all the rows in the country table in the world database.
00:43Of course, you've seen this before many times.
00:47The Count function in MySQL also accepts the DISTINCT keyword, and what this
00:54will do is will count the number of distinct values in the Region column in the
00:59country table in the world database.
01:03So we see that there are 25 distinct values in the Region column, and of course
01:07that means that there are 25 distinct regions in the country table.
01:11Now you may want to know how many countries are there in each region.
01:15You can do this like this.
01:18SELECT Region, COUNT (Region), FROM Country, GROUP BY Region, and what this will
01:30give you is a count of all of the different values in the Region column for each
01:37grouping of region by different region values.
01:41So for each value of region, it will count the number of rows in that group, and
01:48that will give us the number of different countries in each region.
01:52And so again the way this does is by grouping by region, what it does is it
01:56groups all of the different rows that have a single value of region and counts
02:00those, and so there are five rows that have the value of Antarctica in region,
02:06and there are 24 rows that have the value of Caribbean in region.
02:11And so by grouping them together this way, we can get a count within each group
02:16of how many rows there are with that value.
02:20And so that tells us how many countries there are in each region.
02:23Now that we have this data, maybe we'd like to know the names of all of those
02:27countries in each region.
02:29And for this we have the GROUP_CONCAT function, and that looks like this, SELECT
02:40Region, GROUP_CONCAT.
02:42That's the name of the function with the argument of (Name) FROM Country and
02:46GROUP BY Region, and what this will do for each region, it will list in one
02:51value the names of all the countries separated by commas.
02:55Now this is a very powerful grouping aggregate function.
02:59You see, Antarctica we have these countries, the Caribbean we have all of these countries.
03:05Now there is a couple of things about this that I don't like.
03:08I don't like the uses of single comma and no spaces to separate them, because
03:12that makes things like this look strange.
03:14Virgin Islands, U.S., Turks.
03:17You don't even realize that Virgin Islands, U.S. is the name of a country.
03:22We can see because it's got this space after the comma that it's not that kind
03:26of a comma, and so we'd like to change that, and also the names of the countries
03:31are not in any order.
03:33So we'd like to order those as well.
03:34The GROUP_CONCAT function is very flexible in these ways.
03:38We can put in here ORDER BY and we can also specify a Separator.
03:47And what I'm going to do for a Separator is I'm going to use (space) / (slash)
03:50(space), so that it doesn't get confused with the commas that may already exist
03:55in some of the names.
03:56This will order the list within the group by name.
04:02So it will be in alphabetical order, and it will change the Separator from this
04:05bare comma to something that's a little bit more readable. Here we go.
04:10So now if we look at the Caribbean, it starts with Anguilla, and Antigua
04:15and Barbuda, and Aruba.
04:17So it starts with the As and it ends with Virgin Islands, U.S. Virgin
04:20Islands, British as well.
04:24So this is a list that we can actually use.
04:27Those are the aggregate functions that are different or unique in MySQL.
04:32Most of the aggregate functions that MySQL supports are available in other
04:36platforms and are documented in SQL Essential Training course here on Lynda.com.
04:41What we are focusing on here are the ones that are different or unique to MySQL.
Collapse this transcript
Full-text search
00:00MySQL has a very simple, and very powerful full text search feature.
00:05So let's take a look at how that works.
00:07I'm going to create a table in the test database.
00:38So this will create a table with three columns in it, id, title and body.
00:43The id column is auto increment, the title column is a string, and the body
00:49column is a text column.
00:51The word FULLTEXT specifies a type of index, which is used with the FULLTEXT searches.
00:57So this creates two full text indexes, one on title, and one on body.
01:01Let's go ahead and create the table.
01:05So our table is created, and now let's go ahead and insert some data.
01:11So we're going to insert a few rows of data and there are these little articles
01:45with just a little line of text in them.
01:50And we just modify this for each one. There we go.
03:25So we've got six articles in our table now, let's take a look at them, and there they are.
03:36Now let's do some searches on them.
03:50So we have a WHERE clause here for the FULLTEXT search, and we are using two
03:54keywords we haven't seen before, MATCH and AGAINST.
03:58MATCH specifies the columns to use for the FULLTEXT search, and AGAINST
04:03specifies the search term, in this case the word database.
04:08So let's go ahead and see what happens.
04:11So we see we have the word database in two results.
04:15One has exactly the same case as the one that we've searched for, and the other
04:21one has different capitalization.
04:24It has a capital D and a capital B. And so we can see that we've found both
04:28of those results, and this is called a natural language search, and it's a
04:33very simple search.
04:34It's interesting if we had another word here, say MySQL, this doesn't change our
04:42results at all, because the word MySQL appears in every single article, and so
04:48the natural language search is specifically designed to not return results for
04:54search terms that exist in more than 50% of the rows in the database.
05:00And this is actually a very important feature, because if you were to choose
05:04search for a bunch of words, and one of those words occurs in say 80% of the
05:10possible results, you are going to have two new results.
05:13So you are not really going to be able to find what it is you are looking for.
05:17So the natural language search ignores those results for search terms that occur
05:23in more than 50% of the rows in the database.
05:27On the other hand, if we look for the word tutorial with database, this will
05:32actually expand our results a little bit, because the word tutorial occurs in
05:38this row here, as well as in this one that has word database in it as well.
05:42If we look for the word management, database management, we'll see one of our
05:50results has both of those words in it, and it comes up at the top.
05:54And one of them also just has the word database, and so it finds both of those results.
05:59If we want to find results that only have both the words database and
06:03management, we need to do a different type of search and it's called a
06:06BOOLEAN MODE search.
06:07We put the words here IN BOOLEAN MODE, and then we have to put symbols on the
06:13words that we want to, make sure that we match.
06:15This symbol is called the add symbol, and it make sure that both of these terms
06:21exist in any returned results.
06:23So this should just return this one or with the id one, this first row.
06:29And there we have it.
06:31On the other hand, we can look for results that do not have the word
06:35management in them.
06:37Just the word database but specifically not the word management, and that will
06:43return just this row, which has database but does not have management.
06:47Finally, there is a third type of search that is supported by the MySQL
06:51FULLTEXT search engine, and this is called WITH QUERY EXPANSION, and this is
07:02actually very interesting.
07:04You'll notice that we have three results and one of them does not have the word
07:09database in it at all.
07:09We're searching for the word database, but we're searching for a WITH QUERY
07:13EXPANSION, and it has given us three results and it thinks we might actually be
07:17interested in this third result, as well as the ones they have the word database in them.
07:22And the reason for that is and how it does this search.
07:26So the QUERY EXPANSION search works like this.
07:29It goes two passes through the database.
07:31The first pass, it looks for the search terms that you've provided and then it
07:36takes the results from those search terms, and finds words that are common in
07:41those results, and then does another search for those terms.
07:46So in our first results it found some other words.
07:48It found Tutorial, and it also found Management and System and following and
07:53comparison, and it didn't find any of those elements, but it did find the word Tutorial.
07:58And so it thought that result just may be relevant.
08:00We'll throw that in at the bottom, and see if you are interested in that result as well.
08:06So this is called QUERY EXPANSION.
08:08It's actually a very useful tool, and it will help to find results that maybe
08:12you might not have thought of when you are searching a large database of text.
08:17So in a nutshell, that's how FULLTEXT search works.
08:20I encourage you to create some tables and fill them with large amounts of text,
08:24and experiment with this feature because it's actually very powerful, and very simple to implement.
Collapse this transcript
8. PHP's MySQLi Interface
PHP programming interfaces
00:00PHP provides several interfaces for using MySQL.
00:04The original interface will simply called MySQL.
00:07It is modeled closely after MySQL's basic C language interface, and is still
00:11widely used, especially by programmers with the background in C.
00:15The MySQLLi interface was introduced in PHP Version 5.0.
00:19It's designed for a newer versions of the MySQL database engine.
00:23It's also object-oriented and it's being actively developed.
00:27If you're doing new development using modern versions of MySQL in PHP, I
00:32strongly recommend that you use MySQLi instead of MySQL.
00:37The PDO interface is a newer, more modern object-oriented multi-platform
00:42interface that supports more than just MySQL.
00:44That supports other database engines as well.
00:48This is the interface that I prefer and the interface that I do my new
00:52development, when I'm using MySQL with PHP.
Collapse this transcript
Introducing CRUD
00:00As we talk about the MySQL interface in PHP, I want to show you some code, and
00:06use this code as an example to explain how it works.
00:09And this code is in a program called CRUD, which does the Create, Read, Update
00:14and Delete functions in a database.
00:17It's the most basic kind of a database program, and I created it for an example,
00:23so that we can illustrate these basic functions.
00:25So, we are going to go ahead and install CRUD first, so you can see how it works.
00:28And so that you can use it and test it, and as you play with the code.
00:32So, this is the XAMPP directory in the local C drive.
00:34I'm going to double-click there, and put it in htdocs, and just copy the program over.
00:44This is exactly how we did the SID installation, and for those of you who are on
00:49a Mac, you can just do exactly what you do with SID.
00:51I am going to go into the CRUD directory, and I'm going to edit the CRUD
00:56program, and make sure we put in our web password, foo.baz, just like we did
01:03with SID and with CRUD there is actually a second copy crud-mysqli which is for
01:11the MySQLi interface.
01:12I'll right-click on it, click on Edit with Notepad++, and put in the password.
01:21Now we can go ahead and run it in the web browser, and it's in the same place.
01:29It's called CRUD, and crud.php.
01:33So this is the CRUD application.
01:40It uses the album database, and you can see it's got a list of albums, and
01:44inside each of these albums there is a list of tracks.
01:50And so, this is basically how it works, you can add a new album, and album
01:58must have an artist, so new artist, and you can add tracks, track number 1,
02:07this is track one, and it's in 12 minutes and 32 seconds, and a track two,
02:17this is another track.
02:20It's 3 minutes and 16 seconds, and so on, and say Done, and there it is new
02:34album, new artist etcetera. Here it is.
02:36You can delete tracks.
02:38Here is a little Confirmation page.
02:43You can delete whole albums.
02:45See that this deletes the album and all of its tracks.
02:49Now, what we are going to do is we are going to look at the code that makes
02:52all of this work, and see how that code is written, and how it interfaces with
02:57the MySQL database.
Collapse this transcript
Connecting to the database
00:00So, let's take a look under the hood in the CRUD Application.
00:04This is a special version of CRUD that uses the MySQLi interface.
00:08The version of CRUD as I originally wrote it, uses the PDO interface and that's
00:13the interface that I prefer, and that's the version that I'll maintain.
00:15I created this separate version to demonstrate how the MySQLi interface works.
00:21So, this is the MySQLi version of CRUD.
00:25This is the line here where the database is initialized.
00:28This is where we set up the connection to MySQL.
00:32So, it creates a MySQLi object.
00:35We are using the Object Interface of MySQLi.
00:37MySQLi has both an Object Interface and a Procedural Interface or a
00:43non-object interface.
00:44We are using the Object Interface to MySQLi.
00:47The code looks a little bit different, but it works pretty much the same, and if
00:51you can follow along with this, if you see code that's Procedural using MySQLi,
00:54it should be obvious what it does.
00:57So, this sets up the MySQL connection.
01:00It provides the hostname, the username, the password and the database.
01:07The hostname is a variable, and that's setup here.
01:10It set to be localhost.
01:13It set to literal value localhost because CRUD is not designed for,
01:17production environment.
01:19It's only designed for a personal testing environment.
01:22So, we really only want to run our localhost.
01:25We are not going to run it on a remote host ever.
01:26It sets up the database, is also setup to album here.
01:33The username, and the password, and the database are all set up at the top of the program.
01:40And here they are, and those are constants, they are not variables.
01:45So, a couple of things to note about the MySQLi interface here.
01:48One is that MySQLi does not support persistent connections with the
01:53Default MySQL driver.
01:55Persistent connections are very useful in a web environment.
01:58What a persistent connection is, is a connection between the web server and the
02:02database server that stays active, and stays alive and does not need to be
02:07created every time there is a web connection.
02:09Without persistent connections, every time somebody brings up a webpage, even if
02:13it's the same user in the same session.
02:16From page to page, there is a whole new instance of the web server, there is a
02:20whole new instance of the program.
02:22And that new instance actually has to create its own connection to the database server.
02:27And this takes time and resources.
02:30Persistent connections reuse the connection, so they don't have to be
02:33setup every time, and that every time can be a lot, especially in a busy web environment.
02:38So, I consider persistent connections viable, and they are not supported by
02:42MySQLi in the default configuration.
02:44And there is actually very few configurations where it will work.
02:48Another thing to keep in mind is that there is a bug in versions of MySQLi
02:53before PHP Version 5.3 that do not allow the use of the Connect_error function.
03:03So instead we have to do our errors this way.
03:05So this is just the buggy nature of the code.
03:08MySQLi is still being actively supported and developed, but not very actively.
03:15The developers are really paying more of attention to PDO and so that's the
03:19more reliable code.
03:20So, we have to do a few little things like this to work around some of
03:23the problems in MySQLi.
03:25So, this is the code that sets up the connection.
03:28It's actually very simple.
03:29You create an object using the standard new object syntax in MySQL.
03:35And it puts the object in the variable called dbh.
03:38It's called dbh for traditional reasons.
03:41That's just the traditional name of a database handle.
03:43So I like to use traditional names for things, because it's easier to read the
03:47code later when I come back to it, and I haven't seen it for a while, I can know
03:52pretty much instantly what it is that it's doing.
03:54So this is how the connection is set up.
03:57If the connection setup fails it calls the Error function, which is an
04:00internal function that exists in the CRUD Application, for displaying errors
04:05in red on the screen.
04:06And it uses this @ sign to prevent a PHP from throwing an error to the screen,
04:11and then it allows us to use our own error reporting mechanism.
04:14So, that's pretty much it.
04:15That's initialization code for setting up the database connection using MySQLi in PHP.
Collapse this transcript
Prepared statements
00:00This is the function update_album_SQL.
00:04This is where the SQL lives for updating the Album table in the album database
00:10for the CRUD Application.
00:12One of the significances of this piece of code is that it introduces us to
00:18prepared statements and bound parameters.
00:19We are going to talk a little about that here.
00:21And talk about how this works.
00:23So, here is the query.
00:25You can see it's standard SQL, UPDATE album, SET title, artist, label etcetera.
00:31You'll notice that the values that are being set to these column names are
00:35question marks, and those question marks correspond to this bind parameter
00:41function call and each of these variables title, artist, label, released and
00:47id which is hiding off to the side there.
00:50These question marks get replaced with these values.
00:54The bind parameter function in the MySQL drivers actually go to the trouble of
01:00properly quoting these for inclusion in the SQL.
01:04So, that you don't have to worry about that, and that's a big convenience.
01:07It also prevents errors, and makes it work more reliably in runtime, to have the
01:14system doing that, rather than you having to do that manually in the code.
01:18So, that's what prepared statements are, and that's why they exist.
01:21They also allow some other efficiencies.
01:23There are some cases where you might be able to prepare a statement and then
01:27loop bind different parameters, to the same statement without having to prepare
01:31the statement, over and over and that can create some efficiencies.
01:34But the major advantage here is in the reliability and scalability of having
01:39these parameters automatically bound to the placeholders in the code.
01:43So, I like prepared statements.
01:45One of the things that disappointed me about the MySQLi interface is that you
01:49cannot use prepared statement in all of the different contexts.
01:53So that's one of the reasons why I choose PDO over MySQLi for my work.
01:57But this is how it works in MySQLi and it's very useful.
02:01You'll notice in the bind parameter the first argument of the bind parameter
02:05function specifies which types these different variables are.
02:09So, we have ssssi, which means string, string, string, string, integer.
02:14And that tells it, how to do the coding.
02:16So, there you have it.
02:17That's how the bound parameter and the prepared statements work in MySQLi in PHP.
Collapse this transcript
Fetching results
00:00So, let's take a look at how you execute a query and fetch the results.
00:05This is get_albums_sql and it's a simple function that just assigns a query.
00:10SELECT * (everything) from the album ORDER BY title and executes that query, and
00:14returns a handle to the results, so it does not actually return the results, and
00:19this is an important distinction.
00:21Query Result Handle.
00:22qrh stands for Query Result Handle.
00:24There in the comments is a handle to be executed query and allows you to fetch
00:32the results one by one.
00:34The importance of this is that if your table is very, very large, and you don't
00:39know how many albums somebody might have in their albums database, there may be
00:42more data that it would even fit in memory in the computer.
00:47If you go and load up a variable with more data then it will fit in memory,
00:50well, something is going to give, and it could create security problems.
00:53It could create stack overflows.
00:55It could crash your computer.
00:57So, instead what we do here.
00:58As we perform this query and it gets all set up and then the results are simply
01:03grabbed one by one off of the disk.
01:05And that happens up here in the list_albums function.
01:10It calls get_albums_sql, so it gets the query result handle and then down here,
01:15it grabs the first row and that way it can test if there is no first row, then
01:19we know that there is no albums in the database.
01:21And otherwise, we eventually get down here, and we set a row_count.
01:27We go ahead and build the results, album_result_row.
01:31That's a function that builds the HTML for the result from the row, and it
01:35increments the count, and then goes ahead and fetches more using this Fetch
01:39Associative Function.
01:40The Fetch Associative Function is great because it assigns an associative
01:45array to the result.
01:46That associative array has all of the column names in the keys.
01:49So that's really, really useful.
01:52So, this is how you do a query and get the results.
01:55You don't want to call some fetchAll function that's going to load up your
01:59memory with some unknown amount of data.
02:01That way lies tragedy.
02:04Instead, what you want to do is be smart about it, execute the query and then
02:07fetch the results one by one.
02:09So, that you can deal with them in bite-size chunks, and they will fit in memory
02:12one at time and this is how that's done in MySQLi in PHP.
Collapse this transcript
Auto-increment IDs
00:00So, this is how we insert a row into the database.
00:03This is insert_album_sql.
00:05This is for creating a new album record in the database.
00:08Here is our query, INSERT INTO album, and we got title, artist, label,
00:12released and the values are replacement parameters that are given in this bind
00:17parameter function.
00:18So, what we have here is a prepared statement.
00:20Now, the interesting thing is at this particular table has an Auto Increment ID
00:27Field, so you notice that we don't insert an ID number.
00:29The ID number is automatically generated.
00:32And yet when we are done with this function, we want to be able to pass the ID
00:36number back, so that it can be used in other context because that's important.
00:40That's how we join our tables together.
00:42So we need to keep track of that.
00:44So, there is a special function in MySQL called insert_id that returns the last
00:50created Auto Increment ID.
00:53So, this is generates an Auto Increment ID.
00:55MySQL keeps track of it, and allows us to grab it here, and return it as the
00:59return value of this function.
Collapse this transcript
9. PHP's PDO Interface with MySQL
PDO is multi-platform
00:00In this chapter we are going to look at the code that is used to create the CRUD Application.
00:05And this is the PDO version of the CRUD Application.
00:08This is the version that I intended to write, this is the version that I'll
00:12continue to maintain and this uses the PDO Interface in PHP.
00:16If you want a little tour around the CRUD Application, look at the Intro movie
00:19for the MySQLi Version in the last chapter.
00:22This is the CRUD Application, and this is the CRUD code.
00:29It's written in PHP, and it uses the PDO interface through the MySQL database.
00:34So let's go ahead and get started and start looking at the code.
Collapse this transcript
Connecting to the database
00:00So, this is the initialization code in the CRUD application, using the PDO interface.
00:05The PDO interface is the one that I chose for this application, so this is the
00:09code that I actually use and maintain.
00:12Here is where the connection to the database happens and it's a little bit,
00:16more complicated and little bit different than the interface from the MySQLi interface.
00:22It creates a new PDO object using the new and it names that object dbh, which is
00:28a traditional name for a database handle and it uses this string for
00:32initializing the database.
00:33This is a carry over from the Perl interface that PDO is based upon.
00:37It's called DBI and Perl.
00:39It's very common, very popular and it works with multiple different databases here.
00:43So, you specify which database you are using.
00:45In this case it's MySQL.
00:46This will work with Postgres or with Oracle or Microsoft SQL server or all kinds
00:52of different databases, using the same code.
00:54You specify your host, you specify your database name and we use the
00:58constant MYSQLDB from that.
01:00That's initialized at the top of the code and username and password they are
01:04also constant and initialized at the top of the code, and here we specify that
01:08we are going to use the Persistent Connections.
01:12So, Persistent Connections are a valuable feature and here what a
01:15Persistent Connection is.
01:17Whenever somebody clicks on a web page or they move from one page to another
01:20within the same site and within the same session, that click creates a whole new
01:24connection between the web browser and the web server.
01:26Now, under normal circumstances, that would create a new instance over your
01:30program, your program would have to set up it's database connections and then
01:34destroy the database connections at the end of the program which is just when
01:38they click on another page.
01:39Now, this is resource intensive.
01:42It uses a lot of resources to create and destroy all of these database
01:46connections, which slows things down and makes things seem sluggish.
01:49So, instead with a persistent connection what happens is that web server manages
01:54a pool of connections and every time somebody clicks on a page, one of those
01:59connections can be reused.
02:01It's handed off to the program, they can use that existing connection and then
02:04the connection goes back in the pool when they are done with it. This saves time.
02:07It saves resources.
02:08It makes your application feel snappier to the user and it improves the user experience.
02:13So, we are using these persistent connections and PDO makes these persistent
02:17connections readily available without depending upon a particular driver for
02:21MySQL or for whatever database.
02:24So, this is how the database connection is created and it's very simple, this
02:29is the simple code in the initialization section of the CRUD application using PHP and MySQL.
Collapse this transcript
Prepared statements
00:00This is the update_album_sql function, which updates the album table in
00:04the album database.
00:06You notice the SQL here UPDATE album, SET title = ?, artist = ? etcetera.
00:13The question marks are replaced with actual values later on.
00:17This is called a prepared statement and it uses these tokens and allows the
00:21execute statement later to replace the tokens with the actual values.
00:26That way the system can quote them properly and it can format them properly for
00:30the SQL without you having to decipher is this an integer? Is this a string?
00:36And it allows this all to happen automatically.
00:38So, it's less prone to error, it's less prone to crashing and it's less prone to
00:42problems in general.
00:44It also allows you under different circumstances to prepare one statement once
00:49and to execute it with a different set of values over and over and over again,
00:53which could be another type of efficiency.
00:55So, this function uses prepared statements and we use prepared statements
00:59wherever possible throughout this code.
Collapse this transcript
Fetching results
00:00So, let's talk about how we fetch data from the database using PDO in PHP with MySQL.
00:06This is the get_album_sql function. It's very simple.
00:09All it does is it executes a query and it returns a handle to that query
00:14statement, the statement handle.
00:16The query is simple, SELECT * FROM album ORDER by title and that gets prepared
00:22and executed and what gets returned is called a statement handle.
00:26This statement handle represents the data, but it doesn't actually contain the data.
00:31With a statement like this SELECT * FROM, you could end up with a lot of data
00:36depending on how many albums are in the database or how many tracks are in the
00:39database, if we were doing this with the tracks.
00:41You could easily fill up memory in the server and you don't want to do that.
00:45Instead what you want to do is you want to be able to execute that query and
00:50then take the results one by one.
00:52That's what this does.
00:53This returns a statement handle, which represents a pointer into those result
00:57sets, and then way up here where this gets called into list albums, the first
01:02thing it does is it calls that get_albums _sql and it takes that statement handle
01:06and it's starts fetching rows.
01:08It fetches the first row separately and it fetches it using this
01:11FETCH_ASSOCIATIVE flag so that the row variable is in an associative array where
01:17each of the column names are key names in the associative array.
01:22So now you know the names of the columns and then down here in this block of
01:28code we actually loop through grabbing the result rows one at a time and
01:34building them onto the screen using this album_result_row function which
01:38actually builds the HTML to put it up on the screen.
01:41So this is the efficient way to do this and it's the safe way to do this where
01:45you execute the query once that sets up the interface to start returning result
01:52rows and then you grab the result rows one at a time out of the database rather
01:57than collecting them all in memory and potentially having a problem with
02:00crashing the program.
02:02So this is how you fetch results from a database using PDO and PHP in MySQL.
Collapse this transcript
Auto-increment IDs
00:00Let's take a look at how you insert data into a table that has an auto
00:04increment id field.
00:06This is the insert_album_sql function, which inserts data into the album table
00:11and here is the SQL INSERT INTO album, title, artist label, released and it's a
00:16prepared statement like we've seen before with the replacement parameters.
00:19But the interesting thing here is how the id field is handled.
00:22You will notice that we don't specify it.
00:24We don't insert a value and that's correct, because it's automatically generated
00:28by MySQL when the data is inserted into the table.
00:31But it becomes available when this function called LastInsertId.
00:33So we are able to access it because we actually need it to be able to tie other
00:39data together using that id field.
00:43That's an important piece of our drawing tables.
00:45So we are able to access that and return the value in this id variable.
00:49So it's as simple as that.
00:50This is how you insert data into a table that has an auto increment id.
00:55You are able to access that value with the last insert id function and this
01:00function here returns the value in the id variable.
Collapse this transcript
Conclusion
Goodbye
00:00In this course my goal was to give you a good overview of MySQL's unique
00:04features and tools so that you can get the most out of this freely available and
00:08valuable database management system.
00:10I have shown you how to install XAMPP, a complete package with MySQL, Apache, and
00:14PHP, how to use MySQL's powerful command line interface, how to use SQL features
00:19that are unique to MySQL, and how to code from MySQL and PHP.
00:23I have also provided you with some useful tools in the SID and CRUD applications
00:28and you have the source code for both of them.
00:30So you can use them to learn how to create your own useful tools and applications.
00:34I have covered the most common and useful features here, but there is a lot of
00:38interesting ground left for you to explore, if you are inclined to do so.
00:41I strongly encourage you to play around with what you have learned, experiment
00:44to try different things and different combinations of things.
00:47MySQL is a powerful and complex system with lots of interesting nooks and
00:51crannies to keep you engaged in the process of learning for many interesting
00:55hours of experimentation.
00:56I have really enjoyed creating this course for you and I sincerely hope all of
01:00this is as useful for you as it has been fun for me.
Collapse this transcript


Suggested courses to watch next:

SQL Essential Training (2h 22m)
Bill Weinman

PHP with MySQL Beyond the Basics (10h 27m)
Kevin Skoglund


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,141 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked