创建一个库
CREATE DATABASE g2 ON PRIMARY
( NAME = 'g2', FILENAME = 'E:\benet\data1\g1.mdf' , SIZE = 307200KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = 'g2_log', FILENAME = 'E:\benet\data1\g1_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
创建一个表
CREATE
TABLE biao2
(
id int NOT NULL,
xingming char(10) NOT NULL,
xingbie float(53) NOT NULL,
nianling int NOT NULL,
chengji float(53) NULL
)
查询
select
* from biao1
where
chengji between 400 and 600
select
* from biao1
where
nianling like 55
select
* from biao1
where
nianling between 10 and 100
select
* from biao1
where
id = 5
insert
into biao1
values
(6 ,'
原迟
'
,
1 , 100 , 1542.22)
update
biao1
set
chengji = chengji + 1000
select
* from biao1
delete
from
biao1
where
id = 1
select
* from biao1
select
* from biao1
排序
order
by no asc
update
biao2
set
zongji = shuxue + yuwen + huaxue
where
xingming = 'a'
insert
into biao1
values
(
7,'
骨血松涛
'
,
1 , 700)
select
* from biao1
where
xingming like '%
血
%'
select
distinct xingbie from biao1
alter
table biao1
drop
column chengji
删除一列
alter table biao2
add
liehao int
select * from biao1,biao2
where biao1.id=biao2.id
将表一和表二联系起来,建立主索引,
注意小数点的应用
select * from biao1
inner join biao2
on biao1.id=biao2.id
和上面的一样
查询时间
select * from biao2
where riqi between '1993-01-01 00:00:00.000' and '1994-6-29 00:00:00.000'
select xingming from biao2
where bumen ='xiaoshou' and xingbie ='na'
delete
from biao2
where xingming='jinpeng'
select id ,xingming into biao2_backup
from biao2
where xingbie='na'
备份选择的数据
create view shitu as
select xingming ,riqi
from biao2
where xingbie = 'na'
创建一个自己匹配的视图
视图相当于是一个符合你的目的的一个查询结果集
.
他不占用空间
,
方便你以后的查找
.
多文件还原代码
ESTORE DATABASE [benet] FROM DISK = N'G:\bak\wz.bak' WITH FILE = 1, NORECOVERY, NOUNLOAD, REPLACE, STATS = 10
GO
RESTORE DATABASE [benet] FROM DISK = N'G:\bak\cy.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10
GO
恢复数据库
benet
来自文件
DISK = N'G:\bak\cy.bak' with
文件数
1
个,
NORECOVERY
指定不发生回滚。从而使前滚按顺序在下一条语句中继续进行。
在这种情况下,还原顺序可还原其他备份,并执行前滚。
REPLACE
覆盖
A.
还原完整数据库
注意:
MyAdvWorks
数据库仅供举例说明。
以下示例还原完整数据库备份。
复制代码
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1
注意:
对于使用完全恢复模式或大容量日志恢复模式的数据库,在大多数情况下,
SQL Server 2005
都要求您在还原数据库前备份日志尾部。有关详细信息,请参阅尾日志备份。
B.
还原完整数据库备份和差异备份
以下示例还原完整数据库备份后还原差异备份。另外,以下示例还说明如何还原媒体上的另一个备份集。差异备份追加到包含完整数据库备份的备份设备上。
复制代码
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1
WITH NORECOVERY
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1
WITH FILE = 2
C.
使用
RESTART
语法还原数据库
以下示例使用
RESTART
选项重新启动因服务器电源故障而中断的
RESTORE
操作。
复制代码
-- This database RESTORE halted prematurely due to power failure.
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1
-- Here is the RESTORE RESTART operation.
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1 WITH RESTART
D.
还原数据库并移动文件
以下示例还原完整数据库和事务日志,并将已还原的数据库移动到
C:\Program Files\Microsoft SQL Server\MSSQL\Data
目录下。
复制代码
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1
WITH NORECOVERY,
MOVE 'MyAdvWorks' TO
'c:\Program Files\Microsoft SQL Server\MSSQL\Data\NewAdvWorks.mdf',
MOVE 'MyAdvWorksLog1'
TO 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\NewAdvWorks.ldf'
RESTORE LOG MyAdvWorks
FROM MyAdvWorksLog1
WITH RECOVERY
E.
使用
BACKUP
和
RESTORE
创建数据库的副本
以下示例使用
BACKUP
和
RESTORE
语句创建
AdventureWorks
数据库的副本。
MOVE
语句使数据和日志文件还原到指定的位置。
RESTORE FILELISTONLY
语句用于确定待还原数据库内的文件数及名称。该数据库的新副本称为
TestDB
。有关详细信息,请参阅
RESTORE FILELISTONLY (Transact-SQL)
。
复制代码
BACKUP DATABASE AdventureWorks
TO DISK = 'C:\AdventureWorks.bak'
RESTORE FILELISTONLY
FROM DISK = 'C:\AdventureWorks.bak'
RESTORE DATABASE TestDB
FROM DISK = 'C:\AdventureWorks.bak'
WITH MOVE 'AdventureWorks_Data' TO 'C:\testdb.mdf',
MOVE 'AdventureWorks_Log' TO 'C:\testdb.ldf'
GO
F.
使用
STOPAT
语法还原到时间点和使用多个设备进行还原
以下示例将数据库还原到它在
2005
年
4
月
15
日中午
12
点时的状态,并显示涉及多个日志和多个备份设备的还原操作。
复制代码
RESTORE DATABASE MyAdvWorks
FROM MyAdvWorks_1, MyAdvWorks_2
WITH NORECOVERY, STOPAT = 'Apr 15, 2005 12:00 AM'
RESTORE LOG MyAdvWorks
FROM MyAdvWorksLog1
WITH NORECOVERY, STOPAT = 'Apr 15, 2005 12:00 AM'
RESTORE LOG MyAdvWorks
FROM MyAdvWorksLog2
WITH RECOVERY, STOPAT = 'Apr 15, 2005 12:00 AM'
G.
将事务日志还原到标记
以下示例将事务日志还原到名为
ListPriceUpdate
的标记事务中的标记处。
复制代码
USE AdventureWorks
GO
BEGIN TRANSACTION ListPriceUpdate
WITH MARK 'UPDATE Product list prices'
GO
UPDATE Production.Product
SET ListPrice = ListPrice * 1.10
WHERE ProductNumber LIKE 'BK-%'
GO
COMMIT TRANSACTION ListPriceUpdate
GO
-- Time passes. Regular database
-- and log backups are taken.
-- An error occurs.
USE master
GO
RESTORE DATABASE AdventureWorks
FROM AdvWorks1
WITH FILE = 3, NORECOVERY
GO
RESTORE LOG AdventureWorks
FROM AdvWorks1
WITH FILE = 4,
STOPATMARK = 'ListPriceUpdate'
H.
使用
TAPE
语法还原
以下示例从
TAPE
备份设备还原完整数据库备份。
复制代码
RESTORE DATABASE MyAdvWorks
FROM TAPE = '\\.\tape0'
I.
使用
FILE
和
FILEGROUP
语法还原
以下示例还原一个包含两个文件、一个文件组和一个事务日志的数据库。
复制代码
RESTORE DATABASE MyAdvWorks
FILE = 'MyAdvWorks_data_1',
FILE = 'MyAdvWorks_data_2',
FILEGROUP = 'new_customers'
FROM MyAdvWorks_1
WITH NORECOVERY
-- Restore the log backup.
RESTORE LOG MyAdvWorks
FROM MyAdvWorksLog1
J.
恢复到数据库快照
以下示例将数据库恢复到数据库快照。此示例假定该数据库当前仅存在一个快照。有关创建此数据库快照的示例,请参阅如何创建数据库快照
(Transact-SQL)
。
注意:
恢复到快照将删除所有全文目录。
复制代码
USE master
RESTORE DATABASE AdventureWorks FROM DATABASE_SNAPSHOT = 'AdventureWorks_dbss1800';
GO