zoukankan      html  css  js  c++  java
  • python学习笔记 day42 对数据表的操作---增删改查

    1. 创建表

    首先使用create table info1()创建表结构,创建了几个字段:id name,age,sex,salary

    create table info1(
      id int not null auto_increment primary key,
      name varchar(50) not null,
      age int not null,
      sex char(2) not null,
      salary int not null)

    可以使用desc info1 或者 show create table info1 来查看创建的表信息;

    2. 往表中增加数据

    2.1 insert into info values(表中所有字段的值都的写上)

    # 插入数据
    insert into info1 values(1,"璇璇",22,"女",20000),(2,"西西",23,'',15000),(3,"东东",26,"男",30000),(4,"萌萌",24,"女",17000),(5,"楠楠",27,"男",40000)
    select * from info1;  # 查看 表中所有数据项
    desc info1;

    运行结果:

    2.2 insert into info(字段1,字段2) values(字段1的值,字段2的值) (这里要求其他非空字段得有默认值,或者字段本身允许null)

    alter table info1 modify sex char(2) null;  # 修改字段sex属性可以为空;
    alter table info1 alter salary set default 10000;  # 为表中字段salary设置默认值
    insert into info1(name,age) values("哈哈",23),("多多",25)  # 往表中增加数据项(只增加两个字段的信息--name,age)
    select * from info1 # 查看增加数据项之后的表info1
    desc info1

    运行结果:

    2.3 insert into info1(name,age) select name,age from info   # 为表增加数据项 只有name age字段的值,数据从info中获取;

    insert into info1(name,age) select name,age from info where id=3  # 把表info中id=1的数据信息插入到表info1中
    select * from info1;

    运行结果:

    3.删除表中数据;

    3.1 delect from info1 where id=1 ;  # 删除表中id=1的数据项;

          delete from ifno1 ;  # 删除表中所有数据项;

          truncate info! ;       # 清空表中数据

    delete from info  VS  truncate info :

    前者是删除表中数据,如果id是自增主键,delete删除数据后,再往表中增加数据项,id接着之前的序号来;

    后者 是清空表中数据,如果id设为自增主键,truncate 清空数据后,再往表中增加数据项id从1开始,不会记录之前的id号;

    另外,前者删除数据比较慢,后者清空数据效率更高;

     4. 修改表中数据项的值;

    4.1  update info1 set name="宁宁" where id=3;   # 修改表info1 id=3的数据项,把名字改为"宁宁";

    4.2 update info1 set name="夏夏" sex="女" where id=6;   # 同时修改表中数据项多个属性值;

    update info1 set name="宁宁" where id=3;  # 修改表info1中id=3的数据项,修改名字;
    select * from info1;
    update info1 set name="夏夏",sex="女" where id=7;  # 同时修改某一数据的多个字段属性值;
    select * from info1;

    运行结果:

     

    5. 对表中数据项查询

    5.1 简单查询

    5.1.1 select * from info1 ; # 查询表中所有数据项;

            select name,age from info1 ; #  查询表中name age 字段的数据

            select name,sex as "性别" from info1 ;  # 查询表中name sex 字段的信息(并且把sez按照"i性别显示")但是表字段仍然是sex;

            select salary+2000 from info1; # 把表info1中salary显示,并且加上2000 (只是在显示时+2000 表中数据的salary并未真的+2000)---想要修改表中某一数据的某一字段的值,可以 使用update info set salary=22000 where id=1 

            select distinct age from info1;   # 选出表info1中不同年龄的数据;

    select name,sex as "性别" from info1;  #  这里仅仅是显示时把sex作为"性别显示
    desc info1; # 实际上表info结构字段仍然是sex
    select salary +2000 from info1;   # 只是在显示时把表中数据项salary+2000显示出来,但是表中每一个数据的salary值并未真的+2000
    select * from info1; 
    select distinct age from info1;  # 筛选出表中age不同的数据

    运行结果:

     

     

    5.2 条件查询

    运算符 :       select * from info1 where age>20; 

                           select * from info1 where id=3;

    null :              select * from info1 where age is null;

                           select * from info1 where name=" "

    逻辑运算符: select * from info1 where age<30 or salary >20000;

                           select * from info1 where age>20 and sex="女";

    5.3  区间查询

    select * from info1 where age between 20 and 50;   # 查找age 在20-50之间的所有数据项;

    select * from info1 where age between 20 and 50;

    运行结果:

    5.4 集合查询

    select * from info1 where id in (1,3,5,7,9) ;  # 查找 id 在1,3,5,7,9的数据;

    5.5  模糊查询

    select * from info1 where name like "%e%";  # 查找所有name中带有字母e的;

    select * from info1 where age like "%e";  # 查找所有name中以e结尾的;

    select * from info1 where age like "e%" ;  # 查找name中以e开头的所有数据;

    select * from info1 where name like "___";  # (三个下划线代表三个占位符) 查找name中长度是3的所有数据项;

    select * from info1 where name like "__e%"  # 查找name中第三位是e的所有数据项;

    update info1 set name="xuanxuan" where id=1;
    update info1 set name="xixi" where id=2;
    update info1 set name="ningning" where id=3;
    update info1 set name="mengmeng" where id=4;
    update info1 set name="nannan" where id=5;
    update info1 set name="haha" where id=6;
    update info1 set name="xiaxia" where id=7;
    update info1 set name="doudou" where id=8;
    insert into info1(name,age) values("wepon",23),("egg",28),("hello",25)
    select * from info1;
    select * from info1 where name like "%e%"   # 查找名字中带有字母e的
    select * from info1 where name like "%e"   # 查找名字中以字母e结尾的
    select * from info1 where name like "e%"  # 查找名字中以e开头的
    select * from info1 where name like "___"  # 查找名字中有三位的(三个下划线,代表三个占位符,就是三位)
    select * from info1 where name like "_e%"  # 查找名字第二位是e 的

    运行结果:

    5.6  排序查询

     select * from info1 order by age asc;  # 按照age排序(asc 从小到大);

    select * from info1 order by salary desc;  # 按照salary 排序(desc 从大到小)

    select * from info1 ORDER BY age asc;  # 按照age排序(asc从小到大)
    select * from info1 order by salary desc;  # 按照salary排序(desc 倒序---从大到小)

    运行结果:

     

    当要排序的对象是中文,则需要进行gbk编码: select * from info order by CONVERT(name using "gbk") desc   # name是中文时 按照gbk编码 desc 排序

    select * from info order by CONVERT(name using "gbk") desc   # name是中文时 按照gbk编码 desc 排序

    运行结果:

    talk is cheap,show me the code
  • 相关阅读:
    五、生产者消费者模型_ThreadLocal
    四、多线程基础-线程池的创建和使用
    spring根据beanName获取bean
    spring容器的功能扩展
    机甲大师S1机器人编程学习,Windows 10 安装Scratch和简单实例学习
    如何建设高可用系统
    详解Condition的await和signal等待/通知机制
    从源码角度彻底理解ReentrantLock(重入锁)
    MySQL 分库分表及其平滑扩容方案
    机甲大师S1机器人编程学习
  • 原文地址:https://www.cnblogs.com/xuanxuanlove/p/9861368.html
Copyright © 2011-2022 走看看