1.注释语法:--,#
2.后缀是.sql的文件是数据库查询文件
3.保存查询
4.在数据库里面 列有个名字叫字段   行有个名字叫记录
5.一条数据即为表的一行

CRUD操作:
create 创建(添加)
read 读取
update 修改
delete 删除

1、添加数据
insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ;
给特定的列添加数据
insert into Info (code,name) values('p010','李四');
自增长列的处理
insert into family values('','p001','数据','T001','数据',1);

insert into 表名 values(值)

2、删除数据
删除所有数据
delete from family
删除特定的数据
delete from Info where code='p001'

delete from 表名 where 条件

3、修改数据

在mysql里面给sql语句某个列的值加1:

如图:

实现的sql语句:  update table set de_name = de_name+1 where id =1;

修改所有数据
update Info set name='123'
修改特定数据
update Info set name='321' where code='p002'  #把列中code=p002的项中的名字name改为321;
修改多列
update Info set name='321',sex=1 where code='p003'       #把列中code=p003的项中的名字name改为321且sex改为1;

update 表名 set 要修改的内容 where 条件

4、读取数据
(1)简单读取,查询所有列(*)  所有行(没有加条件)
select * from Info
(2)读取特定列
select code,name from Info
(3)条件查询
select * from Info where code='p003'
(4)多条件查询   or ;  and
select * from Info where code='p003' or nation='n002'    # or是或的关系
select * from Info where sex=0 and nation='n002'    #and是与的关系
(5)关键字查询(模糊查询)  like   以下price是(价格);表名是car;
查所有包含奥迪的汽车
select * from 表名 where name like '%奥迪%';    #百分号%代表任意多个字符
查以'皇冠'开头的所有汽车
select * from 表名 where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from 表名 where name like '_马%'; #下划线_代表任意一个字符
(6)排序查询  order by ; 降序 desc  
select * from 表名 order by powers  #默认升序排列
select * from 表名 order by powers desc #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from 表名 order by brand,price desc

(7)范围查询   and

1、select * from 表名 where 范围 ; 例如:price>40 and price<60

2、select * from 表名 where price between 40 and 60

(8)离散查询   or...or...or... ; in; not in

1、select * from 表名 where price=30 or price=40 or price=50 or price=60;

2、select * from 表名 where price in(30,40,50,60)

3、select * from 表名 where price not in(30,40,50,60)

(9)聚合函数(统计查询)    count   sum   avg  max   min
select count(*) from 表名
select count(code) from 表名 #取所有的数据条数
select sum(price) from 表名 #求价格总和
select avg(price) from 表名 #求价格的平均值
select max(price) from 表名 #求最大值
select min(price) from 表名 #求最小值

(10)分页查询    limit x,y;
select * from 表名 limit 0,10  #分页查询,跳过几条数据(0)取几条(10)
规定一个每页显示的条数:m
当前页数:n
select * from 表名 limit (n-1)*m,m

(11)去重查询    distinct   对这列去重,适合于查一列
select distinct 列名 from 表名

(12)分组查询    group by...having(条件);给某列分组。。根据什么条件;条件可以使多个
查询汽车表中,每个系列下汽车的数量
select 条件 from 表名 group by 列名
分组之后,只能查询该列或聚合函数

取该系列价格平均值大于40的系列代号
select 列名 from 表名 group by 列名 having avg(price)>40

取该系列油耗最大值大于8的系列代号     
select 列名 from 表名 group by 列名 having max(oil)>8

高级查询: Info为表1  Nation为表2

1、连接查询  a.Nation.name as '民族':可以在表中通过 as 把 name改为‘民族’,name为列名        #查出的数列 ,‘列叠加’

b.join...on...

select * from Info,Nation
形成笛卡尔积
select * from Info,Nation where Info.nation=Nation.code

select Info.code,Info.name,Info.sex,Nation.name as '民族',Info.birthday from Info,Nation where Info.nation=Nation.code

select 条件 from Info join Nation on Info.nation=Nation.code

2.联合查询   union
select code,name from Info
union                                        #查询时的列数必须要一致,查出的数据共两列,行叠加
select code,name from Nation

3.子查询
子查询查询的结果作为父查询的条件

(1)无关子查询:子查询执行的时候和父查询没有关系
查民族为'汉族'的所有学生信息
select * from Info where nation=(select code from nation where name='汉族')

查询生产厂商为'一汽大众'的所有汽车信息
select * from car where brand=()
select brand_code from brand where prod_code=()
select prod_code from productor where prod_name='一汽大众'

select * from car where brand in(select brand_code from brand where prod_code=(select prod_code from productor where prod_name='一汽大众'))
                                       #确定的值用 '='; 不确定右多少数据的值的时候用 'in'
(2)相关子查询
子查询在执行的时候需要用到父查询的内容

查询汽车表中,汽车油耗小于该系列平均油耗的所有汽车信息

select * from car where oil<(该系列平均油耗)
select avg(oil) from car where brand =(该系列)

select * from car a where oil<(select avg(oil) from car b where b.brand =a.brand)

数据库CRUD操作:C:create创建(添加)、R:read读取、U:update:修改、D:delete删除;高级查询的更多相关文章

  1. 【转】数据库CRUD操作

    数据库CRUD操作 一.删除表   drop table 表名称 二.修改表 alter   table 表名称 add  列名 数据类型   (add表示添加一列) alter  table  表名 ...

  2. 10月16日下午MySQL数据库CRUD操作(增加、删除、修改、查询)

    1.MySQL注释语法--,# 2.2.后缀是.sql的文件是数据库查询文件. 3.保存查询. 关闭查询时会弹出提示是否保存,保存的是这段文字,不是表格(只要是执行成功了表格已经建立了).保存以后下次 ...

  3. 数据库CRUD操作

    CRUD操作: C:create 增加数据: insert into 表名 values('N001','汉族') 普通 insert into 表名 values('','','') 如果有自增长列 ...

  4. 数据库CRUD操作以及MyBatis的配置使用

    • 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发     • 业务字段设计 实体: name: ...

  5. oracle数据库常用操作语句 、创建视图

    新增字段:alter table 表名 add (NAME VARCHAR(12), NAME NUMBER(10) );--如果添加单个字段可以不用括号包起来,例如 alter table cust ...

  6. django notes 六:数据库 CRUD 操作

    CRUD 也没什么可说的,django 提供了完善的 orm  api, 直接用就行了. 我只贴几个列子,一看就明白了,自己再用用就熟了. # create b = Blog(name='Beatle ...

  7. SharePoint 服务器端对象模型操作用户组(创建/添加/删除)

    摘要:几个操作SharePoint用户组的方法,已经测试通过,但是没有提升权限,如果没有权限的人操作,需要提升权限(提权代码附后).大家需要的话,可以参考下,写在这里也给自己留个备份~~ //创建用户 ...

  8. 使用node_redis进行redis数据库crud操作

    正在学习使用pomelo开发游戏服务器,碰到node.js操作redis,记录一下 假设应用场景是操作一个用户表的数据 引入node_redis库,创建客户端 var redis = require( ...

  9. AngularJS的 $resource服务 关于CRUD操作

    AngularJs 的CRUD 操作 是指对数据库的操作: CRUD其实是数据库基本操作中的Create(创建).ReadRetrieve(读取).Update(更新).Delete(删除).而这里的 ...

随机推荐

  1. CC2540开发板学习笔记(三)——外部中断

    一.实验内容 通过外部中断方式依次按下按键S1控制LED1的亮灭 二.实验过程 1.电路原理图同上 2.中断的概念 比如说我们在执行main函数时,突然来了个指令.优先级比现在执行的main还高,那我 ...

  2. Eval有什么功能?

    它的功能是把对应的字符串解析成JS代码并运行.应该尽量避免使用eval,因为不安全,非常耗性能.解析成JS代码要耗能,执行时也要耗能.

  3. 简单几何(凸包) POJ 1113 Wall

    题目传送门 题意:求最短路线,使得线上任意一点离城堡至少L距离 分析:先求凸包,答案 = 凸包的长度 + 以L为半径的圆的周长 /*********************************** ...

  4. DP ZOJ 3735 Josephina and RPG

    题目传送门 题意:告诉你C(m,3)个队伍相互之间的胜率,然后要你依次对战n个AI队伍,首先任选一种队伍,然后战胜一个AI后可以选择替换成AI的队伍,也可以不换,问你最后最大的胜率是多少. 分析:dp ...

  5. Log4net源码View之Logger解析

    1.简介 Logger是Log4net的三大核心载体之一,搞清楚它的意义很重大.另外两个分别是Appender和Layout.其对应关系为一个Logger对应多个Appender,一个Appender ...

  6. BZOJ2388 : 旅行规划

    考虑分块,每块维护两个标记$ts,td$. 那么对于块中一个位置$i$,它的实际值为$i\times td+ts+v_i$. 修改的时候,对于整块,直接打标记,对于零散的暴力修改,然后重构凸壳,时间复 ...

  7. Quest Central for DataBase 5.0.1,6.1 (软件+注册)

    找寻了多天,终于找到了,记录下,以后重装用.输入所有组件的licenses后,提示要注册,我选择了Canada,Google了一个地方的PostCode和phone number,填写,注册成功! 软 ...

  8. 面试题中遇到的SQL题目

    1.假设有一张表示cj表 Name Subject Result 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 82 要求查询结果: 姓名 语文 ...

  9. new在c#方法中的使用

    new在c#中有三种用法: 1.实例化对象 2.泛型约束 3.用在方法前.new和override的区别在于:override用于重写父类的方法:new用于隐藏方法,它调用的方法来自于申明的类,如果申 ...

  10. winform学习-----理解小概念-20160517

    1.MouseDown事件 当鼠标指针位于控件上并按下鼠标键时发生. 2.MouseUp事件 当鼠标指针在控件上并释放鼠标按键时发生. 与 mouseout 事件不同,只有在鼠标指针离开被选元素时,才 ...