码迷,mamicode.com
首页 > 数据库 > 详细

数据库--数据操作

时间:2018-02-27 01:00:59      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:esc   upd   into   post   数据库   mysq   int   去重   参数   

一、介绍

 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括

  1. 使用INSERT实现数据的插入
  2. UPDATE实现数据的更新
  3. 使用DELETE实现数据的删除
  4. 使用SELECT查询数据。

二、插入数据INSERT

INSERT into info values(1,‘田硕,‘男);

INSERT into info(id,NAMES) values(2,‘田硕);

INSERT into info(id,NAMES) values(3,‘田硕),(4,‘田硕),(5,‘田硕);

NSERT into info(names,sex) select nameS,sex from info ;    

三、更新操作UPDATE

update info set sex = , names = 田硕 where id=5;

四、删除操作DELETE

DELETE from info where id = 11; -- 删除指定数据
 
DELETE from info; -- 删除整张表中所有的数据(一行一行删)
        
TRUNCATE info; -- 清空整张表(直接都删,比delete快)

五、查询操作DELECT

  5.1  简单查询

select * from person; -- 查询所有

select name,SEX from person; -- 按指定字段查询

select name,SEX as性别 from person; -- as 表示为字段起别名

select salary+200 from person; -- 可以进行数据列运算

select DISTINCT age,name FROM person;  -- 去重复查询

  5.2  条件查询

    1.运算符

select * FROM person WHERE age >20;
select * FROM person WHERE age <=20;

select * FROM person WHERE age <>20;(也是不等于)
select * FROM person WHERE age !=20;

    2.关键字

select * FROM person where dept_id is null;

select * FROM person where dept_id is not null;

select * FROM person where name = abc;

    3.逻辑运算符 and or

select * from person where age = 28 and salary =53000;

select * from person where age = 23 or salary =2000;
                
select * from person where not(age = 28 and salary =53000);

  5.3  区间查询

select * from person where age BETWEEN 18 and 20;

ps: between...and 前后包含所指定的值
等价于 select * from person where salary >= 4000 and salary <= 8000;

  5.4  集合查询

select * from person where id = 1 or id = 3 or id = 5;

select * from person where id not in(1,3,5);

  5.5  模糊查询

select * from person where name like %e%; -- 包含指定参数
select * from person where name like %e; -- 以什么结尾
select * from person where name like e%; -- 以什么开头

select * from person where name like __e%; -- _表示单个字符站位符

select * from person where name like __;

  5.6 排序查询

select * from person where age >30 ORDER BY salary desc; -- ASC正序 DESC倒序
            
select * from person ORDER BY CONVERT(name USING gbk);-- 中文排序

 

数据库--数据操作

标签:esc   upd   into   post   数据库   mysq   int   去重   参数   

原文地址:https://www.cnblogs.com/tsboy/p/8476616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!