Modifying database alterdatabase in T-SQL

Source: Internet
Author: User
Tags filegroup mssqlserver
Change Database Name (1) Partition (2) sp_renamedbolddbname, newdbname add data file and file group (expand) (1) add data file USEmasterGODECLARE @ data_pathnvarchar (256 ); -- Obtain the storage location of the primary data file. SELECT @

Change database name (1) alter database database_name modify name = new_database_name (2) sp_renamedb olddbname, newdbname add data file and file group (expand) (1) add the data file USE master go declare @ data_path nvarchar (256); -- Obtain the storage location of the master data file SELECT @

Change Database Name

(1)

Alter database database_name

Modify name = new_database_name

(2)

Sp_renamedb olddbname, newdbname

Add data files and file groups (expand)

(1) Add a data file

USE master

GO

DECLARE @ data_path nvarchar (256 );

-- Obtain the storage location of the primary data file

SELECT @ data_path = physical_name

FROM MyDatabase. sys. database_files

WHERE file_id = 1;

SET @ data_path = SUBSTRING (@ data_path, 1, CHARINDEX (LOWER ('mydatabase. mdf '), LOWER (@ data_path)-1 );

-- Add a data file (put it in the directory where the main data file is located)

Go

Alter database MyDatabase

ADD FILE

(

NAME = LogicNameOfDataFile1_1,

FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/DataFile1_1.ndf ',

SIZE = 5 MB,

Max size = 100 MB,

FILEGROWTH = 5 MB

)

(2) Add a log file

USE master

GO

DECLARE @ data_path nvarchar (256 );

-- Obtain the storage location of the primary data file

SELECT @ data_path = physical_name

FROM MyDatabase. sys. database_files

WHERE file_id = 1;

SET @ data_path = SUBSTRING (@ data_path, 1, CHARINDEX (LOWER ('mydatabase. mdf '), LOWER (@ data_path)-1 );

-- Add a data file (put it in the directory where the main data file is located)

Go

Alter database MyDatabase

ADD LOG FILE

(

NAME = LogicNameOfLogFile1_1,

FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/LogFile1_1.ldf ',

SIZE = 2 MB,

MAXSIZE = 50 MB,

FILEGROWTH = 3 MB

)

(3) add files and file groups

USE master

GO

-- Create File Group _ 1

Alter database MyDatabase

Add filegroup UserFG1_1;

GO

DECLARE @ data_path nvarchar (256 );

-- Obtain the storage location of the primary data file

SELECT @ data_path = physical_name

FROM MyDatabase. sys. database_files

WHERE file_id = 1;

SET @ data_path = SUBSTRING (@ data_path, 1, CHARINDEX (LOWER ('mydatabase. mdf '), LOWER (@ data_path)-1 );

-- Add a data file (put it in the directory where the main data file is located)

Go

Alter database MyDatabase

ADD FILE

(

NAME = LogicNameOfDataFile1_2,

FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/DataFile1_2.ndf ',

SIZE = 10 MB,

MAXSIZE = 50 MB,

FILEGROWTH = 5 MB

),

(

NAME = LogicNameOfDataFile1_3,

FILENAME = 'd:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/DATA/DataFile1_3.ndf ',

SIZE = 5 MB,

MAXSIZE = 50 MB,

FILEGROWTH = 2 MB

)

To filegroup UserFG1_1

Note: If you do not specify the file group to which the data file is added, it will be added to the main file group by default.

Change Data Files and file groups

(1) Change the database data file

USE master

GO

Alter database MyDatabase2

MODIFY FILE

(

NAME = LogicNameOfDataFile2,

NEWNAME = newLogicNameOfDataFile2,

FILENAME

= 'C:/Program Files/Microsoft SQL Server/MSSQL10.MSSQLSERVER/MSSQL/DATA/newDataFile2.mdf ',

SIZE = 25 MB,

Max size = 150 MB,

FILEGROWTH = 10 MB

)

(2) Change the log file of the database

USE master

GO

Alter database MyDatabase3

MODIFY FILE

(

NAME = MyDatabase3_log,

FILENAME

= 'C:/Program Files/Microsoft SQL Server/MSSQL10.MSSQLSERVER/MSSQL/DATA/logfile3.ldf ',

SIZE = 10 MB, -- set the initial SIZE

MAXSIZE = 50 MB, -- set the maximum storage space of the file

FILEGROWTH = 5 MB -- set the automatic growth rate

)

(3) change the file group name

USE master

GO

Alter database MyDatabase6

Modify filegroup UserFG6_2 NAME = newUserFG6_2

GO

(4) change the default file group

USE master;

GO

Alter database MyDatabase6

Modify filegroup newUserFG6_2 DEFAULT;

GO

Alter database MyDatabase6

Modify filegroup [PRIMARY] DEFAULT;

GO

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.