MySQL database command set

Source: Internet
Author: User
Tags mysql commands

As the use of MySQL Databases continues to expand, its related commands are also increasing. The following article introduces MySQL database commands, if you are interested in the actual operations, you can click to view the following articles.

1: Use the SHOW statement to find out the current database on the server:

 
 
  1. MySQL> SHOW DATABASES; 

2. Create a database named MySQLDATA

 
 
  1. MySQL> CREATE DATABASE MySQLDATA; 

3: select the database you created

 
 
  1. MySQL> USE MySQLDATA; (when you press the Enter key to see Database changed, the operation is successful !)

4: view the tables in the current database

 
 
  1. MySQL> SHOW TABLES; 

5. Create a database table

 
 
  1. MySQL> CREATE TABLE MYTABLE (name VARCHAR(20), sex CHAR(1)); 

6: display the table structure:

 
 
  1. MySQL> DESCRIBE MYTABLE; 

7. Add records to the table

 
 
  1. MySQL> insert into MYTABLE values (”hyq”,”M”); 

8: load data into a database table in text format, for example, D:/MySQL.txt)

 
 
  1. MySQL> LOAD DATA LOCAL INFILE “D:/MySQL.txt” INTO TABLE MYTABLE; 

9: import the. SQL file MySQL database command such as D:/MySQL. SQL)

 
 
  1. MySQL>use database;  
  2. MySQL>source d:/MySQL.sql; 

10: delete a table

 
 
  1. MySQL>drop TABLE MYTABLE; 

11: Clear the table

 
 
  1. MySQL>delete from MYTABLE; 

12: Update table data

 
 
  1. MySQL>update MYTABLE set sex=”f” where name=’hyq’; 

The following are the management experiences of using MySQL on the Internet,

In windows, MySQL exists as a service. Before using MySQL, make sure that the service has been started. You can run the net start MySQL database command to start MySQL. In Linux, the "/etc/rc. d/init. d/MySQLd start" command is available. Note that the initiator must have administrator privileges.

The newly installed MySQL contains a root account with a blank password and an anonymous account, which poses a major security risk. For some important applications, we should improve the security as much as possible, here, you should delete anonymous accounts and Set passwords for root accounts. You can run the following command:

 
 
  1. use MySQL;  
  2. delete from User where User=”";  
  3. update User set Password=PASSWORD(’newpassword’) where User=’root’; 

If you want to restrict the logon terminal used by the User, you can update the Host field of the corresponding User in the User table. After making the above changes, restart the database service, at this time, the following commands can be used for Logon:

 
 
  1. MySQL -uroot -p;  
  2. MySQL -uroot -pnewpassword;  
  3. MySQL mydb -uroot -p;  
  4. MySQL mydb -uroot -pnewpassword; 

The preceding MySQL database command parameters are part of common parameters. For details, refer to the documentation. Here, mydb is the name of the database you want to log on.

In development and practical applications, users should not only use root users to connect to the database. Although it is convenient to use root users for testing, it will bring significant security risks to the system, it is not conducive to the improvement of management techniques. We grant the most appropriate database permissions to the users used in an application. For example, a user who only inserts data should not be granted the permission to delete data.

MySQL User management is implemented through the User table. There are two common methods to add new users. One is to insert the corresponding data rows in the User table and set the corresponding permissions; the second is to use the GRANT command to create a user with certain permissions. The common usage of GRANT is as follows:

 
 
  1. grant all on mydb.* to NewUserName@HostName identified by “password” ;  
  2. grant usage on *.* to NewUserName@HostName identified by “password”;  
  3. grant select,insert,update on mydb.* to NewUserName@HostName identified by “password”;  
  4. grant update,delete on mydb.TestTable to NewUserName@HostName identified by “password”; 

To GRANT the user the ability to manage permissions on the corresponding object, you can add the with grant option after GRANT. For users inserted into the User table, use the Password function to update and encrypt the PASSWORD field to prevent unauthorized users from stealing the Password. Users who do not need permissions should be cleared, and those who pass the permissions should be revoked in a timely manner. To REVOKE permissions, you can update the corresponding fields in the User table or use the REVOKE operation.

The following is my interpretation of common permissions from other information (www.cn-java.com:

Global Management permissions:

FILE: read and write files on the MySQL server.

PROCESS: displays or kills service threads of other users.

RELOAD: RELOAD Access Control tables and refresh logs.

SHUTDOWN: Shut down the MySQL service.

Database/data table/data column permissions:

ALTER: Modify existing data tables (such as adding/deleting columns) and indexes.

CREATE: CREATE a new database or data table.

DELETE: DELETE table records.

DROP: delete a data table or database.

INDEX: Create or delete an INDEX.

INSERT: Add Table records.

SELECT: displays/searches for table records.

UPDATE: Modify existing records in the table.

Special permissions:

ALL: allow anything (same as root ).

USAGE: Only Logon Allowed-nothing else is allowed.

The above content is a summary of MySQL database commands, and I hope you will get something.

Original article title: Summary of common MySQL commands

Connection: http://www.cnblogs.com/vicenteforever/articles/1576796.html

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.