In fact, MYSQL's database operations are similar to those of other SQL-type databases. you 'd better find a book that describes SQL. 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. it is not only free to download from every related website, but also the most authoritative.
In fact, MYSQL's database operations are similar to those of other SQL-type databases. you 'd better find a book that describes SQL. 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. it is not only free to download from every related website, but also the most authoritative.
MYSQL beginner's guide and introduction
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 mysqlbin, then type the command mysql-uroot-p, and press enter to prompt you to enter the password. if you have just installed MYSQL, super User root has no password, so press enter to 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 110.
. 110.110.110, the user name is root, and the password is abcd123. Enter the following command:
Mysql-h110.110.110.110-uroot-pabcd123
(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 (do not add a command character in password)
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
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"
Example 1: add a user named "test1" with the password "abc" so that the user 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 test1 @ "%" Identified by "abc ";
However, the user added in Example 1 is 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. 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, you can also query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located), so that the user knows the password of test2, he cannot access the database directly from the internet, but can only access the database through the web pages 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 "";
In the previous article, we talked about logon, adding users, and changing passwords. Next, let's take a look at the 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. 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 the MYSQL system information. we change the password and add new users to use this database for operations.
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 name;
5. create a table:
Use database name;
Create table name (field setting list );
6. delete databases and tables:
Drop 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 of 3: int (3) and let it automatically add one: auto_increment for each record. it cannot be blank: not null and set it to 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 discussed later.
(4) set YEAR as the date field.
If you type the preceding command at the mysql prompt, debugging is not convenient. You can write the above commands as they are in a text file, assume it is school. SQL, then copy it to c: \, and enter \ mysql \ bin in the DOS state, 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 ).
4. transfer text data to the database
1. text data should conform to the format: field data is separated by the tab key, and the null value is replaced by \ n.
Example:
3 rose:
4 mike: Shenzhen No. 1,-12-23
2. data import command load data local infile "file name" into table name;
Note: You 'd better copy the file to the \ mysql \ bin directory and use the use command to create the database where the table is located.
V. back up the database: (execute the command in the \ mysql \ bin directory of DOS)
Mysqldump -- opt school> school. bbb
Note: back up the database school to the school. bbb file. school. bbb is a text file with any file name. open it and you will find new discoveries.
Note: In fact, MYSQL's database operations are similar to those of other SQL databases. you 'd better read this SQL book. 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. it is not only free to download from every related website, but also the most authoritative. It is a pity that the chm format is not like the "PHP4 Chinese Manual", and it is not convenient to search for function commands.