zoukankan      html  css  js  c++  java
  • CRUD的操作,增删改查!

      1 1.注释语法:--,#
      2 2.后缀是.sql的文件是数据库查询文件
      3 3.在创建查询里,那个需要保存的对话框只是,保存查询。
      4 4.在数据库里面 列有个名字叫字段 行有个名字叫记录
      5 
      6 CRUD操作:
      7 create 创建(添加)
      8 read 读取
      9 update 修改
     10 delete 删除
     11 
     12 1、添加数据    Info代表的是文件表名,后面括号里是给所有列里添加的内容
     13 
     14 insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ;
     15 
     16 给特定的列添加数据   Info是文件表名,第一个括号里是只给这个文件里这两个列添加值;
     17 
     18 insert into Info (code,name) values('p010','李四');
     19 
     20 自增长列的处理     family是表名   直接给它值就行 
     21 insert into family values('','p001','数据','T001','数据',1);
     22 
     23 insert into 表名 values(值)
     24 
     25 2、删除数据
     26 删除所有数据      family 是表名,意思是将这个表里所有的东西全部删除
     27 delete from family
     28 删除特定的数据     将Info表名里的code列里的p001删除
     29 delete from Info where code='p001'
     30 
     31 delete from 表名 where 条件
     32 
     33 3、修改数据
     34 修改所有数据      把Info表名里的name列都修改成徐业鹏
     35 update Info set name='徐业鹏' 
     36 修改特定数据      把Info表名里的name列改成吕永乐,但只给code列的p002那一行改
     37 update Info set name='吕永乐' where code='p002' 
     38 修改多列            把Info表名里的name列改成吕永乐,还将sex列的改成1, 但只给code列的p003那一行改。
     39 update Info set name='吕永乐',sex=1 where code='p003'   #布尔型,整数和小数类型不用加单引号
     40 
     41 update 表名 set 要修改的内容 where 条件
     42 
     43 4、读取数据
     44 (1)简单读取,查询所有列(*) 所有行(没有加条件)       读取全部Info表里的内容
     45 select * from Info
     46 (2)读取特定列        只读取Info表里的code,name列
     47 select code,name from Info
     48 (3)条件查询            只查询Info表里code列的p003行的内容
     49 select * from Info where code='p003'
     50 (4)多条件查询      找到Info表里的code列p003行的内容  或 nation列n002行的内容      
     51 select * from Info where code='p003' or nation='n002' #或的关系
     52 select * from Info where sex=0 and nation='n002' #与的关系
     53 
     54 or是或的意思, and是与的意思
     55 
     56 
     57 (5)关键字查询(模糊查询) %代表不知道
     58 查所有包含奥迪的汽车    找car表里name列叫其中关键字叫奥迪的
     59 
     60 select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
     61 
     62 
     63 查以'皇冠'开头的所有汽车       找car 表里name列里是皇冠开头的
     64 select * from car where name like '皇冠%';
     65 
     66 
     67 查询汽车名称中第二个字符是''的   找car表里的那么列第二个字符叫“_马%”的后面不清楚什么名
     68 
     69 select * from car where name like '_马%';     #下划线_代表任意一个字符
     70 
     71 
     72 (6)排序查询        让car表里的powers列默认从小到大往下排列,也叫升序排列
     73 select * from car order by powers #默认升序排列
     74 select * from car order by powers desc #升序asc 降序 desc
     75 先按brand升序排,再按照price降序排             主要将car表里的brand列默认升序,然后再讲price列降序排列
     76 select * from car order by brand,price desc
     77 
     78  
     79 
     80  
     81 
     82 
     83 (7)范围查询      查询Car表筛选出 price列>40的 和 price列<60的
     84 select * from car where price>40 and price<60
     85 select * from car where price between 40 and 60     #between是之间的意思
     86 
     87  
     88 
     89 (8)离散查询
     90 select * from car where price=30 or price=40 or price=50 or price=60;
     91 select * from car where price in(30,40,50,60)
     92 select * from car where price not in(30,40,50,60)
     93 
     94  
     95 
     96 (9)聚合函数(统计查询)
     97 select count(*) from car
     98 select count(code) from car #取所有的数据条数
     99 select sum(price) from car #求价格总和
    100 select avg(price) from car #求价格的平均值
    101 select max(price) from car #求最大值
    102 select min(price) from car #求最小值
    103 
    104  
    105 
    106 (10)分页查询
    107 select * from car limit 0,10 #分页查询,跳过几条数据(0)取几条(10)
    108 规定一个每页显示的条数:m
    109 当前页数:n
    110 select * from car limit (n-1)*m,m
    111 
    112  
    113 
    114 (11)去重查询
    115 select distinct brand from car
    116 
    117  
    118 
    119 (12)分组查询
    120 查询汽车表中,每个系列下汽车的数量
    121 select brand,count(*) from car group by brand
    122 分组之后,只能查询该列或聚合函数
    123 
    124  
    125 
    126 取该系列价格平均值大于40的系列代号
    127 select brand from car group by brand having avg(price)>40
    128 
    129  
    130 
    131 取该系列油耗最大值大于8的系列代号
    132 select brand from car group by brand having max(oil)>8
    133 
    134  
    135 
    136  
    137 
    138 高级查询:
    139 1.连接查询 同时查两张表        这种查询是扩展列,按列来排的。
    140 select * from Info,Nation
    141 形成笛卡尔积 如果第一个表里有五条数据第二个表里有十条数据,链接的结果就是5*10=50条 
    142 数据 这样的结果是一一对应,第二张表的第一条数据,会跟第一张表的所有数据对应一遍 也 
    143 就是说,第一张表的数据按照他规定的顺序该往下排往下排,第二张表的数据走第一遍时候只给 
    144 第一条数据一直复制一直复制。然后第一张表再走一遍,第二张表的第二条数据再复合一遍,以 
    145 此类推。直到第二个表的数据每一行复制完。
    146 
    147 
    148 找出Info表和Nation中的Info表里的nation列=Nation表里的code列
    149 select * from Info,Nation where Info.nation=Nation.code
    150 
    151 找Info表里的code列,name列,sex列,取出Nation表里的name列,(as)是讲Nation列名改为(民 
    152 族)再取出Info表里的bithday列 下面就是上面那句话的意思。
    153 select Info.code,Info.name,Info.sex,Nation.name as '民族',Info.birthday from 
    154 Info,Nation where Info.nation=Nation.code
    155 
    156 注:Nation是Info表的外键,Info是主键
    157 (as)是讲Nation列名改为(民族)数据库里没改。数据库里没改。只是让你在这查询里看看这 
    158 一列的意思。
    159 
    160 关键字链接 :
    161 找Info表 链接 Nation表在讲Info表里的nation列=Nation表里的code列
    162 select * from Info join Nation on Info.nation=Nation.code
    163 
    164 注:join是链接的意思
    165 
    166 2.联合查询 查询的列数要一至。 查出code列,name列,这个表名是Info, 下面那个表名是Nation union是联合的意思 这种查询是扩展行,按行来排列的。
    167 select code,name from Info
    168 union
    169 select code,name from Nation
    170 
    171  
    172 
    173 3.子查询
    174 子查询查询的结果作为父查询的条件
    175 
    1761)无关子查询:子查询执行的时候和父查询没有关系
    177 查民族为'汉族'的所有学生信息
    178 先查出Info表里的nation列=(找出Nation表的name列='汉族的')
    179 select * from Info where nation=(select code from nation where name='汉族')
    180 
    181 查询生产厂商为'一汽大众'的所有汽车信息
    182 select * from car where brand=()
    183 select brand_code from brand where prod_code=()
    184 select prod_code from productor where prod_name='一汽大众'
    185 
    186 
    187 select * from car where brand in(select brand_code from brand where prod_code= 
    188 (select prod_code from productor where prod_name='一汽大众'))
    189 
    1902)相关子查询
    191 子查询在执行的时候需要用到父查询的内容
    192 
    193 查询汽车表中,汽车油耗小于该系列平均油耗的所有汽车信息
    194 
    195 select * from car where oil<(该系列平均油耗)
    196 select avg(oil) from car where brand =(该系列)
    197 
    198 select * from car a where oil<(select avg(oil) from car b where b.brand =a.brand)
    199 
    200  
  • 相关阅读:
    Axure 8 注册码,市面上很多注册码都不行用,但是这个可以。
    SmartGit Mac、Liunx、Windows过期后破解方法
    iOS蓝牙4.0开发例子
    苹果发布【新开发语言】Swift
    iOS Web开发
    [置顶]JB开发之制作系统级Application
    mac 10.9 dock在多屏幕间移动
    IOS Application Security Testing Cheat Sheet
    看上去很美 国内CDN现状与美国对比
    iOS 企业证书发布app 流程
  • 原文地址:https://www.cnblogs.com/zc290987034/p/6029283.html
Copyright © 2011-2022 走看看