美文网首页
04-数据的增删改查

04-数据的增删改查

作者: 喝酸奶要舔盖__ | 来源:发表于2019-01-05 14:53 被阅读0次

    插入数据

    • 插入数据
    示例一:
    先要创建一张表
    create table if not exists stu(
        id int,
        name varchar(20)
    );
    insert into stu (id, name) values (1, 'lnj');
    # 字段名称不用和表中的顺序一致
    insert into stu (name, id) values ('zq', 2);
    # 值顺序和个数必须和字段的顺序个数一致
    insert into stu (name, id) values ('ww');
    insert into stu (name, id) values (3, 'ww');
    # 如果值的顺序和个数和表中字段的顺序和个数一致, 那么字段名称可以省略
    insert into stu values (3, 'ww');
    
    # 一次性插入多条数据, 每条数据的值用逗号隔开
    insert into stu values (4, 'it'), (5, 'zb');
    
    • 插入数据指定参数
      • 被not null修饰的字段必须传值
      • 默认字段都是被null修饰的, 所以可以不传值
      • 如果字段被default修饰, 那么不传值就会使用默认值
      • 被auto_increment修饰的字段, 会从1开始自动增长
      • 给auto_increment修饰的字段传递null或者default, 都是使用默认自增长的值
      • 企业开发一般传递null, default用于告诉MySQL使用默认值
    create table [if not exists] 表名(
        字段名称 数据类型 [null | not null],
        字段名称 数据类型 [auto_increment],
        字段名称 数据类型 [primary key],
        字段名称 数据类型 [default],
        字段名称 数据类型 [comment]
    )engine=存储引擎;
    
    create table if not exists stu2(
        name varchar(20) not null,
        score int default 59,
        age int
    );
    # 被not null修饰的字段必须传值
    insert into stu2 (score, age) values (88, 13); #报错
    
    # 默认字段都是被null修饰的, 所以可以不传值
    # 如果字段被default修饰, 那么不传值就会使用默认值
    insert into stu2 (name) values ('lnj'); #不报错
    
    # default用于告诉MySQL使用默认值
    insert into stu2 (name, score, age) values ('zs', default, 13);
    
    
    create table if not exists stu3(
        id  int auto_increment primary key,
        name varchar(20)
    );
    # 被auto_increment修饰的字段, 会从1开始自动增长
    # 给auto_increment修饰的字段传递null或者default, 都是使用默认自增长的值
    # 企业开发一般传递null, default用于告诉MySQL使用默认值
    insert into stu3 (id, name) values (null, 'lnj');
    insert into stu3 (id, name) values (default, 'zq');
    
    create table if not exists stu4(
        id  int auto_increment primary key,
        name varchar(20) comment '姓名'
    );
    

    修改表数据

    update 表名 set 字段名=值 [where 条件];

    示例一:
    # 如果没有指定条件会修改表中所有的数据
    update stu2 set age=66;
    
    # 修改所有满足条件的数据
    update stu2 set age=88 where name='zs';
    
    # 添加多个条件 AND === &&  OR === ||,where后面是条件,只修改条件满足的
    update stu2 set age=44 where name='zs' AND score=98;
    
    # 同时修改多个字段的值
    update stu2 set score=100,name='it' where age=66;
    

    删除表中的数据

    delete from 表名 where 条件;

    示例一:
    # 删除满足条件的所有数据
    delete from stu2 where age=88;
    delete from stu2 where age<66;
    
    # 删除表中所有的数据
    delete from stu2;
    
    删除表中的数据和清空表中的数据
    delete from 表名; 删除表中所有的数据
    truncate table 表名; 清空表中所有的数据
    
    
    create table if not exists stu5(
        id  int auto_increment primary key,
        name varchar(20),
        age int
    );
    insert into stu5 values (null, 'lnj', 33),(null, 'zq', 34),(null, 'ls', 45);
    
    # 如果通过delete删除表中所有的数据, 自增长的字段不会被清空
    # 本质是遍历表中的每一条数据, 逐条删除
    delete from stu5;
    insert into stu5 values (null, 'ww', 67);
    
    # 如果通过truncate清空表中所有的数据, 自增长的字段会被清空
    # 本质是将原有的表删除, 然后再创建一个一模一样的
    truncate table stu5;
    insert into stu5 values (null, 'ww', 67);
    

    查询表数据

    select 字段名1,字段名2 from 表名 where 条件;

    # 查询指定字段的所有数据
    select name from stu5;
    # 查询多个制定字段的所有数据, 会按照查询时指定的字段顺序返回
    select name, id from stu5;
    # 如果需要查询所有字段, 可以用*代替字段名称
    select id, name, age from stu5;
    select * from stu5;
    # 查询所有满足条件的数据
    select * from stu5 where age>=40;
    

    表复制

    • 复制数据, 但不复制结构
    create table 新表名 select 字段 from 旧表名;
    
    create table newStu select * from stu5;
    
    • 复制结构, 但不复制数据
    create table 新表名 like 旧表名;
    
    create table newStu2 like stu5;
    

    相关文章

      网友评论

          本文标题:04-数据的增删改查

          本文链接:https://www.haomeiwen.com/subject/ccvrrqtx.html