MySQL command line Common commands

Source: Internet
Author: User
Tags select from where import database mysql command line
Common MySQL operations: The following are all tests passed under mysql5.0. First, note that you must add them at the end of each command. (semicolon)
1. Export the entire database
Mysqldump-u username-p -- default-character-set = Latin1 Database Name> exported file name (the default database encoding is Latin1)
Mysqldump-u wcnc-P smgp_rj_wcnc> wcnc. SQL
2. Export a table
Mysqldump-u user name-P database name Table Name> exported file name
Mysqldump-u wcnc-P smgp_rj_wcnc users> wcnc_users. SQL
3. Export a database structure
Mysqldump-u wcnc-p-D-add-drop-Table smgp_apps_wcnc> D: wcnc_db. SQL
-D no data-add-drop-table add a drop table before each create statement
4. Import the database
Common source commands
Go to the MySQL Database Console,
For example, MySQL-u root-P
Mysql> Use Database
Then run the source command. The following parameter is the script file (for example,. SQL used here)
Mysql> source D: wcnc_db. SQL

1. Start and exit
1. Go to MySQL: Start MySQL command line client (MySQL dos Interface) and enter the password for installation. The prompt is: mysql>
2. Exit MYSQL: Quit or exit
Ii. Database Operations
1. Create a database
Command: Create Database
For example, create a database named xhkdb.
Mysql> Create Database xhkdb;
2. display all databases
Command: Show databases (Note: The last S is available)
Mysql> show databases;
3. delete a database
Command: DROP DATABASE
For example, delete a database named xhkdb.
Mysql> drop database xhkdb;
4. Connect to the database
Command: Use
For example, if the xhkdb database exists, try to access it:
Mysql> Use xhkdb;
On-screen prompt: Database changed
5. Database Selected (connected)
Mysql> select database ();

6. Table information contained in the current database:
Mysql> show tables; (Note: There is a last S)

Iii. Table operations. A database should be connected before operations
1. Create a table
Command: Create Table ([,...]);

Mysql> Create Table myclass (
> ID int (4) not null primary key auto_increment,
> Name char (20) not null,
> Sex int (4) not null default ''0 '',
> Degree double (16, 2 ));
2. Get the table structure
Command: DESC table name or show columns from Table Name
Mysql> describe myclass
Mysql> DESC myclass;
Mysql> show columns from myclass;
3. delete a table
Command: Drop table
For example, delete a table named myclass.
Mysql> drop table myclass;
4. insert data
Command: insert into [([,...])] values (value 1) [, (value n)]
For example, insert two records into the myclass table. The two records indicate that the result of Tom numbered 1 is 96.45, and the result of Joan numbered 2 is 82.99, wang, numbered 3, scored 96.5.
Mysql> insert into myclass values (1, ''tom '', 96.45), (2, ''joand'', 82.99), (2, ''wang '', 96.59 );
5. query the data in the table
1) query all rows
Command: select from where
For example, you can view all data in the myclass table.
Mysql> select * From myclass;
2) query the first few rows of data
For example, view the first two rows of data in the myclass table.
Mysql> select * From myclass order by ID limit 0, 2;
6. Delete table data
Command: delete from table name where expression
For example, delete the record numbered 1 in myclass.
Mysql> Delete from myclass where id = 1;
7. Modify Table data: Update table name set field = new value ,... Where condition
Mysql> Update myclass set name = ''mary ''where id = 1;
7. Add fields to the table:

Command: alter table table name, add, other field types;
For example, a passtest field is added to the myclass table. The type is int (4) and the default value is 0.
Mysql> alter table myclass add passtest int (4) default ''0''
8. Change the table name:
Command: rename table original table name to new table name;
For example, the myclass name in the table is changed to youclass.
Mysql> rename table myclass to youclass;

Update field content
Update table name set field name = new content
Update table name set field name = Replace (field name, ''old content'', ''new content '');

Add four spaces before the article
Update article set content = Concat (''', content );

Field Type
1. Int [(m)] type: normal Integer type
2. Double [(m, D)] [zerofill] type: normal size (Double Precision) floating point number type
3. date type: the supported range is 1000-01-01 to 9999-12-31. MySQL displays date values in YYYY-MM-DD format, but allows you to assign values to the date column using strings or numbers
4. Char (m) type: fixed-length string type. When stored, it always fills the Right to the specified length with spaces
5. Blob text type, with a maximum length of 65535 (2 ^ 16-1) characters.
6. varchar: variable-length string type

5. Import database tables
(1) create a. SQL File
(2) first generate a database such as auction. C: mysqlbin> mysqladmin-u root-P creat auction. A prompt is displayed, indicating that the password is entered and created successfully.
(2) import the auction. SQL File
C: mysqlbin> mysql-u root-P auction grant select, insert, delete, create, drop
On *. * (or test. */user .*/..)
To username @ localhost
Identified by ''password '';
For example, to create a new user account for database access, perform the following operations:
Mysql> grant usage
-> On test .*
-> To testuser @ localhost;
Query OK, 0 rows affected (0.15 Sec)
Then a new user named testuser is created. This user can only connect to the database from localhost and connect to the test database. Next, we must specify the operations that the user testuser can perform:
Mysql> grant select, insert, delete, update
-> On test .*
-> To testuser @ localhost;
Query OK, 0 rows affected (0.00 Sec)
This operation enables testuser to perform select, insert, delete, and update queries on the tables in each test database. Now we end the operation and exit the mysql client program:
Mysql> exit
Bye9!

1: Use the show statement to find out the current database on the server:
Mysql> show databases;
2. Create a database named mysqldata
Mysql> Create Database mysqldata;
3: select the database you created
Mysql> Use mysqldata; (when you press the Enter key to see database changed, the operation is successful !)
4: view the tables in the current database
Mysql> show tables;
5. Create a database table
Mysql> Create Table mytable (name varchar (20), sex char (1 ));
6: display the table structure:
Mysql> describe mytable;
7. Add records to the table
Mysql> insert into mytable values ("hyq", "M ");
8: load data into database tables in text mode (for example, D:/mysql.txt)
Mysql> load data local infile "D:/mysql.txt" into Table mytable;
9: import the. SQL file command (for example, D:/MySQL. SQL)
Mysql> use database;
Mysql> source D:/MySQL. SQL;
10: delete a table
Mysql> drop table mytable;
11: Clear the table
Mysql> Delete from mytable;
12: Update table data
Mysql> Update mytable set sex = "F" where name = ''hyq '';

The following are the management experiences of using MySQL on the Internet,
From: http://www1.xjtusky.com/article/htmldata/2004_12/3/57/article_1060_1.html

In Windows, MySQL exists as a service. Before using MySQL, make sure that the service has been started and the Net start MySQL command is not enabled. In Linux, the "/etc/rc. d/init. d/mysqld start" command is available. Note that the initiator must have administrator privileges.

Source: http://www.diybl.com/course/7_databases/mysql/Mysqljs/200855/113189.html

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.