Common MYSQL commands and common MYSQL commands

Source: Internet
Author: User
Tags mysql commands

Common MYSQL commands and common MYSQL commands
Start: net start mySql;
Enter: mysql-u root-p/mysql-h localhost-u root-p databaseName;
List databases: show databases;
Select Database: use databaseName;
List tables: show tables;
Show columns from tableName;
Create a database: source fileName.txt;
Matching character: wildcard _ can be used to represent any character, and % represents any string;
Add a field: alter table tabelName add column fieldName dateType;
Add multiple fields: alter table tabelName add column fieldName1 dateType, add columns fieldName2 dateType;
Multi-line command input: note that words cannot be broken; when data is inserted or changed, the field strings cannot be expanded into multiple rows; otherwise, the hard press enter will be stored in the data;
Add an Administrator Account: grant all on *. * to user @ localhost identified by "password ";
After each statement is entered, enter the plus sign ';' At the end, or add '\ G;
Query time: select now ();
Query the current user: select user ();
Query the database version: select version ();

Query the currently used database: select database ();

1. Delete the students data table in the student_course database:
Rm-f student_course/students .*
  
2. Back up the database: (back up the database test)
Mysqldump-u root-p test> c: \ test.txt
Backup table: (back up the mytable table under the test database)
Mysqldump-u root-p test mytable> c: \ test.txt
Import the backup data to the database: (import back to the test database)
Mysql-u root-p test <c: \ test.txt
  
3. Create a temporary table: (create a temporary table zengchao)
Create temporary table zengchao (name varchar (10 ));
  
4. To create a table, first determine whether the table exists.
Create table if not exists students (......);
  
5. Copy the table structure from an existing table
Create table table2 select * from table1 where 1 <> 1;
  
6. Copy a table
Create table table2 select * from table1;
  
7. Rename the table
Alter table table1 rename as table2;
  
8. Modify the column type
Alter table table1 modify id int unsigned; // modify the column id type to int unsigned
Alter table table1 change id sid int unsigned; // modify the column id name to sid and the attribute to int unsigned
  
9. Create an index
Alter table table1 add index ind_id (id );
Create index ind_id on table1 (id );
Create unique index ind_id on table1 (id); // create a unique index
  
10. delete an index
Drop index idx_id on table1;
Alter table table1 drop index ind_id;
  
11. Combine characters or multiple columns (link the column id with ":" And column name and "=)
Select concat (id, ':', name, '=') from students;
  
12. limit (10 to 20 records selected) <the number of the first record set is 0>
Select * from students order by id limit 9, 10;
  
13. functions not supported by MySQL
Transaction, view, foreign key and reference integrity, stored procedure and trigger
  
  
14. MySQL uses the index operator symbol
<, <=, >=, >,=, Between, in, like without the start of % or _
  
15. disadvantages of using Indexes
1) slowing down the speed of adding, deleting, and modifying data;
2) occupied disk space;
3) increase the burden on the query optimizer;
When the query optimizer generates an execution plan, it will consider indexes. Too many indexes will increase the workload for the query optimizer, resulting in the inability to select the optimal query solution;
  
16. Analyze index Efficiency
Method: Add explain before a general SQL statement;
Meaning of analysis results:
1) table: The table name;
2) type: Connection type (ALL/Range/Ref ). Among them, ref is the most ideal;
3) possible_keys: query the available index names;
4) key: actually used index;
5) key_len: the length (in bytes) of the used part in the index );
6) ref: displays the column name or "const" (I don't understand what it means );
7) rows: displays the number of rows that MySQL considers to be scanned before finding the correct results;
8) extra: MySQL recommendations;
  
17. Use a short fixed-length Column
1) try to use a shorter data type;
2) use the fixed-length data type as much as possible;
A) Replace varchar with char, and process data with a fixed length faster than that with a variable length;
B) For tables that are frequently modified, disks are easy to form fragments, which affects the overall performance of the database;
C) In case of a data table crash, it is easier to reconstruct a table with a fixed length of data rows. When a data row with a fixed length is used, the start position of each record is a multiple of the fixed record length, which can be easily detected. However, it is not necessary to use a variable length data row;
D) For MyISAM data tables, although converting to a fixed-length data column can improve performance, it occupies a large space;
  
18. Use not null and enum
Define the column as not null as much as possible, so that data can be generated faster and less space is required. In addition, MySQL does not need to check whether there is a special case, that is, the null value, optimize the query;
If a column contains only a limited number of specific values, such as gender, validity, or enrollment year, you should consider converting it to the value of the enum column, mySQL processes faster, because all enum values are represented by an Identifier value in the system;
  
19. Use optimize table
For tables that are frequently modified, fragments are easily generated, so that more disk blocks must be read during database query to reduce query performance. All tables with variable lengths have disk fragmentation issues. This problem is more prominent FOR blob data types because of its large size changes. You can use optimize table to organize fragments to ensure that the database performance is not degraded and optimize the data tables affected by fragments. Optimize table can be used for MyISAM and BDB data tables. In fact, any fragment method uses mysqldump to store data tables, and then creates a new data table using the converted files;
  
20. Use procedure analyze ()
Procedure analyze () can be used to display the best type of recommendations. It is easy to use. Add procedure analyze () after the select statement. For example:
Select * from students procedure analyze ();
Select * from students procedure analyze (16,256 );
The second statement requires that procedure analyze () should not contain more than 16 values or enum types containing more than 256 bytes. If there is no limit, the output may be very long;
  
21. Use query Cache
1) How to query the cache:
When a select statement is executed for the first time, the server remembers the text content and query results of the query and stores them in the cache. When this statement is executed for the next time, the server returns the results directly from the cache; when a data table is updated, any cache query of the data table becomes invalid and is discarded.
2) Configure cache parameters:
Variable: query_cache _ type, the operation mode of the query cache. There are 3 moderate modes, 0: no cache; 1: cache query, unless it starts with select SQL _no_cache; 2: only queries starting with select SQL _cache are cached as needed; query_cache_size: set the maximum result set size of the query cache, which is greater than this value and will not be cached.
  
22. Adjust hardware
1) add more memory to the machine;
2) increase the speed of the hard disk to reduce the I/O wait time;
Track-seeking time is the main factor that determines performance. The speed of moving the head is the slowest word by word. Once the head is located, it is fast to read from the track;
3) re-allocate disk activities on different physical hard disk devices;

If possible, the busiest data inventory should be placed on different physical devices, which is different from using different partitions of the same physical device, because they will compete for the same physical resources (head ).

Create database name; create a database

Use databasename; select database

Drop database name directly deletes the database, no reminder

Show tables; displays tables

Describe tablename; Detailed description of the table

Add distinct to the select statement to remove duplicate fields.

Before mysqladmin drop databasename deletes a database, a message is displayed.

Display current mysql version and current date

Select version (), current_date;

2. Modify the root password in mysql:

Shell> mysql-u root-p

Mysql> update user set password = password ("xueok654123") where user = 'root ';

Mysql> flush privileges // refresh the database

Mysql> use dbname; open the database:

Mysql> show databases; displays all databases

Mysql> show tables; display all tables in mysql: use mysql first; then

Mysql> describe user; displays the column information of the user table in the mysql database );

3. grant

Create a full Super User that can connect to the server from anywhere, but you must use a password something to do this

Mysql> grant all privileges on *. * to identified by 'something'

Add new users

Format: grant select on database. * to username @ login host identified by "password"

Grant all privileges on *. * to identified by 'something' with grant option;

Grant all privileges on *. * TO "identified by 'something' with grant option;

Delete authorization:

Mysql> revoke all privileges on *. * from ";

Mysql> delete from user where user = "root" and host = "% ";

Mysql> flush privileges;

Create a User custom to log on to it363.com on a specific client and access the specific database fangchandb.

Mysql> grant select, insert, update, delete, create, drop on fangchandb. * to custom @ it363.com identified by 'passwd'

Rename a table:

Mysql> alter table t1 rename t2;

4. mysqldump

Back up database

Shell> mysqldump-h host-u root-p dbname> dbname_backup. SQL

Restore database

Shell> mysqladmin-h myhost-u root-p create dbname

Shell> mysqldump-h host-u root-p dbname <dbname_backup. SQL

If you only want to unload the table creation command, the command is as follows:

Shell> mysqladmin-u root-p-d databasename> a. SQL

If you only want to unload the SQL command for inserting data without the table creation command, the command is as follows:

Shell> mysqladmin-u root-p-t databasename> a. SQL

What should I do if I only want data and do not want any SQL commands?

Mysqldump-T./phptest driver

Only when the-T parameter is specified can the plain text file be detached, indicating the directory where the data is detached and./indicates the current directory, that is, the same directory as mysqldump. If no driver table is specified, the data of the entire database is detached. Each table generates two files, one of which is a. SQL file, including table creation and execution. The other is a. txt file that only contains data and does not contain SQL commands.

5. You can store the query in a file and tell mysql to read the query from the file instead of waiting for keyboard input. You can use the shell to type the redirection utility to do this. For example, if the file my_file. SQL contains a query

Query:

For example, if you want to write a table prefix in SQL .txt:

Mysql> mysql-h myhost-u root-p database <SQL .txt

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.