数据的增查改删

表中数据的操作

1、插

  • 插入数据--insert into student values(1, 'TOM', '男');    全列插入

                     --insert into student values(2, 'JACK', '男'), (3, 'ALICE', '女');    批量插入

                     --insert into student(name) values('Bob');    指定列插入

  • 主键冲突--insert into student values(1, '汤姆', '男')

                        on duplicate key update sn=1, name='汤姆', gender='男';

            on duplicate key update ---直接替换冲突数据

            -- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等

            -- 1 row affected: 表中没有冲突数据,数据被插入

            -- 2 row affected: 表中有冲突数据,并且数据已经被更新

  • 唯一键冲突--replace into students(sn, name, qq) values(20003, '唐僧', 123456);

            replace ---是先删除冲突项然后插入新项

            -- 1 row affected: 表中没有冲突数据,数据被插入

            -- 2 row affected: 表中有冲突数据,删除后重新插入

2、查

    通常情况下不使用select * 进行查询

        -查询的列越多,意味着需要传输的数据量越大;

        -可能会影响到索引的使用。

  • 数据查询--select * from table;                全列查询

                     --select COL1,COL2 from table;             指定列查询

                      --其中COL1和COL2可以进行数据运算

  • 指定别名查询--select name, math+chinese+english as total from table;

                     as total--指定别名,替代math+chinese+english

        注意:where后面不能跟别名

  • 数据去重查询--select distinct math from table;

                     distinct--去掉重复的数据

  • where条件查询--select * from student where [条件];

  • 结果排序查询--select * from student where [条件] order by 列名 asc/desc       默认是升序 

                     ASC--升序    DESC--降序

                    -主列为第一个字段,以第一个字段进行排序,重复的以第二列数据排序,前两列都重复的以第三列                            数据排序

                    null不参与排序,默认比所有的有效数据都小

                    - 时间戳为,最新的在前面,使用降序。

                    没有order by的数据的查询是随机的未定义顺序!!!

  • 筛选分页查询--select * from table limit n;                       从 0 开始,筛选 n 条数据

                     --select * from table limit s,n;                           从 s 开始, 筛选 n 条数据  

                     --select * from table limit n offset s;                 筛选 n 条数据,offset 后跟起始位置 s

  • 聚合函数查询--select fun(column) from table; 

  • 分组查询--select avg(sal) as myavg from EMP group by deptno having myavg<2000;

        分组查询的结果想要进行比较的话只能使用having

3、改

  • 修改数据--update table set [math=80] where [条件];

  • 全部数据修改--update table set [math=80];

4、删

  • 删除数据--delete from table where [条件];

  • 全部数据删除--delete from table;

  • 截断表--truncate table;                    影响行==0,

                只能对整表操作,不能像 DELETE 一样针对部分数据操作;

                实际上 MySQL 不对数据操作,所以比 DELETE 更快

                会重置 AUTO_INCREMENT 项

完整复制一张表:

  • 复制表结构---create table new_TAB like TAB; 

  • 复制表数据,且去掉重复数据---insert into new_tab select distinct * from tab;

修改表名:

  • ---rename table TAB to new_TAB, TAB2 to new_TAB2;

关键词的执行顺序:from > on > join > where > group by > with > having > select > distinct > order by > limit

 

where条件

        比较运算符

        null值的比较,null不参与运算

        逻辑运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值