博客
关于我
SQL语句大全
阅读量:191 次
发布时间:2019-02-28

本文共 3920 字,大约阅读时间需要 13 分钟。

SQL Server操作指南

一、基础操作

  • 创建数据库

    使用CREATE DATABASE命令创建新的数据库。

    CREATE DATABASE database-name
  • 删除数据库

    使用DROP DATABASE命令删除指定的数据库。

    DROP DATABASE dbname
  • 备份SQL Server数据库

    使用sp_addumpdevice创建备份设备,并执行备份操作。

    USE masterEXEC sp_addumpdevice ‘disk’, ‘testBack’, ‘c:mssql7backupMyNwind_1.dat’
    BACKUP DATABASE pubs TO testBack
  • 创建新表

    使用CREATE TABLE命令创建新表,支持多种数据类型。

    create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],…)
    • 根据已有表创建新表:
      A: create table tab_new like tab_oldB: create table tab_new as select col1,col2… from tab_old definition only
  • 删除新表

    使用DROP TABLE命令删除指定表。

    drop table tabname
  • 添加新列

    使用ALTER TABLE命令添加新列,注意列添加后不能删除。

    Alter table tabname add column col type
  • 添加主键

    使用ALTER TABLE命令添加主键。

    Alter table tabname add primary key(col)
    • 删除主键:
      Alter table tabname drop primary key(col)
  • 创建索引

    使用CREATE INDEX命令创建索引。

    create [unique] index idxname on tabname(col….)
    • 删除索引:
      drop index idxname
  • 创建视图

    使用CREATE VIEW命令创建视图。

    create view viewname as select statement
    • 删除视图:
      drop view viewname
  • 基本SQL语句

    • 查询数据:
      select * from table1 where 范围
    • 插入数据:
      insert into table1(field1,field2) values(value1,value2)
    • 删除数据:
      delete from table1 where 范围
    • 更新数据:
      update table1 set field1=value1 where 范围
    • 查找数据:
      select * from table1 where field1 like ’%value1%’
    • 排序数据:
      select * from table1 order by field1,field2 [desc]
    • 统计数据:
      select count as totalcount from table1select sum(field1) as sumvalue from table1select avg(field1) as avgvalue from table1select max(field1) as maxvalue from table1select min(field1) as minvalue from table1
  • 二、查询与操作

    1. 高级查询运算词

      • UNION:组合两个结果集。
        select a.a, a.b from table1 union select c.c, c.d from table2
      • EXCEPT:排除另一个结果集中的数据。
        select a.a, a.b from table1 except select c.c, c.d from table2
      • INTERSECT:仅保留两个结果集的交集。
        select a.a, a.b from table1 intersect select c.c, c.d from table2
    2. 外连接

      • 左外连接:保留左连接表的所有记录。
        select a.a, a.b, b.c, b.d from a left outer join b on a.a = b.c
      • 右外连接:保留右连接表的所有记录。
        select a.a, a.b, b.c, b.d from a right outer join b on a.a = b.c
      • 全外连接:保留两个连接表的所有记录。
        select a.a, a.b, b.c, b.d from a full outer join b on a.a = b.c
    3. 分组与聚合

      • 分组标准:使用GROUP BY命令。
        select a.title, count(a.username) as submitCount from table1 group by a.title
      • 统计函数:在GROUP BY中使用统计函数。
        select a.title, sum(a.submitCount) as totalSubmit from table1 group by a.title
    4. 数据库操作

      • 分离数据库:
        sp_detach_db
      • 附加数据库:
        sp_attach_db 'dbname', 'C:\path\dbname.mdf'
      • 修改数据库名称:
        sp_renamedb 'old_name', 'new_name'
    5. 三、数据库优化与管理

    6. 复制表

      • 只复制表结构:
        select * into b from a where 1 > 1
      • 拷贝数据:
        insert into b(a, b, c) select d, e, f from b
    7. 子查询

      • 查询多个表:
        select a.b, a.c from table1 where a.b in (select d from table2)
    8. 外连接查询

      • 使用外连接表达业务关系。
        select a.a, a.b, b.c, b.d from a left outer join b on a.a = b.c
    9. 数据库分页

      • 使用TOP命令实现分页。
        select top 10 * from table1
      • 动态分页:
        declare @start int, @end intset @start = 10, @end = 15select top @end * from (select top @start * from table1 order by id) a, table1 b where b.id = a.id order by id
    10. 删除重复记录

      • 删除重复记录:
        delete from table1 where id not in (select max(id) from table1 group by col1, col2)
    11. 列出数据库表名

      select name from sysobjects where type=‘U’
    12. 列出表中列名

      select name from syscolumns where id=object_id(‘TableName’)
    13. 初始化表数据

      • 清空表数据:
        truncate table table1
    14. 查询范围限制

      • 使用between限制数据范围:
        select * from table1 where date between '2023-01-01' and '2023-12-31'
      • 排除边界值:
        select * from table1 where date not between '2023-01-01' and '2023-12-31'
    15. 获取随机数据

      • 随机获取记录:
        select newid()
      • 随机获取10条记录:
        select top 10 * from table1 order by newid()
    16. 四、技术技巧

    17. 常用条件

      • 1=1:表示无条件匹配。
      • 1=2:表示无匹配条件。
    18. 数据库优化

      • 收缩数据库:
        dbcc shrinkdatabase(dbname)
      • 重建索引:
        dbcc reindex
      • 分割数据库文件:
        ALTER DATABASE [dbname] MODIFY FILEGROUP FILENAME('C:\path\filename.mdf'), NEWNAME('new_filename.mdf')
    19. 用户权限管理

      • 更改表所有者:
        exec sp_changeobjectowner 'tablename', 'dbo'
      • 批量更改表所有者:
        CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch @OldOwner as NVARCHAR(128), @NewOwner as NVARCHAR(128)
    20. 数据迁移

      • 使用BULK导入数据:
        BULK INSERT 'C:\file.mdf' 'dbo.table' (FILETOWORKBOOK)
    21. 循环写入数据

      declare @i intset @i=1while @i<30begininsert into test (userid) values(@i)set @i=@i+1end
    22. 通过以上操作,可以有效地管理和优化SQL Server数据库,提升开发和维护效率。

    转载地址:http://tyqn.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现EulersTotient欧拉方程算法(附完整源码)
    查看>>
    Objective-C实现eval函数功能(附完整源码)
    查看>>
    Objective-C实现even_tree偶数树算法(附完整源码)
    查看>>
    Objective-C实现Exceeding words超词(差距是ascii码的距离) 算法(附完整源码)
    查看>>
    Objective-C实现extended euclidean algorithm扩展欧几里得算法(附完整源码)
    查看>>
    Objective-C实现ExtendedEuclidean扩展欧几里德GCD算法(附完整源码)
    查看>>
    Objective-C实现Factorial digit sum阶乘数字和算法(附完整源码)
    查看>>
    Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
    查看>>
    Objective-C实现factorial recursive阶乘递归算法(附完整源码)
    查看>>
    Objective-C实现factorial阶乘算法(附完整源码)
    查看>>
    Objective-C实现Fast Powering算法(附完整源码)
    查看>>
    Objective-C实现fenwick tree芬威克树算法(附完整源码)
    查看>>
    Objective-C实现FenwickTree芬威克树算法(附完整源码)
    查看>>
    Objective-C实现fft2函数功能(附完整源码)
    查看>>
    Objective-C实现FFT算法(附完整源码)
    查看>>
    Objective-C实现fibonacci斐波那契算法(附完整源码)
    查看>>
    Objective-C实现FigurateNumber垛积数算法(附完整源码)
    查看>>
    Objective-C实现first come first served先到先得算法(附完整源码)
    查看>>
    Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
    查看>>
    Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
    查看>>