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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 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

     

    相关实践学习
    基于CentOS快速搭建LAMP环境
    本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
    全面了解阿里云能为你做什么
    阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
    相关文章
    |
    1月前
    |
    关系型数据库 MySQL
    Mysql常用语法总结
    Mysql常用语法总结
    23 0
    |
    1月前
    |
    SQL 关系型数据库 MySQL
    MySQL 数据库基本语法
    SQL,全称Structured Query Language(结构化查询语言),是一种用于管理关系型数据库(RDBMS)的编程语言。SQL用于创建、修改、查询和删除数据库中的数据,以及定义数据库架构。它是数据库管理系统(DBMS)与应用程序之间的标准通信协议。
    77 6
    |
    1月前
    |
    SQL 关系型数据库 MySQL
    |
    1月前
    |
    SQL 数据库
    sql server中创建数据库和表的语法
    sql server中创建数据库和表的语法
    18 1
    |
    4天前
    |
    SQL XML 数据库
    sql导入数据库命令
    在SQL Server中,数据库导入可通过多种方式实现:1) 使用SSMS的“导入数据”向导从各种源(如Excel、CSV)导入;2) BULK INSERT语句适用于导入文本文件;3) bcp命令行工具进行批量数据交换;4) OPENROWSET函数直接从外部数据源(如Excel)插入数据。在操作前,请记得备份数据库,并可能需对数据进行预处理以符合SQL Server要求。注意不同方法可能依版本和配置而异。
    |
    10天前
    |
    SQL 数据库 HIVE
    Hive【基础知识 05】常用DDL操作(数据库操作+创建表+修改表+清空删除表+其他命令)
    【4月更文挑战第8天】Hive【基础知识 05】常用DDL操作(数据库操作+创建表+修改表+清空删除表+其他命令)
    21 0
    |
    17天前
    |
    关系型数据库 MySQL
    如何解决cmd命令窗口无法运行mysql命令的问题
    如何解决cmd命令窗口无法运行mysql命令的问题
    10 0
    |
    1月前
    |
    Shell Linux 数据库
    【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
    【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
    29 1
    |
    1月前
    |
    监控 Shell Linux
    【Shell 命令集合 磁盘管理 】Linux 检查和创建磁盘配额数据库 quotacheck命令使用教程
    【Shell 命令集合 磁盘管理 】Linux 检查和创建磁盘配额数据库 quotacheck命令使用教程
    32 0
    |
    1月前
    |
    存储 Shell Linux
    【Shell 命令集合 文件管理】Linux 更新locate命令所使用的数据库 updatedb命令解析
    【Shell 命令集合 文件管理】Linux 更新locate命令所使用的数据库 updatedb命令解析
    155 0