Home > php教程 > PHP开发 > body text

A complete list of commonly used MySQL commands

高洛峰
Release: 2016-12-14 11:07:49
Original
1586 people have browsed it

The following are MySQL commands that we often use and are very useful. Below you see # means executing the command under the Unix command line, and seeing mysql> means that you are currently logged in to the MySQL server and the mysql command is executed on the mysql client.
Log in to MySQL. If you want to connect to a remote database, you need to specify the hostname with -h.

# [mysql dir]/bin/mysql -h hostname -u root -p
Copy after login

Create a database.

mysql> create database [databasename];
Copy after login

List all databases.

mysql> show databases;
Copy after login

Switch to a database.

mysql> use [db name];
Copy after login

Display all tables of a database.

mysql> show tables;
Copy after login

View the field format of the data table.

mysql> describe [table name];
Copy after login

Delete a database.

mysql> drop database [database name];
Copy after login

Delete a data table.

mysql> drop table [table name];
Copy after login

Display all data of a data table.

mysql> SELECT * FROM [table name];
Copy after login

Returns the column information of the specified data table.

mysql> show columns from [table name];
Copy after login

Use the value "whatever" to filter to display selected certain rows.

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
Copy after login

Displays all records containing the name "Bob" and the phone number "3444444".

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
Copy after login

Displays all records that do not contain name "Bob" and phone number "3444444", and sort by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
Copy after login

Displays all records whose name starts with the letters "bob" and whose phone number is "3444444".

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';
Copy after login

Displays the 1st to 5th records whose name starts with the letters "bob" and whose phone number is "3444444".

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
Copy after login

Find records using regular expressions. Use "regex binary" to force case sensitivity. This command finds any record starting with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
Copy after login

Return unique records that are different.

mysql> SELECT DISTINCT [column name] FROM [table name];
Copy after login

Display selected records in ascending or descending order.

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
Copy after login

Returns the number of rows.

mysql> SELECT COUNT(*) FROM [table name];
Copy after login

Counts the sum of the specified column values.

mysql> SELECT SUM(*) FROM [table name];
Copy after login

Join table.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
Copy after login

Create a new user. Log in as root. Switch to the mysql database, create a user, and refresh permissions.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
Copy after login

Change user password from unix command line.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
Copy after login

Change user password from mysql command line. Log in as root, set password, and update permissions.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
Copy after login

Set the root password when the root password is empty.

# mysqladmin -u root password newpassword
Copy after login

Update root password.

# mysqladmin -u root -p oldpassword newpassword
Copy after login

Allow user "bob" to connect to the server from localhost with password "passwd". Log in as root and switch the mysql database. Set permissions, update permissions.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;
Copy after login

Set permissions for database db. Log in as root, switch to the mysql database, grant permissions, and update permissions.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
Copy after login

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
Copy after login

Update data in an existing table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
Copy after login

Delete rows in the table where [field name] = ‘whatever’.

mysql> DELETE from [table name] where [field name] = 'whatever';
Copy after login

Update permissions/privileges on database.

mysql> flush privileges;
Copy after login

Delete columns.

mysql> alter table [table name] drop column [column name];
Copy after login

Add column to db.

mysql> alter table [table name] add column [new column name] varchar (20);
Copy after login

Change column names.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);
Copy after login

Add unique column.

mysql> alter table [table name] add unique ([column name]);
Copy after login

Set the column value larger.

mysql> alter table [table name] modify [column name] VARCHAR(3);
Copy after login

Remove unique columns.

mysql> alter table [table name] drop index [colmn name];
Copy after login

Import a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
Copy after login

Export all databases to sql files.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
Copy after login

Export a database.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
Copy after login

Export a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
Copy after login

Restore database (data table) from sql file.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
Copy after login

Create data table example 1.

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
Copy after login

Create data table example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default &#39;bato&#39;);
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template