• mysql入门练习


    创建数据库day1db字符集utf8并使用

    create database day1db default charset utf8;
    
    • 1

    创建t_hero表,有name字段字符集utf8

    create table t_hero (name char)charset=utf8;
    
    • 1

    修改表名为hero

    alter table t_hero rename hero;
    
    • 1

    最后面添加价格字段money,最前面添加id字段, name后面添加age字段

    alter table hero add money int(4);
    alter table table hero add  id int(5)  FIRST;
    alter table hero add age tinyint after name;
    
    • 1
    • 2
    • 3

    表中添加以下数据:1,李白,50,6888 2,赵云,30,13888 3,刘备,25,6888

     insert into hero values
        ("1","李白","50","6888"),
        ("2","赵云","30","13888"),
        ("3","刘备","25","6888");
    
    • 1
    • 2
    • 3
    • 4

    修改刘备年龄为52岁

     update hero set age="52" where name="刘备";
    
    • 1

    修改年龄小于等于50岁的价格为5000

    update hero set money="5000" where age<="50";
    
    • 1

    删除价格为5000的信息

    delete from hero where money="5000";
    
    • 1

    删除表,删除数据库

    drop table hero;
     drop database day1db;
    
    • 1
    • 2

    综合练习题2、
    创建数据库newdb1,字符集utf8并使用

    create database newdb1 charset=utf8;
    use newdb1;
    
    • 1
    • 2

    在数据库中创建员工表emp字段id,name,sal(工资),deptld(部门id)字符集utf8

     create table emp
        (id varchar(8),
        name varchar(8),
        sal char(9),
        deptld char(20)
        )charset=utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建部门表dept字段id,name,loc(部门地址)字符集utf8

    create table dept
       (id varchar(8),
        name varchar(8),
        sal char(9)
        )charset=utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    部门表插入一下数据1.神仙部 天庭 2.妖怪部 盘丝洞

     insert into dept values
        ("1","神仙部","天庭"),
        ("2","妖怪部","盘丝洞");
    
    • 1
    • 2
    • 3

    员工表插入1.悟空 5000 1,2 八戒 2000 1, 3 蜘蛛精 8000 2, 4 白骨精 9000 2

    insert into emp values
        ("1","悟空","5000","1"),
        ("2","八戒","2000","1"),
        ("3","蜘蛛精","8000","2"),
        ("4","白骨精","9000","2");
    
    • 1
    • 2
    • 3
    • 4
    • 5

    查询工资6000一下的员工姓名和工资

    select name,sal from emp where sal<"6000";
    
    • 1

    修改神仙部的名字为取经部

     update dept set name="取经部" where name="神仙部";
    
    • 1

    给员工添加奖金comm字段

    alter table emp add comm char(20);
    
    • 1

    修改员工表中部门id为1的奖金为500

    update emp set comm ="500" where id="1";
    
    • 1

    把取经部门的地址改成五台山

    update dept set loc ="五台山" where name="取经部";
    
    • 1

    修改奖金字段为性别gender字段 类型为varchar

    alter table emp change comm gender varchar;
    
    • 1

    修改孙悟空和猪八戒性别为男

    update emp set gender="男" where name="悟空";
    update emp set gender="男" where name="八戒";
    
    • 1
    • 2

    删除没有性别的员工(null不能用=要用is)

    delete from emp where gender="is no";
    
    • 1

    删除性别字段

    alter table emp drop gender;
    
    • 1

    删除表和删除数据库

    drop table emp;
    drop table dept;
     drop database newdb1;
    
    • 1
    • 2
    • 3
  • 相关阅读:
    基于springboot的物品回收系统
    Zookeeper高级_选举机制
    HTML常见标签用法
    Jetty 的线程策略 EatWhatYouKill
    MVP模式根模块
    Differentiable Scaffolding Tree for Molecule Optimization(论文解读)
    shell脚本中getopt介绍
    SQL Server 跨库/服务器查询
    Apache Spark 的基本概念
    web —— css(1)
  • 原文地址:https://blog.csdn.net/m0_48096446/article/details/127885862