notes_mysql
9264NOTESmysql2018-10-10

sql语法特点:

  • 没有””双引号,字符串使用’’单引号包含;

  • 没有逻辑相等,赋值和逻辑相等都是=;

  • 类型不再是最严格的,任何数据都可以包含在’’单引号内;

  • 没有布尔值的概念,但是在视图中可以输入true/false;

  • 它也有关系运算符:> < >= <= = <> !=,返回一个布尔值;

  • 它也有逻辑运算符:!(not) &&(and) ||(or);

  • 它不区别大小写。

  • 以下语句,user为表名, id,name,sex,card,phone,address为字段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    #数据库创建#
    "create database"

    "drop table if exists user"
    # 如果存在(if exists)就删除表

    # 如果没有(if not exists)就新增表,设置id为主键,address为外键关联address表的id字段
    """create table if not exists user(
    id int auto_increment primary key,
    name varchar(5),
    sex char(1),
    card char(18),
    phone char(11),
    address varchar(30),
    foreign key(address) references address(id))"""

    # 修改自增长Id,数据库新增的数据会从1000开始但是原来的数据不会改变
    "alter table user auto_increment=1000"

    # 新增数据库表字段plus
    "alter table user add plus varchar(8)"
    # --------------------------插入数据----------------------------------------------------------------
    # 数据插入时一定要注意 类型对应,数量对应,顺序对应
    # 字符串值必须包含在''以内,赋值可以使用default,null

    # 插入单条数据,如果不列出字段名mysql会按照顺序添加
    """insert into user(name, sex, id_card, phone, address)
    values ('李四', '女', '511569845612354879', '10086', '太古里77号')"""

    # 批量插入数据
    """insert into user(name, sex, id_card, phone, address)
    values (%s, %s, %s, %s, %s)"""

    # ----------------------------------------查询数据--------------------------------------------------

    # order by 对查询结果进行排序 select 字段1,字段2,字段3 from 表名 order by 字段 desc(降序)||asc(升序)
    "select address,id from user order by id desc"

    # 去重复查询 select distinct 字段 from 表名
    "select distinct address from user"

    # 分页查询 limit select 字段1,字段2 from 表名 limit 初始位置,记录数
    "select id, name, sex, id_card from user limit 3,6"

    # 精准查询 in 只查找括号内的数据; not in 则相反
    "select * from user where ID in (1001,1003)"

    # 精准查询 between and 查找1001-1003范围内的数据;not between and 相反
    "select * from user where ID between 1001 and 1003"

    # 模糊查询 like (% 代表任意字符 _ 代表单个字符);not like相反
    "select * from user where name like '_麻%'"

    # 多条件查询 and 关键字
    "select * from user where sex='女' and address='太古里77号'"

    # 多条件查询 or 关键字 只需满足一个条件
    "select * from user where name='王麻子子' or address='太古里77号' "

    # 多表查询,两个表comment,movie
    # 语法:select * from 表1(左表) left join 表2(右表) on 表2.字段 = 表1.字段
    "select * from comment left join movie on movie.id = comment.movieid"
    # inner join / join单独用 返回所有匹配的行
    # left join 不论是否匹配会返回“left join”左边表所有的行,匹配不上的字段为null
    # right join 和left join相反,会返回右边表的所有的行
    # full join 只要存在一条匹配就会返回两个表所有的行


    # ------删除数据和更新数据 or,and,between and,like等关键字可以查询到多条数据然后批量删除或者更新-------------

    # 删除数据
    #delete [from] 表名 where 条件
    "delete from user where name='王麻子子' or address='太古里77号'"
    # 数据库清空
    "truncate table user"

    # 更新数据
    "update user set name = '更新' where ID in (1005,1006)"

参考文章

https://www.cnblogs.com/esofar/p/6185210.html 史上最全的MSSQL复习笔记