MySQL学习笔记-1

Mysql学习笔记-1

  1. net start/stop [service name] 启动/中止某种服务mysql

  2. mysql [-h host_name -p port_number] -u user_name -p 回车后输入密码sql

  3. 基本相关 :良好的书写习惯,每条sql命令后添加" ; "做为标识数据库

    • 显示全部数据库 :show databases;
    • 切换到某一数据库 :use database_name ;
    • 显示当前数据库全部的表 show tables;
    • 在A数据库下显示B数据库中的某个表(当前仍然处于A数据库) show table_name from A;
    • 查看当前安装的 mysql 版本号: select version() ;
    • 查看当前所处的数据库 : select database();
    • 建立表结构:create table table_name (stuid int ,stu_name varchar(20) , gender char , borndate datetime);
    • 查看表的结构 desc table_name; 或者 select columns from table_name;
    • 查看表中具体数据内容 select * from table_name; (查看特定的某些字段,各个字段之间应使用逗号隔开,若字段名属于reserved keywords 则须要 讲字段名包含在反冒号 `对内 )
    • 插入具体数据:insert into table_name values
      (1,'张三','男','1993-01-04'),
      (2,'李四','男','1983-03-25'),
      (3,'芙蓉','女','1995-02-20');
    • 更新(修改)表内具体数据值; update table_name set born_date='1983-02-18' where stuid = '李四';
    • 删除具体的某一条数据: delete from table_name where stu_name='张三';
    • 修改表中某个字段的属性类型:alter table table_name modify column col_name 数据类型 其余属性;
    • 修改表结构(添加某一属性字段); alter table table_name add column new_col_name 数据类型 其余属性;
    • 删除某一表格总体(结构+内容):drop table table_name ;
    • sql 命令 通常不区分大小写;
    • 注释写法:
      • 单行注释 #注释内容 或者 -- 具体注释内容 (注意中间含有一个空格)
      • 多行注释: /* 注释内容
        注释内容
        注释内容 */
    • 语句的执行顺序:from clause , where clause, select clause , order by clause .(故排序表达式中能够使用别名,而where语句中不能够)
  4. SQL语言; DDL , DML , DCL(TCL) , DQLwindows

  5. 一些关键字&函数安全

    • as 重命名字段名 select col_1 as rename_col from table_name ; 也能够使用空格代替函数

    • concat() 用于链接两个字符串 : select concat(字段1, 字段2) from table_name; 若函数中有一个参数为空,则最终结果显示为NULL,为避免此种状况出现,在可能为空的字段外套用:
      isnull(field_name,为空时的替代值) 函数学习

    • length(str) 提取字符串字节长度,char_length(str) 以字符为度量单位返回str长度。ui

    • 其他字符串函数如:trim系列函数,left,right函数,lpad,rpad函数,substr函数,space函数,strcmp等字符串相关函数,now,curdate,date_format,get_format等日期函数,if,case等系列字符流控制函数以及其余函数,须要时可翻阅手册12章。spa

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

    • where 用于条件查询.

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

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

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

        mysql> SELECT 'David!' LIKE 'David\_'; -> 0
        mysql> SELECT 'David_' LIKE 'David\_'; ->1
        mysql> 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 部分。
  • 各语句之间的执行顺序:
    from clause ——> where clause——>group by ** clause——>having** clause——>select clause
    ——>order by clause

  1. ①. 查看警告信息: show warnings;

    ②. mysql8.0版本以上windows下支持命令清屏:system cls

相关文章
相关标签/搜索