杰网资源 Design By www.escxy.com

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语法,sql基本语法

杰网资源 Design By www.escxy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
杰网资源 Design By www.escxy.com