MySQL client instruction (mysql.client-side.command)

xiaoxiao2021-03-06  21

Mysql beginners use guide

First, connect mysql: format: mysql -h host address -u username -P user password

1, Example 1: Connect to MySQL on the unit. First open the DOS window, then enter the directory mysqlbin, type the command mysql -uroot -p, then prompt you to lose your password, if you just install MySQL, the superuser root is no password, so you can enter it directly. MySQL, MySQL prompts are: mysql>

2, Example 2: Connect to MySQL on the remote host. Suppose the IP of the remote host is: 110.110.110.110, the username is root, the password is ABCD123. Type the following command: mysql -h110.110.110.110 -uroot -PABCD123 (Note: u and root can no need to bind, other is also the same)

3, exit mysql command: exit (Enter)

Second, modify the password: format: mysqladmin -u username -P old password Password new password

1, Example 1: Add a password to the root AB12. First enter the directory mysqlbin under DOS, then type the following command mysqladmin -uroot -password AB12 Note: Because the root does not have a password, the -p old password can be omitted.

2, Example 2: Change the root password to DJG345. mysqladmin -uroot -Pab12 Password DJG345

Third, add new users: (note: BY "Password"

Example 1. Add a user TEST1 password to ABC, allowing him to log in on any host and have the permissions of all databases, insert, modify, delete. First, use the root user into MySQL, then type the following command: Grant SELECT, INSERT, Update, delete on *. * To test1 @ "%" Identified by "ABC"; but Example 1 Increased users is very dangerous, you I want someone to know the password of Test1, then you can log in to your MySQL database on any computer on the Internet and to see you for your data.

Example 2, add a user TEST2 password to ABC, let him log in in localhost, and can query, insert, modify, and delete the database MYDB (localhost refers to the host host, the host where the mysql database is located) In this way, users can use the password that knows Test2, he cannot access the database directly on the Internet, and can only access the web page on the MySQL host. Grant SELECT, INSERT, UPDATE, DELETE ON MYDB. * to Test2 @ localhost Identified by "abc"; if you don't want Test2 with password, you can fight a command to remove the password. Grant SELECT, INSERT, UPDATE, DELETE ON MYDB. * To Test2 @ localhost iDentified by ""

Fourth, display command

1. Display the database list: show Databases; two databases just started: mysql and test. The mysql library is very important that there is a MySQL system information, we change your password and add users, actually use this library to operate.

2, the data table in the display library: use mysql; // Open the library, learn FoxBase will not be unfamiliar with Show Tables;

3, display the structure of the data sheet: Describe table name;

4, build the library: CREATE DATABASE library name;

5, Jian Table: USE library name; CREATE TABLE table name (field setting list);

6, delete libraries and deletion: DROP DATABASE library name; Drop Table table name;

7. Record the table in the table: DELETE FROM table name;

8, the record in the table: SELECT * FROM table name;

V. An instance of building a library and construction form and inserting data

CREATE DATABASE SCHOOL; / / Establishment School Use School; // Open Library SCHOOL CREATE TABLE TEACHER / / Establishment TEACHER (ID INT (3) Auto_INCREMENT NOT NULL PRIMARY Key, // ID Settings to the main keyword, and automatically set value , That is, when you add, you don't have to write content name char (10) Not null, address varchar (50) default 'Shenzhen', // set the default to Shenzhen Year Date; //

/ / The following is inserted insert INTO TEACHER VALUES ('', 'Glchengang', 'Shenzhen No.1 ",' 1976-10-10 '); // ID No Write Insert Into Teacher Values ​​(', 'Jack' , 'Shenzhen Yizhong', '1975-12-23');

6. Modify the database structure

1. Add a field ALTER TABLE DBNAME Add Column

2, modify the field ALTER TABLE DBNAME CHANGE

3, delete fields ALTER TABLE DBNAME DROP Column

Mysql Beginner User Guide 2

Welcome back to this tutorial! In the previous chapter, we have learned two software installed and configured PHP and MySQL. In this chapter, we will focus on working in the mysql database using the Structured Query Language (SQL).

Database

As I am in the previous chapter, PHP is a server-side scripting language. You can add instructions in your web page through this language so your web service software (probably Apache, Personal Web Server or anything else) Software) Execute it first before sending these pages in a browser that issues a request. In that simple example, I show how to insert the current date each time you accept the requested web page.

Everything is now clear, but if you add a database, you will really lead to us. A database server (we are mysql here) is a program that stores a lot of information in a certain format. With this program you can simply use scripting languages ​​such as PHP to access data. For example, you can use PHP to get a joke list in the database and display it to your Web site. In this example, the joke is fully stored in the database. There are two benefits. First, you no longer need to write an HTML file for each of your jokes, you only need to write a PHP file to bring any jokes from the database and display it; second, you have to add a joke to your Web site, and only just Yes Add a joke into the database. PHP code can automatically display new jokes when new jokes are included in the list.

Now let's take a look at how the data is stored in the database. A database contains one or several data tables, each of which contains a list of things. For our joke database, we will need a data table named "Jokes" at first, and this data table contains a joke list. Each data table in the database contains one or several data columns (Column) or DILDs. Go back to our example, our "Jokes" data sheet may have two columns: jokes and the date of jokes to the database. The jokes stored in the data table are called a row. To understand all the terms mentioned here, you can look at this picture:

Please note that in the two data columns of Joke Text ("Joketext") and the Add Date ("Jokedate"), I also added a data column called "ID". The role of this data column is to assign a unique number to each joke so that we can easily review and distinguish these jokes.

Now review it, there are three data columns in the above data table, two rows. Each line contains a joke ID, its body and its added date. After mastering these basic terms, we will start using MySQL.

Log in Mysql

Operating the standard interface of the MySQL database is connected to the MySQL service software (installed in the first chapter) and simultaneously enter the command. To connect the server, we need to use the MySQL client program. If you install MySQL service software yourself, whether you are installed under Windows, or installing some UNIX version, you should have installed this client program in the same location on the installation service program. In Linux, this program is called mysql, and its location is default

/ usr / local / mysql / bin

table of Contents. Under Windows, this program is called mysql.exe, and its location is the c: mysqlin directory by default.

If you are not installing MySQL servers (for example, you are working on your web host provider), then there are two ways to connect to the MySQL server, the first method is to log in to your web host with Telnet On the server, then run mysql there. The second method is to download and install the MySQL client program from http://www.mysql.com/ (for Windows and for Linux). Both methods can work well, your web host may support one of them, or both support (you need to ask questions).

No matter which method you choose, no matter which system you use, you will eventually execute the mysql client program to connect your MySQL server in a command line, you need to enter the following command:

MySQL -H -u -p You need to change to your host name or IP address of your MySQL server is running. If you run a client program on the same computer of the service, you can use -h and use -h localhost directly. should be your MySQL username. If you are installing the MySQL server, this should be root. If you are using your web host provider's MySQL service, this should be their mysql user specified for you.

"-P" parameter tells the program to prompt you to enter your password, which will display immediately after you enter the above command. If you are installing mySQL, your password is your password for the root selected in the first chapter. If you are using your web host provider's MySQL service, this should be the MySQL password they give you.

If you enter everything, the MySQL client program will connect to the mysql server and return to you a mysql command line:

MySQL>

Now, MySQL servers are actually connected to several databases (this makes a web host to set the same mysql server for several users). So your next step should be a job database. First, get a list of databases on the current server. Enter the following command (don't forget the semicolon!) And then return to the car.

mysql> show data;

MySQL displays a list of databases on your server. If this is a newly installed server (that is, this is you installed in the first chapter). This list will be like this:

-------- | Database | ---------- | MySQL | | TEST | ----------

2 rows in set (0.11 sec)

MySQL servers use the first database that is called MySQL to manage users and their passwords and permissions. Now we will not care about this database, in the later chapter, we will discuss it. The second called Test is a data module. You can delete this database, you will not use it in our tutorial (we will build some database yourself). Deleting something is called "Dropping" in MySQL, to delete the Test database, the correct command should be:

mysql> DROP DATABASE TEST;

If you entered this command and hit it, MySQL will delete this database and return Query OK. Note that information such as "this is determined" is not prompted. So you must be very careful in MySQL. Just like we have seen here, you can completely delete the database - contain all the information on it - just use only one command!

Let's take a look at the connection of the mysql command line before we enter the next step. As we have noted, all commands in MySQL must end with semicolons (;). If you have forgotten this semicolon, MySQL will think that you have not ended your command and will let you continue to enter this line:

Mysql> Show -> Databases;

When Mysql is waiting for the remainder in the command, the prompt will change from mysql> to ->. For a long command, this is useful, you can enter your command a few lines. If you find that your command is wrong in the middle, you can completely cancel the current command (the translator Note: means a command that has not been executed) and is from the head. To do this, you only need to enter C and press Enter: MySQL> DROP DATABASECMYSQL>

MySQL will completely ignore the command you just entered and return to the prompt waiting for your next command.

Finally, when you want to quit MySQL client programs, you only need to enter quit or exit (both commands are exactly the same). This is the only command that can be executed without the end of the semicolon.

Mysql> quitbye

What is SQL?

In this tutorial, we used to tell Mysql to do what is actually a part of the structured query language (SQL). The command in SQL is also called inquiry (in this tutorial, we will alternately use these two names).

SQL is a standard language that implements interaction with the vast majority of databases, so even if you no longer use MySQL, you will use Microsoft SQL Server, you will find that most commands are the same. You must understand the difference between SQL and MySQL. MySQL is the database service software you are using. SQL is a language you use to implement and interact with the database.

Create a database

If your web host provider has assigned a database that you work. You should wait patiently, wait for us to continue to discuss the following problems. If you are working on your MySQL server installed. Execute the following command, you can easily create a database:

Mysql> Create Database Jokes;

My database's name is Jokes, which is consistent with the example of our work. In fact, you can give your database from any name you like. However, if you work on the MySQL server of the web host provider, they may have established a database for you, you can't choose the name of the database.

Now we already have a database, we need to tell Mysql we want to use this database. Below this command should be less difficult:

Mysql> Use Jokes;

Now you can start using your database. Before you add a data table, this database will be empty, our first step should be to create a data sheet to save our jokes.

Create a data sheet

The SQL command we have encountered before, but because the data table is more flexible, it is much more complicated to establish their commands. The basic format of the establishment of a data table is this:

Mysql> Create Table

(-> , -> , > ... ->);

Go back to our example "Jokes" table. This table has three data columns: ID (a number), Joketext (the body of jokes) and JokeDate (add dates). The order to build this table should be like this: mysql> create table Jokes (-> id int not null auto_increment primary key, -> joketext text, -> jokedate date not null ->); look complicated, right? Let us break it down:

The first line is relatively simple; it explains what we want to create a new data table called Jokes.

The second line shows that we need a data column called ID, and this column should be an integer (int). This line also defines some other information on this data column. First, this line does not allow NOT NULL. Second, if you don't specify a value for a column, MySQL will choose to use a value larger than the current maximum (Auto_InCrement). Finally, this data column is still the unique identifier of this data sheet, so all values ​​in this data column should be non-repetitive.

The third line is very simple; this shows that we need a data column called Joketext, the type of this column should be a text (Text).

The fourth line defines our last column. The column name is Jokedate. The type of this column is the date (date) column cannot be empty (Not null).

Note that when we enter the SQL command, the case is completely free, but in a UNIX-based system running Mysql service, because we must match the directory and files in the MySQL data directory, when you encounter database names and tables When we are named, we must be sized. Otherwise, MySQL is completely unoccupied, only one case exception, the table name, column name, and other names that appear in the same command must be exactly the same. We should also notice that we specify a specified type for each column established. ID is an integer, Joketext is a text, Jokedate is a date type. MySQL allows us to define a type for each column. This can not only help you organize data, but you can use it to compare data (we will see below). To get a full list of data types supported by mysql, you can see the MySQL User Manual.

In short, if you enter the above command correctly, MySQL will return Query OK and create your first data sheet for you. If you have an error in the input, MySQL will tell you that there is a problem with the name, and you will give you some prompts, explain where it cannot be understood.

For such a complex command, only Query OK is quite monotonous. Let us see if your new data table is not established. Enter the following command:

mysql> show tables;

The response should be like this:

--------------- | Tables in jokes | ---------------- | Jokes | --- -------------- 1 ROW in Set

This is a list of all tables about our database (we name it in front of Jokes ". This list only contains a data table: Jokes, this table is our just built. It seems that everything is fine, let's take a look at this Jokes table: mysql> describe Jokes;

-------- ------- ---- ---- ------------ - - | Field | TYPE | NULL | Key | Default | ... ---------- -------- ------ --- - ----------- - - | ID | INT (11) | | Pri | 0 | ... | JOKETEXT | TEXT | YES | | Null | | JOKEDATE | DATE | | | 0000-00 -00 | ---------- -------- ------ --- ---------- - 3 ROWS IN SET

This provides a list of data columns. As we see, there are three columns in this table, which is expressed as three lines in the return result table. Details are more difficult to understand, but you carefully look at it, you should still understand the rough meaning. Don't worry about this. We will continue to learn, we will add some jokes in this table.

We also need to know some other things, such as deleting a table. This is the same as deleting a database, simple and horrible. The order is almost the same:

Mysql> Drop Table ;

Add data to the database

Our databases and data sheets have been established, and now we should add jokes to our database. The command to add data to the database is called INSERT. This command has two basic formats:

MySQL> Insert Into

Set -> ColumnName1 = Value1, -> ColumnName2 = Value2, -> ... ->; mysql> Insert Into
-> (ColumnName1, ColumnName2, ...) -> Values ​​(Value1, Value2, ...);

So add jokes to our data table, we can select one in the following two commands:

Mysql> Insert Into Jokes Set -> Joketext = "Why Did To the the Other Side!", -> Jokedate = "2000-04-01"; MySQL> INSERT INTO JOKES -> (JokeText, Jokedate VALUES (-> "Why Did the chicken cross the road? TOGET TOTHER SIDE!", -> "2000-04-01" ->);

Note that in the second format of the insert command, the order of the columns you gives must be consistent with the order you give.

Now you know how to add it to the table, let's take a look at how to display the contents of the table.

Display stored data

Show the command of the stored data in your database, called SELECT, this command is undoubtedly the most complex command in the SQL language. It is said that it is complicated because the most important advantage of the database is that data can be moved. And mobile is achieved by this command. The following command lists all the data stored in Table Jokes:

mysql> select * from Jokes;

This command means "picking all things from Jokes". If you try to enter this command, you will see this display:

-- --------------------------------------------- ------------------- ---------- | ID | JOKETEXT | JOKEDATE | ---- ---- ------------------------------------- --------- ---------- | 1 | Why Did THE chicken cross the road? To get to the other side! | 2000-04-01 | - - --------------------------------- ---------------- ------------ 1 ROW IN SET (0.05 sec)

This looks a little confusing, because the Joketext column is too long. For this reason, we may want to tell Mysql to save the Joketext column, this command will implement this function:

MySQL> SELECT ID, JOKEDATE from Jokes;

This time we no longer tell it "Pick all things", let's tell it what is interested in displaying. The result should be like this:

-- ------------ | ID | JOKEDATE | ---- ---------- | 1 | 2000-04- 01 | - ------------ 1 ROW IN Set (0.00 sec)

But can we display the content of a little joke? As a supplement to the columns we want, we can use the "function" to change the columns we want to display. There is a function called Left, let us tell Mysql to display a few characters when we display a data column. For example, if we just want to display the top 20 characters of the Joketext column:

Mysql> Select ID, Left (Joketext, 20), Jokedate from Jokes;

-- -------------------- ------------ | ID | Left (Joketext, 20 ) | JOKEDATE | ---- -------------------- ---------- | 1 | Why Did the chicken | 2000-04-01 | ---- --------------------- --------- - 1 ROW IN Set (0.05 sec)

Understand it if it works? Another useful function is Count, through this function, we can get the number of returns to the result. For example, if we want to find out how much jokes we store in our table, we can use the following command: mysql> select count (*) from Jokes;

-------- | count (*) | ---------- | 1 | -------- 1 ROW IN SET 0.06 SEC)

As we see, there is only one joke in our table.

So far, all of our examples are all records in the table. As a supplement to the Select command, we can use the "WHERE clause" so that we can limit the result of the returned result. Take a look at this example:

MySQL> SELECT Count (*) from Jokes -> Where Jokedate> = "2000-01-01";

This query statement will have a number of "greater than or equal to" on January 1, 2000, "greater than or equal to" means "bigger or later" for a date.

There is a relatively special usage to find a record containing a certain paragraph. You can look at this inquiry statement:

MySQL> Select Joketext from Jokes -> Where Joketext Like "% Chicken%";

This query statement shows all the Joketext columns that contain the text of the smile in the word Chicken. The LIKE keyword tells the mysql specified column must match the given expression. Here, the expression we use is "% chicken%". The% of this illustrates the word Chicken can appear in front or behind any string. The conditions can also be used in combination in the WHERE clause, so you can do more complex queries. For example, we have to show Knock-Knock jokes in April 2000, we can use the following query statement:

MySQL> Select Joketext from Jokes Where -> Joketext Like "% KNOCK KNOCK%" and -> Jokedate> = "2000-04-01" and -> Jokedate <"2000-05-01";

Add some jokes to the table, then test the SELECT statement. Familiar with the SELECT statement will help to learn this tutorial.

We can also do a lot of things in the SELECT statement, but we will no longer discuss it in detail, we will talk about some other advanced features when we need it. If you are so curious, if you can't wait, you can go to the mysql user manual.

Modify the stored data

Once you have entered some data to the database's table, you may want to make some modifications. For example, correcting spelling errors, and other data about jokes, all of these changes can be done with an UPDATE command. This command contains some INSERT commands (the value aspects of the set column) and the select command (in selecting change objects). The basic format of the update command is like this:

Mysql> Update Set -> = , ... -> where ;

For example, if you want to change the date on the joke you entered, you need to enter the following command:

MySQL> Update Jokes Set Jokedate = "1990-04-01" Where id = 1; here we used ID columns. With it you can easily specify the joke you want to change. The WHERE clause can also be used here, just like it in the select command. The following command is to change the date of the joke containing the word Chicken:

mysql> Update Jokes Set Jokedate = "1990-04-01"

-> Where Joketext Like "% chicken%";

Delete storage data

Deleting a content in SQL is a horrible simple, the following is the format of this command:

Mysql> Delete from Where

To delete all the jokes with Chicken, you only need to enter the following command:

Mysql> Delete from Jokes Where Joketext Like "% Chicken%";

The WHERE clause here is optional, but if you don't need it, you should know what you are doing, because at this time you are clever this data sheet. The following command will clear the Jokes data sheet:

mysql> delete from Jokes;

Conclusion

In fact, in addition to our basic commands you have seen here, there are many other things worth learning, but the commands we mentioned is some of the most common commands. So far, we just work in a data table. To understand the real power of relational database engines, you also need to learn if you use several tables to describe the complex relationships that may exist between tables. All of these contents and some other content, we will talk about the fourth chapter of our tutorial. In Chapter 4, we will discuss the design principle of the database and refer to some complicated examples. Now, we need to be able to use the command line interface to operate MySQL.

Source: CCID NET: Kevin Yang Translation: Everywhere