MySQL数据库 mysql常用命令

什么是数据库

数据库:存储数据以及数据结构的仓库,称为DB

注意:数据结构比数据更为重要 数据结构不合理 数据库也会慢

① 数据库本身是透明的空的

image-20211007104529267.png

② 数据库是需要管理的 :数据库管理系统 作用就是 管理数据

什么是数据库管理系统

专门管理数据库的,称为DBMS phpmyadmin

谁去管理

用户

数据库系统包含:

** 用户 User

数据库管理系统 DBMS

数据** DB

数据库的作用

帮助 查询 存储 管理 数据的

数据库的本质

数据库的本质是一个二维数组的表格,和excel是一样的

1 表名: 对应的是每一个实体,按照对象的概念来划分,

① teacher_table

② student_table

③ message_table

④ course_table

2 属性: 字段

数据库的种类

1 关系型数据库:mysql sql Server oracle db2

2 非关系型 (现在不用了) redis

1 mysql 登录和退出
        mysql [-h] -u -p
        mysql  -u root -p 123456
                   exit  退出

        quit   
2 查看辅助信息
        select now();  查看 当前时间

        select curdate();  当前日期

        select  curtime();  当前时间

        select  version();   mysql版本

        select user();   查看用户
3 数据库和数据类型
        ①  show  database:显示数据库

        ② create  database  数据库名称:创建数据库

        ③  drop  database   数据库名称:删除数据库

        ④  use 数据库名称
4 数据表
        create table 数据表名称(

    列定义
                列名称  类型 【默认值】【auto_increment自增长】

    【主索引   primany key】

      列名称   类型  【默认值】
          列名称   类型  【默认值】
          列名称   类型  【默认值】
               ......
           )
5 Mysql 数据类型
1 整型
tinyint1 字节有符号-128===>127
无符号0===>225
smallint2字节有符号-32768===>32767
无符号
int4字节有符号-2147483648===>2147483647
无符号0===4294967295
bigint8字节
2 浮点
float4字节会丢失字节
double4字节会丢失字节
DECIMAL[m,d]精度小数
3 字符
char定长字符255 只有一个字符也占255
varchar变长字符0-255
text65535个字符评论 文章
MEDIUMBLOB2的24次方
enum(val1,val2,val3)列枚举只能取其中一个 sex
4时间日期
DATE日期
DATETIME时间
time日期时间
6 创建表
        create table  stu(

            id  int(6)  auto_increment primany  key,

            stuNum varchar(6),

            stuName varchar(20),

            stuAge  tinyint(2),

            stuSex enum("1","2"),

            stuTel varchar(20)



    )

        查看表   show  table   

        查看表结构    desc  表名称

SQL 命令的DDL操作

1 表添加字段
alter table 表名称 add  列定义

alter table  stu  add email varchar(20);
2 修改字段
alter table 表名称  change 旧字段名称 新字段定义

alter  table  stu  change   email  stuEmail varchar(20);
3 删除字段
alter table  表名  drop   字段名

alter  table   stu  drop  student
4 修改表名称
alter table  表名  rename   新名字

alter  table   stu  rename  student
5 删除表
drop  table  表名称

drop  table student

SQL命令的DML操作(增删改查)

1 增
insert into  表名称(字段1,字段2,字段3,...)values(val1,val2,val3);
2 删
delete from 表名称  where  条件
3 改
update  表名称  set 字段=值,字段=值,字段=值,...  where  id=3;
4 查
select * from 表名
完整格式
select 字段列表 form 表名称[where 条件][order by 字段 asc/desc][limit 起始位置,长度][group by 字段名称(分组)]
查找多个字段
select 字段名,字段名,字段名...from  表名称(查询指定字段)
别名
可以给字段名或表名起别名,别名的作用  字段简单 方便调用

select  StuNum  as  Stn,  StuName  as  Sn  from student  as Sd;
order by
asc  升序
desc  降序

select * from student order by id  desc;
select * from student  order by  pid  asc;
limit
limit    起始位置,长度     用于分页  首页

select  * from student limit 1,3(从第二条开始截取  截取3条);
select  * from student limit 4,4  (从第五条开始截取  截取4条)

倒序截取

select * from student order by id desc limit 0,4;
group by 分组
select * from books group by TypeId 按类型id分组
分组之后,每组种的记录都会取1条
where 条件
1 比较 < > <= >= =
select * from books where id=100;
select * from books where pid>100;
2 逻辑运算 and or
select * from books where  bid>100 and bid<110;
3 模糊搜索
字符种含有某个关键词就能找到   like  %关键词% 表示任意字符
                                _关键字_  表示一个字符
                                
select * from books where bName like "%入门%";
4 范围
1  连续范围[not]between...and    (not  表示不是这个范围的其他)

select * from books where bid between 10 and 50;
等同于
select * from   books where bid>=10 and bid<=50;


2   不连续范围  [not] in()

select * from books where bid in(105,108,110);
等同于
select * from  books  where bid=100 or bid=103 or bid=108;
5 子查询
在select语句中又出现查询语句,称为子查询
Btype表Books表
bTypeIdbTypeNamebidbNamebTypeId
1windows应用93书名11
2<u>网站</u>94书名21
33D动画95书名3<u>2</u>
4Delphi学习96书名43
5黑客97书名18
6网络技术98书名58
7网络安全99书名65
8Linux学习100书名75
9AutoCAD101书名85
10Photoshop102书名93
select bTypeId from bType where bTypeName="网站"

select * from books where bTypeId=2;

上面语句的结果,作为了下面语句的开始,结合在一起

select * from books where bTypeId=(select bTypeId from bType where bTypeName="网站");

括号里面的 select bTypeId from bType where bTypeName="网站" 结果是2
加上前面的
select * from books where bTypeId=2

这段代码的含义是,查询在所有图书中查询 类型是 网站 的

所以查询是网站 这个条件是主要条件 那么 select bTypeId from bType where bTypeName="网站" 这句是主表

  被查询的表是  从表

主表:后面需求中涉及到的表

从表:前面需求中涉及到的表

此种方法效率比较低,一般使用链接查询

6 链接查询
通过多张表的共有字段,查找多张表构成的   合并的表       至少两张表以上
1)内连接
        **共同字段相等(没有地位之分,都是一样的)**            
语法
    select * from 表1,表2 where 表1.共有字段=表2.共有字段
    
    select * from book,bType  where books.bTypeId=bType.bTypeId
    
    select * from book as bs,bType as bt where bs.bTypeId=bt.bTypeId
2) 外连接
            **有主次之分 表分为  主表   和  从表**

            主表的数据  全部展示

            从表的数据   有和主表对应的数据展示

                                  不和主表对应的数据不展示

            保证主要内容都要显示



    left join  查主表             
语法:
select * from 主表 left join 从表 on  主表.共有字段=从表.共有字段  where [其他条件]

select * from books left join bType on books.bTypeId=bType.bTypeId
select * from books as bs left join bType as bt on bs.bTypeId=bt.bTypeId
    right join   查从表
语法:
select * from 从表 right join 主表 on 主表.共有字段=从表.共有字段  where[其他条件]

select * from bType right join books on bType.bTypeId=books.bTypeId
select * from bType as bt right join books as bs on bType.bTypeId=books.bTypeId
7 mysql中的聚合函数
**什么是聚合函数:**mysql提供的系统实现特定功能的函数

1) 算数函数:

            sum()求和  select  sum(bTypeId)  from books

            avg()求平均数   select  avg(bTypeId)  from books

            count()求个数  select  count(bTypeId)  from books

2) 拼接字符串 (将多个字符串拼接成一个字符串)

            concat(str1,str2,str3,...);

            select contat(bName,bTypeId) from books;

            select contat(bName,bTypeId) as  p    from books;//将两个字段拼接为 p 这个字段
8 mysql中的索引
        索引:相当于图书的目录,类似于查询目录  是以文件的形式存储的

        特点:数据的类型和索引的更新是一样的(类似于 书的内容更新了 ,目录也需要更新)

        作用:提高查询效率

        索引的类型:  1  主索引:   primary  key     确定唯一记录的   where id=???

                                 2  普通索引    ?

                                 3  唯一索引    ?

                                 4  全文索引    ?
9 mysql中的外键
        作用:让用户不更新数据,或者是用户删除数据的时候,让订单也同步删除
mysql的编码: utf8-general_ci


喜欢的朋友记得点赞、收藏、关注哦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武昌库里写JAVA

您的鼓励将是我前进的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值