1. Operations on databases and database "files"

Operations on the database

1. Create a database [CREATE DATABASE Student2…]

Example: Create the database "Student2", which contains a main data file and a transaction log file.

  1. The logical name of the main data file is "Student2_DATA", the file name of the operating system is "Student2_DATA.MDF", the initial size is 15M, the maximum size is 20M, and the file growth rate is 20%.
  2. The logical file name of the transaction log file is "Student2_LOG", the physical file name is "Student2_LOG.LDF", the initial size is 8M, the maximum size is 12M, and the file growth amount is 2M.
  3. Data files and transaction log files are stored in the "SQLDATA" folder of the F drive.
CREATE DATABASE Student2  
ON PRIMARY
 (NAME = Student2_DATA,
  FILENAME = 'F:\SQLDATA\Student2_DATA.MDF' , 
  SIZE = 15MB, 
  MAXSIZE = 20MB, 
  FILEGROWTH = 20%)
LOG ON 
(NAME =Student2_LOG, 
  FILENAME = 'F:\SQLDATA\Student2_LOG.LDF', 
  SIZE = 8MB, 
  MAXSIZE = 12MB, 
  FILEGROWTH = 2MB)



Example: Create a database specifying multiple data files and log files.

  1. The database name is STUDENT3, with 1个18MB和1个20MB的数据文件and 2个10MB的事务日志文件.

  2. The logical names of the data files are STUDENT3_1 and STUDENT3_2, and the physical file names are STUDENT3_1.mdf and STUDENT3_2.ndf.
    The main data file Student3_1 belongs to the PRIMARY file group, and the auxiliary data file Student3_2 belongs to the newly created file group FG1.
    The maximum size of the two data files is infinite and 100MB respectively, and the growth rates are 10% and 1MB respectively.

  3. The logical names of the transaction log files are STUDENT3_LOG1 and STUDENT3_LOG2, the physical file names are STUDENT3_LOG1.ldf and STUDENT3_LOG2.ldf, the maximum size is 50MB, and the file growth rate is 1MB.

  4. Data files and log files are required to be stored in the SQLDATA folder of the F drive.

CREATE  DATABASE  STUDENT3
ON PRIMARY
(NAME='STUDENT3_1',
FILENAME='F:\SQLDATA\STUDENT3_1.mdf',
SIZE=18,
MAXSIZE=unlimited,
FILEGROWTH=10%),

FILEGROUP FG1
(NAME= 'STUDENT3_2',
FILENAME= 'F:\SQLDATA\STUDENT3_2.ndf',
SIZE=20,
MAXSIZE=100,
FILEGROWTH=1)

LOG ON 
(NAME='STUDENT3_LOG1',
FILENAME= 'F:\SQLDATA\STUDENT3_LOG1.ldf',
SIZE=10,
MAXSIZE=50,
FILEGROWTH=1),
(NAME='STUDENT3_LOG2',
FILENAME= 'F:\SQLDATA\STUDENT3_LOG2.ldf',
SIZE=10,
MAXSIZE=50,
filegrowth=1)



2. Database rename (logical name) [sp_renamedb]

sp_renamedb changes the name of the database.

sp_renamedb 'Studnet2' , 'STUDENT_BACK' 

3. Delete the database [DROP DATABASE Student3]

DROP DATABASE Student3

Note: Users can only delete user databases according to their own permissions; cannot delete databases that are currently in use (opening for users to read and write); cannot delete system databases (msdb, model, master, tempdb).



Operations on database "files"

1. Modify database file attributes [ALTER DATABASE database name MODIFY FILE…]

The syntax format is as follows:

ALTER DATABASE 数据库名
MODIFY FILE
(NAME=逻辑文件名,
SIZE=文件大小)

Example: To increase the capacity of the Student2 database, the initial allocation space of the original data file Student2_DATA is 15M, and now the allocated space of Student2_DATA is increased to 20M.

ALTER DATABASE Student2
MODIFY FILE
 (NAME= Student2_DATA,
  SIZE=20MB) 



2. Add a database file [ALTER DATABASE database name ADD FILE…]

insert image description here

[Example] Add the data file
Student2_DATA1 for the database Student2, the initial size is 10M, the maximum is 50M, and the growth rate is 5%.

ALTER DATABASE Student2 
ADD FILE
(NAME = 'Student2_DATA1',
  FILENAME = 'F:\SQLDATA\ Student2_DATA1.ndf',
  SIZE = 10MB,
  MAXSIZE = 50MB,
  FILEGROWTH = 5%)



3. Delete the database file [ALTER DATABASE database name REMOVE FILE xxxxx]

[Example] Delete the data file Student2_DATA1 added in the database Student2

ALTER DATABASE Student2
	REMOVE FILE Student2_DATA1

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324117643&siteId=291194637