当前位置:网站首页>What are the common commands of mysql
What are the common commands of mysql
2022-08-10 05:04:00 【Gargoyle MMQ!!】
MySQL 数据库常用命令
1、MySQL常用命令
create database name; 创建数据库
use databasename; 选择数据库
drop database name 直接删除数据库,不提醒
show tables; 显示表
describe tablename; 表的详细描述
select 中加上distinct去除重复字段
mysqladmin drop databasename 删除数据库前,有提示.
显示当前mysql版本和当前日期
select version(),current_date;
2、修改mysql中root的密码:
shell>mysql -u root -p
mysql> update user set password=password(”xueok654123″) where user=’root’;
mysql> flush privileges //刷新数据库
mysql>use dbname; 打开数据库:
mysql>show databases; 显示所有数据库
mysql>show tables; 显示数据库mysql中所有的表:先use mysql;然后
mysql>describe user; 显示表mysql数据库中user表的列信息);
3、grant
创建一个可以从任何地方连接服务器的一个完全的超级用户,但是必须使用一个口令something做这个
mysql> grant all privileges on . to [email protected] identified by ’something’ with
增加新用户
格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”
GRANT ALL PRIVILEGES ON . TO [email protected] IDENTIFIED BY ’something’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO [email protected]”%” IDENTIFIED BY ’something’ WITH GRANT OPTION;
删除授权:
mysql> revoke all privileges on . from [email protected]”%”;
mysql> delete from user where user=”root” and host=”%”;
mysql> flush privileges;
创建一个用户custom在特定客户端it363.com登录,可访问特定数据库fangchandb
mysql >grant select, insert, update, delete, create,drop on fangchandb.* to [email protected] it363.com identified by ‘ passwd’
重命名表:
mysql > alter table t1 rename t2;
4、mysqldump
备份数据库
shell> mysqldump -h host -u root -p dbname >dbname_backup.sql
恢复数据库
shell> mysqladmin -h myhost -u root -p create dbname
shell> mysqldump -h host -u root -p dbname < dbname_backup.sql
如果只想卸出建表指令,则命令如下:
shell> mysqladmin -u root -p -d databasename > a.sql
如果只想卸出插入数据的sql命令,而不需要建表命令,则命令如下:
shell> mysqladmin -u root -p -t databasename > a.sql
那么如果我只想要数据,而不想要什么sql命令时,应该如何操作呢?
mysqldump -T./ phptest driver
其中,只有指定了-T参数才可以卸出纯文本文件,表示卸出数据的目录,./表示当前目录,即与mysqldump同一目录.如果不指定driver 表,则将卸出整个数据库的数据.每个表会生成两个文件,一个为.sql文件,包含建表执行.另一个为.txt文件,只包含数据,且没有sql指令.
5、可将查询存储在一个文件中并告诉mysql从文件中读取查询而不是等待键盘输入.可利用外壳程序键入重定向实用程序来完成这项工作.
例如,如果在文件my_file.sql 中存放有查
询,可如下执行这些查询:
例如,如果您想将建表语句提前写在sql.txt中:
mysql > mysql -h myhost -u root -p database < sql.txt
1、数据查询语言(DQL-Data Query Language):代表关键字:select
13.2.7. SELECT语法_MySQL 中文文档 (mysqlzh.com)
1.1、简单查询:(You can control the number of fields in the result,可以控制列数)
查询多个字段:select 字段名,字段名 from 表名;
查询所有字段:select * from 表名;
用 as 关键字重命名表字段,as 也可以省略:
select empno as ‘员工编号’, ename as ‘员工姓名’, sal*12 as ‘年薪’ from emp; # 列出员工的编号,姓名和年薪
1.2、条件查询:(where关键字,位置是在from关键字之后)
1.3、排序数据(order by)
排序采用 order by 子句,order by 后面 + 排序字段,排序字段可以放多个,多个采用逗号间隔.
order by 默认采用升序asc,如果存在 where 子句那么 order by 必须放到 where 语句的后面 .
如果采用多个字段排序,会先根据第一个字段排序,重复了,再根据第二个字段排序.
单一字段排序
按照薪水由小到大排序(默认升序):select * from emp order by sal;
取得 job 为 MANAGER 的员工,按照薪水由小到大排序:select * from emp where job=‘MANAGER’ order by sal asc;
Manually specify to sort by salary from largest to smallest(Manual descending):select * from emp order by sal desc;
多个字段排序
首先按照 job 排序,再按照 sal 排序(默认升序):select * from emp order by job,sal;
按照 job 和薪水倒序:select * from emp order by job desc, sal desc;
1.4、分组查询(group by 和 having)
注意:若有 group by 语句,那么在 select 语句后面只能跟分组函数+参与分组的字段.
区别:whereThe data excluded by the statement is the data in the table,havingThe data excluded by the statement is the group data after grouping.
所以 having Keywords cannot appear alone,It must be used after groupinghaving语句.
group by 位置是在where关键字之后,order by排序之前.
2、数据处理函数(又称为单行处理函数,特点:输入一行输出一行)
3、分组函数(又称为聚合函数、多行处理函数)
注意:分组函数自动忽略空值,不需要手动的加 where 条件排除空值.
select count(*) from emp where 条件; # 符合条件的所有记录总数.
select count(comm) from emp; # comm 这个字段中不为空的元素总数.
注意:分组函数不能直接使用在 where 关键字后面.
mysql> select ename,sal from emp where sal > avg(sal); # 报错
边栏推荐
猜你喜欢
Joomla漏洞复现
2022年R2移动式压力容器充装考试题库模拟考试平台操作
Using the DatePicker date control, Prop being mutated: "placement" error occurs
什么是“大小端字节序”存储模式?
什么是遗留代码:有效地处理遗留代码的8个小贴士
leetcode每天5题-Day11
线性模型中的高级特征选择技术——基于R
EasyGBS connects to mysql database and prompts "can't connect to mysql server", how to solve it?
【LeetCode】41、 缺失的第一个正数
法定代表人和股东是什么关系
随机推荐
Ask you guys.The FlinkCDC2.2.0 version in the CDC community has a description of the supported sqlserver version, please
Big guys, mysql cdc (2.2.1 and previous versions) sometimes has this situation since savepoint, is there anything wrong?
ORA-16018 异常处理记录
Ueditor编辑器任意文件上传漏洞
深度梳理:防止模型过拟合的方法汇总
MySQL simple tutorial
基于 EasyCV 复现 DETR 和 DAB-DETR,Object Query 的正确打开方式
Kubernetes资源编排系列之一: Pod YAML篇
元宇宙 | 你能通过图灵测试吗?
aliases节点分析
Joomla漏洞复现
awk of the Three Musketeers of Shell Programming
EasyGBS连接mysql数据库提示“can’t connect to mysql server”,该如何解决?
From entry to mastery of PHPCMS imitation station, Xiaobai is enough to watch this set of courses
Pulsar中游标的工作原理
2022 T Elevator Repair Exam Questions and Mock Exams
`id` bigint(20) unsigned NOT NULL COMMENT '数据库主键',
各位大佬,idea中测试使用FlinkCDC SQL 读取Mysql 数据写入Kafka中,代码中创
About the problem that the mongodb driver count method of rust cannot be used with the near condition
summer preschool assignments