MySQL-Part1:Base

base

  1. 启动/停止某种服务

    • windows 下 net start/stop [service name]
    • linux 下 systemctl action service_name ; service service_name action
  2. 本地、远程登陆到数据库

    • 本地:mysql [-P port_number ] -u user_name -p 回车后键入对应的pwd. (缺省了-h localhost
    • 远程:mysql [-P port_number] -h mysql_host_ip -u user_name -p 回车后键入对应的pwd.
  3. 基础 :良好的书写习惯,每条sql命令后添加" ; "作为标识

    • 显示所有数据库 :show databases;
    • 切换到某一数据库 :use database_name ;
    • 显示当前数据库所有的表 show tables;
    • 在A数据库下显示B数据库中的某个表(当前仍然处于A数据库) show table_name from A;
    • 查看当前安装的 mysql 版本号: select version() ;
    • 查看当前所处的数据库 : select database();
    • sql 命令 一般不区分大小写;
    • 注释的写法:
      • 单行注释 #注释内容 或者 -- this is comment (注意中间含有一个空格)
      • 多行注释: /* 注释内容
        注释内容
        注释内容 */
    • 查看警告信息show warnings;
    • mysql8.0版本以上windows下支持命令清屏:system cls
  4. 对表结构进行操作

    • 创建表:create table table_name (stuid int ,stu_name varchar(20) , gender char , borndate datetime);
    • 查看表结构 desc table_name; 或者 select [full]columns from table_name;
    • 编辑表结构----修改字段属性/增加或删除某字段
      • alter table table_name modify column col_name filed_type 其他属性;
      • alter table table_name add column new_col_name 数据类型 其他属性;
      • alter table table_name drop column col_name1,col_name2;
    • 丢弃该表(及其中的数据):drop table table_name;
  5. 对表数据进行操作:

    • 插入新的数据:

      insert into table_name values  
      (1,'张三','男','1993-01-04'), 
      (2,'李四','男','1983-03-25'),
      (3,'芙蓉','女','1995-02-20');
      
    • 查看数据内容:

      • select * from table_name;
      • 查看特定的某些字段,各个字段之间应使用逗号隔开,若字段名属于reserved keywords 则需要将字段名包含在反冒号 `对
    • 更新(修改)表内具体数据值------关键字SET:update table_name set born_date='1983-02-18' where stuid = '李四';

    • 删除[具体的某一条]数据: delete from table_name [where stu_name='张三'];

    • 语句的执行顺序:from clause , where clause, select clause , order by clause .(故排序表达式中可以使用别名,而where语句中不可以)

  6. SQL语言; DDL , DML , DCL(TCL) , DQL

  7. 一些关键字&函数

    • as 重命名字段名 select col_1 as rename_col from table_name ; 也可以使用空格代替

    • count函数(忽略null值):

      • 统计某字段count(col_name)
      • 统计行数:count(*) | count(1) 其中1可被任何常量所替代
    • 字符串函数

      • concat() 用于连接两个字符串 : select concat(字段1, 字段2) from table_name; 若函数中有一个参数为空,则最终结果显示为NULL,为避免此种情况出现,在可能为空的字段外套用:
        ifnull(field_name,为空时的替代值) 函数
      • length(str) 提取字符串字节长度;char_length(str) 以字符为度量单位返回str长度。
      • left,right函数:左起|右起截取字符串共n个字符。
      • trim系列函数:删除指定字符两端|左端|右端的空格(默认为空格,可指定其他字符)。
      • lpad,rpad函数:填充函数
      • substr函数:随意截取连续的字符串
      • space函数,strcmp函数
    • 日期函数:now,curdate,date_format ,str_to_date, get_format|date_formate等日期函数,

    • if(judge_exp,yes_re,no_re) ; case [ exp ] when condi_1 then action end 等系列字符流控制函数以及其他函数,需要时可翻阅手册12章。

    • distinct 筛选不重复的数据值 select distinct col_name from table_name;

    • where 用于条件查询.

      • 关系表达式 : > , < , >= . <= , = , <> (不等于,一般不使用 != ) ;

      • 逻辑运算符 : and or not 也可表述为: && || !

      • [ not ] like,用于模糊查询。 通配符: % 匹配多个字符; _ 匹配单个字符; 若需要真正的匹配%或者_需要在相应的符号前使用 反斜杠 \ ,或者也可以使用 escape 语句来指定某个字符作为转义字符。详细参见 Mysql refman-8.0-en.a4.pdf 第12.8节 P1939 。

        SELECT 'David!' LIKE 'David\_';                                    `-> 0`
        SELECT 'David_' LIKE 'David\_'; `                                      `->1`
        SELECT 'David_' LIKE 'David|_' ESCAPE '|';`               `-> 1`
        
    • [ not ] in (字段1,字段2 ...)**
      例如: select * from where stu_id in (1,3,5); 筛选出学号为 1,3,5的学生的所有数据。

    • [ not ] between A and B 其中 A,B数据类型应该一致&应为除text、blob 之外的其他类型。(手册不会使用,没有找到详细阐述) , 可以使用逻辑运算符来替代。

      • is [not] null 用于限定 字段值是否为空值NULL
        ( 注: '=' 只能用来判断普通内容 ; '<=>' 安全等于 :既可以判断普通内容也可以判断NULL值)。

      • 分组筛选group by 与 分组后条件筛选having

        • 查询结果排序呈现 **order by exp1 ASC/DESC [,exp2 ASC/DESC...] **

          • ASC 升序排列 ,DESC 降序排列,缺省值为:ASC
          • 表达式可以为多种形式:单个字段,多个字段,表达式,别名,列序号(与使用相应的字段名效果一样)
          • 详见手册Select Statement 部分。
  8. 各种语句:

    • 总览: from ——> where(分组前条件筛选)——>group by——>having(分组后条件筛选) ——>select ——>order by ——>limit

    • select 中的定义的别名可以被 group by,order by ,having 等语句引用

      A select_expr can be given an alias using AS *alias_name*. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses.

    • 使用group by 对进行分组之后,select 语句中不能出现除① 聚合函数② group by 语句中的字段 外的其他字段(原始table 中的字段不再能被识别)

    • having语句 && where语句

      • The HAVING clause, like the WHERE clause, specifies selection conditions. The WHERE clause specifies conditions on columns in the select list, but cannot refer to aggregate functions. The HAVING clause specifies conditions on groups, typically formed by the GROUP BY clause. The query result includes only groups satisfying the HAVING conditions. (If no GROUP BY is present, all rows implicitly form a single aggregate group.)
      • The HAVING clause is applied nearly last, just before items are sent to the client, with no optimization. (LIMIT is applied after HAVING.)

      • The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.

      • If the HAVING clause refers to a column that is ambiguous, a warning occurs. In the following statement, col2 is ambiguous because it is used as both an alias and a column name:

      SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;
      

      Preference is given to standard SQL behavior, so if a HAVING column name is used both in GROUP BY and as an aliased column in the select column list, preference is given to the column in the GROUP BY column.

      • Do not use HAVING for items that should be in the WHERE clause. For example, do not write the following:
      SELECT col_name FROM tbl_name HAVING col_name > 0;
      

      Write this instead:

      SELECT col_name FROM tbl_name WHERE col_name > 0;
      
      • The HAVING clause can refer to aggregate functions, which the WHERE clause cannot:
      SELECT user, MAX(salary) FROM users
        GROUP BY user HAVING MAX(salary) > 10;
      

      (This did not work in some older versions of MySQL.)

  9. MySQL中的匹配

    • 基本用法
      • [NOT ]LIKE :%用于匹配任意个字符 ; _用于匹配单个字符;
      • 转义:① 直接转义: \需转义的字符ESCAPE '需转义的字符'
    • 使用符合“扩展正则表达式”的语法进行匹配:[NOT] REGEXP|RLIKE
posted @ 2021-01-20 23:08  Walker-r  阅读(73)  评论(0编辑  收藏  举报