Mysql related and common commands

SQL Quick Start:

-- 基于SQLyog操作演示:
-- 1.创建数据库
create database day01;
-- 2.在数据库中创建表结构
create table student(id int,name varchar(10));
-- 3.在表中添加数据
insert into student values(1,'laofang');
-- 4.在表中查询数据
select * from student;

SQL classification:

  • DDL (Data Definition Language): used to define database objects: databases, tables, etc.
  • DML (Data Manipulation Language): Data update, add and delete records in database tables.
  • DQL (Data Query Language): Query of data table records.
  • DCL (Data Control Language): is a statement used to set or change database user or role permissions.

DDL create and view databases

  • Create database syntax format:
--直接创建数据库语法格式:
create database [if not exists] 数据库名称 [character set 字符集名称];
/*
	说明:括号中命令为可选项;
    if not exists:表示数据库不存在则创建,存在则不创建;
    character set 字符集名称:表示创建过程指定数据库字符集;
*/
  • View database information:

    --查看所有数据库
    show databases;
    --查看数据库的定义信息
    show create database 数据库名称;
    

    DDL modify, delete and switch databases:

  • Related commands

    --将数据库的字符集修改,语法格式:
    alter database 数据库名称 character set 字符集;
    --删除数据库
    drop database 数据库名称;
    --查看正在使用的数据库
    select database();
    --切换数据库
    use 数据库名称;
    

    DDL create table

  • Create table syntax format:

    CREATE TABLE 表名 (字段名1 字段类型1, 字段名2 字段类型2,...);
    -- 可以写成
    CREATE TABLE 表名 (
        字段名1 字段类型1, 
        字段名2 字段类型2
    );
    比如:
    CREATE TABLE user(
        id int,
        name varchar 
    );
    -- create表示创建,table 表示表类型
    
    
  • data types in mysql

    • Integer: int

    • Decimal: double

    • character:

      • varchar: variable-length character type
      • char: fixed-length character type
      • Difference between the two: store 'itheima' 7 letters
        • char(10) fixed length: store 'itheima' //must store 10 characters, fill spaces when insufficient
        • varchar(10) variable length: store 'itheima' //actually store 7 characters, can not exceed 10 characters
    • date:

      • date: format 2021-08-23
      • datetime: format 2021-08-23 11:06:32

DDL view table and delete table

  • Related commands

    -- 1)查看表信息
    -- 1.1 查看某个数据下的所有表
    show  tables;
    -- 1.2 查看表结构包含字段信息 description
    desc 表名;
    -- 1.3 查看创建表的SQL信息
    show create table 表名;
    -- 2)删除表信息
    drop table [if exists] 表名;
    /*说明:
    drop:删除关键字
    table:表示删除的对象是表类型
    if exists:表示如果存在就删除,不存在则不做操作(不会报错)
    */
    
    

DDL modify table structure

  • Table field DDL operations:

    -- 1.为表添加字段
    ALTER TABLE 表名  ADD   字段名 类型;
    -- 2.修改表中字段的类型
    ALTER TABLE 表名 MODIFY 字段名 新的类型;
    -- 3.修改列名
    ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型;
    -- 4.删除列
    ALTER TABLE 表名 DROP 字段名;
    
  • Entire table attribute DDL modification:

    -- 1.修改表名
    RENAME TABLE 旧表名 TO 新表名;
    -- 6.修改字符集
    ALTER TABLE 表名 character set 字符集;
    

DML insert record

  • insert syntax format:

    -- 1.单条插入
    INSERT INTO 表名 (字段名1, 字段名2,...) VALUES (字段值1, 字段值2,...);
    -- 如果插入的是全部字段,表名后的字段名称可以忽略不写,格式如下
    INSERT INTO 表名 VALUES (字段值1, 字段值2,...);
    -- 2.批量插入
    INSERT INTO 表名 VALUES (字段值1, 字段值2,...), (字段值1, 字段值2,...),......;
    
  • Update syntax format:

    -- 1.无条件全表更新,语法格式(开发中慎用):
    UPDATE 表名 SET 字段名=新的值,字段名=新的值,..;
    -- 2.带条件修改
    UPDATE 表名 SET 字段名=新的值,字段名=新的值,.. WHERE 条件;
    
  • DML delete syntax:

    -- 1.无条件全表删除,语法格式(开发中慎用):
    DELETE FROM 表名;
    -- 2.带条件删除
    DELETE FROM 表名 WHERE 条件;
    
  • truncate delete

    -- 格式
    TRUNCATE TABLE 表名; 
    -- 说明:truncate表示清空表的结构和数据
    --delete是将表中的数据一条一条删除数据,不影响表结构;
    --truncate是将整张表删除,然后重新创建一个新的表,
        新的表结构和原来表结构一致;
    

DQL query

  • DQL unconditional query and alias query

    • Unconditional query

      --查询指定列数据
      select 字段名1,字段名2,....from 表名;
      --如果要从查询表所有的列可以使用*(开发中不让用)
      select * from 表面;
      
    • AS alias query

      --查询时可以给列或表取别名
      select 字段名1 AS 别名, 字段名2 AS 别名 from 表名 AS 表别名;
      
      --查询的语法结构:
      select 字段名1, 字段2
      from 表名
      where 条件
      group by 字段, ....
      having  分组条件
      order by 字段, ....
      
      --SQL查询语句的执行顺序
      1. from     --先找表
      2. where    --查询的数据进行条件过滤
      3. group by  -- 分组
      4. select   --查询具体的数据结果
      5. having  --分组条件
      6. order by  --排序
      
    • distinct deduplication query

      --查询指定列并且结果不出现重复数据
      select distinct 字段名,字段名,....from 表名;
      --重点说明:distinct关键字后的所有字段值都相同,才去重。
      
    • Worm replication

      insert into 表名1 select * from 表名2;
      --将表名2中的数据复制到表名1中
      --前提:查询的数据要与插入的表中的字段类型和顺序要一致
      create table student3 like student2;
      --基于student2的表结构,创建一个相同表结构的student3
      
    • Conditional query

      #当查询条件有多个时:逻辑运算符
      and
      or
      not
      
      #当查询的条件在某些范围内时,可以使用范围运算符:
      in  --在指定的in范围内
      between 值1 and 值2 --(闭区间,包头又包尾) 在值1和值2之间
      
    • fuzzy query like

      #  %表示0到多个任意字符
      #  _仅有一个任意字符
      select * from 表名 where 字段名 like  '通配符字符串'
      
    • Sort query

      #通过order by 可以将查询出的结果进行排序
      select 字段1, 字段2, .... from 表名 where 条件
      order by 字段 [ASC|DESC];
      --ASC 升序排序(默认,可不写)
      --DESC 降序排序
      
    • Aggregate function

      select 聚合函数(字段) from 表名;
      --round(3.14159,3) 四舍五入 结果:3.14
      --truncate(3.15149, 2)  单纯的截取2位数字  结果:3.15
      
    • Group query

      select column_name, 聚合函数(column_name) from 表名 where 条件 group by column_name;
      --group by 后面的字段值相同才能划分为一组
      --分组之后数据过滤用having
      select 字段, .... from 表名 group by 字段 having 条件;
      
      having 和 where 的区别
      1. where 关键字后面不能跟聚合函数,但是 having 可以
      2. where 关键字一般在分组之前执行, having 分组之后执行
      3. where 能用的地方, having 都可以用, 但是不建议乱用
      4. where 可以走索引,加快查询速度,但是使用 having 条件查询不走索引
      
      # 注意事项:
      # select 查询 和 group by 一起使用时, select 关键字后面只能跟:聚合函数或group by 关键字后面的字段
      select  color, sum(price) from car group by color;
      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324123308&siteId=291194637