Mysql Command Encyclopedia (full version) _mysql

Source: Internet
Author: User
Tags db2 mul mysql commands mysql host mysql update mysql version table definition create database

One, the connection database
Format: mysql-h host address-u user name-P user Password
1.1. Connect to MySQL on this computer.
first open the DOS window, and then enter the directory Mysql\bin, and then type the command Mysql-u root-p, after the carriage return prompts you to lose the password.
Note that before the user name can have spaces or no spaces, but the password must have no space before, otherwise let you re-enter the password.
If you have just installed MySQL, superuser root is no password, so direct return can enter into MySQL, the MySQL prompt is: mysql>

1.2 Connect to MySQL on the remote host.

Assume the IP of the remote host is: 110.110.110.110, username is root, password is abcd123. Type the following command:
Mysql-h110.110.110.110-u Root-p 123; (Note: There is no space between u and Root, others are the same)
1.3 exit MySQL command: exit (Enter)

Second, increase user
Format: Grant Select on database. * To User name @ login host identified by "password"
2.1 Add a user test1 password to ABC, so that he can log on any host, and all databases have query, insert, modify, delete permissions.

First connect to MySQL with root user, and then type the following command:

Copy Code code as follows:
Grant Select,insert,update,delete on *.* to [email=test1@ '%]test1@ '%[/email] ' identified by ' ABC ';

But the added user is very dangerous and you want someone who knows Test1 's password so that he can log on to your MySQL database on any computer on the Internet and can do whatever it wants with your data, see 2.2.
2.2 Add a user test2 password for ABC, so that he can only login on the localhost, and can query the database mydb, insert, modify, delete operations (localhost refers to the local host, that is, the MySQL database is the host), This allows a user to use a password that knows test2, and he cannot access the database directly from the Internet, only through a Web page on the MySQL host.

Copy Code code as follows:
Grant Select,insert,update,delete on mydb.* to [Email=test2@localhost]test2@localhost[/email] identified by "ABC";

If you do not want to test2 the password, you can make another command to eliminate the password.
Copy Code code as follows:
Grant Select,insert,update,delete on mydb.* to [Email=test2@localhost]test2@localhost[/email] identified by ""

Third, the operation of the database

3.1 Creating a Database
Note: You must first connect to the MySQL server before creating the database
Command: CREATE DATABASE < database name >
Example 1: Create a database named XHKDB

Copy Code code as follows:
mysql> CREATE DATABASE xhkdb;

Example 2: Creating a database and assigning users
①create database name;
②grant select,insert,update,delete,create,drop,alter on database name. * To user name @localhost identified by ' password ';
③set PASSWORD for ' database name ' @ ' localhost ' = old_password (' password ');
Execute 3 commands sequentially to complete the database creation.
Note: The Chinese "password" and "database" is the user needs to set.

3.2 Display Database
command: Show databases (note: Last has an S)

Copy Code code as follows:
Mysql> Show databases

3.3 Deleting a database
Command: Drop Database < database name >
For example, to delete a database named Xhkdb
Copy Code code as follows:
mysql> drop Database xhkdb;

Example 1: Delete a database that is already determined to exist

Copy Code code as follows:
mysql> drop Database drop_database;
Query OK, 0 rows Affected (0.00 sec)

Example 2: Delete a database with an indeterminate existence

mysql> drop database drop_database;
ERROR 1008 (HY000): Can ' t drop database ' drop_database '; DB doesn ' t exist
//error, cannot delete ' drop_database ' database, the database does not exist.
mysql> drop database if exists drop_database;
Query OK, 0 rows affected, 1 Warning (0.00 sec)//generates a warning stating that the database does not exist
mysql> CREATE database drop_database;
Query OK, 1 row Affected (0.00 sec)
mysql> drop database if exists drop_database;//if exists to determine whether the database exists, does not exist, and does not produce errors 
   query OK, 0 rows Affected (0.00 sec)

3.4 Connection Database
Command: Use < database name >
For example: If the XHKDB database exists, try to access it:mysql> use XHKDB;
ScreenTip: Database changed
The USE statement informs MySQL that the Db_name database will be used as the default (current) database for subsequent statements. The database remains in the default database until the end of the paragraph, or until a different use statement is published:

mysql> use DB1;
Mysql> SELECT COUNT (*) from mytable; # selects from db1.mytable
mysql> to use DB2;
Mysql> SELECT COUNT (*) from mytable; # selects from db2.mytable

Using the USE statement to mark a specific current database does not prevent you from accessing tables in other databases. The following example can access the author table from the DB1 database and access the edit table from the DB2 database:

mysql> use DB1;
Mysql> SELECT author_name,editor_name from Author,db2.editor
 ->  WHERE author.editor_id = db2.editor.editor_id;

To exit the database or connect to another database directly user ' other database name ' is OK.

3.5 Current Selection Database
command:mysql> Select Database ();
The select command in MySQL is similar to print or write in other programming languages, and you can use it to display the results of a string, a number, a mathematical expression, and so on. How do I use the special features of the Select command in MySQL?

(1). Show MySQL version

Mysql> select version (); 
+-----------------------+ 
| version ()    | 
+-----------------------+ 
| 6.0.4-alpha-community | 
+-----------------------+ 
1 row in Set (0.02 sec) 

(2). Show Current Time

Mysql> Select Now (); 
+---------------------+ 
| Now ()    | 
+---------------------+ 
| 2009-09-15 22:35:32 | 
+---------------------+ 
1 row in Set (0.04 sec) 

(3). Display Year Month day

SELECT DayOfMonth (current_date); 
+--------------------------+ 
| DayOfMonth (current_date) | 
+--------------------------+ 
|      | 
+--------------------------+ 
1 row in Set (0.01 sec) 
 
SELECT MONTH (current_date); 
+---------------------+ 
| MONTH (current_date) | 
+---------------------+ 
|     9 | 
+---------------------+ 
1 row in Set (0.00 sec) 
 
SELECT Year (current_date); 
+--------------------+ 
| Year (current_date) | 
+--------------------+ 
|    2009 | 
+--------------------+ 

(4). Display string

Mysql> Select "Welecome to my blog!"; 
+----------------------+ 
| welecome to my blog! | 
+----------------------+ 
| welecome to my blog! | 
+----------------------+ 
1 row in Set (0.00 sec) 

(5). When the calculator uses

Select ((4 * 4)/ten) +; 
+----------------------+ 
| ((4 * 4)/ten | 
+----------------------+ 
|    26.60 | 
+----------------------+ 
1 row in Set (0.00 sec) 

(6). String Connection Strings

Select CONCAT (F_name, "", L_name) as 
name from 
employee_data 
where title = ' Marketing Executive '; 
+---------------+ 
| Name   | 
+---------------+ 
| Monica Sehgal | 
| Hal Simlai | 
| Joseph Irvine | 
+---------------+ 
3 rows in Set (0.00 sec) 

Note: the concat () function is used here to string strings together. In addition, we used the previously learned as to the result column ' CONCAT (f_name, "", L_name) ' as a pseudonym

Iv. operation of the table
4.1 Creating a table
command: CREATE table < table name > (< Field name 1> < type 1> [,.. < Field name N> < type n>];
For example, to create a table named MyClass

Mysql> CREATE TABLE MyClass (
> ID int (4) NOT null primary key auto_increment,
> Name char (a) not null,
   > sex int (4) NOT null default ' 0 ',
> Degree double (16,2));

4.2 Get Table structure
Command: DESC table name, or Show columns from table name

mysql> desc MyClass;
Mysql> show columns from MyClass;

When using the MySQL database desc table name, we see the key column, there may be 4 kinds of values, namely ', ' PRI ', ' UNI ', ' MUL '.
(1). If the key is empty, the value of the column can be duplicated, indicating that the column has no index, or a non-preamble column of a non-unique composite index;
(2). If the key is a PRI, then the column is part of the primary key;
(3). If key is uni, then the column is the first column (leading column) of a unique value index, and cannot contain null values (NULL);
(4). If key is Mul, then the value of the column can be duplicated, the column is a leading column of a non unique index (the first column) or part of a unique index but can contain null values.
If the definition of a column, while satisfying the above 4 kinds of situations, such as a column is both PRI and UNI, then "desc table name", the display of key values according to the priority, Pri->uni->mul. So at this point, the PRI is displayed.
A unique indexed column can be displayed as a PRI, and the column cannot contain null values, and the table does not have a primary key.
A unique indexed column can be displayed as Mul, if multiple columns make up a unique composite index, because although the multiple-column combination of an index is unique, such as Id+name is unique, no single column can still have duplicate values, as long as the id+name is unique.

4.3 Deleting a table
Command: DROP table < table name >
For example, to delete a table named MyClass

Copy Code code as follows:
mysql> drop table MyClass;

DROP table is used to cancel one or more tables. You must have drop permissions for each table. All table data and table definitions will be canceled, so be careful with this statement!
Note: For a table with partitions, the DROP table permanently cancels the table definition, cancels the partitions, and cancels all data stored in those partitions. DROP table will also be canceled and canceled

Table has an associated partition definition (. par) file.
For tables that do not exist, use if exists to prevent errors from occurring. When you use the if exists, a note is generated for each table that does not exist.
Restrict and cascade can make partitioning easier. At present, restrict and cascade do not work.

4.4 Inserting data into a table
Command: INSERT into < table name > [< field name 1>[,.. < field name n >]] VALUES (value 1) [, (value N)]
For example: Insert two records into the table MyClass, two records indicate that: Tom with a number 1 is 96.45, and the number 2 for Joan is 82.99, and the number 3 is named

Wang's score was 96.5.
mysql> INSERT INTO MyClass values (1, ' Tom ', 96.45), (2, ' Joan ', 82.99), (2, ' Wang ', 96.59);
Note: INSERT into only one record in the table at a time.

4.5 Query Table
(1), query all rows
Command: Select < Field 1, field 2,...> from < table name > where < expression >
For example: View all data in table MyClass
Mysql> select * from MyClass;
(2), query the first few lines of data
For example: View the first 2 rows of data in table MyClass
Mysql> SELECT * from MyClass ORDER by ID limit 0, 2;
The select is typically used with where to query for more precise and complex data.

4.6 Deleting a table
command: Delete from table name where expression
For example: Delete a record in table MyClass that is numbered 1
Mysql> Delete from MyClass where id=1;

4.7 Modifying data in a table
Syntax: Update table name SET field = new value,... WHERE condition
mysql> Update MyClass set name= ' Mary ' where id=1;

Example 1: Tanku's MySQL UPDATE statement:

Copy Code code as follows:
UPDATE [low_priority] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE Where_definition] [Order BY ...] [LIMIT Row_count]

Example 2: UPDATE statements for multiple tables:
Copy Code code as follows:
UPDATE [low_priority] [IGNORE] table_references SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE Where_definition]

The update syntax can update columns in existing table rows with new values. The SET clause indicates which columns to modify and what values to give. The WHERE clause specifies which rows should be updated. If there is no WHERE clause, all rows are updated. If an ORDER BY clause is specified, the row is updated in the sequence specified. The limit clause is used to limit the number of rows that can be updated, given a limit.

4.8 Add Table field
Command: ALTER TABLE name add field type other;
For example, a field passtest is added to the table MyClass, the type is int (4), and the default value is 0
mysql> ALTER TABLE MyClass add passtest int (4) Default ' 0 ' [/code]
Add Index:Mysql> ALTER TABLE name add index index name (field name 1[, field Name 2 ...]);
Example:Mysql> ALTER TABLE employee ADD index emp_name (name);
index of the primary keyword:Mysql> ALTER TABLE name add primary key (field name);
Example: mysql> ALTER TABLE employee ADD primary key (ID);
index with unique restriction criteria:Mysql> ALTER TABLE name add unique index name (field name);
Example: mysql> ALTER TABLE employee add unique emp_name2 (cardnumber);
To delete an index:mysql> ALTER TABLE name DROP INDEX index name;
Example: mysql>alter Table employee DROP index emp_name;
Add fields:mysql> ALTER TABLE table_name ADD field_name Field_type;
To modify the original field name and type:mysql> ALTER TABLE table_name change old_field_name new_field_name field_type;
Modify a field type: Mysql>alter TABLE table_name MODIFY colum_name field_type New_type
To delete a field:MySQL ALTER TABLE table_name DROP field_name;

4.9 Modifying table names
Command: Rename table original table name to new name;
For example: In the table MyClass the name is changed to Youclass

Copy Code code as follows:
Mysql> Rename table MyClass to Youclass;

When you perform RENAME, you cannot have any locked tables or active transactions. You must also have ALTER and DROP permissions on the original table, as well as CREATE and INSERT permissions on the new table.
If MySQL encounters any errors in multiple table renaming, it will reverse the renaming of all renamed tables and return everything to its original state.
RENAME TABLE is added to the MySQL 3.23.23.

V. Backup data

command executes in DOS [Url=file://\\mysql\\bin]\\mysql\\bin[/url] Directory
(1). Export the entire database
The export file defaults to the existence of the Mysql\bin directory
Mysqldump-u user name-p database name > exported file name
Mysqldump-u user_name-p123456 database_name > Outfile_name.sql
(2). Export a table
Mysqldump-u user name-P database name Table name > exported file name
MYSQLDUMP-U USER_NAME-P database_name table_name > OUTFILE_NAME.SQL
(3). Export a database structure
Mysqldump-u user_name-p-d–add-drop-table database_name > Outfile_name.sql
-D No data –add-drop-table add a drop table before each CREATE statement
(4). With language parameter export
mysqldump-uroot-p–default-character-set=latin1–set-charset=gbk–skip-opt database_name > Outfile_name.sql
For example, the AAA library is backed up to file back_aaa:

Copy Code code as follows:
[Root@test1 root]# Cd/home/data/mysql
[Root@test1 mysql]# mysqldump-u root-p--opt aaa > BACK_AAA

六、一个 A complete database creation instance

Drop database if exists school; If there is school, delete the
CREATE DATABASE school//build library school use
School;//Open Library school
CREATE TABLE teacher// CREATE TABLE teacher
(
 ID int (3) auto_increment NOT NULL primary key,
 name char (x) not NULL, address
 varchar D Efault ' Shenzhen ', year
 date
);//table End/

/below Insert field inserts into
teacher values (', ', ' Glchengang ', ' Shenzhen one "," "1976-10-10");
Insert into teacher values (', ', ' Jack ', ' ' Shenzhen One ', ' 1975-12-23 ');

Note: In the construction of the table
(1), set the ID to a number field of length 3: Int (3), and let it automatically add one to each record: auto_increment; not null; and let him be the main field primary key.
(2), set name to a character field of length 10
(3), the address is set to length 50 of the character field, and the default value is Shenzhen.

If you type the above command at the MySQL prompt, it's not easy to debug.
(1), you can write the above command as is written in a text file, assume the School.sql, and then copy to C:\\, and enter the directory in the DOS state [url=file://\\mysql\\bin]\

\mysql\\bin[/url], and then type the following command:mysql-uroot-p Password < c:\\school.sql
If successful, there is no display on a single line, and if there is an error, there is a hint. (The above command has been debugged and you can use it only if you remove the comment.)
(2), or after entering the command line, use mysql> source c:\\school.sql; You can also import the School.sql file into the database.

The above is the full version of the MySQL command encyclopedia, I hope to be proficient in using MySQL commands help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.