zoukankan      html  css  js  c++  java
  • 第七节:DML操作之插入、更新、删除

    一. 插入

    1. 全字段插入

    (1). 标准格式

    insert into tableName (c1,c2,c3 ...) values (x1,x2,x3,...)

    可以省略:

    insert into tableName values (x1,x2,x3,...)

    (2). 插入多条数据 

    insert into LoginRecords values('0003','1','中桥','xx.xx.xx.x','2020-03-26'),
    ('0004','1','中桥','xx.xx.xx.x','2020-03-26'),
    ('0005','1','中桥','xx.xx.xx.x','2020-03-26'),
    ('0006','1','中桥','xx.xx.xx.x','2020-03-26');

    PS:全字段插入的时候可以省略字段名称。

    2. 部分字段插入

     (1). 标准格式

    insert into tableName (c2,c3 ...) values (x2,x3,...)

    PS:不能省略字段名称。

    (2). 插入多条数据

    insert into LoginRecords (id,userId) values('0007','1'),
    ('0008','1'),
    ('0009','1'),
    ('00010','1')

    3. 插入查询结果

    insert into table1 (x1,x2)
    select c1,c2 from table2 where condition

    注:没有values关键字哦

    二. 删除

    1. delete关键字

    delete from tableName where condition

    2. truncate关键字

    truncate tableName

    PS:truncate是删除全表,和delete删除全表而言,如果是自增主键,truncate 后从1开始,delete后还要接着原先的自增。

     

     

     

     

     

    三. 更新

    1. 单表更新

    update tableName set x1=xx [, x2=xx] where condition

    2. 关联表更新

    (1). SQLServer

    update table1
    set table1.x1='abc'
    from table1
    join table2 on table1.id=table2.uId  where condition

    (2). MySQL

    update table1,table2
    set table1.x1='abc'
    where table1.id=table2.uId

    eg: 可以同时更新多张关联表的数据

    update T_SysUser u,T_SysLoginLog l
    set u.userPhone ='111111',l.userName='ypf'
    where u.id=l.userId and u.id='1'

    3. 案例

     

     

     

     

     

    !

    • 作       者 : Yaopengfei(姚鹏飞)
    • 博客地址 : http://www.cnblogs.com/yaopengfei/
    • 声     明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
    • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
     
  • 相关阅读:
    【PAT甲级】1079 Total Sales of Supply Chain (25 分)
    CQOI2018 Day1 社交网络
    codeforces 707E Garlands (离线、二维树状数组)
    NOI2018 Day1 归程(Kruskal重构树)
    NOI2018 Day2 屠龙勇士(扩展孙子定理+multiset)
    知识点:二叉(重量)平衡树——替罪羊树
    BZOJ3065 带插入区间K小值
    知识点:斜率优化DP
    知识点:FFT详解
    博客园test(搭博客用)
  • 原文地址:https://www.cnblogs.com/yaopengfei/p/7192455.html
Copyright © 2011-2022 走看看