Home  >  Article  >  php教程  >  Mysql command list

Mysql command list

高洛峰
高洛峰Original
2016-12-14 10:40:441072browse

1. Connect to the database
Format: mysql -h host address -u username -p user password
1.1. Connect to MYSQL on this machine.
First open the DOS window, then enter the directory mysqlbin, and then type the command mysql -u root -p. After pressing Enter, you will be prompted to enter the password.
Note that the user name may or may not have spaces before it, but there must be no spaces before the password, otherwise it will be You re-enter your password.
If MYSQL has just been installed, the super user root does not have a password, so just press Enter to enter MYSQL. The MYSQL prompt is: mysql>

1.2 Connect to MYSQL on the remote host.

Assume that the IP of the remote host is: 110.110.110.110, the user name is root, and the password is abcd123. Then type the following command:
mysql -h110.110.110.110 -u root -p 123; (Note: There is no need to add a space between u and root, and the same is true for others)
1.3 Exit the MYSQL command: exit (Enter)

2. Add a user
Format: grant select on database.* to username@login host identified by “password”
2.1 Add a user test1 with password abc, so that he can log in on any host and query all databases. Permissions to insert, modify, and delete.

First connect to MYSQL as the root user, and then type the following command:

grant select,insert,update,delete on *.* to [email=test1@”%]test1@”%[/email]” Identified by “abc”;

But adding users is very dangerous. If someone knows the password of test1, then he can log in to any computer on the Internet. Log in to your mysql database and do whatever you want with your data. See 2.2 for the solution.
2.2 Add a user test2 with the password abc, so that he can only log in on localhost, and can query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located), In this way, even if the user knows the password of test2, he cannot directly access the database from the Internet, and can only access it through the web page on the MYSQL host.

grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “abc”;

If you don’t want test2 to have a password, you can type another command to eliminate the password.

grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “”

3. Operating the database

3.1 Create database
Note: You must connect to the Mysql server before creating the database
Command: create database e5c249aa15ff4c64720f99bfaecf8a60
Example 1: Create a database named xhkdb

mysql> create database xhkdb;

Example 2 : Create a database and assign users
①CREATE DATABASE database name;
②GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON database name.* TO username@localhost IDENTIFIED BY 'password';
③SET PASSWORD FOR 'database name '@'localhost' = OLD_PASSWORD('password');
Execute 3 commands in sequence to complete the database creation.
Note: The Chinese “password” and “database” need to be set by the user themselves.

3.2 Show databases
Command: show databases (note: there is an s at the end)

mysql> show databases

3.3 Delete database
Command: drop database e5c249aa15ff4c64720f99bfaecf8a60
For example: delete the database named xhkdb

mysql> drop database xhkdb;

Example 1: Delete a database that is sure to exist

mysql> drop database drop_database;
Query OK, 0 rows affected (0.00 sec)

Example 2: Delete a database that is not sure to exist

mysql> drop database drop_database;
ERROR 1008 (HY000): Can't drop database 'drop_database'; database doesn't exist
//发生错误,不能删除'drop_database'数据库,该数据库不存在。
mysql> drop database if exists drop_database;
Query OK, 0 rows affected, 1 warning (0.00 sec)//产生一个警告说明此数据库不存在
mysql> create database drop_database;
Query OK, 1 row affected (0.00 sec)
mysql> drop database if exists drop_database;//if exists 判断数据库是否存在,不存在也不产生错误
Query OK, 0 rows affected (0.00 sec)

3.4 Connect to the database
Command: use e5c249aa15ff4c64720f99bfaecf8a60
For example: If the xhkdb database exists, try to access it: mysql> ; use xhkdb;
Screen prompt: Database changed
The use statement can notify MySQL to use the db_name database as the default (current) database for subsequent statements. This database remains the default database until the end of the segment, or until a different USE statement is issued:

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 a USE statement to mark a specific current database does not prevent you from accessing tables in other databases. The following example can access the author table from the db1 database and 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.editor_id;

To exit the database or connect to other databases, just user 'other database name'.

3.5 Current database selection
Command: mysql> select database();
The SELECT command in MySQL is similar to print or write in other programming languages. You can use it to display the results of a string, number, mathematical expression, etc. wait. How to use the special features of the SELECT command in MySQL?

(1). Display the MYSQL version

mysql> select version(); 
+-----------------------+ 
| version()    | 
+-----------------------+ 
| 6.0.4-alpha-community | 
+-----------------------+ 
1 row in set (0.02 sec)

(2). Display the current time

mysql> select now(); 
+---------------------+ 
| now()    | 
+---------------------+ 
| 2009-09-15 22:35:32 | 
+---------------------+ 
1 row in set (0.04 sec)

(3). Display the year, month and day

SELECT 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 the string

mysql> SELECT "welecome to my blog!"; 
+----------------------+ 
| welecome to my blog! | 
+----------------------+ 
| welecome to my blog! | 
+----------------------+ 
1 row in set (0.00 sec)

(5). When calculating The tool uses

select ((4 * 4) / 10 ) + 25; 
+----------------------+ 
| ((4 * 4) / 10 ) + 25 | 
+----------------------+ 
|    26.60 | 
+----------------------+ 
1 row in set (0.00 sec)

(6) to concatenate strings

select 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 concatenate strings. In addition, we also used the AS we learned before to give the result column 'CONCAT(f_name, " ", l_name)' a pseudonym

4. Table operations
4.1 Create table
Command: create table 893e75d027ce6b089b1a03b1ace263d6 ( 750e0a03802304dbf14002a3ec506208 c6cafeb87469110057f37b79bf37c88d [,..001365cd9b4200304fd3fdf3a503078c ff39ef658d626caf44c7ab4f85277f8e]);
For example, create a table named MyClass

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));

4.2 Get the table structure
Command: desc table name, or show columns from table name

mysql> desc MyClass;
mysql> show columns from MyClass;

使用MySQL数据库desc 表名时,我们看到Key那一栏,可能会有4种值,即' ','PRI','UNI','MUL'。
(1).如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列;
(2).如果Key是PRI,  那么该列是主键的组成部分;
(3).如果Key是UNI,  那么该列是一个唯一值索引的第一列(前导列),并别不能含有空值(NULL);
(4).如果Key是MUL,  那么该列的值可以重复, 该列是一个非唯一索引的前导列(第一列)或者是一个唯一性索引的组成部分但是可以含有空值NULL。
如果对于一个列的定义,同时满足上述4种情况的多种,比如一个列既是PRI,又是UNI,那么"desc 表名"的时候,显示的Key值按照优先级来显,PRI->UNI->MUL。那么此时,显示PRI。
一个唯一性索引列可以显示为PRI,并且该列不能含有空值,同时该表没有主键。
一个唯一性索引列可以显示为MUL, 如果多列构成了一个唯一性复合索引,因为虽然索引的多列组合是唯一的,比如ID+NAME是唯一的,但是没一个单独的列依然可以有重复的值,只要ID+NAME是唯一的即可。

4.3删除表
命令:drop table a26d98d33123a70024fa8ba5642906c6
例如:删除表名为 MyClass 的表

mysql> drop table MyClass;

DROP TABLE用于取消一个或多个表。您必须有每个表的DROP权限。所有的表数据和表定义会被取消,所以使用本语句要小心!
注意:对于一个带分区的表,DROP TABLE会永久性地取消表定义,取消各分区,并取消储存在这些分区中的所有数据。DROP TABLE还会取消与被取消

的表有关联的分区定义(.par)文件。
对与不存在的表,使用IF EXISTS用于防止错误发生。当使用IF EXISTS时,对于每个不存在的表,会生成一个NOTE。
RESTRICT和CASCADE可以使分区更容易。目前,RESTRICT和CASCADE不起作用。

4.4向表插入数据
命令:insert into a26d98d33123a70024fa8ba5642906c6 [( d4e7ec1944da0629f9db5d84216a428c[,..2c85b2db0c99e1ea238a1572644c7331 ])] values ( 值1 )[, ( 值n )]
例如:往表 MyClass中插入二条记录, 这二条记录表示:编号为1的名为Tom的成绩为96.45, 编号为2 的名为Joan 的成绩为82.99, 编号为3 的名为

Wang 的成绩为96.5。
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
注意:insert into每次只能向表中插入一条记录。

4.5查询表
(1)、查询所有行
命令: select f7b669960012ec3b697d0ecedc519e2d from 4d76b20d91d2b1cadcd47c2c81e2bca2 where b3edc94e70b2c7ee0c3cfeb91d0d0010
例如:查看表 MyClass 中所有数据
mysql> select * from MyClass;
(2)、查询前几行数据
例如:查看表 MyClass 中前2行数据
mysql> select * from MyClass order by id limit 0,2;
select一般配合where使用,以查询更精确更复杂的数据。

4.6删除表
命令:delete from 表名 where 表达式
例如:删除表 MyClass中编号为1 的记录
mysql> delete from MyClass where id=1;

4.7修改表中的数据
语法:update 表名 set 字段=新值,… where 条件
mysql> update MyClass set name='Mary' where id=1;

例子1:单表的MySQL UPDATE语句:

 UPDATE [LOW_PRIORITY] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition] [ORDER BY ...] [LIMIT row_count]

例子2:多表的UPDATE语句:

UPDATE [LOW_PRIORITY] [IGNORE] table_references SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition]

UPDATE语法可以用新值更新原有表行中的各列。SET子句指示要修改哪些列和要给予哪些值。WHERE子句指定应更新哪些行。如果没有WHERE子句,则更新所有的行。如果指定了ORDER BY子句,则按照被指定的顺序对行进行更新。LIMIT子句用于给定一个限值,限制可以被更新的行的数目。


4.8增加表字段
命令:alter table 表名 add字段 类型 其他;
例如:在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0
mysql> alter table MyClass add passtest int(4) default '0'[/code]
加索引:mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);
例子: mysql> alter table employee add index emp_name (name);
加主关键字的索引:mysql> alter table 表名 add primary key (字段名);
例子: mysql> alter table employee add primary key(id);
加唯一限制条件的索引:mysql> alter table 表名 add unique 索引名 (字段名);
例子: mysql> alter table employee add unique emp_name2(cardnumber);
删除某个索引:mysql> alter table 表名 drop index 索引名;
例子: mysql>alter table employee drop index emp_name;
增加字段:mysql> ALTER TABLE table_name ADD field_name field_type;
修改原字段名称及类型:mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
修改字段类型:mysql>ALTER TABLE table_name MODIFY colum_name field_type new_type
删除字段:MySQL ALTER TABLE table_name DROP field_name;

4.9修改表名
命令:rename table 原表名 to 新表名;
例如:在表MyClass名字更改为YouClass

mysql> rename table MyClass to YouClass;

当你执行 RENAME 时,你不能有任何锁定的表或活动的事务。你同样也必须有对原初表的 ALTER 和 DROP 权限,以及对新表的 CREATE 和 INSERT 权限。
如果在多表更名中,MySQL 遭遇到任何错误,它将对所有被更名的表进行倒退更名,将每件事物退回到最初状态。
RENAME TABLE 在 MySQL 3.23.23 中被加入。


五、备份数据

命令在DOS的[url=file://\\mysql\\bin]\\mysql\\bin[/url]目录下执行
(1).导出整个数据库
导出文件默认是存在mysql\bin目录下
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u user_name -p123456 database_name > outfile_name.sql
(2).导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u user_name -p database_name table_name > outfile_name.sql
(3).导出一个数据库结构
mysqldump -u user_name -p -d –add-drop-table database_name > outfile_name.sql
-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table
(4).带语言参数导出
mysqldump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-opt database_name > outfile_name.sql
例如,将aaa库备份到文件back_aaa中:

[root@test1 root]# cd /home/data/mysql
[root@test1 mysql]# mysqldump -u root -p --opt aaa > back_aaa

六、一个完整的数据库创建实例

drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
 id int(3) auto_increment not null primary key,
 name char(10) not null,
 address varchar(50) default ''深圳'',
 year date
); //建表结束
 
//以下为插入字段
insert into teacher values('''',''glchengang'',''深圳一中'',''1976-10-10'');
insert into teacher values('''',''jack'',''深圳一中'',''1975-12-23'');

注:在建表中
(1)、将ID设为长度为3的数字字段:int(3);并让它每个记录自动加一:auto_increment;并不能为空:not null;而且让他成为主字段primary key。
(2)、将NAME设为长度为10的字符字段
(3)、将ADDRESS设为长度50的字符字段,而且缺省值为深圳。

如果你在mysql提示符键入上面的命令也可以,但不方便调试。
(1)、你可以将以上命令原样写入一个文本文件中,假设为school.sql,然后复制到c:\\下,并在DOS状态进入目录[url=file://\\mysql\\bin]\

\mysql\\bin[/url],然后键入以下命令:mysql -uroot -p密码 541df8edea06165a8e9c40987b9b8ba8 source c:\\school.sql; 也可以将school.sql文件导入数据库中。

以上就是完整版的Mysql命令大全,希望对大家熟练使用Mysql命令有所帮助。


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