zoukankan      html  css  js  c++  java
  • 数据库操作2

    1.创建数据库

    create  database 数据库名称

    2.删除数据库

    drop database 数据库名称

    3.备份sql server

    创建备份数据的device

    use master

    exec sp_addumpdevice '名称','新的名称','路径'

    开始备份

    backup database pubs to 新的名称

    4.创建表

    create table 表名(列名1 类型,列名2 类型)

    5.根据已有表创建新表

    create table 新表名称 like 旧表名称

    create table 新表名称 as select 列名 from 旧表名称 defintion only

    6. 增加一个列

    Alter table 表名称 add 列名称 数据类型

    7.添加主键

    alter table 表名称 add primary key(列名称)

    8.自增id属性从1开始每次加1

    identity (1,1)

    9.创建索引

    create index 索引名 on 表名(列名)

    10.删除索引

    drop index idx_name

    11.CTE查询

    ;with t as(

    select openid,ROW_NUMBER()over(partition by openid order byopenid)as row

    from #temp)delete t where row>1

    12.case when的用法(修改表名称id,当 t 的名字不为空,则还是 t 的名字,否则变为表名称的名字,把被修改的数据输出到临时表)

    update pn

    set pn.id=case when t.id>'' then t.id else pn.id end

    output deleted.id into  临时表

    from 表名称 pn with(nolock)

    join #temp t

    13.查询、插入、删除、求和、平均、最大值

    select * from table

    insert into new_table(id,name)values(1,'张三')

    delete from table where 范围

    select sum(field1) as sumvalue from table1

    select avg (field1) as avgvalue  from table1

    select max(field1) as maxvalue from table1

  • 相关阅读:
    ACM-ICPC ShangHai 2014
    DEBUG感想
    WireShark 使用日记
    C++ 备忘录
    BZOJ 1022 [SHOI2008]小约翰的游戏John
    高斯消元
    BZOJ3236 [Ahoi2013]作业
    BZOJ P3293&&P1045
    ZKW费用流的理解
    BZOJ 几道水题 2014-4-22
  • 原文地址:https://www.cnblogs.com/Eaglery/p/5099244.html
Copyright © 2011-2022 走看看