Experience in using common MySQL commands

Source: Internet
Author: User
Tags mysql commands

The following articles mainly describe the experience of using common MySQL commands. In practice, we often use MySQL commands, so here we want to summarize the specific usage of common MySQL commands. The common methods are as follows:

MySQL Common commands 1. Use of MySQLdump commands

Back up and export Databases

 
 
  1. MySQLdump -h database_ip -u Username -p --opt databasename > backup-file.sql 

Export only the database table structure

 
 
  1. MySQLdump -h database_ip -d -u Username -p databasename >database_structure.sql 

Export only a table in the database

 
 
  1. MySQLdump --opt --add-drop-table -u Username -p databasename tablename > dump.sql 

If you do not want to manually enter the password, use the -- password parameter.

 
 
  1. MySQLdump -h database_ip -u Username --password=123456 --opt databasename > backup-file.sql  
  2. MySQLdump -h database_ip -d -u Username --password=123456 databasename >database_structure.sql 

Common MySQL commands 2. Use MySQL commands

Save query results to files

 
 
  1. select title from book into outfile '/tmp/outfile.txt'; 

Searches for redundant duplicate records in the Table. duplicate records are determined based on the peopleId of a field.

 
 
  1. select * from people where peopleId in (select peopleId from people group by   
  2. peopleId having count(peopleId) > 1); 

Query non-repeated records in a table (excluding Repeated Records)

 
 
  1. select * from phome_ecms_wma where title in (select distinct title from phome_ecms_wma); 

Delete duplicate records in a table. duplicate records are determined based on the title of a field.

 
 
  1. select *,count(distinct title) INTO OUTFILE '/tmp/table.bak' from phome_ecms_wma group by title;  
  2. delete from phome_ecms_wma;  
  3. LOAD DATA INFILE '/tmp/table.bak' REPLACE INTO TABLE phome_ecms_wma character set utf8; 

Query the current encoding of the database

 
 
  1. MySQL> show variables like "character_set%"; 

Modify Table Field Type

 
 
  1. MySQL> alter table table_name change last_action last_action datetime NOT NULL default '0000-00-00 00:00:00'; 

Add a new field to the table

 
 
  1. MySQL> ALTER TABLE host ADD ks_mac VARCHAR(100); 

Delete a field from the table

 
 
  1. MySQL> ALTER TABLE table_name DROP field_name;  

Rename a table

 
 
  1. MySQL>alter table t1 rename t2; 

Add an index to a field

 
 
  1. MySQL> alter table tablename add index name (field name 1 [, field name 2…]);
  2. MySQL> alter table tablename add index emp_name (name );

Index with primary keywords

 
 
  1. MySQL> alter table tablename add primary key(id); 

Add an index with unique conditions

 
 
  1. MySQL> alter table tablename add unique emp_name2(cardnumber); 

Delete An index

 
 
  1. MySQL>alter table tablename drop index emp_name; 

Remote Access to MySQL settings

 
 
  1. MySQL> GRANT ALL PRIVILEGES ON database_test.* to root@192.168.1.9 IDENTIFIED BY '123456';  
  2. MySQL> FLUSH PRIVILEGES; 

The above content is an introduction to commonly used MySQL commands. I hope you will get some benefits.
 

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.