本文共 3920 字,大约阅读时间需要 13 分钟。
创建数据库
使用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
高级查询运算词
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
外连接
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
分组与聚合
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
数据库操作
sp_detach_db
sp_attach_db 'dbname', 'C:\path\dbname.mdf'
sp_renamedb 'old_name', 'new_name'
复制表
select * into b from a where 1 > 1
insert into b(a, b, c) select d, e, f from b
子查询
select a.b, a.c from table1 where a.b in (select d from table2)
外连接查询
select a.a, a.b, b.c, b.d from a left outer join b on a.a = b.c
数据库分页
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
删除重复记录
delete from table1 where id not in (select max(id) from table1 group by col1, col2)
列出数据库表名
select name from sysobjects where type=‘U’
列出表中列名
select name from syscolumns where id=object_id(‘TableName’)
初始化表数据
truncate table table1
查询范围限制
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'
获取随机数据
select newid()
select top 10 * from table1 order by newid()
常用条件
1=1:表示无条件匹配。1=2:表示无匹配条件。数据库优化
dbcc shrinkdatabase(dbname)
dbcc reindex
ALTER DATABASE [dbname] MODIFY FILEGROUP FILENAME('C:\path\filename.mdf'), NEWNAME('new_filename.mdf')用户权限管理
exec sp_changeobjectowner 'tablename', 'dbo'
CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch @OldOwner as NVARCHAR(128), @NewOwner as NVARCHAR(128)
数据迁移
BULK导入数据:BULK INSERT 'C:\file.mdf' 'dbo.table' (FILETOWORKBOOK)
循环写入数据
declare @i intset @i=1while @i<30begininsert into test (userid) values(@i)set @i=@i+1end
通过以上操作,可以有效地管理和优化SQL Server数据库,提升开发和维护效率。
转载地址:http://tyqn.baihongyu.com/