zoukankan      html  css  js  c++  java
  • MySQL随记

    一、插入

    USE girls;
    
    # DML语言 - 数据操作语言
    /*
    插入:insert
    修改:update
    删除:delete
    */
    
    # 一、插入语句
    /*
    语法一:
    insert into 表名(列名, ...) values(值1, ...); 
    要求:插入值类型要与列类型一致
    
    语法二:
    insert into 表名
    set 列名=值, 列名=值, ...;
    */
    SELECT * FROM beauty;
    
    # 经典插入
    INSERT INTO beauty(id, NAME, sex, borndate, phone, photo, boyfriend_id)
    VALUE(13, '唐心怡', '女', '1992-4-23', '189203849308', NULL, 2)
    # 可省略列名
    INSERT INTO beauty 
    VALUE(14, '娜扎', '女', '1998-5-23', '179203849308', NULL, 3)
    
    INSERT INTO beauty
    SET id=15, NAME='刘涛';
    

    二、修改

    /*
    1. 修改单表记录
    语法:
    update 表名
    set 列名=新值, 列名=新值...
    where 筛选条件;
    
    2. 修改夺标的记录
    sql-192语法:
    update 表1 as 别名, 表2 as 别名
    set 列=值, ...
    where 筛选条件 and 筛选条件;
    
    sql-199语法:
    update 表1 as 别名
    inner|left|right join 表2 as 别名
    on 连接条件
    set 列=值,...
    where 筛选条件;
    */
    
    # 案例1:修改姓唐的电话为13228483930
    UPDATE beauty SET phone='13228483930'
    WHERE NAME LIKE '唐%';
    
    SELECT * FROM beauty;
    
    # 案例2:修改张无忌女朋友手机号为114
    UPDATE boys AS bo
    INNER JOIN beauty AS be ON bo.`id` = be.`boyfriend_id`
    SET be.`phone`='114'
    WHERE bo.`boyName`='张无忌';
    

    三、删除

    /*
    语法一:
    delete from 表名 where 筛选条件
    
    多表删除
    delete 表1别名, 表2别名
    from 表1 别名
    inner|left|right join 表2 别名 on 连接条件
    where 筛选条件;
    
    语法二:
    truncate table 表名;
    */
    
    # 案例1:删除手机号以9结尾的女神信息
    SELECT * FROM beauty WHERE phone LIKE '%9';
    
    DELETE FROM beauty WHERE phone LIKE '%9';
    
    
    # 案例2:删除张无忌的女朋友的信息
    DELETE be
    FROM beauty AS be
    INNER JOIN boys AS bo ON be.`boyfriend_id`=bo.`id`
    WHERE boyName='张无忌';
    
    SELECT * FROM beauty;
    
    
    # truncate语句 - 清空数据
    /*
    delete PK truncate
    1. delete 可以加where条件; truncate不能加
    2. 若要删除表中自增长列,用delete删除,再插入数据,自增长列的值从断点开始
       而truncate删除后,再插入数据,自增长列的值从1开始
    3. truncate删除没有返回值,delete删除有返回值
    4. truncate删除不可回滚,delete删除可以回滚
    
    */
    
  • 相关阅读:
    例题6-8 Tree Uva548
    例题6-7 Trees on the level ,Uva122
    caffe Mac 安装
    Codeforces Round #467 (Div. 1) B. Sleepy Game
    Educational Codeforces Round37 E
    Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
    Good Bye 2017 E. New Year and Entity Enumeration
    Good Bye 2017 D. New Year and Arbitrary Arrangement
    Codeforces Round #454 D. Seating of Students
    浙大紫金港两日游
  • 原文地址:https://www.cnblogs.com/hq82/p/12248479.html
Copyright © 2011-2022 走看看