博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 常用的命令
阅读量:7307 次
发布时间:2019-06-30

本文共 3668 字,大约阅读时间需要 12 分钟。

hot3.png

1.打开数据库:

                1.cmd-->mysql -u root -p123456 ;

                2.开始--应用程序----mysql---mysql sever 5.7----第二个

2.数据库;

             1. 创建

                create database 表名;

             2.查询

              show databases(显示数据库)。

             3.创建指定字符

                create datebase 表名 character set utf8;

           4.查看数据库的字符集

               show create datebase 表名

          5.删除数据库:

             drop database 表名。

            drop database if exists 表名。

3.表

        1.指向数据库:

            use 库名; 

       2.创建表名:

            create table name(列表1,类型1);

             2.1:创建字符集表,有编码格式

                      create table 表名(列名 数据类型 )character set 字符集。

                      eg:alter table t1 charater set gdk;

                             check :show create table t1;  

         3.查看表:

           show tables;

        4.删除表

             drop table 表名;(没有会报错)

              drop table if exists 表名;

           5.给表添加数据,会添加一行新的数据

                         5.1   insert into 表名  values(列1, 列2,......)      

                        151931_zoix_2841440.png

                                         151936_IhB6_2841440.png

                      5.2: 插入一行数据,对应的列值改变,其余 null

                               insert into 表名  set 列=值;

                             153344_9j8a_2841440.png

                                       153352_ZT3L_2841440.png

                    5.3 插入数据,对应的列值改变,其余为Null

                              insert into 表名  (列,列。。。) values(值,值。。)

                         154612_9jMs_2841440.png

                                       154619_UiBH_2841440.png

           6.查看表里的具体列

                  select * from 表名                        *表示所有列

                 select  id from 表名

           7.表添加1列

                alter table 表名 add 列名 数据类型。

                145430_J1Nl_2841440.png

                                     145605_rJrN_2841440.png

             8.修改列的数据类型

                 alter table 表名 modify 列名 新的数据类型

                  145231_ch7x_2841440.png

           9.删除某一列

                   alter table 表名 drop 列名

                   144833_dd7b_2841440.png

                                     145808_f3TB_2841440.png

          10.改变表的列名

                 alter table 表名 change column 旧表名  新表名  类型

               150319_tAOS_2841440.png

                                        150511_cAPO_2841440.png

       11. 显示创建表的指令

                 show create  table 表名;

                 150931_L5pL_2841440.png

                 150934_YlMo_2841440.png

12. 改变数据:

       update 表名 set 列名 =表达式 where 条件 注意:如果没有where条件,则对整个数据改变。

                160220_j8uj_2841440.png

                                 160229_BgZ3_2841440.png

13.删除某一行的数据,where对应的条件  注意:如果没有where条件,则将删除表中的数据

                                    delete from 表名 where 条件

        条                  件162527_uamp_2841440.png

                                162536_MPI7_2841440.png 

 14.删除数据,找回来,Mysql 自动提交,需要设置不自动提交

             14.1.设置不自动提交

                             set  autocommit=false

             14.2.设置保存点

                            savepoint aaa;

                         170757_xGrL_2841440.png

             14.3.删除表里的数据

                             delete from 表名

                           170418_BNJB_2841440.png

               14.4.回滚数据

                          rollback to aaa;

                          170706_gx41_2841440.png

15.筛选 select

               15.1 select 列名 .......from  表名  where 条件。

              192151_Gv38_2841440.png

                                      192204_ZN0R_2841440.png

              15.2 select distinct 列 from  表名(指定过滤重复的数据)

                            194826_EQKD_2841440.png

                             194833_oSYT_2841440.png   

                          15.3 列可以参与元素

                                              select 列+数字 from 表名

                                           195914_rNRu_2841440.png

                          15.4:select  *from 表名  betwent ....and  .....  注意:包括上下边

                                   204844_cvyS_2841440.png

                                                      204852_tDsa_2841440.png

                                         

                         15.5   select * from 表名 not betwent .....and .....  注意:不包括边界

                             205322_aJD1_2841440.png

                                                 205342_8gJd_2841440.png

                         15.6   select  *from 表名  条件 and  条件

               t205855_eCUr_2841440.png

                                 205903_qTAS_2841440.png

                           15.7 select * from   表名  条件  or 条件

                      210032_EhgS_2841440.png

                                  210042_INxg_2841440.png

             15.8   like   模糊查询  %代表0个或多个任意字符(汉子一个字符),_代表一个字符                         select   *from 表名  where  name  like '彭%'_

              s      e211656_F8R7_2841440.png

                                 211707_NEBV_2841440.png   

                            select *from 表名 where name like '彭_' ;  注意:_代表 彭 后只能有一个字符。

                         212243_iRJR_2841440.png

                                 212302_hAhx_2841440.png         

                        212317_j5Fl_2841440.png      

 15.8   : is  null

                   select * from 表名 where  列  is  null;

                         212654_LQm6_2841440.png

                                     212657_YOqH_2841440.png

                     select * fome 表名  where 列 is not null;

                          213300_Qofq_2841440.png

                                    213304_q7r0_2841440.png

           15.9 降续,升续

                  select 列1,列2  from 表名  order by 列;

                   141618_ETRD_2841440.png

                    141625_I7A2_2841440.png

     15.10 降续

              select 列 from 表名  order by 列 desc.

                     注意事项:  关键字:order by ,在末尾加 desc.

                                                     

                      

 

                   142250_bryO_2841440.png

                    142312_9aMx_2841440.png

15.11 别名,as

             select  m as '别名' 表名 from  order by 列。

                  142729_luK0_2841440.png

                  142734_YoB0_2841440.png

  15.12   in选数据

              select * from 表名 where 列 in (数字);

                143147_yVHp_2841440.png

                   143153_60tf_2841440.png

16.函数:

       16.1  count()表示计算的个数。如果是null,不加入计算。

                           select count( 列) from 表名。

                  191643_wtYK_2841440.png

       16.2   sum求和,

                select sum(列) * from 列名。求几列的总和时(列1+列2+列3)

                 192057_JzsH_2841440.png

      16.3    avg 返回平均值,如果为null,则不计算。

                  221302_rEXL_2841440.png

          16.4     max  min 最小值

                221636_VFbH_2841440.png

           16.5 分组:select    from 表名 group by 那列分组;

                         过滤   
                    having  和where 一样,可以实现筛选。
                         having 写在 group by 后。

                 221904_Y0TM_2841440.png

                221939_yCnR_2841440.png

          16.6时间 selecct current_date() from dual;  年月日

                    222400_QCWK_2841440.png       

                        select current_time() from dual; 时分秒

                     222829_JPaV_2841440.png  

                          当前年:月,日

                          select  hour(current_time()) from dual;

                        223145_9Ht5_2841440.png

                         223156_v8xv_2841440.png

   16.7  添加时间,减少时间

                 selecct date_add(now(),interval 1month)from 表名

                    select date_sub(now(),interval 1 month )from 表名

                          223443_Rla0_2841440.png

    

             16.8   两个时间之差:

                          select datediff(时间,时间) from dual

                            223712_C2Zq_2841440.png

                16.9 拼接   concat  追加数据:不会修改数据库

                                    select concat(任意数据,任意数据) from dual;

                                224225_zoDV_2841440.png

                16.10 

                                小写变大写:lcase

                                 select lcase(字符串)  from dual.

                  224954_q6MY_2841440.png

                                          大写变小写:ucase

                          225039_9qHF_2841440.png

                              截取 substring(开始,结束)

       bb   225813_0w4V_2841440.png

     
   向上取整
              select ceiling(12.1)from dual;(12.0=12)

  向下取整
                 select floor(12.8) from dual; 
   求最小数;
            select least (12,4,6)from dual.
    取与
             select mod(31,2)from dual.

  17.链表查询:

                 将两个链表连在一起:2种方式

                 1.sele

                                                     115914_Vc50_2841440.png

                          115341_QHPT_2841440.png

                         115400_X0dK_2841440.png

           

 

 

 

4.备份数据 在cmd环境下输入 

            1mysqldump -u root -p123456 库名 >d:/pc.sql;   尖括号。

5.导入数据:yunsuan

              1.创建一个新的空的数据库

                   create database 数据库名;

              2.  指向新的数据库

                    use 新建的数据库名

              3. 导入

                    source d:/pc.sql

6.mysql里的数据类型

                1.short----->smallint;

                2.int------->int

                3.long------->integer

               4.float ------->float(m,d) m:  有效数字,d:小数点后几位,如果小数大于d+1,四舍五入。小数等于d+1,五舍六入。

                  5.double------>double 与flaot类似;

                  6.boolean----->tinyint  0,1;可以输入true,false,显示的是0,1

                  7.string----varchar(45)

                  8.string-----text 大文本.

                  9date -------date

7.date 详解:

          date :年月日

         datetime: 年月日时分秒    时间不变   now()获取当前时间。update()更新时不变。

         timestamp: 获取当前时间  now() ,update某条记录时,该列的值会自动更新。

                           update 表名 set 列=值;

8.主键自增:primary key auto_increment

     create table users(id int primary key auto_increment);

当添加新的一列时;

    alter table users add i int primary key auto_increment

 

 

         

                   

 

 

 

           

 

        

 

 

 

 

 

 

转载于:https://my.oschina.net/18829297883/blog/918230

你可能感兴趣的文章
热修复初探
查看>>
leetcode刷题记之76 Minimum Window Substring
查看>>
【Nodejs篇五】Node js 使用 superagent 与 cheerio 完成简单爬虫
查看>>
[译]React高级话题之Render Props
查看>>
前端菜鸟之SASS入门笔记
查看>>
熔断Hystrix使用尝鲜
查看>>
OAuth 2.0协议在SAP产品中的应用
查看>>
树的重心
查看>>
快速搭建你的 github pages 个人博客 —— 基于 Create-React-App 的单页面应用实践...
查看>>
NPM酷库:winston 多路日志记录
查看>>
vue中的methods、computed和watch
查看>>
js 中的基本类型,引用类型,基本包装类型
查看>>
【341天】我爱刷题系列100(2018.01.12)
查看>>
arch linux安装过程
查看>>
Word强大插件Writage使用手册
查看>>
CentOS7下PHP7.2安装redis扩展
查看>>
聊聊FilterSecurityInterceptor
查看>>
让数据库变快的10个建议
查看>>
Spring Boot 揭秘与实战
查看>>
【327天】跃迁之路——程序员高效学习方法论探索系列(实验阶段85-2017.12.29)...
查看>>