Database and database file creation

Database and database file creation

First, create a database syntax

CREATE DATABASE database_name

[ON

[<filespec>[,…n]]

[,<filegroup>[,…n]]]

[LOG ON  {<filespec>[,…n]}]

[COLLATE <collation_name>]

[FOR LOAD|FOR ATTACH]

Explanation of available parameters

<filespec>::=  [PRIMARY]

(NAME=' logical file name ',

FILENAME=' The physical path and file name where the database is stored '

[, SIZE= initial size of data file ]

[, MAXSIZE= Specify the maximum size of the file ]

[, FILEGROWTH= Indicate the increment of the file each time ] )

The <filegroup> item is used to define the user filegroup and its files. The format of <filegroup> is as follows:

<filegroup>::= FILEGROUP filegroup name

 

example:

       1. Create an archive database that contains three data files, two transaction log files and two custom filegroups. The logical file name of the main data file is file1, the actual file name is filedata1.mdf, the logical file names of the two secondary data files are file2 and file3, and the actual file names are filedata2.ndf and filedata3.ndf respectively. Secondary data files belong to filegroups Fgroup1 and Fgroup2, respectively. The logical file names of the two event log files are filelog1 and filelog2 respectively, and the actual file names are filedatalog1.ldf and filedatalog2.ldf respectively. The initial capacity of the above files is 10MB, the maximum capacity is 100MB, and the increment is 1MB. (path: C:\c)

 1).创建数据库(先在c盘创建文件夹c)
Create database archive
on
(
  name = file1,
  filename = 'c:\c\filedata1.mdf',
  size = 10MB,
  maxsize = 100MB,
  filegrowth =1MB
),
filegroup Fgroup1
(
  name = file2,
  filename = 'c:\c\filedata2.ndf',
  size = 10MB,
  maxsize = 100MB,
  filegrowth = 1MB
),
filegroup Fgroup2
(
  name = file3,
  filename = 'c:\c\filedata3.ndf',
  size = 10MB,
  maxsize = 100MB,
  filegrowth = 1MB
)
log on
(
  name = filelog1,
  filename = 'c:\c\filedatalog1.ldf',
  size = 10MB,
  maxsize = 100MB,
  filegrowth = 1MB
),
(
  name = filelog2,
  filename = 'c:\c\filedatalog2.ldf',
  size = 10MB,
  maxsize = 100MB,
  filegrowth = 1MB
)

 2). View database information
  sp_helpdb archive;

 

Second, modify the database syntax


ALTER DATABASE database_name
{ADD FILE <filespec>[,…n][TO FILEGROUP filegroup_name]
                                                      /*Add a data file to the filegroup*/
|ADD LOG FILE <filespec>[,…n] /*Add a transaction log file*/
|REMOVE FILE logical_file_name /*delete data file*/
|ADD FILEGROUP filegroup_name /*add filegroup*/
|REMOVE FILEGROUP filegroup_name /*delete filegroup*/
|MODIFY FILE <filespec> /*modify file attributes*/
|MODIFY NAME= new_dbname /*Update database name*/
}

 

example: 

1. Adjust the size of the main data file file1 of the archive database to 20MB.
 alter database archive
 modify file
 (
 name = file1,
 size = 20MB
 )
sp_helpdb archive;
2. Add a secondary data file file4 to the archive database, the physical file name is filedata4.ndf, the initial size is 5MB, the maximum size is 50MB, each expansion 1MB.

a、添加文件
alter database archive
add file
(
   name = file4,
   filename = 'c:\c\filedata4.ndf',
   size = 5MB,
   maxsize = 50MB,
   filegrowth = 1MB
)

b. View the modified database

sp_helpdb archive;


3. First create a database named test, the logical and actual file names of its main data files are testdat1 and tdat1.mdf respectively. Then add a data file to the database using the logical and actual file names testdat2 and tdat2.ndf respectively. The initial capacity of the two database files is 5MB, the maximum capacity is 10MB, and the increment is 20%.
create database test
on
(
  name = testdat1,
  filename ='c:\c\ tdat1.mdf',
  size = 5MB,
  maxsize = 10MB,
  filegrowth = 20%
)
alter database test
add file
(
  name = testdat2,
  filename ='c: \c\ tdat2.ndf',
  size = 5MB,
  maxsize = 10MB,
  filegrowth = 20%
)
4. Delete the database archive and test.
drop database archive;
drop database test;

Check if the deletion is successful
sp_helpdb archive;
sp_helpdb test;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326489035&siteId=291194637