• 数据库基本操作--DML


    基本介绍

    DML是指数据操作语言,英文全称是Data Manipulation Language,用来对数据库中表的数据记录进行更新。

    关键字

    • 插入 insert
    • 删除 delete
    • 更新 update
    插入

    语法格式:

    //向表中插入某些列的数据
    insert into 表(列名1,列名2,...)values (值1,值2, ...)
    insert into 表 values(值1,值2,值3....) //向表中插入所有列
    
    -- 将所有学生的地址修改为重庆 
    update student set address = '重庆’; 
    
    -- 讲id为1004的学生的地址修改为北京 
    update student set address = '北京' where id = 1004 
    
    -- 讲id为1005的学生的地址修改为北京,成绩修成绩修改为100 
    update student set address = '广州',score=100 where id = 1005
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    删除

    语法格式:

    delete from 表名 [where 条件];//不加 where 就是清空表
    truncate table 表名 或者 truncate 表名
    
    -- 1.删除sid为1004的学生数据
    delete from student where sid  = 1004;
    -- 2.删除表所有数据
    delete from student;
    -- 3.清空表数据
    truncate table student;
    truncate student;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注意:delete和truncate原理不同,delete只删除内容,而truncate类似于drop table,可以理解为是将整个表删除,然后再创建该表

    更新

    语法格式:

    update 表名 set 字段名=值,字段名=值...; //不要忘了字段之间的逗号
    update 表名 set 字段名=值,字段名=值... where 条件;
    --例子:
    -- 将所有学生的地址修改为重庆 
    update student set address = '重庆’; 
    
    -- 讲id为1004的学生的地址修改为北京 
    update student set address = '北京' where id = 1004 
    
    -- 讲id为1005的学生的地址修改为北京,成绩修成绩修改为100 
    update student set address = '广州',score=100 where id = 1005
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    一文带你迅速掌握python装饰器
    OpenMesh 计算封闭网格体积
    一体机电脑辐射超标整改
    2022.11.25Dungeon Master POJ - 2251
    Java之Collections的综合小练习
    MySQL 索引
    经济数据预测 | Python实现机器学习(MLP、XGBoost)金融市场预测
    双层循环和循环语句
    windows批处理 将当前路径添加到Windows的`PATH`环境变量中 %~dp0
    高精度随流检测技术助力金融行业实现智能运维
  • 原文地址:https://blog.csdn.net/m0_73282576/article/details/133419441