文章目录

  • 基础命令回顾
  • 数据类型
  • 常用SELECT命令
  • 导入数据库
  • 导出数据库
  • 扩展知识
  • SQL查询语句进阶
  • 连接查询:
  • 破解mysql数据库密码

基础命令回顾

  • 添加字段:
alter table 表名 add 字段名 列类型 [not null|null][primary key][unique][auto_increment][default value]
alter table 表名 add 字段定义 after ar_id;
  • 删除字段:
alter table 表名 drop 字段名
  • 修改字段:
alter table 表名 modify 字段名 字段新类型
  • 完整修改字段:
alter table 表名  change 旧字段名称  新字段定义
  • 修改表名称
alter table 表名 rename 新名字
  • 删除表
drop table [if (not) exists] 表名;
  • 表中行的操作
insert
insert [into] 数据表名称 [(字段列表)] values|value (表达式|null|default,...),(表达式|null|default,...)
insert [into] 数据表名称 set 字段名称=值,...
insert与insert...set的区别是后者可以带有子查询。
  • update – 单表
update 表名 set 字段名称=值,... [where 条件]
如果省略WHERE条件将更新全部记录。
  • 删除记录 – 单表
delete from 数据表名称 [where 条件]
如果省略where条件,将删除全部记录
  • select 查询
select 字段列表 from 数据表 [[as] 别名] [where 条件]
别名的用法:
Select * from 数据表 [[as] 别名]
字段名称 [[as]别名]
Select product_offer_instance_object_id as ID, product_offer_instance_object_name name,coumn33 ‘金额’From table
select btypeid as '图书类别ID',btypename as '图书类型' from category;  MariaDB [category]> select id as '编号',name '学校名称' from class;
+--------+--------------------------------+
| 编号   | 学校名称                       |
+--------+--------------------------------+
|      1 | IE面试大全                     |
|      1 | 武汉软件工程职业学院           |
+--------+--------------------------------+
2 rows in set (0.00 sec)或者:不用as  也可以
MariaDB [category]> select id '序号',name '名字',autor '作者' from class;
+--------+--------------------------------+-----------+
| 序号   | 名字                           | 作者      |
+--------+--------------------------------+-----------+
|      1 | IE面试大全                     | 任正非    |
|      1 | 武汉软件工程职业学院           | 马蜂      |
+--------+--------------------------------+-----------+
2 rows in set (0.00 sec)MariaDB [category]> select *from class;  不用as 就是英文的别名()
+------+--------------------------------+-----------+
| id   | name                           | autor     |select语句返回零条或多条记录;属于记录读操作
insert、update、delete只返回此次操作影响的记录数;属于写操作

数据类型

  • MySQL中定义数据字段的类型对你数据库的优化是非常重要的。

  • MySQL支持多种类型,大致可以分为三类:

     数值、日期/时间和字符串(字符)类型。
    
  • 数值类型

  • 时间、日期类型

  • 字符串类型

  • 整型

tinyint,占1字节,有符号:-128127,无符号位:0255
smallint,占2字节,有符号:-32768 32767,无符号位:065535
mediumint占3字节,有符号:-83886088388607,无符号位:016777215
int,占4字节,有符号:-21474836482147483647,无符号位:04284967295
bigint,占8字节
bool 等价于tinyint(1) 布尔型

  • 浮点型

float([m[,d]]) 占4字节,1.17E-38~3.4E+38
double([m[,d]]) 占8字节
decimal([m[,d]]) 以字符串形式表示的浮点数

  • 字符型
    char([m]):固定长度的字符,占用m字节
    varchar[(m)]:可变长度的字符,占用m+1字节,大于255个字符:占用m+2
    tinytext,255个字符(2的8次方)
    text,65535个字符(2的16次方)
    mediumtext,16777215字符(2的24次方)
    longtext,(2的32次方)
    enum(value,value,…)占1/2个字节 最多可以有65535个成员
    set(value,value,…)占1/2/3/4/8个字节,最多可以有64个成员

常用SELECT命令

使用select命令查看mysql数据库系统信息:

打印当前的日期和时间
select now();
打印当前的日期
select curdate();
打印当前的时间
select curtime();
打印当前数据库
select database();
打印MySQL版本
select version();
打印当前用户
select user();
查看系统信息
show variables;
show global variables;
show global variables like '%version%';
show variables like '%storage_engine%'; 默认的存储引擎
like模糊搜索还可用户where字句,例如
select * from students where stname like '%l%1%2%3%';
除了like 还有not like
show engines;查看支持哪些存储引擎
查看系统运行状态信息
show status;
show global status like 'Thread%';

导入数据库

导入数据库前必须创建一个空数据库
mysql -e 'create database book' -uroot -p123456
或者登陆 mysql
create database book;
导入(方法一)
mysql -uroot -p123456 book < book.sql
mysql> use book;
mysql> show tables;
+----------------+
| Tables_in_book |
+----------------+
| books          |
| catego
+----------------+
导入(方法二)
create database book;
mysql> use book;
mysql> source /root/book.sql  #sql脚本的路径
mysql> show tables;
+----------------+
| Tables_in_book |
+----------------+
| books          |
| category       |
+----------------+

导出数据库

导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u system -p123456 book>book2.sql

扩展知识


Mysqldump –uroot –p123456 –B 库名>文件.sql
-B : 导出整个库包含建库语句
-A:导出全部数据库
如何把一个select的结果导出到文本
select * into outfile '/tmp/123.txt' from books; 此处有个文件访问权限问题,mysql用户是可以访问/tmp路径的,所以这里放到tmp下
select * from books into outfile '/tmp/456.txt';
其实就是备份数据库
  • 扩展: 5.7版本导出报错,可以设置my.cnf 加上secure-file-priv="/ "

SQL查询语句进阶

在我们刚导入的book数据库进行测试

  • 查看表的内容:
mysql> select * from category;
mysql> select * from books;
mysql> select * from books\G
  • 查看字段类型:
desc 表名
mysql> desc books;
  • 逻辑运算符:
and or not
and 且
or  或
not 非
  • 选择出书籍价格为(30,40,50,60)的记录,只显示书籍名称,出版社,价格
mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;
+--------------------------------------+--------------------------+-------+
| bName                                | publishing               | price |
+--------------------------------------+--------------------------+-------+
| Illustrator 10完全手册    | 科学出版社 |    50 |
| FreeHand 10基础教程   | 北京希望电子出版  |    50 |
| 网站设计全程教程  | 科学出版社 |    50 |
| ASP数据库系统开发实例导航    | 人民邮电出版社   |    60 |
| Delphi 5程序设计与控件参考 | 电子工业出版社   |    60 |
| ASP数据库系统开发实例导航    | 人民邮电出版社   |    60 |
+--------------------------------------+--------------------------+-------+
  • 算术运算符:
=   等于
<>    不等于  !=
>    大于
<    小于
>=  大于等于
<=  小于等于
  • in 运算符
    in 运算符用于 where 表达式中,以列表项的形式支持多个选择,语法如下:
    where column IN (value1,value2,…)
    WHERE column NOT IN (value1,value2,…)
  • Not in 与in相反
    当 IN 前面加上 NOT 运算符时,表示与 IN 相反的意思,即不在这些列表项内选择。
找出价格大于60的记录
mysql> select bName,price from books where price>60;
找出价格为60的
mysql> select bName,price from books where price=60;
找出价格不等于60的
mysql> select bName,price from books where price<>60;
找出价格是60,50,70的记录
mysql> select bName,price from books where price in (50,60,70);
找出价格不是60,50,70的记录
mysql> select bName,price from books where price not in (50,60,70);
  • 排序:
    升序:order by “排序的字段” asc 默认
    降序:oredr by “排序的字段” desc
mysql> select bName,price from books where price  in (50,60,70) order by price asc;
+------------------------------------------------+-------+
| bName                                | price |
+------------------------------------------------+-------+
| Illustrator 10完全手册    |    50 |
| FreeHand 10基础教程   |    50 |
| 网站设计全程教程  |    50 |
| ASP数据库系统开发实例导航    |    60 |
| Delphi 5程序设计与控件参考     |    60 |
| ASP数据库系统开发实例导航    |    60 |
mysql> select bName,price from books where price  in (50,60,70) order by price desc;
+--------------------------------------+-----------------+
| bName                                | price |
+--------------------------------------+-----------------+
| ASP数据库系统开发实例导航    |    60 |
| Delphi 5程序设计与控件参考     |    60 |
| ASP数据库系统开发实例导航    |    60 |
| Illustrator 10完全手册            |    50 |
| FreeHand 10基础教程               |    50 |
| 网站设计全程教程              |    50 |
多个字段排序
select bName,price from books where price  in (50,60,70) order by price desc,bName desc;
  • 范围运算:
between ....and....
Between and 可以使用大于小于的方式来代替,并且使用大于小于意义表述更明确
查找价格不在30到60之间的书名和价格
mysql> select bName,price from books where price not between 30 and 60 order by price desc;
注:
这里的查询条件有三种:between。。。and,or 和 in
(30,60) >30 and <60
[30,60] >=30 and <=60
模糊匹配查询:
字段名 [not]like '通配符'  ----》% 任意多个字符查找书名中包括"程序"字样记录
mysql> select bName from books where bName like '%程序%';
不含有
mysql> select bName from books where bName not like '%程序%';
  • MYSQL子查询:
    概念:在select 的where条件中又出现了select
    查询中嵌套着查询

选择 类型名为“网络技术”的图书:

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='网络技术');

选择类型名称为“黑客”的图书;

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='黑客');
  • limit 限定显示的条目:
SELECT * FROM table LIMIT [offset,] rows 偏移量  行数

LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1):

比如select * from table limit m,n语句
表示其中m是指记录开始的index,从0开始,表示第一条记录
n是指从第m+1条开始,取n条。

查出category表中第2条到第6行的记录。
首先2到6行有2,3,4,5,6总共有5个数字,从2开始,偏移量为1
mysql> select * from category limit 1,5;
+---------+--------------+
| bTypeId | bTypeName    |
+---------+--------------+
|       2 | 网站       |
|       3 | 3D动画     |
|       4 | linux学习  |
|       5 | Delphi学习 |
|       6 | 黑客       |
+---------+--------------+
查看所有书籍中价格中最低的三条记录
我们对所有记录排序以升序排列,取出前面3个来
mysql> select bName,price from books order by price asc limit 0,3;
+-----------------------------+-------+
| bName                       | price |
+-----------------------------+-------+
| 网站制作直通车       |    34 |
| 黑客与网络安全       |    41 |
| 网络程序与设计-asp |    43 |

连接查询:

以一个共同的字段,求两张表当中符合条件的并集。 通过共同字段把这两张表连接起来。
常用的连接:
内连接:根据表中的共同字段进行匹配
外连接分两种:左外连接、右外链接。

  • 内连接
语法:
select 字段  from 表1 inner join 表2  on 表1.字段=表2.字段
内连接:根据表中的共同字段进行匹配
测试
Select a.bname,a.price,b.btypename from books a inner join category b on a.btypeid=b.btypeid;
实际使用中inner可省略掉跟WHERE 子句结果一样
select a.bname,a.price,b.btypename from books a, category b where a.btypeid=b.btypeid;
  • 外连接 (分为左外连接;右外连接)
1.左连接: select  字段 from a表 left join b表  on 连接条件
a表是主表,都显示。
b表从表
主表内容全都有,从表内没有的显示null。
Select a.bname,a.price,b.btypename from books a left join category b on a.btypeid=b.btypeid;

2.右连接:select 字段 from a表 right join  b表 on 条件
a表是从表,
b表主表,都显示。
Select a.bname,b.* from books a right join category b on a.btypeid=b.btypeid;
右连接,可以多表连接
  • 聚合函数
    函数:执行特定功能的代码块。
    算数运算函数:

  • Sum()求和

 显示所有图书单价的总合mysql> select sum(price) from books; 或select sum(price) as 图书总价 from books;
+------------+
| sum(price) |
+------------+
|      10048 |
+------------+
  • avg()平均值:
求书籍Id小于3的所有书籍的平均价格
mysql> select avg(price) from books where bId<=3;
+------------+
| avg(price) |
+------------+
|    39.3333 |
+------------+
  • max() 最大值:
求所有图书中价格最贵的书籍
mysql> select bName,max(price) from books; 这种方法是错误的
我们来查一下最贵的图书是哪本?
select bname,price from books order by desc price limit 0,3;
可见最贵书是Javascript与Jscript从入门到精通,而不是网站制作直通车
select bName,price from books where price=(select max(price) from books);
+----------------------------------------+-------+
| bName                          | price |
+----------------------------------------+-------+
| Javascript与Jscript从入门到精通 |  7500 |
+----------------------------------------+-------+
  • min()最小值:
求所有图书中价格便宜的书籍
mysql> select bName,price from books where price=(select min(price) from books);
+-----------------------+-------+
| bName                 | price |
+-----------------------+-------+
| 网站制作直通车 |    34 |
+-----------------------+-------+
  • count()统计记录数:
统计价格大于40的书籍数量
mysql> select count(*) from books where price>40;
+----------+
| count(*) |
+----------+
|       43 |
+----------+
Count()中还可以增加你需要的内容,比如增加distinct来配合使用
select count(distinct price) from books where price>40;
算数运算:+ - * / 
给所有价格小于40元的书籍,涨价5元mysql> update books set price=price+5 where price<40;给所有价格高于70元的书籍打8折mysql> update books set price=price*0.8 where price>70;

破解mysql数据库密码

  • 应用场景:密码忘记了,此时就需要自己破解密码

  • 修改配置文件 /etc/my.cnf
    在[mysqld]段的最后添加上skip-grant-tables


[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# head -n 10 /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
skip-grant-tables       #这里是我加的参数
[root@localhost ~]# 
  • 重启服务
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
[root@localhost ~]# 
  • 登陆进去mysql -uroot
[root@localhost ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB ServerCopyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> 
  • 改密码
MariaDB [(none)]> show databases;
……MariaDB [(none)]> use mysql
………MariaDB [mysql]> update mysql.user set Password=password('新密码') where User='root'
Query OK, 3 rows affected (0.50 sec)
Rows matched: 3  Changed: 3  Warnings: 0MariaDB [mysql]> flush privileges;
  • 从配置文件中删除skip-grant-tables并重启服务
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# mysql -uroot -pwyh
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB ServerCopyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]>

mysql的话,修改密码可以这样,基本操作一样,只有一下区别:

 update user set authentication_string = password("lwq") where user = 'root' 不用设计权限,直接修改密码就行啦

mysql 基础命令进阶相关推荐

  1. linux mysql运维_Linux运维常用的 MySQL基础命令

    # MySQL基础命令 ------------- ### 创建MySQL库,授权. >create database mysqldb character set utf8; >grant ...

  2. Mysql 基础命令

    MySQL基础命令 创建用户 1 -- 查看系统用户2 select Host,User,Password from mysql.user;3 4 -- 创建一个远程用户 5 create user ...

  3. MySQL基础命令-学习的时候记录一下

    MySQL基础命令 DDL-操作数据库命令 R(Retrieve):查询功能 ① 查询所有数据库 SHOW DATEBASES; ② 查询数据库的创建语句 SHOW CREATE DATABASE d ...

  4. Mysql基础到进阶精品视频教程附讲义文档 91课

    课程介绍 Mysql基础 本章主要是php开发中Mysql基础知识的学习,包括MySQL的简单介绍和安装.MySQL管理工具的使用.表的建立.数据的查询.数据的修改和数据的增加等等,全面掌握php网站 ...

  5. mysql基础命令大全

    http://xy5300.blog.51cto.com/951650/672075 备份数据库: mysqldump  -uroot(用户) -pP2s3D7rVfeTS209sksp8(密码)  ...

  6. 04 : mysql 基础命令操作,字符集

     insert 插入讲解 : 1:创建一个表: create table test( id int(4) not null auto_increment, name char(20) not null ...

  7. Linux环境下MySQL基础命令(3)----增、删、改、查语句

    MySQL中的增.删.改.查命令是最基础的语句,通常应用在对表内数据的维护工作中.本文是对mysql数据库name库中的users表进行实例演示.name库以及users表均为本人建立,并非自带. 1 ...

  8. mysql基础命令学习笔记

    这里不会涉及一些mysql数据库安装之类的知识,但是会将自己学习以及使用mysql一些最常用的mysql语句总结到本篇文章,作为自己的学习记录和笔记.基本的环境为CentOS 6.5 数据库为mysq ...

  9. mysql基础命令学习(尚硅谷李玉婷老师教学)

    文章目录 一.基础查询方法 案例 二.条件查询 三.排序查询 四.常见函数 单行函数 练习题 五.分组函数 练习题 六.分组查询 练习解析: 七.连接查询 练习解析: 八.sql99语法 一.内连接 ...

最新文章

  1. java newline_Java BufferedWriter.newLine()方法示例
  2. 杭电oj1087最长递增子序列java实现
  3. Jsoup 简介书写
  4. echarts地图在ie浏览器上不显示
  5. 图像对齐(image alignment)
  6. protected的继承方式有什么特点_草莓的授粉方式有哪些?各有什么特点
  7. avalon使用笔记
  8. html搜索联系人,联系人列表.html
  9. PPT绘图导出高清图
  10. python集合排序_集合排序python
  11. 苍天有眼,我终于搞定了win7使用建行华大智宝U盾的问题了!
  12. git send-email 使用126邮件发送patch
  13. 女孩起名取名字:聪明美丽、好听委婉的女孩名字
  14. 2020年区块链行业十大趋势
  15. 数据库实现递归查询,获取节点的所有子孙节点
  16. 【自然语言处理】从词袋模型到Transformer家族的变迁之路
  17. 批量手机号归属地查询
  18. Go应用构建工具(3)--cobra
  19. linux清除僵尸进程,如何清理和避免linux系统僵尸进程
  20. IAR中eww、ewp、ewd···等各文件的含义和用途

热门文章

  1. 区块链具有诸多技术优势 在国外基础教育中的应用案例
  2. 乔布斯自传自述-新浪
  3. 艾永亮:创新凭运气?老板该如何避坑?
  4. Android studio学习(一)
  5. Matlab与外部接口:MAT文件基础
  6. C语言实现因式分解输出
  7. su模型如何画?免费模型库如何下载?草图溜溜之无限获得
  8. 金融类考试有什么公式汇总?
  9. android 常用编译工具,Android 抖音常用反编译工具
  10. 第八章数组和矩阵问题(一)