MySQL command Daquan

Source: Internet
Author: User
Tags mysql host mysql update

Format: mysql-h host address-u user name-P user Password
1. Connect to MySQL on this machine. First open the DOS window, then enter the directory Mysql\bin, and then type the command Mysql-u root-p, enter after the prompt you to lose the password. Note that the user name can have a space or no space, but before the password must have no space, or let you re-enter the password.
If you have just installed MySQL, superuser root is no password, so the direct return to enter the MySQL, MySQL prompt is: mysql>
2. Connect to MySQL on the remote host. Assume the remote host IP is: 110.110.110.110, the user name is root, the password is abcd123. Type the following command: Mysql-h110.110.110.110-u root-p 123; (Note: You can use no space between the root and the other)
3. Quit MySQL command: Exit (Enter) 2, change password format: mysqladmin-u user Name----old password password new password
1, add a password to root ab12. First enter directory Mysql\bin under DOS, and then type the following command mysqladmin-u Root-password ab12 Note: Because Root does not have a password at the beginning, the-p old password can be omitted.
2, then change the root password to djg345. Mysqladmin-u root-p ab12 Password djg3453, add new users Note: Unlike the above, the following is because it is a command in the MySQL environment, so it is followed by a semicolon as a command terminator
Format: Grant Select on database. * To User name @ login host identified by "password"
1, add a user test1 password for ABC, so that he can log on any host, and all databases have query, insert, modify, delete permissions. First connect to MySQL with the root user, and then type the following command:Grant Select,insert,update,delete on * * to [[email protected] '%][email protected] '%[/email] ' identified by ' ABC ';
But the added user is very dangerous, you want to like someone to know test1 password, then he can be on any computer on the Internet to log into your MySQL database and your data can do whatever you like, solution see 2.
2, add a user test2 password for ABC, so that he can only login on localhost, and the database mydb can query, insert, modify, delete operations (localhost refers to the local host, that is, the MySQL database host), so that the user knows Test2 's password, and he cannot access the database directly from the Internet, but only through a Web page on the MySQL host. Grant Select,insert,update,delete on mydb.* to [[E-mail Protected]][email protected][/email] identified by "ABC";
If you do not want to test2 have a password, you can call another command to erase the password. Grant Select,insert,update,delete on mydb.* to [[E-mail Protected]][email protected][/email] identified by ""; 4.1 Creating a Database Note: Connect to the MySQL server before creating the database
Command: Create databases < database name >
Example 1: Establishing a database named Xhkdbmysql> CREATE DATABASE xhkdb;
Example 2: Creating a database and assigning users
①create database name;
②grant select,insert,update,delete,create,drop,alter on database name. * To database name @localhost identified by ' password ';
③set PASSWORD for ' database name ' @ ' localhost ' = old_password (' password ');
Execute 3 commands in turn to complete the database creation. Note: The Chinese "password" and "database" are the user's own needs to set. 4.2 Show Database command: Show databases (note: Finally there is a s) mysql> show databases;
Note: To modify the default encoding of the database in order to not display the garbled characters. The following is an example of the GBK encoding page:
1. Modify the MySQL configuration file:My.ini inside Modify DEFAULT-CHARACTER-SET=GBK 2. Code Runtime Modification:①java Code: jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=gbk②php Code: Header ("Content-Type:    text/html;charset=gb2312 "); ③c language code: int mysql_set_character_set (mysql * mysql, char * csname); This function is used to set the default character set for the current connection. The string csname specifies 1 valid character set names. Connection proofing becomes the default proofing for character sets. This function works similarly to the set names statement, but it also sets the value of mysql-> CharSet, which affects the character sets set by the Mysql_real_escape_string (). 4.3 Delete database command: Drop databases < database name > For example: Delete the database named Xhkdb mysql> drop DB xhkdb;
Example 1: Delete a database that has been determined to exist mysql> drop databases drop_database; Query OK, 0 rows Affected (0.00 sec)
Example 2: Delete a database that is not deterministic mysql> drop DB drop_database; ERROR 1008 (HY000): Can ' t drop database ' drop_database ';    Database doesn ' t exist//error occurred, cannot delete ' drop_database ' databases, the database does not exist.    Mysql> drop database if exists drop_database;    Query OK, 0 rows affected, 1 Warning (0.00 sec)//generates a warning stating that this database does not exist mysql> create database drop_database;    Query OK, 1 row Affected (0.00 sec) mysql> drop database if exists drop_database;//if exists determine if the databases exist, do not exist, and do not produce errors Query OK, 0 rows Affected (0.00 sec) 4.4 Connection Database command: Use < database name >
For example: If the XHKDB database exists, try to access it: mysql> use XHKDB; Screen tip: Database changed
The Use statement can advertise that MySQL uses the Db_name database as the default (current) database for subsequent statements. The database remains as the default database until the end of the segment, or until a different use statement is published: Mysql> use DB1;   Mysql> SELECT COUNT (*) from mytable;    # selects from db1.mytable mysql> use DB2;   Mysql> SELECT COUNT (*) from mytable; # selects from db2.mytable
using the USE statement to mark a specific current database does not prevent you from accessing tables in other databases. The following example accesses the author table from the DB1 database and accesses the edit table from the DB2 database: mysql> use DB1; Mysql> SELECT author_name,editor_name from Author,db2.editor, WHERE author.editor_id = Db2.editor.ed itor_id;
The USE statement is set up to be compatible with Sybase.
Some netizens asked, how to quit after the connection. In fact, you do not have to quit, use the database, using show databases can query all the database, if you want to jump to other databases, use the other database name. 4.5 The currently selected database command:mysql> select Database ();
The select command in MySQL is similar to print or write in other programming languages, and you can use it to display the results of a string, a number, a mathematical expression, and so on. How do I use the special features of the Select command in MySQL?
1. Display the MySQL versionMysql> select version (); +-----------------------+ | Version () | +-----------------------+ | 6.0.4-alpha-community | +-----------------------+ 1 row in Set (0.02 sec)
2. Display the current timeMysql> Select Now (); +---------------------+ | Now () | +---------------------+ | 2009-09-15 22:35:32 | +---------------------+ 1 row in Set (0.04 sec)
3. Display DateSELECT DayOfMonth (current_date); +--------------------------+ | DayOfMonth (current_date) |                       +--------------------------+ | 15 | +--------------------------+ 1 row in Set (0.01 sec) SELECT MONTH (current_date); +---------------------+ | MONTH (current_date) |                   +---------------------+ | 9 | +---------------------+ 1 row in Set (0.00 sec) SELECT Year (current_date); +--------------------+ | Year (current_date) |               +--------------------+ | 2009 | +--------------------+ 1 row in Set (0.00 sec)
4. Display StringMysql> Select "Welecome to my blog!"; +----------------------+ | Welecome to my blog! | +----------------------+ | Welecome to my blog! | +----------------------+ 1 row in Set (0.00 sec)
5. When the calculator usesSelect ((4 * 4)/10) + 25; +----------------------+ | ((4 * 4)/10) + 25 |                +----------------------+ | 26.60 | +----------------------+ 1 row in Set (0.00 sec)
6. Threaded stringSelect CONCAT (F_name, "", L_name) as name from Employee_data where title = ' Marketing Executive '; +---------------+ | Name | +---------------+ | Monica Sehgal | | Hal Simlai | | Joseph Irvine | +---------------+ 3 rows in Set (0.00 sec) Note: the concat () function is used here to string strings together. In addition, we used the previously learned as to give the result column ' CONCAT (f_name, "", L_name) ' a pseudonym. 5.1 Creating a data Table command: CREATE table < table name > (< Field name 1> < type 1> [,.. < Field name N> < type n>]);
For example, to create a table named MyClass,
Field name Number Type Data width is empty Whether the primary key Automatically add Default value
Id Int 4 Whether Primary key Auto_increment
Name Char 20 Whether
Sex Int 4 Whether 0
Degree Double 16 Is
Mysql> Create TableMyClass (> ID int (4) NOT NULL PRIMARY KEY Auto_increment, > Name char () NOT NULL, > Sex int (4) is not NULL default' 0 ', > Degree double (16,2)); 5.3 Delete data Table command: DROP table < table name >
For example: Delete table named MyClass table mysql> drop table MyClass;
DROP table is used to cancel one or more tables. You must have drop permissions for each table. All table data and table definitions are canceled, so use this statement with caution!
Note: For a partitioned table, the DROP table permanently cancels the tables definition, cancels the partitions, and cancels all the data stored in those partitions. DROP table also cancels the partition definition (. par) file that is associated with the canceled table.
For tables that do not exist, use the if exists to prevent errors from occurring. When using the if exists, a note is generated for each table that does not exist.
Restrict and cascade can make partitioning easier. Currently, restrict and cascade do not work. 5.4 Table Insert Data command: INSERT into < table name > [(< Field name 1>[,.. < field name n >])] VALUES (value 1) [, (value N)]
For example: Insert two records into table MyClass, these two records indicate that: 1 is named Tom with a score of 96.45, 2 for the named Joan, and 82.99 for the number 3. mysql> INSERT INTO MyClass values (1, ' Tom ', 96.45), (2, ' Joan ', 82.99), (2, ' Wang ', 96.59);
Note: INSERT into only one record can be inserted into the table at a time. 5.5 Querying the data in a table 1), query all rowsCommand: Select < Field 1, field 2,...> from < table name > where < expression > For example: View all data in table MyClass mysql> select * from Mycla ss
2), query the first few rows of dataFor example: View the first 2 rows of data in table MyClass mysql> select * FROM MyClass ORDER by ID limit 0, 2;
Select is typically used with where to query for more accurate and complex data. 5.6 Delete Table Data command: Delete from table name where expression
For example, delete the record numbered 1 in table MyClass mysql> delete from MyClass where id=1;
Here is a comparison of the table before and after deleting the data. Age
FirstName LastName
Peter Griffin 35
Glenn Quagmire 33
The following is an example of PHP code to delete all Lastname= ' Griffin ' records in the "Persons" table:
<?php    $con = mysql_connect ("localhost", "Peter", "abc123");    if (! $con)    {die      (' Could not connect: '. Mysql_error ());    }    mysql_select_db ("my_db", $con);    mysql_query ("DELETE from Persons WHERE lastname= ' Griffin '"); Mysql_close ($con);?>
After this deletion, the table is like this: Age
FirstName LastName
Glenn Quagmire 33
5.7 Modify data syntax in table: Update table name SET field = new value,... where Condition mysql> update MyClass set name= ' Mary ' where id=1;
Example 1: MySQL UPDATE statement for single table:UPDATE [low_priority] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE Where_definition] [ORDER by ...] [LIMIT Row_count]
Example 2: Multi-table UPDATE statement:UPDATE [low_priority] [IGNORE] table_references SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE Where_definition]
The update syntax can update the columns in the original table row with the new values. The SET clause indicates which columns to modify and which values to give. The WHERE clause specifies which rows should be updated. If there is no WHERE clause, all rows are updated. If an ORDER BY clause is specified, the row is updated in the order specified. The limit clause is used to limit the number of rows that can be updated, given a limit value. 5.8 Add Field command: ALTER TABLETable name Addfield type Other; For example, a field passtest is added to table MyClass, the type is int (4), and the default value is 0 mysql> ALTER TABLEMyClass AddPasstest Int (4 ) Default' 0 '
Gazzo PrimerMysql> ALTER TABLE name add index index name (field name 1[, field Name 2 ...]); Example: mysql> ALTER TABLE employee ADD index emp_name (name);
index of the plus primary keywordMysql> ALTER TABLE name add primary key (field name); Example: mysql> ALTER TABLE employee ADD primary key (ID);
index with unique restriction criteriaMysql> ALTER TABLE name add unique index name (field name); Example: mysql> ALTER TABLE employee add unique emp_name2 (cardnumber);
Delete an indexmysql> ALTER TABLE name DROP INDEX name; Example: Mysql>alter table employee DROP index emp_name;
Add Field:mysql> ALTER TABLE table_name ADD field_name Field_type;
Modify the original field name and type:mysql> ALTER TABLE table_name change old_field_name new_field_name field_type;
To delete a field:MySQL ALTER TABLE table_name DROP field_name;5.9 Modify table name command: Rename table name to new table name;
For example: Change the name in table MyClass to youclass mysql> rename table MyClass to Youclass;
When you execute RENAME, you cannot have any locked tables or active transactions. You must also have ALTER and DROP permissions on the original table, as well as CREATE and INSERT permissions on the new table.
If MySQL encounters any errors in the multi-table renaming, it reverses all renamed tables, returning everything to its original state.
RENAME TABLE was added to MySQL 3.23.23. 6. Backup DATABASE command executes in DOS [Url=file://\\mysql\\bin]\\mysql\\bin[/url] Directory
1. Export the entire databaseThe export file defaults to the existence of the Mysql\bin directory under mysqldump-u username-P database name > exported filename mysqldump-u user_name-p123456 database_name > OUTF Ile_name.sql
2. Export a tableMysqldump-u user name-P database name table name > exported filename mysqldump-u user_name-p database_name table_name > OUTFILE_NAME.SQL
3. Export a database structureMysqldump-u user_name-p-d–add-drop-table database_name > outfile_name.sql-d No data –add-drop-table before each CREATE statement Add a drop table
4. Export with language parametersmysqldump-uroot-p–default-character-set=latin1–set-charset=gbk–skip-opt database_name > Outfile_name.sql
For example, back up the AAA library to file back_aaa: [[email protected] root]# cd/home/data/mysql [[email protected] mysql]# mysqldump-u root-p --opt AAA > back_aaa7.1 An example of building a library and building a table 1drop database if exists school; Delete Create Database school if school is present; Build Library school Use school; Open the Library school CREATE TABLE teacher//Set up tables teacher (ID int (3) auto_increment NOT null primary key, name Char (TEN) not n ull, Address varchar (+) Default ' Shenzhen ', year date); End of Build table
The following inserts the Insert field INSERT INTO teacher values (", ' Allen ', ' Dalian One ', ' 1976-10-10′ '); Insert into teacher values (", ' Jack ', ' Dalian II ', ' 1975-12-23′ ');
It is also possible to type the above commands at the MySQL prompt, but it is not easy to debug. 1, you can write the above command as is written in a text file, assuming that school.sql, and then copied to c:\\, and in the DOS state into the directory [Url=file://\\mysql\\bin]\\mysql\\bin[/url], Then type the following command: mysql-uroot-p password < c:\\school.sql If successful, empty a row without any display, if there is an error, you will be prompted. (The above command has been debugged, you can use it only if you remove//comment).
2, or enter the command line after using mysql> source C:\\school.sql; You can also import the School.sql file into the database. 7.2 An instance of building and building a table 2drop database if exists school; Delete Create Database school if school is present; Build Library school Use school; Open the Library school CREATE TABLE teacher//Set up tables teacher (ID int (3) auto_increment NOT null primary key, name Char (TEN) not n ull, Address varchar (+) Default "Shenzhen", year date); End of Build table
The following inserts the Insert field INSERT INTO teacher values ("'", ' Glchengang ', ' ' Shenzhen One ', ' 1976-10-10 '); Insert into teacher values (' ' ', ' Jack ', ' Shenzhen One ', ' 1975-12-23 ');
Note: In the table 1, set the ID to a number field of length 3: Int (3) and let it automatically add one to each record: auto_increment; not null; and let him be the main field primary key.
2. Set name to a character field of length 10
3. Set address as the character field of length 50, and the default value is Shenzhen.
4. Set year as the Date field.

MySQL command Daquan

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.