ViewDatabaseSelect version (), current_date ();
Show databases for the database;
Select use db_1 for a database;
Create db_1 for creating a database;
Insert into db_1 values (...);
Select database () is used for the selected database ();
Create Table tb_1 (...) for creating a table (...);
Show tables is used for all tables;
Show all the contents of a table select * From tb_1;
Select * From tb_1 where name = 'hes' for a table that meets the conditions is displayed ';
Show all mysql users select user, host, password from mysql. user;
Add new user: grant permission on database. * to user name @ login host identified by "password"
For example, you can add a user user1 with the password password1 so that the user can log on to the machine and have the permission to query, insert, modify, and delete all databases. First, use the root user to connect to MySQL, and then type the following command:Grant select, insert, update, delete on *. * To user1 @ localhost identified by "password1 ";
If you want the user to log on to MySQL on any machine, change localhost to "% ".
If you do not want user1 to have a password, you can run another command to remove the password.
Grant select, insert, update, delete on mydb. * To user1 @ localhost identified "";
The following command is used for simple mathematical computation:
Mysql> select (20 + 5) * 4;
Mysql> select (20 + 5) * 4, sin (PI ()/3 );
Mysql> select (20 + 5) * 4 as result, sin (PI ()/3); (as: Specify the alias as result)
Multiple commands in one line are separated by semicolons: mysql> Select User (); select now ();
When the command input is wrong and cannot be changed (multi-line statement), you can use C to cancel the command before the semicolon appears.
Connect to MySQL: mysql-H host address-u user name-P User Password
1. Connect to MySQL on the local machine.
First, open the DOS window, enter the directory MySQL/bin, and then type the command mysql-u root-P. Note that there is no semicolon after the sentence. You can omit-H localhost during local login. After you press enter, you will be prompted to enter the password. Note that there can be space or no space before the user name, but there must be no space before the password; otherwise, you will be asked to re-enter the password.
If you have just installed MySQL, the Super User Root has no password, so press enter to enter mysql. The MySQL prompt is: mysql>
2. Connect to MySQL on the remote host. Assume that the IP address of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Enter the following command:
Mysql-h110.110.110.110-u root-P 123)
3. Exit MySQL command: exit (Press ENTER)
2. Change the password.
Format: mysqladmin-u username-P old Password New Password
1. Add a password ab12 to the root user. First, enter the MySQL/bin directory under DOS, and then type the following command
Mysqladmin-u root-Password ab12
Note: because the root account does not have a password at the beginning, the old-P password can be omitted.
2. Change the root password to djg345.
Mysqladmin-u root-P ab12 password djg345
3. Add new users.
(Note: Unlike the above, the following commands in the MySQL environment are followed by a semicolon as the command Terminator)
Format: grant select on database. * To username @ login host identified by "password"
1. Add a user named "test1" with the password "ABC" so that he can log on to any host and have the permission to query, insert, modify, and delete all databases. First, use the root user to connect to MySQL, and then type the following command:
Grant select, insert, update, delete on *. * to [email = test1 @ "%] test1 @" % [/Email] "identified by" ABC ";
However, the added users are very dangerous. If someone knows the password of test1, then he can log on to your MySQL database on any computer on the Internet and do whatever he wants for your data. See solution 2.
2. Add a user named "Test2" with the password "ABC" so that the user can only log on to localhost and query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host of the MySQL database ),
In this way, the user knows the password of Test2 and cannot directly access the database from the Internet. He can only access the database through the web page on the MySQL host.
Grant select, insert, update, delete on mydb. * to [email = Test2 @ localhost] Test2 @ localhost [/Email] 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 [email = Test2 @ localhost] Test2 @ localhost [/Email] identified by "";
Next I will perform database operations in MySQL. Note: you must first log on to MySQL. The following operations are performed at the MySQL prompt and each command ends with a semicolon.
I. Operation Skills
1. If you forget the extra points after you press Enter when making the command, you don't have to repeat the command. You just need to press a semicolon to press Enter.
That is to say, you can divide a complete command into several lines, and then use a semicolon as the end sign to complete the operation.
2. You can use the cursor to bring up or down the previous commands.
Ii. Display commands
1. display the list of databases on the current database server:
Mysql> show databases;
Note: the MySQL database contains the MySQL system information. We change the password and add new users to use this database for operations.
2. display data tables in the database:
Mysql> Use Database Name;
Mysql> show tables;
3. display the data table structure:
Mysql> describe table name;
4. Create a database:
Mysql> Create Database database name;
5. Create a data table:
Mysql> Use Database Name;
Mysql> Create Table Name (field name varchar (20), field name char (1 ));
6. delete a database:
Mysql> drop database database name;
7. delete a data table:
Mysql> drop table name;
8. Clear records in the table:
Mysql> Delete from table name;
9. display the records in the table:
Mysql> select * from table name;
10. insert records into the table:
Mysql> insert into table name values ("hyq", "M ");
11. Update table data:
Mysql-> Update table name: Set field name: 1 = 'a'; field name: 2 = 'B' where field name: 3 = 'C ';
12. load data into a data table in text mode:
Mysql> load data local infile "D:/mysql.txt" into Table table name;
13. Import the. SQL FILE command:
Mysql> Use Database Name;
Mysql> source D:/MySQL. SQL;
14. Change the root password on the command line:
Mysql> Update mysql. User SET Password = PASSWORD ('new password') where user = 'root ';
Mysql> flush privileges;
15. display the Database Name of use:
Mysql> select database ();
16. display the current user:
Mysql> Select User ();
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 (", 'allen ', 'dalian Zhongyi', '2017-10-10 ′);
Insert into teacher values (", 'jack', 'dalian No. 2 middle school ', '2017-12-23 ′);
If you type the preceding command at the MySQL prompt, debugging is not convenient.
(1) You can write the above commands into a text file as they are, for example, school. SQL, then copy to C: //, and enter the directory [url = file: //// MySQL // bin] // MySQL // bin [/url], 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 preceding command has been debugged. You only need to remove the // annotation to use it ).
(2) You can use mysql> source C: // school. SQL after entering the command line. You can also import the school. SQL file to the database.
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 values are separated by [url = file: /// N] // n [/url] instead. example:
3 rose Dalian No. 2 Middle School 1976-10-10
4 Mike Dalian No. 1 1975-12-23
Assume that you save these two sets of data as a school.txt file and put it under the C-drive root directory.
2. Data input command load data local infile "C: // school.txt" into table name;
Note: You 'd better copy the file to the [url = file: // MySQL // bin] // MySQL // bin [/url] Directory, use the use command to create the database where the table is located.
V. Back up the database: (run the command in the DOS directory [url = file: // MySQL // bin] // MySQL // bin [/url)
1. Export the entire database
The exported files are stored in the MySQL/bin directory by default.
Mysqldump-u username-P Database Name> exported file name
Mysqldump-u user_name-p123456 database_name> outfile_name. SQL
2. Export a table
Mysqldump-u user name-P database name Table Name> exported file name
Mysqldump-u user_name-P database_name table_name> outfile_name. SQL
3. Export a database structure
Mysqldump-u user_name-p-D-add-drop-Table database_name> outfile_name. SQL
-D no data-add-drop-table add a drop table before each create statement
4. Export with language Parameters
Mysqldump-uroot-p-default-character-set = Latin1-set-charset = GBK-Skip-opt database_name> outfile_name. SQL