MySQL常用语句

连接MySQL

-- 语法:mysql -u用户名 -p密码  注:--空格 起到注释的作用
mysql -uroot -p123456

数据库操作

-- 显示当前时间、用户名、数据库版本(可以单独查询)
    select now(), user(), version();

-- 查看所有数据库
    show databases;

-- 创建数据库
-- 语法:create database 数据库名 [数据库选项];  []可缺省
    create database test; 
    create database test1 charset=utf8;

-- 查看数据库的创建信息
-- 语法:show create database 数据库名;
    show create database test;

-- 修改数据库的选项
-- 语法:alter database 数据库名 修改的数据选项;
    alter database test1 charset=utf8mb4;

-- 使用数据库
-- 语法:use 数据库名;
    use test1;

-- 查看当前使用的数据库    
    select database();

-- 删除数据库 
-- 语法:drop database 数据库名; 
    drop database test;

 表的操作

-- 创建表
-- 语法:create table 表名(字段 类型 [约束]);  -- []表示可缺省
-- 常用约束:
    -- 主键:primary key
    -- 非空:not null
    -- 自增:auto_increment
    -- 无符号:unsigned
    -- 默认:default 默认值
    create table user_info(id int,name varchar(30));
    create table user_info(
        id int unsigned primary key not null auto_increment, 
        name varchar(30),
        gender enum("男", "女", "保密") default "保密",
                age TINYINT
    );

-- 查看表结构
-- 语法:desc 表名;
    desc user_info;
        
-- 修改表结构
-- 语法:alter table 表名 操作名;
    -- 操作名
        -- 增加字段:add column 字段名 字段属性;
            alter table user_info add column addr varchar(30);
        -- 删除字段:drop column 字段名
            alter table user_info drop column addr;
        -- 修改字段属性:modify column 字段名 字段属性;
            alter table user_info modify column addr int;
        -- 修改字段名和属性:change column 原字段名 新字段名 字段属性;
            alter table user_info change column addr address varchar(20);
        -- 修改约束:
            -- 添加主键:add primary key(字段名)
                alter table user_info_cp add primary key(id);
            -- 删除主键:drop primary key  (删除主键前需删除其AUTO_INCREMENT属性)
                alter table user_info_cp drop primary key; 
                
-- 删除表
-- 语法:drop table 表名;
    drop table user_info;

-- 查看创建表的语句
-- 语法:show create table 表名;
    show create table user_info;

-- 复制表结构
-- 语法:create table 表名 like 要复制的表名
    create table user_info_cp like user_info;

-- 复制表结构和数据    
-- 语法:create table 表名 select * from 要复制的表名
    create table user_info_cpdata select * from user_info;

 数据操作

-- 插入数据
-- 语法:insert into user_info[(指定字段)] values(字段值);
  -- 全部插入
    insert into user_info values(4, "李四", "女", 19);
  -- 部分插入
    insert into user_info(name, gender, age) values("张三", "男", 22);
  -- 插入多条
    insert into user_info(name, gender, age) values("王麻子", "男", 22),("老王",1,33);
  -- 插入一列数据,注意没有values
  insert into class(class_name) (SELECT class_name from user_info group by class_name);  -- 将user_info表中搜索出的class_name字段的内容在class表的class_name字段的最后条记录后插入

--
查询表中数据 -- 语法:select 字段 from 表名 [where 字段=字段值]; -- 简单查询 select * from user_info; -- 条件查询 select name,gender from user_info where gender="女"; -- 多条件查询 select name,gender from user_info where gender="女" and age>18; -- 修改数据 -- 语法:update 表名 set 字段=新字段值 where 字段=字段值 update user_info set name="王五" where gender="男" and age=22; -- 修改整个字段内容 update user_info u left join class c on u.class_name=c.class_name set u.class_name=c.id; -- 将user_info表中class_name字段内容全部替换成class表中对应的班级ID -- 删除数据 -- 语法:delete from 表名 条件; delete from user_info where name="李四";

 查询进阶

user_info表

score表

 

-- 使用 distinct 去重查询
    select distinct gender from user_info;

-- 使用 as 取别名,as 可缺省
        -- 为字段取别名
      select name "姓名",gender as g from user_info where g="男";
        -- 为表取别名
          select * from user_info as u,score s where u.name=s.name;

-- 查询常用函数
    -- 使用 avg 查询平均数
      select avg(age) from user_info;
    -- 使用 sum 查询总和
      select sum(age) from user_info;
    -- 使用 min(max) 查询最小(最大)值
      select min(age) from user_info;
    -- 使用 count 统计查询记录数
      select count(*) from user_info;
    
-- 使用 group by 分组查询
    select class from user_info group by class; -- 查询班级名称
    select avg(age) from user_info group by class; -
    -- 使用group by 查询语句不能使用where 条件筛选数据,可以使用 having 条件筛选
      select class from user_info group by class having avg(age)>20; -- 查询平均年龄大于20的班级
        -- 使用 group_concat 查看分组的详细信息
          select class,group_concat(name) from user_info group by class;

-- 使用运算符查询
    -- 使用比较运算符(> < =)
      select * from user_info where age>18;
    -- 使用between...and...查询
      select * from user_info where age between 18 and 30;
    -- 使用 in 查询
      select * from user_info where age in (11,22,33);
    -- 使用 is null (is not null) 查询
      select * from user_info where address is null;
    -- 使用 and  or  not 查询
      select * from user_info where (age=18 and gender="女") or gender!="女"
        
-- 使用 like 模糊查询
    select * from user_info where name like "王%"

-- 使用limit分页查询
-- 分页公式   limit (页码-1)*分页大小,分页大小
    select * from user_infor limit 10;  -- 显示最前面10条数据
    select * from user_infor limit 0,10;  -- 从第1条数据开始,显示后面10条数据
    select * from user_infor limit 10,10;  -- 从第11条数据开始,显示后面10条数据
    select * from user_infor limit 20,10;  -- 从第21条数据开始,显示后面10条数据
        
-- 使用 order by 给查询数据排序
    -- asc 升序查询,默认升序
      select * from user_info order by age;
      select * from user_info order by age asc;
    -- desc 降序查询
      select * from user_info order by age desc;
        
-- 多表查询
    -- 把查询结果当成条件
      select * from user_info where name in (select name from score where score>59);
    -- 把查询结果当成一张新表
      select * from (select name from user_info) as n left join score on n.name=score.name;

-- 等值连接查询
    select * from user_info as a, score as b where a.`name`=b.`name`;
        
-- 自连接查询
    select a.* from user_info a, user_info b where a.name=b.team_leader; -- 查询班长信息
        
-- 多列子查询
    select * from user_info where (age, class)=(select age, class from user_info where name="李四");  -- 查询与李四同班同龄的信息
        
-- 连接查询
    -- inner join on 内连接。只返回相互匹配部分的数据
      select * from user_info inner join score on user_info.name=score.name; -- 查询score中有成绩的人员信息
    -- 外连接分为 左连接 left join 和右连接 right join
      -- 左连接。返回左表全部数据和右表匹配数据
        select * from user_info left join score on user_info.name=score.name; -- 左表为user_info,右表为score
      -- 右连接。与左连接相反。
        select * from user_info right join score on user_info.name=score.name; -- 左表为user_info,右表为score
原文地址:https://www.cnblogs.com/testlearn/p/12442582.html