![]() |
Computer Code and
Tutorials Project Examples Research Don't throw away your vote on the status quo. |
Welcome. These instructions provide new MySQL users with very basic, step by step, instructions on how to get started.
MySQL runs under a broad array of operating systems. The MySQL commands discussed here apply to all platforms. The installation instructions in particular are guaranteed to work if the following conditions are true:
c:
drive in the mysql
directory.
If your conditions are different, the instructions should still work, but keep an eye out for slight differences, like which working directory you'll need to be in.
The commands you need to type are displayed in the bold fixed width
font
. Resulting screen text is shown here in standard fixed
width font
.
If you don't already have MySQL on your system, you'll need to start by getting and installing it.
- Download MySQL-Win32 version of the program.
- Open up Windows Explorer.
- Find the downloaded file and copy it into a temporary directory.
- Double click on the file. At this point, if you have a zip utility, it should start. If you don't have a program to unzip files, you'll need to get one. I'm partial to PKZIP for Windows.
- Once the files are extracted, go back into Explorer and double click on Setup.exe.
- Follow the instructions within the installation program. Note: The do not change the installation directory from the default
c:\mysql
. If you want it installed somewhere else, the Win32 section of the MySQL manual says to install it toc:\mysql
then move it later. Once moved, you'll need to use the--basedir
option when callingmysqld
commands.- Open up a MS-DOS Prompt window. The default location for starting such windows is: Start Menu | Programs | MS-DOS Prompt.
- Switch to the c: drive if you're not already there:
F:\>c:
- Switch into the MySQL working directory:
C:\>cd mysql\bin
- Change the name of the daemon program:
c:\mysql\bin\ren mysqld-shareware.exe mysqld.exe
- Load the MySQL daemon:
c:\mysql\bin\mysqld --install
- Close the DOS Prompt window:
c:\mysql\bin\exit
- Open up the Services Manager
- NT 4.0: Start Menu | Settings | Control Panel | Services
- 2000: Start Menu | Programs | Administrative Tools | Services
- Highlight the line with "MySql" on it.
- If the "Startup" column says "Manual" or "Disabled, click on the "Startup" button, hit the "Automatic" radio button and click OK.
- Click on the Start button.
- Close the Services Manager and the Control Panel.
If you're using Windows 95 or 98, do not run
mysqld --install
. These operaing systems don't have the ability to host a "service." So, you need to run MySQL as a standalone application by executing the commandmysqld --standalone
.I haven't run MySQL on these systems myself, so, for more information, check out the MySQL Mailing List Archive.
Open up a MS-DOS Prompt window
The default location for starting such windows is: Start Menu | Programs | MS-DOS Prompt.Get to the working directory
F:\>c: C:\>cd mysql\binInstruct MySQL to setup a new database
All this really does is create a new subdirectory in your c:\mysql\data directory.
C:\mysql\bin>mysqladmin create database01 Database "database01" created.Startup MySQL
C:\mysql\bin>mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 22 to server version: 3.21.29a-gamma-debug Type 'help' for help.Open the database
mysql> use database01 Database changedCreate a table
mysql> create table table01 (field01 integer,field02 char(10)); Query OK, 0 rows affected (0.00 sec)
Enclose entire list of field names between one pair of parentheses.
Commas are used between each field.
A space may be used after the comma between fields.
A comma is not used after last field.
This, and all SQL statements, are concluded by a semicolon "
;
".List the tables
mysql> show tables; +----------------------+ | Tables in database01 | +----------------------+ | table01 | | table02 | +----------------------+List the fields in a table
mysql> show columns from table01; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | field01 | int(11) | YES | | | | | field02 | char(10) | YES | | | | +---------+----------+------+-----+---------+-------+Congratulations! Pretty straightforward, eh?
Insert a record
mysql> insert into table01 (field01,field02) values (1,'first'); Query OK, 1 row affected (0.00 sec)
Enclose entire list of field names between one pair of parentheses.
Enclose the values to be inserted between another pair of parentheses.
Commas are used between each field and between each value.
A space may be used after the comma between fields.
List all the records in a table
mysql> select * from table01; +---------+---------+ | field01 | field02 | +---------+---------+ | 1 | first | +---------+---------+Excellent!
...one field at a time
mysql> alter table table01 add column field03 char(20); Query OK, 1 row affected (0.04 sec) Records: 1 Duplicates: 0 Warnings: 0...more than one at a time
mysql> alter table table01 add column field04 date,add column field05 time; Query OK, 1 row affected (0.04 sec) Records: 1 Duplicates: 0 Warnings: 0
The "add column" must be restated for each column.
Commas are used between each add column statement.
A space may be used after these commas.
The MySQL Manual fully explains each possible column data type.
Did it work?
mysql> select * from table01; +---------+---------+---------+---------+---------+ | field01 | field02 | field03 | field04 | field05 | +---------+---------+---------+---------+---------+ | 1 | first | NULL | NULL | NULL | +---------+---------+---------+---------+---------+Now we're getting somewhere!
The MySQL command line interface allows you to put a statement on one line or spread it across multiple lines. There's no difference in syntax between the two. Using multiple lines allows you to break down the SQL statement into steps you may more easily comprehend.
In multiple line mode, the interpreter appends each line to the prior lines. This continues until you enter a semicolon "
;
" to close out the SQL statement. Once the semicolon is typed in and you hit enter, the statement is executed.Here's an example of the same exact SQL statement entered both ways:
Single Line Entry mysql> create table table33 (field01 integer,field02 char(30));Multiple Line Entry mysql> create table table33 -> (field01 -> integer, -> field02 -> char(30));
Don't break up words:
Valid Invalid mysql> create table table33 -> (field01 -> integer, -> field02 -> char(30)); mysql> create table table33 -> (field01 inte -> ger, -> field02 -> char(30));
When inserting or updating records, do not spread a field's string across multiple lines, otherwise the hard returns are stored in the record:
Standard Operation Hard Return Stored in Record mysql> insert into table33 (field02) -> values -> ('who thought of foo?'); mysql> insert into table33 (field02) -> values -> ('who thought -> of foo?');Results mysql> select * from table33; +---------+---------------------+ | field01 | field02 | +---------+---------------------+ | NULL | who thought of foo? | | NULL | who thought of foo? | +---------+---------------------+
Add this record
mysql> insert into table01 (field01,field02,field03,field04,field05) values -> (2,'second','another','1999-10-23','10:30:00'); Query OK, 1 row affected (0.00 sec)
Quotes must go around text values.
Standard date format is "yyyy-mm-dd".
Standard time format is "hh:mm:ss".
Quotes are required around the standard date and time formats, noted above.
Dates may also be entered as "yyyymmdd" and times as "hhmmss". If entered in this format, values don't need to be quoted.
Numeric values do not need to be quoted. This holds true regardless of the data type a column is formatted to contain (e.g. text, date, time, integer).
MySQL has a useful command buffer. The buffer stores the SQL statements you've entered thus far. Using it keeps you from having to retype the same commands over and over. Let's use this next step as an example.
Add another record using the command buffer (and optional date and time formats)
- Hit the up arrow key twice.
- Hit the ENTER key.
- Type in the new values between a pair parentheses and stick a closing semicolon on the end.
(3,'a third','more foo for you',19991024,103004);
- Hit the ENTER key.
Voilą!
Is it in there?
mysql> select * from table01; +---------+-----------+------------------+------------+----------+ | field01 | field02 | field03 | field04 | field05 | +---------+-----------+------------------+------------+----------+ | 1 | first | NULL | NULL | NULL | | 2 | second | another | 1999-10-23 | 10:30:00 | | 3 | a third | more foo for you | 1999-10-24 | 10:30:01 | +---------+-----------+------------------+------------+----------+It's in there!
Now, we're almost done...
Modify one field at a time
Again, be careful with syntax. Quote marks need to go around text but not around numbers.
mysql> update table01 set field03='new info' where field01=1; Query OK, 1 row affected (0.00 sec)Change multiple fields at once
Remember to put commas between each field you're updating.
mysql> update table01 set field04=19991022, field05=062218 where field01=1; Query OK, 1 row affected (0.00 sec)So, what's up with our data?
mysql> select * from table01; +---------+-----------+------------------+------------+----------+ | field01 | field02 | field03 | field04 | field05 | +---------+-----------+------------------+------------+----------+ | 1 | first | new info | 1999-10-22 | 06:22:18 | | 2 | second | another | 1999-10-23 | 10:30:00 | | 3 | third one | more foo for you | 1999-10-24 | 10:30:01 | +---------+-----------+------------------+------------+----------+Update multiple records in one stroke
mysql> update table01 set field05=152901 where field04>19990101; Query OK, 3 rows affected (0.00 sec)Survey says...
mysql> select * from table01; +---------+-----------+------------------+------------+----------+ | field01 | field02 | field03 | field04 | field05 | +---------+-----------+------------------+------------+----------+ | 1 | first | new info | 1999-10-22 | 15:29:01 | | 2 | second | another | 1999-10-23 | 15:29:01 | | 3 | third one | more foo for you | 1999-10-24 | 15:29:01 | +---------+-----------+------------------+------------+----------+Wee haw!
The delete command
mysql> delete from table01 where field01=3; Query OK, 1 row affected (0.01 sec)mysql> select * from table01; +---------+---------+----------+------------+----------+ | field01 | field02 | field03 | field04 | field05 | +---------+---------+----------+------------+----------+ | 1 | first | new info | 1999-10-22 | 15:29:01 | | 2 | second | another | 1999-10-23 | 15:29:01 | +---------+---------+----------+------------+----------+
mysql> quit Bye
Now you know some rudimentary commands for running a database in MySQL. Since MySQL is operated by executing SQL calls, you have a broad array of very powerful tools at your disposal. For instance, you're able to display data from several tables at once by joining related fields.
Similarly, SQL permits complex displays, updates or deletions of multiple records which fit specific criteria. So, your next step toward mastery is learning all about SQL.
advertisement ![]()
MySQL, by Paul DuBois, has received excellent reviews. I've read it myself and must concur. He's a consistent contributor to the MySQL mailing list. Please purchase his book through this link to help defray my costs incurred providing this important page.
Please throw it in your shopping cart first, then shop around. If you later decide you don't want it, then take it out.
Thanks!James Hoffman has put a tutorial page up on the web entitled Introduction to Structured Query Language. The General SQL Information of the MySQL Manual lists books recommended by many members of their mailing list.
Another thing to note is MySQL offers good security features you'll need to use when operating on networks.
If you don't find what you're looking for there, or can't stand the massive graphics and advertising, use mailing list archive on MARC.
Folks, please read this disclaimer:
- We are not the company that makes MySQL. It's made by TcX.
- We are not on the development team.
- We put this page up here to help people. It gets over 600 hits per day. That's a lot of help.
- We are unable to respond to inquiries seeking more free help.
Of course, if you're interested in hiring our firm, we'll be glad to hear from you.