欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL用命令窗口打开

程序员文章站 2022-05-30 23:36:07
...

MySQL用命令窗口打开

进入bin目录,按住shift键然后点击鼠标右键可以选择在该目录下打开命令窗口,或者在地址栏中输入cmd进入命令窗口。输入mysql -u root -p后回车,然后会提示输入密码,输入密码后就会进入MySQL的操作管理界面。

Microsoft Windows [版本 10.0.19042.630]
(c) 2020 Microsoft Corporation. 保留所有权利。

D:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.7.3-m13 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

进入bin目录,按住shift键然后点击鼠标右键可以选择在该目录下打开命令窗口,或者在地址栏中输入cmd进入命令窗口。输入mysql -u root -p后回车,然后会提示输入密码,输入密码后就会进入MySQL的操作管理界面。
输入show databases;(注意末尾有分号)可以查看当前MySQL中的数据库列表,输入use test;可以进入test数据库(前提是要有此数据库),输入show tables可以查看test数据库中的所有表,输入quit可以退出MySQL的操作管理界面。

MySQL help

mysql> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.#清除当前输入语句
connect   (\r) Reconnect to the server. Optional arguments are db and host.
												#重新连接到服务器。可选参数是db和host。
delimiter (\d) Set statement delimiter.			#设置语句定界符
ego       (\G) Send command to mysql server, display result vertically.
												#发送命令到mysql服务器,垂直显示结果。
exit      (\q) Exit mysql. Same as quit.		#退出mysql。与退出相同
go        (\g) Send command to mysql server.	#发送命令到mysql服务器。
help      (\h) Display this help.				#显示此帮助
notee     (\t) Don't write into outfile.		#不要写入outfile。
print     (\p) Print current command.			#打印当前命令。
prompt    (\R) Change your mysql prompt.		#更改您的mysql提示符。
quit      (\q) Quit mysql.						#退出mysql
rehash    (\#) Rebuild completion hash.			#重建完成哈希。
source    (\.) Execute an SQL script file. Takes a file name as an argument.
												#执行一个SQL脚本文件。以文件名作为参数。
status    (\s) Get status information from the server.
												#从服务器获取状态信息。
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
												#设置输出文件[to_outfile]。将所有内容附加到给定的文件中。
use       (\u) Use another database. Takes database name as argument.
												#使用另一个数据库。将数据库名称作为参数。
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.										#切换到另一个字符集。处理具有多字节字符集的binlog可能需要。
warnings  (\W) Show warnings after every statement.
												#在每个语句后显示警告。
nowarning (\w) Don't show warnings after every statement.
												#不要在每次声明后显示警告。
resetconnection(\x) Clean session context.		#清理会话上下文。

For server side help, type 'help contents'
  • status
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.3-m13, for Win64 (x86_64)

Connection id:          35
Current database:
Current user:           [email protected]
SSL:                    Not in use
Using delimiter:        ;
Server version:         5.7.3-m13 MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    gbk
Conn.  characterset:    gbk
TCP port:               3306
Uptime:                 11 hours 56 min 24 sec

Threads: 3  Questions: 6708  Slow queries: 0  Opens: 138  Flush tables: 1  Open tables: 105  Queries per second avg: 0.156
--------------

输入show databases;(注意末尾有分号)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ncayu101           |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set (0.00 sec)

mysql> use ncayu101;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_ncayu101 |
+--------------------+
| companyinfo        |
+--------------------+
1 row in set (0.00 sec)

mysql> quit
Bye

mysql> select * from companyinfo;
+----+----------------------------+-------+---------+-------------+----------+---------------------+
| id | name                       | count | contact | tel         | remark   | visitTime           |
+----+----------------------------+-------+---------+-------------+----------+---------------------+
|  1 | 湖南崇明电子设备有限公司   |     1 | 张旭东  | 13433218966 | 设备购置 | 2020-02-15 23:03:22 |
|  2 | 湖北李银达科技股份有限公司 |     5 | 胡玲    | 13989453451 | 设备购置 | 2020-03-27 00:00:00 |
|  3 | 浙江秀丽纺织加工厂         |     2 | 李秀丽  | 13455678134 | 管理培训 | 2020-06-18 05:00:00 |
|  4 | 山东格林达设备管理有限公司 |     3 | 张明    | 13845332146 | 设备购置 | 2020-10-15 04:03:05 |
+----+----------------------------+-------+---------+-------------+----------+---------------------+
4 rows in set (0.02 sec)

mysql> select * from companyinfo where id=1;
+----+--------------------------+-------+---------+-------------+----------+---------------------+
| id | name                     | count | contact | tel         | remark   | visitTime           |
+----+--------------------------+-------+---------+-------------+----------+---------------------+
|  1 | 湖南崇明电子设备有限公司 |     1 | 张旭东  | 13433218966 | 设备购置 | 2020-02-15 23:03:22 |
+----+--------------------------+-------+---------+-------------+----------+---------------------+
1 row in set (0.00 sec)

mysql> select * from companyinfo where tel='13989453451';
+----+----------------------------+-------+---------+-------------+----------+---------------------+
| id | name                       | count | contact | tel         | remark   | visitTime           |
+----+----------------------------+-------+---------+-------------+----------+---------------------+
|  2 | 湖北李银达科技股份有限公司 |     5 | 胡玲    | 13989453451 | 设备购置 | 2020-03-27 00:00:00 |
+----+----------------------------+-------+---------+-------------+----------+---------------------+
1 row in set (0.00 sec)

mysql> select * from companyinfo where  count=5;
+----+----------------------------+-------+---------+-------------+----------+---------------------+
| id | name                       | count | contact | tel         | remark   | visitTime           |
+----+----------------------------+-------+---------+-------------+----------+---------------------+
|  2 | 湖北李银达科技股份有限公司 |     5 | 胡玲    | 13989453451 | 设备购置 | 2020-03-27 00:00:00 |
+----+----------------------------+-------+---------+-------------+----------+---------------------+
1 row in set (0.00 sec)

Windows命令行操作数据库

  • cmd//打开命令小黑框

  • e: //从c盘转到e盘

  • cd E:\make\PHPTutorial\MySQL\bin //切换路径

  • mysql -uroot -p;//进入数据库命令行

  • Enter password:root//连接数据库密码

  • mysql>//进入数据库

  • show databases;//展示所有数据库

  • use hehe;//使用数据库

  • set names gbk;//设置windows7数据库编码方式

  • show tables;/展示所有表

  • select * from biao;//查询语句

  • delete from biao where id=2;//删除语句

  • insert into biao(name,age,sex)value(‘xiaohuang’,18,1);//插入语句

  • select * from biao order by id asc;//按照id正序排列

  • select * from biao order by id desc;//按照id倒序排列

  • update biao set name = 'hhyy’where id=5;

  • update biao set money = money+1 where id=5;

  • truncate biao;//清空表的内容

  • exit;//从命令行退出

  • set names gbk;//设置windows7数据库编码方式

  • delete from biao;//删除表数据不重置id

  • truncate biao;//删除表数据重置id

  • delete与update 操作时务必加where条件

  • drop database 数据库名;//完全删除数据库
    创建数据库

  • create database [if not exists]数据库名 [character set = 字符集] [collate = 校对集];

  • create database if not exists hehe character set =utf8 collate = utf8_general_ci;
    创建表

  • create table[if not exists]表名(字段列表[,索引或约束列表])[表选项列表]
    create table hema
    (
    id int,
    name varchar(255), //必须写字符串长度否则命令行报错
    age int,
    sex int
    );

  • show create table hema;//展示建表语句
    在sql中最标准的做法是在表和字段外边都标一对小引号例如:

    name

select id,name from admin;//查询表中的id以及name
select * from admin where name in(‘xhenxu’,‘hippo’);
select * from admin where name like ‘%100%’;//模糊查询 %匹配任意多个字符
select * from admin where name like ‘%100_’;// _匹配任意一个字符
select * from admin where id between 2 and 5;

select 字段名 from 表明 group by 字段名;

select id,name from admin group by type;//按照某个字段把数据进行分组
select id,name,type from admin group by type having id>1;//having与where效果一样,having单独使用与where一样,where不能用在group by之后

聚合函数

avg(字段名)平均值
count(字段名);总数量//求数据条数
max(字段名);最大值
min(字段名);最小值
sum(字段名);总和
select avg(money) as avgmoney from admin;

select count(*) from admin;//查询有多少条数据

在命令行中修改字段名与字段类型

alter table 表名 change 旧字段名 新字段名 新字段类型;
alter table 表名 modify 旧字段名 新字段类型;
添加字段
alter table 表名 add 新字段名 字段类型【字段属性列表】;
命令行修改表名
rename table 旧表名 to 新表名;
限制
limit offset,pagesize
offset偏移量,为0时可以忽略
pagesize 每页显示的行数
select *from user limit 0,4;
select 字段1,字段2 from 表名 where 条件 group by 字段名 having 限制条件 order by 字段名 desc limit 开始值,每页显示数量;

id int not null auto increment; 自动递增
default null 默认空

---------------------------------------------------------------------
删除字段:
alter table 表名 drop 字段名;
删除表
drop table [if exists]表名;

左表名 inner join 右表名 on 条件 //内连接:两个表中都满足条件的数据
select [user.name](http://user.name/),job1.job from user inner join job on [user.id](http://user.id/) = job.pid;
左表名 left join 右表名 on 条件 //左连接:左表数据都出现,满足条件的右表数据出现
左表名 right join 右表名 on 条件

现在运维工程师不得不掌握Python和网络安全