1. Start and stop the mysql service (under Windows)
net stop mysql
net start mysql
2. Display the list of databases in the current database server:
mysql> SHOW DATABASES;
Note: There is MYSQL system information in the mysql library. When we change the password and add users, we actually use this library to operate.
3. Display the data table in the database:
mysql> USE library name;
mysql> SHOW TABLES;
4. Display the structure of the data table:
mysql> DESCRIBE 表名;
Get table structure scheme 2
Command: desc table name, or show columns from table name
example:
mysql> describe MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
5. Delete the database:
mysql> DROP DATABASE 库名;
6. Delete the data table:
mysql> DROP TABLE 表名;
7. Check the mysql version
select version();
8. Clear the records in the table:
mysql> DELETE FROM 表名;
or
teuncate table table name
(What's the difference between the two?)
9. Load the data into the data table in text mode:
mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt” INTO TABLE 表名;
10. Import the .sql file command:
mysql> USE database name;
mysql> SOURCE d:/mysql.sql;
11. Modify the root password from the command line:
mysql> UPDATE mysql.user SET password=PASSWORD('新密码') WHERE User='root';
mysql> FLUSH PRIVILEGES;
12. Display the database name of use:
mysql> SELECT DATABASE();
13. Display the current user:
mysql> SELECT USER();
14. Export the entire database
1) The export file is stored in the mysql\bin directory by default
mysqldump -u username -p database name > exported filename
mysqldump -u user_name -p123456 database_name > outfile_name.sql
2) Export a table
mysqldump -u username -p database name table name > exported file name
mysqldump -u user_name -p database_name table_name > outfile_name.sql
15. Import from external file into database
1) Use the "source" command
First go to the "mysql" command console, then create a database, then use that database. Finally do the following.
mysql>source [Save path of backup file]
2) Use the "<" symbol
First enter the "mysql" command console, then create a database, then exit MySQL and enter the DOS interface. Finally do the following.
mysql -u root -p < [the path to save the backup file]
16. Add fields to the table
Command: alter table table name add field type other;
For example: a field passtest is added to the table MyClass, the type is int(4), the default value is 0
mysql> alter table MyClass add passtest int(4) default '0'
17. Change the table name
Command: rename table original table name to new table name;
For example: in the table MyClass name is changed to YouClass
mysql> rename table MyClass to YouClass;
18. Call the stored procedure
mysql> CALL procedureName(paramentList);
例:mysql> CALL addMoney(12, 500);
19. View stored procedures for a specific database
方法一:mysql> SELECT `name` FROM mysql.proc WHERE db = 'your_db_name' AND `type` = 'PROCEDURE';
Method 2: mysql> show procedure status;
20, delete the stored procedure
mysql> DROP PROCEDURE procedure_name;
mysql> DROP PROCEDURE IF EXISTS procedure_name;
21. View the specified stored procedure definition
mysql> SHOW CREATE PROCEDURE proc_name;
mysql> SHOW CREATE FUNCTION func_name;
22. When a calculator is used
select ((4 * 4) / 10 ) + 25;
+----------------------+
| ((4 * 4) / 10 ) + 25 |
+----------------------+
| 26.60 |
+----------------------+
1 row in set (0.00 sec)
22、FROM_UNIXTIME( unix_timestamp )
Parameter: usually a ten-digit number, such as: 1344887103
Return value: There are two kinds, it may be a string like 'YYYY-MM-DD HH:MM:SS', or it may be a number like YYYYMMDDHHMMSS.uuuuuu, what is returned depends on the form of the function being called .
mysql> select FROM_UNIXTIME(1344887103);
+---------------------------+
| FROM_UNIXTIME(1344887103) |
+---------------------------+
| 2012-08-14 03:45:03 |
+---------------------------+
1 row in set (0.00 sec)
23、FROM_UNIXTIME( unix_timestamp ,format )
Parameter unix_timestamp: the same as the parameter in the method FROM_UNIXTIME( unix_timestamp );
Parameter format : the format of the converted time string displayed;
Return value: a string displayed in the specified time format;
mysql> select FROM_UNIXTIME(1344887103,'%Y-%M-%D %h:%i:%s');
+-----------------------------------------------+
| FROM_UNIXTIME(1344887103,'%Y-%M-%D %h:%i:%s') |
+-----------------------------------------------+
| 2012-August-14th 03:45:03 |
+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> select FROM_UNIXTIME(1344887103,'%Y-%m-%D %h:%i:%s');
+-----------------------------------------------+
| FROM_UNIXTIME(1344887103,'%Y-%m-%D %h:%i:%s') |
+-----------------------------------------------+
| 2012-08-14th 03:45:03 |
+-----------------------------------------------+
1 row in set (0.00 sec)
Date and time functions FROM_UNIXTIME(), UNIX_TIME() in MySQL database
24、UNIX_TIMESTAMP()
Return value: UNIX format numeric string of current time, or UNIX timestamp (seconds since UTC time '1970-01-01 00:00:00'), usually ten digits, such as 1344887103.
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1344887103 |
+------------------+
1 row in set (0.00 sec)
25、UNIX_TIMESTAMP( date )
Parameters: date may be a DATE string, a DATETIME string, a TIMESTAPE string, or a numeric string similar to YYMMDD or YYYYMMDD.
Returns: The number of seconds between the start of UTC time '1970-01-01 00:00:00' and this parameter. The server interprets the parameter date as a value for the current time zone and converts it to an internal time in UTC. The client can set the current time zone by itself. When UNIX_TIMESTAMP() is used on a TIMESTAMP column, the function returns the value of the internal timestamp directly; if you pass an out-of-range time to UNIX_TIMESTAMP(), its return value is zero.
mysql> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
| 1344888895 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT UNIX_TIMESTAMP('2012-08-14 16:19:23');
+---------------------------------------+
| UNIX_TIMESTAMP('2012-08-14 16:19:23') |
+---------------------------------------+
| 1344932363 |
+---------------------------------------+
1 row in set (0.00 sec)
Note: If you use UNIX_TIMESTAMP() and FROM_UNIXTIME() to convert a TIMESTAMP value to a Unix timestamp value, precision is lost because the mapping is not one-to-one in both directions. For example, it is possible for two UNIX_TIMESTAMP()s to map to the same Unix timestamp value due to changes in the local time zone. FROM_UNIXTIME() will only map to the original timestamp value. Here's an example, using TIMESTAMP in CET timezone:
mysql> SELECT UNIX_TIMESTAMP('2005-03-27 03:00:00');
+---------------------------------------+
| UNIX_TIMESTAMP('2005-03-27 03:00:00') |
+---------------------------------------+
| 1111885200 |
+---------------------------------------+
mysql> SELECT UNIX_TIMESTAMP('2005-03-27 02:00:00');
+---------------------------------------+
| UNIX_TIMESTAMP('2005-03-27 02:00:00') |
+---------------------------------------+
| 1111885200 |
+---------------------------------------+
mysql> SELECT FROM_UNIXTIME(1111885200);
+---------------------------+
| FROM_UNIXTIME(1111885200) |
+---------------------------+
| 2005-03-27 03:00:00 |
+---------------------------+
26. The DATE_FORMAT() function is used to display date/time data in different formats.
DATE_FORMAT(date,format)
Available formats are:
format description
%a Abbreviated week name
%b Abbreviated month name
%c month, numeric
%D day of month with English prefix
%d day of the month, numeric (00-31)
%e day of the month, numeric (0-31)
%f microseconds
%H hours (00-23)
%h hours (01-12)
%I hours (01-12)
%i minutes, numeric (00-59)
Day of year %j (001-366)
%k hours (0-23)
%l hours (1-12)
%M month name
%m month, numeric (00-12)
%p AM or PM
%r time, 12-hour (hh:mm:ss AM or PM)
%S seconds (00-59)
%s seconds (00-59)
%T time, 24-hour (hh:mm:ss)
%U week (00-53) Sunday is the first day of the week
%u week (00-53) Monday is the first day of the week
%V week (01-53) Sunday is the first day of the week, used with %X
%v week (01-53) Monday is the first day of the week, used with %x
%W week name
%w day of week (0=Sunday, 6=Saturday)
%X year, where Sunday is the first day of the week, 4 digits, used with %V
%x year, where Monday is the first day of the week, 4 digits, used with %v
%Y year, 4 digits
%y year, 2 digits
Example
The script below uses the DATE_FORMAT() function to display different formats. We use NOW() to get the current date/time:
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
27. Display process/kill process
mysql> show processlist;
+-----+-------------+--------------------+-------+---------+-------+----------------------------------+----------
| Id | User | Host | db | Command | Time| State | Info
+-----+-------------+--------------------+-------+---------+-------+----------------------------------+----------
|207|root |192.168.0.20:51718 |mytest | Sleep | 5 | | NULL
|208|root |192.168.0.20:51719 |mytest | Sleep | 5 | | NULL
|220|root |192.168.0.20:51731 |mytest |Query | 84 | Locked |
select bookname,culture,value,type from book where id=001
Let's briefly talk about the meaning and use of each column. The first column, id, needless to say, an identifier is very useful when you want to kill a statement. The user column, showing the single former user,
If you are not root, this command will only display sql statements within your authority. The host column shows which port on which ip the statement was sent from. Oh, it can be used to track down the user who made the problem statement.
The db column shows which database the process is currently connected to. The command column displays the commands executed by the current connection, generally sleep, query, and connect. time column,
The duration of this state, in seconds. The state column shows the state of the sql statement using the current connection. It is a very important column. There will be descriptions of all states in the future. Please note that
state is only a certain state in the execution of the statement, a sql statement, for example, it has been queried, it may need to go through copying to tmp table, Sorting result, Sending data and other states to complete, the info column,
show this sql statement
If you add, delete, or modify fields in a large table or execute a complex sql query that causes the mysql thread to hang. You can use this command to check which sql hangs, and use the kill command to kill him
How to use the kill command
kill pid
In the above example, we want to kill the thread whose id is 207 and execute it
Just kill 207.
28. Mysql view process SQL
SELECT * FROM information_schema.PROCESSLIST
WHERE info IS NOT NULL
ORDER BY TIME desc,state,INFO;