Mysql数据库基本操作语法命令归纳

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Mysql数据库基本操作语法命令归纳

 

一、数据库相关概念:

    •    DB          Database                                     数据库
    •    DBA        Database Administrator               数据库管理员
    •    DBS        Database System                        数据库系统
    •    DBMS     Database Managerment System 数据库管理系统

       DBS=DBMS+DBA+DB+软件+硬件

       三种模型:层次模型  网状模型  关系模型

       关系型数据库:

          设计数据库的三个步骤:

           1.概念结构设计阶段:E-R 图: 实体(集)  属性  联系

                   一对一(1:1)  一对多(1:n) 多对多(m:n)

           2.逻辑结构设计阶段

           3.物理结构设计阶段

       常见的数据库:

           MySql

           SQLServer

           Oracle

           DB2

       约束:

           primary key    主键约束:不能为空且不能重复:唯一区分记录的标志(表级约束)

           foreign key    外键约束  

           not null    非空约束

           unique        唯一约束  null 'a'   null

           default        默认约束

           check        检查约束(在mysql中无效)   set enum

           auto_increment    自增  (主键)  

           主表  从表

    二、数据库操作:

    sql命令

    status;        查看当前状态
        show databases; 显示所有数据库
        use 数据库名;    使用/打开数据库

    image.gif

    create database bochy;
      create database [if not exists] bochy;(如果不存在)创建数据库
      drop database [if exists] bochy;  (如果存在)删除数据库(谨慎使用)

    image.gif

    三、数据库中表的操作:

    use bochy;    先打开数据库

    image.gif

       创建表:

    create table [if not  exists]  student(
        id int,
        name  varchar(6)  not null ,
        sex set('男','女'),
        ext text
        );

    image.gif

    create table student (
         id int primary key  auto_increment,
         name varchar(8) not null,
         age smallint default 18,
         address varchar(50) default '郑州市金水区',
         ext decimal(10,2)
         );

    image.gif

    create table score (
         id int ,
         cid int ,
         grade int,
         primary key(id,cid),
         foreign key(id) references student(id),
         foreign key(cid) references course(cid)
         );

    image.gif

    显示当前数据库的所有表:show tables; 
      查看表结构:desc student;

    image.gif

    添加数据:

    insert into student  values( 2015001,'张三','男','dfdsfbhfhw'  );

    image.gif

     添加部分列数据:

    insert into student(id ,name)  value(2015005,'小明');

    image.gif

       添加多条数据:

    insert into student values(2015006,'xiao王','男',null) ,(2015007,'ssss','男',null),(2015008,'xxx','女',null);

    image.gif

       删除表中数据:

    delete from student ;(删除表中所有数据)
        delete from student where id=2015006;(删除满足条件的数据)
        delete from student where id  is null;
        delete from student where id!=2015005;
        delete from student where id <>2015005;

    image.gif

       查看表中的数据:

     

    select * | 部分列
        from  表名
        | where (筛选记录的)条件
        | group by 字段名   having 筛选分组的条件
        | order by 字段名|聚合函数  asc|desc 
        | limit [起始下标,]长度 ;

    image.gif

    select * from Student;
        select id, name from student;
        select id as 学号, name  as  姓名  from student;
        select id  学号, name  姓名  from student;

    image.gif

     查询满足条件的前10条数据

    select id,name,sex,sum from student where sex='男'  order by sum  desc  limit 10 ;

    image.gif

    从下标为2的记录开始,往后查询5条数据

    select * from student limit 2,5;

    image.gif

      模糊查询

    select *   from student where name like '%a%';
        select *   from student where name like '%a_';

    image.gif

     比较查询

    select * from student where sex='男' and sum>300 ;
     select * from student where sex='男' or  sum>300 ;

    image.gif

    范围查询

    select * from student where id='2015001' or id='2015003' or id='2015005' or id='2015021'  ;
        select * from student where id in( 2015001,2015003,2015005,2015007,2015021,2015100,2015112);

    image.gif

    统计查询

    select count(*),sum(age),avg(age),max(age),min(age) 最小年龄 from student where age between 20 and 40;

    image.gif

       +----------+----------+----------+----------+----------+

       | count(*) | sum(age) | avg(age) | max(age) | 最小年龄 |

       +----------+----------+----------+----------+----------+

       |        9 |      222 |  24.6667 |       35 |       21 |

       +----------+----------+----------+----------+----------+

       1 row in set (0.05 sec)

     

    select major,count(*) from student where major='信息工程';

    image.gif

       +----------+----------+

       | major    | count(*) |

       +----------+----------+

       | 信息工程 |        2 |

       +----------+----------+

       1 row in set (0.00 sec)

    分组查询

    select sex ,count(*) 人数 from student group by sex;

    image.gif

       +------+------+

       | sex  | 人数 |

       +------+------+

       | 男   |    7 |

       | 女   |    5 |

       +------+------+

       2 rows in set (0.00 sec)

    select major,count(*) from student group by major;

    image.gif

       +--------------+----------+

       | major        | count(*) |

       +--------------+----------+

       | java软件开发 |        2 |

       | 平面设计     |        2 |

       | 网络工程     |        3 |

       | 信息工程     |        2 |

       | 英语         |        3 |

       +--------------+----------+

       5 rows in set (0.05 sec)

    select major,count(*) 人数 from student where age between 20 and 40 
        group by major  having 人数>=2  order by 人数 desc  limit 2;

    image.gif

       +--------------+------+

       | major        | 人数 |

       +--------------+------+

       | 网络工程     |    3 |

       | java软件开发 |    2 |

       +--------------+------+

       2 rows in set (0.00 sec)

    子查询:

    查找所有年龄大于李四年龄的学生的信息

    select * from student where age> ( select age from student where name='李四'   );

    image.gif

    查找成绩大于网络工程专业最高成绩的学生的信息

    select * from student where sum> (  select max(sum) from student where major='网络工程'  );

    image.gif

    any:任意一个

    select * from student where sum > any(   select sum from student where major='网络工程');

    image.gif

    all:所有

    select * from student where sum > all(   select sum from student where major='网络工程');

    image.gif

    some:一些

    select * from student where sum > some(   select sum from student where major='网络工程');

    image.gif

    查询既选修了1001课程又选修了1002课程的学生的信息

    select id from score where cid='1002'  and id in(    select id  from score where cid='1001');

    image.gif

    +---------+

    | id      |

    +---------+

    | 2015001 |

    | 2015021 |

    +---------+

    2 rows in set (0.00 sec)

    自查询:

    select a.eno ,a.ename 员工姓名 ,b.ename 经理名称 from employee a left join employee b on a.boss=b.eno;

    image.gif

    多表查询:

    1.全链接(笛卡尔积):

    select * from 表1,表2;

    image.gif

    2.等值连接:

    select * from 表1,表2 where 表1.字段=表2.字段;

    image.gif

    3.内连接:

    select * from 表1 [inner] join 表2  on  表1.字段=表2.字段  where

    image.gif

    4.外链接

    左外链接:在保留左表所有数据的基础上进行等值连接

    select * from 表1 left[outer] join 表2  on  表1.字段=表2.字段  where

    image.gif

    右外链接:在保留右表所有数据的基础上进行等值连接

    select * from 表1 right[outer] join 表2  on  表1.字段=表2.字段 where

    image.gif

    5.联合查询

    select * from student where major='信息工程'
    union [all | distinct] 
    select * from student where id in(2015001,2015002,2015020,2015021);

    image.gif

     

    相关实践学习
    如何在云端创建MySQL数据库
    开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
    全面了解阿里云能为你做什么
    阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
    相关文章
    |
    15天前
    |
    SQL 自然语言处理 关系型数据库
    MySQL的match匹配多个字符串的语法
    【8月更文挑战第27天】MySQL的match匹配多个字符串的语法
    166 67
    |
    19天前
    |
    存储 关系型数据库 MySQL
    初步了解MySQL数据库的基本命令
    初步了解MySQL数据库的基本命令
    24 0
    |
    12天前
    |
    SQL 自然语言处理 关系型数据库
    MySQL的match匹配多个字符串的语法
    【8月更文挑战第29天】MySQL的match匹配多个字符串的语法
    36 2
    |
    2月前
    |
    存储 关系型数据库 MySQL
    (十五)MySQL命令大全:以后再也不用担心忘记SQL该怎么写啦~
    相信大家在编写SQL时一定有一个困扰,就是明明记得数据库中有个命令/函数,可以实现自己需要的功能,但偏偏不记得哪个命令该怎么写了,这时只能靠盲目的去百度,以此来寻找自己需要的命令。
    103 28
    |
    24天前
    |
    关系型数据库 MySQL Linux
    数据类型和运算符(MySQL服务器的安装,MySQL客户端,数据类型,运算符,MySQL的语法规范)
    无论是对于初学者还是有经验的开发者,了解MySQL的安装、客户端使用、数据类型、运算符和语法规范都是至关重要的。这不仅有助于高效地管理和查询数据,而且对于设计和实现数据库解决方案来说是基础工作。通过深入学习和实践这些知识,您可以更好地发挥MySQL数据库的强大功能。
    16 2
    |
    1月前
    |
    SQL 关系型数据库 MySQL
    INSERT INTO t_a.tableName SELECT * FROM t_b.tableName 如何通过定义一个list对象,包含多个tableName,循环执行前面的sql,用MySQL的语法写
    【8月更文挑战第7天】INSERT INTO t_a.tableName SELECT * FROM t_b.tableName 如何通过定义一个list对象,包含多个tableName,循环执行前面的sql,用MySQL的语法写
    23 5
    |
    11天前
    |
    SQL 关系型数据库 MySQL
    SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
    【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
    70 0
    |
    2月前
    |
    NoSQL MongoDB 数据库
    Mongo 数据库备份和恢复命令
    Mongo 数据库备份和恢复命令
    40 4
    |
    2月前
    |
    SQL 关系型数据库 MySQL
    MySQL删除表数据、清空表命令(truncate、drop、delete 区别)
    MySQL删除表数据、清空表命令(truncate、drop、delete区别) 使用原则总结如下: 当你不需要该表时(删除数据和结构),用drop; 当你仍要保留该表、仅删除所有数据表内容时,用truncate; 当你要删除部分记录、且希望能回滚的话,用delete;
    |
    2月前
    |
    SQL 关系型数据库 MySQL
    mysql性能调优:EXPLAIN命令21
    【7月更文挑战第21天】掌握SQL性能调优:深入解析EXPLAIN命令的神奇用法!
    35 1