您现在的位置是:网站首页> 编程资料编程资料
详解Sql基础语法_MsSql_
2023-05-26
406人已围观
简介 详解Sql基础语法_MsSql_
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死锁检测的方法_MsSql_
- SqlServer使用 case when 解决多条件模糊查询问题_MsSql_
- 将表里的数据批量生成INSERT语句的存储过程 增强版_MsSql_
- SQL Server中数据行批量插入脚本的存储实现_MsSql_
- SQL Server FileStream详解_MsSql_
- SQL Server中的RAND函数的介绍和区间随机数值函数的实现_MsSql_
- 浅析SQL Server中的执行计划缓存(下)_MsSql_
- 浅析SQL Server中的执行计划缓存(上)_MsSql_
- sqlserver锁表、解锁、查看销表的方法_MsSql_
- SQLServer存储过程中事务的使用方法_MsSql_
