CREATE DATABASE database_name [ ON [ PRIMARY ] <filespec> [ ,...n ] [ , <filegroup> [ ,...n ] ] [ LOG ON <filespec> [ ,...n ] ] ] [ COLLATE collation_name ] <filespec> ::= { ( NAME = logical_file_name , FILENAME = { 'os_file_name' | 'filestream_path' } [ , SIZE = size [ KB | MB | GB | TB ] ] [ , MAXSIZE = { max_size [ KB | MB | GB | TB ] | UNLIMITED } ] [ , FILEGROWTH = growth_increment [ KB | MB | GB | TB | % ] ] ) }
ON:用来定义数据库的数据文件。PRIMARY指出其后所定义的文件是主数据文件,如果省略,则第一个定义的文件是主数据文件。
LOG ON:用来定义数据库的日志文件。如果没有LOG ON,SQL Server将自动创建一个日志文件。
数据库中的文件类型与推荐扩展名:主要数据文件.mdf ,次要数据文件.ndf ,事务日志.ldf 。
-- Drop the database if it already exists IF EXISTS ( SELECT name FROM sys.databases WHERE name = N'Portal' ) DROP DATABASE Portal GO CREATE DATABASE Portal GO
CREATE DATABASE [Portal] ON PRIMARY ( NAME = N'Portal', FILENAME = N'F:\Database\Portal.mdf' , SIZE = 5MB , FILEGROWTH = 1MB ) LOG ON ( NAME = N'Portal_log', FILENAME = N'F:\Database\Portal_log.ldf' , SIZE = 2MB , FILEGROWTH = 10% )
CREATE DATABASE [Portal] ON PRIMARY ( NAME = N'Portal', FILENAME = N'F:\Database\Portal.mdf' , SIZE = 5MB , FILEGROWTH = 1MB ), ( NAME = N'Portal_Data_2014', FILENAME = N'F:\Database\Portal_Data_2014.ndf' , SIZE = 5MB , FILEGROWTH = 1MB ) LOG ON ( NAME = N'Portal_log', FILENAME = N'F:\Database\Portal_log.ldf' , SIZE = 2MB , FILEGROWTH = 10% ), ( NAME = N'Portal_log_2014', FILENAME = N'F:\Database\Portal_log_2014.ldf' , SIZE = 2MB , FILEGROWTH = 10% )
CREATE DATABASE [Portal] ON PRIMARY ( NAME = N'Portal', FILENAME = N'F:\Database\Portal.mdf' , SIZE = 10MB , FILEGROWTH = 1MB ), FILEGROUP [SECTION2014] ( NAME = N'Portal_Data_2014', FILENAME = N'F:\Database\Portal_Data_2014.ndf' , SIZE = 5MB , FILEGROWTH = 1MB ) LOG ON ( NAME = N'Portal_log', FILENAME = N'F:\Database\Portal_log.ldf' , SIZE = 2MB , FILEGROWTH = 10% )
ALTER DATABASE database_name { <add_or_modify_files> | <add_or_modify_filegroups> } [;] <add_or_modify_files>::= { ADD FILE <filespec> [ ,...n ] [ TO FILEGROUP { filegroup_name } ] | ADD LOG FILE <filespec> [ ,...n ] | REMOVE FILE logical_file_name | MODIFY FILE <filespec> } <filespec>::= ( NAME = logical_file_name [ , NEWNAME = new_logical_name ] [ , FILENAME = {'os_file_name' | 'filestream_path' | 'memory_optimized_data_path' } ] [ , SIZE = size [ KB | MB | GB | TB ] ] [ , MAXSIZE = { max_size [ KB | MB | GB | TB ] | UNLIMITED } ] [ , FILEGROWTH = growth_increment [ KB | MB | GB | TB| % ] ] [ , OFFLINE ] ) <add_or_modify_filegroups>::= { | ADD FILEGROUP filegroup_name [ CONTAINS FILESTREAM | CONTAINS MEMORY_OPTIMIZED_DATA ] | REMOVE FILEGROUP filegroup_name | MODIFY FILEGROUP filegroup_name { <filegroup_updatability_option> | DEFAULT | NAME = new_filegroup_name } } <filegroup_updatability_option>::= { { READONLY | READWRITE } | { READ_ONLY | READ_WRITE } }
ALTER DATABASE [Portal] ADD FILEGROUP [SECTION2014]
ALTER DATABASE [Portal] ADD FILE ( NAME = N'Portal_Data_2014', FILENAME = N'F:\Database\Portal_Data_2014.ndf' , SIZE = 5MB , FILEGROWTH = 1MB ) TO FILEGROUP [SECTION2014]
ALTER DATABASE [Portal] REMOVE FILE Portal_Data_2014
ALTER DATABASE [Portal] MODIFY NAME = [Portal_2014]
EXEC sp_renamedb [Portal], [Portal_2014]
ALTER DATABASE [Portal] MODIFY FILEGROUP [PRIMARY] DEFAULT
DROP DATABASE { database_name | database_snapshot_name } [ ,...n ] [;]
示例
DROP DATABASE [Portal]
使用系统存储过程sp_detach_db分离数据库
sp_detach_db [ @dbname= ] 'database_name' [ , [ @skipchecks= ] 'skipchecks' ] [ , [ @keepfulltextindexfile = ] 'KeepFulltextIndexFile' ]
EXEC sp_detach_db [Portal]
直接运行分离数据库的SQL语句,可能会提示有进程(用户)正在使用,分离失败。要解决这个问题,先查看哪些进程(用户)正在使用该数据库。
查看用户和进程
USE [master] sp_who
先结束占用数据库的进程,再分离数据库
USE [master] KILL 55 KILL 56 KILL 57 EXEC sp_detach_db [Portal]
CREATE DATABASE [Portal] ON ( FILENAME = 'F:\Database\Portal.mdf' ) FOR ATTACH
CREATE DATABASE [Portal] ON ( FILENAME = 'F:\Database\Portal.mdf' ), ( FILENAME = 'F:\Database\Portal_log.ldf' ) FOR ATTACH
EXEC sp_attach_db [Portal], 'F:\Database\Portal.mdf'
EXEC sp_attach_db [Portal], 'F:\Database\Portal.mdf', 'F:\Database\Portal_log.ldf'
SQL Server中可以使用多种方式查看数据库信息,例如使用目录视图、函数、存储过程等。
使用目录视图查看数据库基本信息:
◊ sys.databse_files:查看数据库文件信息;
◊ sys.filegroups:查看数据库组信息;
◊ sys.master_files:查看数据库文件的基本信息和状态信息;
◊ sys.database:数据库和文件目录视图查看数据库的基本信息。
SELECT * FROM sys.databases WHERE name = 'Northwind'
语法
sp_helpdb [ [ @dbname= ] 'name' ]
示例
EXEC sp_helpdb [Portal]