详解Sql基础语法

所属分类: 数据库 / MsSql 阅读数: 1936
收藏 0 赞 0 分享

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

通过以上内容给大家详解Sql基础语法,希望本文介绍能够给大家带来帮助。

更多精彩内容其他人还在看

如何远程连接SQL Server数据库图文教程

如何远程连接SQL Server数据库图文教程
收藏 0 赞 0 分享

一个SQL语句获得某人参与的帖子及在该帖得分总和

一个SQL语句获得某人参与的帖子及在该帖得分总和
收藏 0 赞 0 分享

通用分页存储过程,源码共享,大家共同完善

通用分页存储过程,源码共享,大家共同完善
收藏 0 赞 0 分享

SQL查找某一条记录的方法

SQL查找某一条记录的方法
收藏 0 赞 0 分享

使用 GUID 值来作为数据库行标识讲解

使用 GUID 值来作为数据库行标识讲解
收藏 0 赞 0 分享

非常详细的SQL--JOIN之完全用法

非常详细的SQL--JOIN之完全用法
收藏 0 赞 0 分享

收缩后对数据库的使用有影响吗?

收缩后对数据库的使用有影响吗?
收藏 0 赞 0 分享

mssql server 存储过程里,bulk insert table from '路径+文件',路径固定,文件名不固定的实现方法

mssql server 存储过程里,bulk insert table from '路径+文件',路径固定,文件名不固定的实现方法
收藏 0 赞 0 分享

请问在mssql“SQL事件探查器”里表格的标题,如CPU,Read,Write,Duration,SPID.........的解释

请问在mssql“SQL事件探查器”里表格的标题,如CPU,Read,Write,Duration,SPID.........的解释
收藏 0 赞 0 分享

SQL Server 2000的安全配置

SQL Server 2000的安全配置
收藏 0 赞 0 分享
查看更多