Learn common MySQL operations

Source: Internet
Author: User
Tags mysql commands mysql host mysql tutorial import database

Turn: http://www.68design.net/Development/Database/25689-2.html

Many friends have installed MySQL but do not know how to use it. In this article, we will connect to MySQL,
You can learn some common MySQL commands by changing passwords and adding users.

1. Connect to MySQL.
Format: mysql-H host address-u user name-P User Password
1. Example 1: connect to MySQL on the local machine.
First, open the DOS window, enter the directory mysqlin, and then enter the command mysql-uroot-P
After the car prompts you to lose the password, if you have just installed MySQL, the Super User Root does not have the password, so directly back
Enter mysql. The MySQL prompt is: mysql>
2. Example 2: connect to MySQL on the remote host. Assume that the IP address of the remote host is
The name is root and the password is abcd123. Enter the following command:
(Note: you do not need to add spaces for u and root. The same applies to others)
3. Exit MySQL command: exit (Press ENTER)

2. Change the password.
Format: mysqladmin-u username-P old Password New Password
1. Example 1: Add a password ab12 to the root user. First, enter the directory mysqlbin in DOS, and then type the following command
Mysqladmin-uroot password ab12
Note: because the root account does not have a password at the beginning, the old-P password can be omitted.
2. Example 2: Change the root password to djg345.
Mysqladmin-uroot-pab12 password djg345
Another method:
Shell> mysql-u root-P
Mysql> set password for root = PASSWORD ("root ");

3. Add new users.(Note: Unlike the above, the following commands in the MySQL environment are followed
A semicolon is used as the command Terminator)
Format: grant select on database. * To username @ login host identified by "password"
Example 1: Add a user named "test1" with the password "ABC" so that the user can log on to any host and
Query, insert, modify, and delete permissions. First, use the root user to connect to MySQL, and then enter the following
Grant select, insert, update, delete on *. * To test1 @ "%" identified
By "ABC ";
However, the User Added to Example 1 is very dangerous. If you want someone to know the password of test1, then he can
Log on to your MySQL database on any computer on the Internet and do whatever you want,
For the solution, see Example 2.
Example 2: Add a user named "Test2" with the password "ABC" so that the user can only log on to localhost and access the database
Mydb query, insert, modify, and delete operations (localhost refers to the local host, that is, MySQL DATA
The host where the database is located.
Access the database only through the web page on the MySQL host.
Grant select, insert, update, delete on mydb. * To Test2 @ localhost
Identified by "ABC ";
If you do not want Test2 to have a password, you can run another command to remove the password.
Grant select, insert, update, delete on mydb. * To Test2 @ localhost
Identified "";

Database Operations. Note: you must first log on to MySQL.
And each command ends with a semicolon.

I. Operation Skills
1. If you forget the extra points when you press enter, you do not need to repeat the command. You only need to enter a semicolon to return
The car is enough. That is to say, you can divide a complete command into several lines and end it with a semicolon.
The flag is OK.
2. You can use the cursor to bring up or down the previous commands. However, an old MySQL version I used earlier does not support this feature.
I am using a mysql-3.23.27-beta-win.

Ii. Display commands
1. display the Database List.
Show databases;
At the beginning, there were only two databases: MySQL and test. The MySQL database contains MySQL system information,
We use this database to change the password and add new users.
2. display the data tables in the database:
Use MySQL; // open the database. If you have learned FOXBASE, you will not be unfamiliar with it.
Show tables;
3. display the data table structure:
Describe table name;
4. database creation:
Create Database database name;
5. Create a table:
Use Database Name;
Create Table Name (field setting list );
6. Delete databases and tables:
Drop database database name;
Drop table name;
7. Clear records in the table:
Delete from table name;
8. Display records in the table:
Select * from table name;

3. An instance for creating a database, creating a table, and inserting data
Drop database if exists school; // Delete if school exists
Create Database school; // create a database School
Use school; // open the school library
Create Table teacher // create table teacher
Id int (3) auto_increment not null primary key,
Name char (10) Not null,
Address varchar (50) default 'shenzhen ',
Year date
); // Table creation ends
// Insert fields as follows
Insert into teacher values ('', 'glengang ', 'shenzhen Zhongyi', '2017-10-10 ');
Insert into teacher values ('', 'jack', 'shenzhen Zhongyi ', '2017-12-23 ');

Note: Table Creation
(1) set the ID to a numeric field with a length of 3: int (3), and automatically add auto_increment to each record,
Cannot be blank: not null, and make it the primary key.
(2) Set name to a 10-character field
(3) set address to a 50-character field, and the default value is Shenzhen. What is the difference between varchar and Char?
It will only be said later.
(4) set year as the date field.
If you type the preceding command at the MySQL prompt, debugging is not convenient. You can run the preceding command
It is written as is to a text file, assuming it is school. SQL, then copied to C:, and enter the directory in DOS status
Mysqlin, and then type the following command:
Mysql-uroot-P password <C: School. SQL
If it succeeds, no display is displayed for a blank row. If there is an error, a prompt is displayed. (The above command has been debugged, you
You only need to remove the // annotation ).

4. Transfer text data to the database
1. Text data should conform to the format: field data is separated by the tab key, and null value is used instead. Example:
4 MIKE: Shenzhen No. 1,-12-23
2. Data Import command load data local infile "file name" into Table table name;
Note: You 'd better copy the file to the mysqlin directory and use the use command to select the database where the table is located.

5. export and import data: (the command is executed in the DOS mysqlin directory)
Export table
Mysqldump -- opt school> school. SQL
Note: Back up all the tables in the school database to the school. SQL file. School. SQL is a text file,
Select the file name. open the file to see if you have any new discoveries.
Mysqldump -- opt School Teacher Student> school. Teacher. Student. SQL
Note: Back up the teacher and student tables in the school database to school. Teacher. Student. SQL.

Import table
Mysql> Create Database School;
Mysql> use school;
Mysql> source school. SQL;
(Or change school. SQL to school. Teacher. SQL/school. Teacher. Student. SQL)

Export Database
Mysqldump -- databases db1 DB2> db1.db2. SQL
Note: Back up database DBL and DB2 to the db1.db2. SQL file. db1.db2. SQL is a text file with a file name.
Choose, open it, and you will see new discoveries.
(For example:
Mysqldump-H host-u user-P pass -- databases dbname> file. Dump
Import the database dbname on the host named "user" and "password pass" to file. Dump .)

Import Database
Mysql <db1.db2. SQL

Copy Database
Mysqldump -- all-databases> all-databases. SQL
Note: backing up all databases to a all-databases. SQL file, a all-databases. SQL is a text file,
File Name.

Import Database
Mysql> drop database;
Mysql> drop database B;
Mysql> drop database c;
Mysql> source all-databases. SQL; (or exit MySQL after exiting MySQL mysql <all-databases. SQL)

In fact, MySQL's database operations are similar to those of other SQL databases. You 'd better read this SQL book.
See. Here I will only introduce some basic things. In fact, I only understand these things.
The best MySQL tutorial is the MySQL Chinese reference manual translated by Yan Zi ". Not only is it free, but every related website has
Download, and it is the most authoritative. Unfortunately, unlike the "PhP4 Chinese manual", it is in chm format.
Time is not convenient.

3. Open the database: Use dbname;
Show all databases: Show databases;
Display All Tables in MySQL: use MySQL first, and then show tables;
Displays the table column information: Describe user; (displays the user table information in the MySQL database );

4. Create a full Super User that can connect to the server from any address, but you must use a password something to do this
Grant all privileges on *. * to Monty @ localhost identified by 'something' with grant option;
Grant all privileges on *. * to Monty @ "%" identified by 'something' with grant option;

5. Delete authorization:
Revoke all privileges on *. * from root @ "% ";
Use MySQL;
Delete from user where user = "root" and host = "% ";
Flush privileges;

6. Create a User custom to log on to a specific client webjx.com and access the specific database bankaccount.
Mysql> grant select, insert, update, delete, create, drop on bankaccount .*
To custom@webjx.com identified by 'stupid ';

7. Rename the table:
Alter table T1 rename T2;

To change column A from integer to tinyint not null (same name ),
Change Column B from Char (10) To char (20), rename it, and change from B to C:
Alter table T2 modify a tinyint not null, change B C char (20 );

Add a new timestamp column named D:
Alter table T2 add D timestamp;

Add an index on column D and set column A as the primary key:
Alter table T2 add index (d), add primary key ();

Delete column C:
Alter table T2 drop column C;

Add a new auto_increment integer column named C:
Alter table T2 add c int unsigned not null auto_increment, add index (C );
Note that we have indexed C, because the auto_increment column must be indexed, and we declare C as not null,
Because the indexed Column cannot be null.

8. Delete record:
Delete from T1 where c> 10;

6. Change several rows:
Update T1 set user = weiqiong, password = weiqiong;

7. Create an index with the first 10 characters of the name column:
Create index part_of_name on customer (name (10 ));

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.