Weibw's World Weibw's World
首页
  • HTML
  • Python

    • Python基础知识
    • Python CookBook第三版
    • Flask
  • MySQL

    • MySQL基础知识
    • MySQL调优
    • MySQL面试题
算法
  • FineReport
  • Kettle
  • Git
  • 微信公众号文章
  • 优秀博客文章
  • 其他
收藏夹
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Weibw

一个没有梦想的咸鱼
首页
  • HTML
  • Python

    • Python基础知识
    • Python CookBook第三版
    • Flask
  • MySQL

    • MySQL基础知识
    • MySQL调优
    • MySQL面试题
算法
  • FineReport
  • Kettle
  • Git
  • 微信公众号文章
  • 优秀博客文章
  • 其他
收藏夹
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • MySQL

    • MySQL基础知识

      • 数据库基本概念
      • 数据库类型和常见的关系型数据库
      • MySQL介绍
      • MySQL8的安装与卸载
      • MySQL的登录、访问、退出操作
      • 使用Navicat连接MySQL
      • SQL语言入门
      • DML
      • DDL
        • 表的完整性约束
        • DQL查询
        • 数据库对象
      • MySQL进阶

      • MySQL面试题

    • 多余文档备份
    • MySQL
    • MySQL基础知识
    weibw
    2021-12-25

    DDL

    # 修改、删除数据库表

    -- 查看数据:
    select * from t_student;
    -- 修改表的结构:
    -- 增加一列:
    alter table t_student add score double(5,2) ; -- 5:总位数  2:小数位数 
    update t_student set score = 123.5678 where sno = 1 ;
    -- 增加一列(放在最前面)
    alter table t_student add score double(5,2) first;
    -- 增加一列(放在sex列的后面)
    alter table t_student add score double(5,2) after sex;
    -- 删除一列:
    alter table t_student drop score;
    -- 修改一列:
    alter table t_student modify score float(4,1); -- modify修改是列的类型的定义,但是不会改变列的名字
    alter table t_student change score score1 double(5,1); -- change修改列名和列的类型的定义
    -- 删除表:
    drop table t_student;
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    # DDL和DML的补充

    -- 创建表:
    create table t_student(
            sno int(6) primary key auto_increment, 
            sname varchar(5) not null, 
            sex char(1) default '男' check(sex='男' || sex='女'),
            age int(3) check(age>=18 and age<=50),
            enterdate date,
            classname varchar(10),
            email varchar(15) unique
    );
    -- 添加数据:
    insert into t_student values (null,'张三','男',21,'2023-9-1','java01班','zs@126.com');
    insert into t_student values (null,'李四','男',21,'2023-9-1','java01班','ls@126.com');
    insert into t_student values (null,'露露','男',21,'2023-9-1','java01班','ll@126.com');
    -- 查看学生表:
    select * from t_student;
    -- 添加一张表:快速添加:结构和数据跟t_student 都是一致的
    create table t_student2
    as
    select * from t_student;
    select * from t_student2;
    -- 快速添加,结构跟t_student一致,数据没有:
    create table t_student3
    as
    select * from t_student where 1=2;
    select * from t_student3;
    -- 快速添加:只要部分列,部分数据:
    create table t_student4
    as
    select sno,sname,age from t_student where sno = 2;
    select * from t_student4;
    -- 删除数据操作 :清空数据
    delete from t_student;
    truncate table t_student;
    
    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

    delete和truncate的区别:

    从最终的结果来看,虽然使用TRUNCATE操作和使用DELETE操作都可以删除表中的全部记录,但是两者还是有很多区别的,其区别主要体现在以下几个方面:

    (1)DELETE为数据操作语言DML;TRUNCATE为数据定义语言DDL。

    (2) DELETE操作是将表中所有记录一条一条删除直到删除完;TRUNCATE操作则是保留了表的结构,重新创建了这个表,所有的状态都相当于新表。因此,TRUNCATE操作的效率更高。

    (3)DELETE操作可以回滚;TRUNCATE操作会导致隐式提交,因此不能回滚(在第十章中会讲解事务的提交和回滚)。

    (4)DELETE操作执行成功后会返回已删除的行数(如删除4行记录,则会显示“Affected rows:4”);截断操作不会返回已删除的行量,结果通常是“Affected rows:0”。DELETE操作删除表中记录后,再次向表中添加新记录时,对于设置有自增约束字段的值会从删除前表中该字段的最大值加1开始自增;TRUNCATE操作则会重新从1开始自增。

    编辑 (opens new window)
    上次更新: 2023/10/13, 17:39:25
    DML
    表的完整性约束

    ← DML 表的完整性约束→

    最近更新
    01
    牛客网非技术快速入门SQL练习题
    03-08
    02
    其他日常SQL题
    03-07
    03
    用户与权限管理
    03-05
    更多文章>
    Theme by Vdoing | Copyright © 2021-2023 | Weibw | 辽ICP备18015889号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式