zoukankan      html  css  js  c++  java
  • SQL中CRUD C——create 添加数据 R——read 读取数据 U——update 修改数据 D——delete 删除数据

    在SQL server中对数据库的操作:

    删除表:
    drop table 表名
    修改表:
    alter table 表名 添加列add 列名 列类型
    alter table 表名 drop column 列名

    删除数据库
    drop database 数据库名

    CRUD操作
    C——create 添加数据 R——read 读取数据 U——update 修改数据 D——delete 删除数据


    1.添加数据
    insert into 表名 values('第一列值','第二列值')——数据库中用单引号 每一列都要添加

    第一种方式:

    insert into 表名 values('第一列值','')——若第二列不想添加不能不写,可用引号为空

    第二种方式:

    给一列添加:insert into Nation(列名) values('列值')

    给多列添加,在列名后用“,”隔开——(列,列)

    如果使用自增长

    SQL server中:不用去管第一列从第二列开始

    My SQL 中需管第一列自增长列

    2.删除数据

    delete from 表名—— 删除所有数据

    delete from 表名 where ids=5——删除ids=5的数据

    3.修改数据

    update 表名 set fcode='p016' 修改所有的fcode值为p016

    update 表名 set fcode='p016' where ids=6

    修改多列
    update 表名 set fcode='',mcode='' where ids= 列与列用逗号隔开

    字符串+'',
    布尔型'true'或'false',0 false或1 true 
    日期型'1999-2-3'+单引号,中按格式,
    整形

    查询:
    1.简单查询
    select * from 表名 ——查所有数据
    select 列名,列名 from 表名——查指定列的数据
    设置别名 select 列名 as '别名' ,列名 as '别名' from 表名

    2.条件查询

    select * from 表名 where 列名='数据'


    select * from 表名 where 列名='数据' and 列名='数据'
    select * from 表名 where 列名='数据' or 列名='数据'——多条件查询

    3.范围查询
    汽车举例:
    select * from 表名 where 列名(Price)>40 and 列名(Price)<50

    select * from 表名 where Price between 40 and 50

    4.离散查询

    select * from 表名 where 列名 in ('c001','c005','c010','c015')
    select * from 表名 where 列名 not in ('c001','c005','c010','c015') ——反选

    5.模糊查询
    以汽车表为例:
    select * from 表名 where 列名 like '%宝马%'
    select * from 表名 where 列名 like '宝马%'——查询以宝马开头的
    select * from 表名 where 列名 like '%宝马'——查询以宝马结尾的
    select * from 表名 where 列名 like '宝马'——查等于宝马的

    _代表一个字符
    select * from 表名 where 列名 like '__E%'——查第三个字符是E的
    % 代表是任意多个字符

    6.排序查询

    select * from 表名 order by 列名 ——默认升序asc
    select * from 表名 order by 列名 desc ——降序排列

    select * from 表名 order by 列名 desc,列名 asc——以两个字段排序,前面主要条件,后面次要条件

    7.分页查询

    select top 5(每页显示几条) * from 表名 where 列名 not in (select top 5 列名 from 表名)

    当前页:page = 2; 每页显示:row = 10;
    select top row(每页显示几条) * from 表名 where 列名 not in (select top (page-1)*row 列名 from 表名)

    8.去重查询

    select distinct 列名 from 表名

    9.分组查询

    select Brand(列名) from 表名 group by Brand(列名) having(在分组中加条件,有必须有group by) count(*)>2

    10.聚合函数 (又叫统计查询)

    select count(*) from 表名 ——查询出来的数据条数

    select count(主键列) from 表名 ——查询出来的所有数据条数

    select sum(Price) from Car——求和
    select avg(Price) from Car——求平均
    select max(Price) from Car——求最大
    select min(Price) from Car——求最小

    关键字不区分大小写

  • 相关阅读:
    PHP断点续传(下载)代码
    PHP及相关服务器防盗链
    工作过程中注意事项(一)
    ubuntu下libtorrent库安装
    ubuntu ftp 服务器搭建
    ubuntu系统搭建php+apache+cumulusclips
    JavaScript HTML DOM 元素(节点)
    JavaScript 闭包
    安装安卓(Android)x86系统
    javascript 错误与调试
  • 原文地址:https://www.cnblogs.com/zl1121102942/p/5723059.html
Copyright © 2011-2022 走看看