Mysql Database Study Notes

Source: Internet
Author: User
Login MySQL:-hlocalhost-uroot-ptest password must have no space before, otherwise let you re-enter the password mysql-h110.110.110.110-uroot-p123, the same applies to others. & nbsp; MySQL Command collection 1. Connect to MYSQL. Format: mysql-h host address-u user name-p User Password 1. log on to MySQL:

-H localhost-u root-ptest must have no space before the password; otherwise, you must enter the password again.

Mysql-h110.110.110.110-u root-p 123;

MySQL Command collection

1. Connect to MYSQL.

Format: mysql-h host address-u user name-p User Password

1. Connect to MYSQL on the local machine.

First open the DOS window, then enter the directory mysqlbin, then type the command mysql-u root-p, and press enter to prompt you to enter

Password. Note that there can be space or space before the user name, but there must be no space before the password, otherwise you will be re-entered


If you have just installed MYSQL, the Super User root does not have a password, so you can directly press enter to enter MYSQL, MYSQL

The prompt is: mysql>

2. Connect to MYSQL on the remote host. Assume that the IP address of the remote host is, the user name is root, and the password is

It 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 directory mysqlbin in 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.

Bundle character)

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

1. Add a user test1 with the password abc so that he can log on to any host and query and insert all databases.

Permission to import, modify, or delete data. 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 user is very dangerous. If someone knows the password of test1, then he can

Log on to your mysql database on any computer and do whatever you want. For the solution, see 2.

2. Add a user named "test2" with the password "abc" so that the user can only log on to localhost, and perform operations on the Database "mydb ".

Query, insert, modify, and delete operations (localhost refers to the local host, that is, the host where the MYSQL database is located ),

In this way, the user knows the password of test2 and cannot directly access the database from the internet.

The web page on the machine is accessed.

Grant select, insert, update, delete on mydb. * to [email = 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 [email = test2 @ localhost]

] Identified by "";

Next I will perform database operations in MYSQL. Note: you must first log on to MYSQL.

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 do not need to re-run the command. You only 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. In fact, we use this database for operation.


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 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 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 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, and then copy them to c,

In the DOS Status, 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 above command has been debugged. You only need


(2) You can use mysql> source c: \ school. SQL after entering the command line, or import the school. SQL file.


4. Transfer text data to the database

1. Text data should conform to the format: field data should be separated by the tab key, and null values should be [url = file: // \ n] \ n

[/Url] to replace. 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 and

Use the use command to create the database where the table is located.

V. Back up the database: (run the command in the DOS [url = file: // \ mysql \ bin] \ mysql \ bin [/url] Directory.


1. Export the entire database

The exported file is stored in the mysqlbin 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



The data type tinyint smallint does not have varchar2, and only char and varchar are used to store strings.


Composite Type

Enum multiple select one Enumeration type

Set multiple choice


<=> Null is safe and matches regexp or rlike.

Aggregate functions (grouping functions Oracle)

Trim (str) removes the space replace (str. Srchstr, rplcstr) replace string str with string rplcstr

All the characters in srchstr.

There can only be one database instance in Oracle, and multiple mysql instances

Set autocommit = 0 is automatically submitted for mysql Default transactions;

Create database zhang;

Use zhang;

Show tables;

Create table test (id int primary key auto_increment, name varchar (15 ));

Select now () does not have an incomplete structure now () Current Time

When the maximum data length is exceeded, the maximum data length is automatically truncated.

When decima does not write a parameter, the default decimal (10, 0)

Mysql is case-insensitive and binary is strictly matched with where binary name = 'zhang'

Insert into test values (), (), () insert multiple rows at the same time.

If time is not given a symbol, it will be searched from the back to the back, and storage will be given for the timing.

Datetime is null.

System Time p85 when timestamp is null



Null + any number = null

Any value is null for division of 0.

If it can be converted to a number, it will be converted. If it cannot, it will be truncated. Starts from the beginning. If not, it is 0.

'A' = 'A' 1 binary 'A' = 'A' 0

If one side is a number, the string is converted back to a number.

If null is involved, null is returned. Null = null is 1

The preceding space cannot be saved, and the following space can be saved. Null in (2, null) is null <=> null is 1 2 <=> null


Regexp ^ indicates the start of a field; ^ ham

Md5 () irreversible encryption algorithm

Group_concat () P121 combines other information with the same attribute 10 abc, def, ghi

SQL script

Source d: 1. SQL execute the SQL script

Select * from table name limit starts from 0 and takes 10 records --- the number starts from scratch.

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: 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.