Linux中MySQL5.7的安装与SQL语句

安装步骤

tips:

帮助文档:
https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html?tdsourcetag=s_pctim_aiomsg

  1. 下载mysl官网安装包

    下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads

    在这里插入图片描述

  2. 安装上传下载工具包

    yum install -y lrzsz

    rz:传至linux

    sz:linux传出

  3. 将安装包移至linux用户家目录

  4. 本地安装

    yum localinstall mysql-community-*

  5. 查看MySQL状态、版本

    systemctl status mysqld

    mysql -V

  6. 启动服务

    systemctl start mysql

  7. 查看临时密码

    grep 'temporary password' /var/log/mysqld.log

  8. 登录MySQL

    mysql -u root -p

    mysql -uroot -pajoIl/px

  9. 修改密码,但学习用,修改密码策略,否则设置密码较为麻烦

    set global validate_password_policy=0;	//策略
    set global validate_password_length=6;	//长度
    ALTER USER "root"@"localhost" IDENTIFIED  BY "123456";
    
  10. 退出验证

    exit

    mysql -u root -p

使用技巧

tips:

前面没有标注为**mysql>**下命令

**[root@localhost ~]#**为终端命令

基本使用

  • 查看数据库信息

    mysql>\s

  • 查看可用数据库

    show databases;

  • 使用数据库

    use mysql(数据库名);

  • 查看表

    show tables;

  • 查看权限

    show grants;

    查看其它用户授权

    SHOW GRANTS FOR 'pig'@'%';

  • 用户授权

    grant all privileges on *.* to 'root'@'%' identified by '123456';

    %:任意ip

  • 创建数据库

    mysql>CREATE DATABASE test DEFAULT CHARACTER SET utf8;

    [root@localhost ~]#echo "show databases;" | mysql -uroot -p123456

    [root@localhost ~]#echo "create database w;" | mysql -uroot -p123456

  • 创建表

    CREATE TABLE users(id INT,name CHAR(30),age INT);

  • 插入数据

    INSERT INTO users VALUES(1,'san',45);


  • 数据库备份

    [root@localhost ~]#mysqldump -uroot -p123456 test > test.sql

  • 数据库导入

    [root@localhost ~]#mysql -uroot -p123456 test < test.sql


  • 查看表结构

    desc user;

查询

select table_name from tables where table_schema=‘test’;

select column_name from columns where table_schema=‘test’ and table_name=‘users’;


查询

  • 查询所有

    select * from t_persons;

  • 查询选定字段

    select last_name,first_name from t_persons;

  • 查询显示字段名优化

    select last_name as 名,first_name as 姓 from t_persons;

  • 查询信息条数优化

    显示前3条

    select * from t_persons limit 3;

    显示2条后的1条

    select * from t_persons limit 2,1;

  • where查询

    select * from t_persons where last_name = 'Adams';

    select * from t_orders where id =2-1;

  • in的使用(允许where子句中有多个值)

    select * from t_orders where id in (1,3);

  • 运算符

    select company from t_orders where order_number <= 4698;

    SELECT 列名称 FROM 表名称 WHERE 列名称 BETWEEN 值1 AND 值2;

    select * from t_orders where order_number between 2500 and 4500;

    select * from t_orders where order_number=2500 or order_number=4500;

    不能将john放在huang之前,与首字母顺序有关

    select * from t_persons where first_name between 'huang' and 'john';

    MySQL:包括两端

    ORACLE:包括两端

    SQL Server 2008:包括两端

    SELECT 列名称 FROM 表名称 WHERE 列名称 LIKE 表达式

    %:替代一个或多个字符 _:仅替代一个字符

    select * from t_orders where company like '%i%';

    select * from t_orders where company like '_i__';

    操作符 描述
    = 等于
    <> 不等于
    > 大于
    < 小于
    >= 大于等于
    <= 小于等于
    [NOT] BETWEEN…AND… 在某个范围(外)内
    LIKE 搜索某种模式 (%、_)

    tips:在某些版本的SQL中,操作符 <> 可以写为 !=

  • 排序(默认升序、desc降序)

    ORDER BY 语句用于根据指定的列对结果集进行排序

    select * from t_persons order by 1;

    select * from t_persons order by 1 desc;

    tips:

    order by 能够猜测指定表含有多少列

  • 查询—表连接

    • 内连接: INNER JOIN(JOIN):如果表中有至少一个匹配,则返回行

      ​ 在表中存在至少一个匹配时,返回行

      SELECT 列名 FROM 表1 INNER JOIN 表2 ON 表1.列名=表2.列名

      select o.id,p.first_name,p.last_name from t_orders as o inner join t_persons as p on o.id=p.id;

    • 左连接: LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行

      ​ 关键字会从左表 (表1) 那里返回所有的行,即使在右表 (表2) 中没有匹配的行

      SELECT 列名 FROM 表1 LEFT JOIN 表2 ON 表1.列名=表2.列名

      select p.first_name,p.last_name,o.company from t_persons as p left join t_orders as o on p.id=o.id;

    • 右连接: RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行

      ​ 关键字会从右表 (表2) 那里返回所有的行,即使在左表 (表1) 中没有匹配的行

      SELECT 列名 FROM 表1 RIGHT JOIN 表2 ON 表1.列名=表2.列名

      select p.first_name,p.last_name,o.company from t_persons as p right join t_orders as o on p.id=o.id;

    • MySQL 不支持全连接

  • 联合查询

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集

    SELECT 列名 FROM 表1 UNION SELECT 列名 FROM 表2

    UNION 内部的 SELECT 语句必须拥有相同数量的列。

    列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同

    默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL

    select id,order_number,company from t_orders union select 1,2,database();

    select id,order_number,company from t_orders union select 1,2,table_name from information_schema.tables where table_schema='w';

    select id,order_number,company from t_orders union select 1,2,column_name from information_schema.columns where table_schema='w' and table_name='t_orders';

猜你喜欢

转载自blog.csdn.net/qq_45149707/article/details/107391244
今日推荐