Vous êtes sur la page 1sur 5

MySQL Help

How to start with MySQL


1. To login to MySQL command line client, enter password test.
2. Now, you will be able to see the MySQL command prompt.
Mysql>
3. Let's try out a couple of commands. Type the following at the mysql> prompt, then press
Enter:
select now();
This tells MySQL to get the current date and time and display it
4. Now try another command:
show databases;
This command lists all the MySQL databases on your computer.
Creating a database
Let's create a simple database for an imaginary book store. At your mysql> prompt, type the
following and press Enter:
Mysql> create database bookstore;

If all goes well, you'll see something like this:


Query OK, 1 row affected (0.05 sec)

MySQL has now created your database. You can check this by typing show databases again:
mysql> show databases;
+--------------------+
| Database

+--------------------+
| information_schema |
| bookstore
| mysql

|
|

| performance_schema |
| test

+--------------------+
4 rows in set (0.00 sec)

Congratulations you've just created your first MySQL database!


Some SQL basics
All of the commands you've issued so far select now(), show databases, and create
database bookstore are SQL statements. SQL, or Structured Query Language, is the
language you use to communicate with most DBMSs, including MySQL. Using SQL, you can
create and delete databases and tables; insert new data into tables; update data; delete data; and
retrieve data.
Statements that retrieve data from a database are also commonly called queries, hence the name
"Structured Query Language".
You'll use SQL in the rest of this tutorial as you create a table in your new database, add a record,
and retrieve a record.
Creating a simple table
Let's set up a books table to hold books in our book store:
Book(id, title, author, price)
USE bookstore;
DROP TABLE IF EXISTS books;
CREATE TABLE books
(
id
record

int unsigned NOT NULL auto_increment, # Unique ID for the

title

varchar(255) NOT NULL,

# Full title of the book

author

varchar(255) NOT NULL,

# The author of the book

price

decimal(10,2) NOT NULL,

# The price of the book

PRIMARY KEY

(id)

);

Let's take a look at the SQL statements in this file and see what they do:
USE bookstore
This tells MySQL to switch to the bookstore database that you created earlier. MySQL
will then carry out all further operations on this database.
DROP TABLE IF EXISTS books
This deletes any previous books table from the database, since you can't redefine a table if
it already exists.
Be careful when using DROP TABLE. When you delete a table like this, any data in
the table is gone forever!

CREATE TABLE books ( ... )


This statement creates a new table called books. The stuff in between the parentheses
defines the table's fields and its primary key, as we'll see next.
id int unsigned NOT NULL auto_increment
The first field we define is id. This is a special type of field that assigns a unique numeric
ID to each book record in the table. Most of the time, you'll want your table to have a unique
field of some sort, so that you can easily identify a particular record. We give the field an
int unsigned type, which can hold large, positive integer numbers. We also add the
auto_increment attribute to the field now, whenever we add a new record to the
table, MySQL will automatically assign a new, unique value to the record's id field (starting
with 1).
The NOT NULL constraint prevents the field containing NULL values. In MySQL,
NULL is a special type of value that can be useful in some situations. However, it can also be
quite confusing for beginners, so we won't use them in this tutorial.
title varchar(255) NOT NULL
Next we define the field to hold each book's title. We give it a varchar(255) type, which
means it can hold a text string up to 255 characters long.
author varchar(255) NOT NULL
The next field is the book's author. As with the title field, we give it the
varchar(255) type.
price decimal(10,2) NOT NULL
The last field is the book's price. We give this field a decimal(10,2) type, which means
that the field can hold a 10-digit decimal number, with 2 of the digits sitting to the right of
the decimal point.
PRIMARY KEY (id)
Finally, we create a primary key based on the table's id field. A primary key uniquely
identifies records in the table; a table can have only one primary key. MySQL also creates an
index using the primary key this lets you retrieve a book record extremely quickly by
referencing its id field, even if the table contains millions of rows.
To check that your books table was created, you can type show tables:
mysql> show tables;
+---------------------+
| Tables_in_bookstore |
+---------------------+
| books

+---------------------+

1 row in set (0.00 sec)


You can even inspect the table schema to make sure it's correct. To do this, use the explain
command, like this:

mysql> explain books;


+--------+------------------+------+-----+---------+----------------+
| Field | Type
| Null | Key | Default | Extra
|
+--------+------------------+------+-----+---------+----------------+
| id
| int(10) unsigned | NO
| PRI | NULL
| auto_increment |
| title | varchar(255)
| NO
|
| NULL
|
|
| author | varchar(255)
| NO
|
| NULL
|
|
| price | decimal(10,2)
| NO
|
| NULL
|
|
+--------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Adding records
You've now created a database called bookstore, and added a books table to it. Let's try adding
a record to the books table.
INSERT INTO books ( title, author, price )
VALUES ( "The Grapes of Wrath", "John Steinbeck", 12.99 );

You should see the following output, indicating that MySQL has added the row to the table:
Query OK, 1 row affected (0.06 sec)

Notice that we haven't specified a value for the id field. Since it's an auto_increment field,
MySQL generates the field value automatically.
Let's add another couple of books to the table:
mysql> INSERT INTO books ( title, author, price )
VALUES ( "Nineteen Eighty-Four", "George Orwell", 8.99 ),
( "The Wind-Up Bird Chronicle", "Haruki Murakami", 7.99 );
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

As you can see, you can insert multiple rows at once by supplying multiple sets of field values,
enclosed in parentheses and separated by commas.
Retrieving records
Now that we've added some records to the table, how can we retrieve them? This is where SQL
queries come in. The SQL SELECT statement lets you retrieve one or more records from a table
or even multiple tables at once based on criteria that you supply. The basic syntax is:
SELECT fieldNames FROM tableName [WHERE criteria]
There's a lot more to the SELECT statement than this, but we'll keep things simple in this tutorial!
Let's try a basic SELECT query on our books table using the MySQL Monitor:
mysql> SELECT * FROM books;
+----+----------------------------+-----------------+-------+
| id | title
| author
| price |
+----+----------------------------+-----------------+-------+
| 1 | The Grapes of Wrath
| John Steinbeck | 12.99 |
| 2 | Nineteen Eighty-Four
| George Orwell
| 8.99 |
| 3 | The Wind-Up Bird Chronicle | Haruki Murakami | 7.99 |
+----+----------------------------+-----------------+-------+

3 rows in set (0.00 sec)

This SELECT query retrieves all fields (*) from the books table. Since we haven't supplied any
additional criteria, the query retrieves all the records in the table, and displays the field values in the
MySQL monitor.
As you can see, MySQL has auto-generated the values for the id field, beginning with 1.
What if we want to retrieve just one record from the table, such as the book "Nineteen EightyFour"? To narrow down the selection, we can add a WHERE clause, like this:
mysql> SELECT * FROM books WHERE id = 2;
+----+----------------------+---------------+-------+
| id | title
| author
| price |
+----+----------------------+---------------+-------+
| 2 | Nineteen Eighty-Four | George Orwell | 8.99 |
+----+----------------------+---------------+-------+
1 row in set (0.00 sec)

As well as selecting by the id field, we can select by any other field we like:
mysql> SELECT * FROM books WHERE title = "Nineteen Eighty-Four";
+----+----------------------+---------------+-------+
| id | title
| author
| price |
+----+----------------------+---------------+-------+
| 2 | Nineteen Eighty-Four | George Orwell | 8.99 |
+----+----------------------+---------------+-------+
1 row in set (0.01 sec)

We can also use other operators, such as < (less than), > (greater than), and the boolean AND
operator, to retrieve a range of records:
mysql> SELECT * FROM books WHERE price < 10 AND price > 5;
+----+----------------------------+-----------------+-------+
| id | title
| author
| price |
+----+----------------------------+-----------------+-------+
| 2 | Nineteen Eighty-Four
| George Orwell
| 8.99 |
| 3 | The Wind-Up Bird Chronicle | Haruki Murakami | 7.99 |
+----+----------------------------+-----------------+-------+
2 rows in set (0.00 sec)

Finally, instead of retrieving all fields using *, we can specify just the field or fields we want to
retrieve. Here's an example:
mysql> SELECT title, author FROM books;
+----------------------------+-----------------+
| title
| author
|
+----------------------------+-----------------+
| The Grapes of Wrath
| John Steinbeck |
| Nineteen Eighty-Four
| George Orwell
|
| The Wind-Up Bird Chronicle | Haruki Murakami |
+----------------------------+-----------------+
3 rows in set (0.00 sec)

As you can see, SELECT queries make it easy to retrieve just the records and fields you want from
your table.

Vous aimerez peut-être aussi