How to Add a Filegroup to a SQL Server Database (T-SQL)

At a minimum, a SQL Server database has two operating system files; the data file, and the log file.

The data file holds the actual data in the database, as well as objects such as tables, indexes, stored procedures, and views. The log file contains the information that is required to recover all transactions in the database.

When you create a database using the default options, both of these files are created. These files are created in the primary filegroup. This filegroup contains the primary data file and any secondary files that are not put into other filegroups. So the primary filegroup is the default filegroup (unless it’s changed by using the ALTER DATABASE statement).

When you create multiple data files, you also have the option of grouping them into a user-defined filegroup. This allows you to group files logically into their own filegroup that you created. This filegroup will then exist in addition to the primary filegroup. Database objects will remain will remain within the primary filegroup.

This article demonstrates how to create a user-defined filegroup and add some data files to it.

Continue reading

How to Add a Data File to a SQL Server Database (T-SQL)

In SQL Server, databases store their data and objects (such as tables, views, stored procedures, etc) in files. When you create a database using the default options, one data file and one log file are created. However, you’re not restricted to just one of each file. You can add data files (and log files) to a database at a later date if required.

This article contains examples of using T-SQL to add a data file to an existing database in SQL Server.

Continue reading

How to Remove a Data File from a SQL Server Database (T-SQL)

SQL Server databases store their data and objects in files. Each database has at least one data file (and a log file), but it’s not restricted to just one – it could have many data files. If you ever find yourself in the situation where you need to remove a data file from a database, you’ll need to empty that file first, before removing it.

But don’t worry, emptying a file doesn’t actually delete the data. It simply migrates the file’s data to other files within the same filegroup.

The examples below demonstrate how to empty a data file, then remove it from the database using Transact-SQL.

Continue reading

How to Reduce the Size of a Data File in SQL Server (T-SQL)

In SQL Server, when using Transact-SQL, reducing the size of a data file requires a different syntax to increasing it. To reduce a file size using T-SQL, use the DBCC SHRINKFILE command, and provide the name of the data file along with the size you’d like to reduce it to.

Below are examples of reducing the file size of a data file in SQL Server using T-SQL.

Continue reading

How to Increase the File Size of a Data File in SQL Server (T-SQL)

If you’re using the SQL Server Management Studio GUI, you can increase the size of a data file by right-clicking on the relevant database, selecting Properties, selecting Files under Database Properties, then increasing the value in the Initial Size (MB) column for the applicable data file.

If you’re using Transact-SQL however, you can increase the data file’s size by using the ALTER DATABASE statement with the MODIFY FILE argument. The following example demonstrates how to do this.

Continue reading

How to Change a Database Name in SQL Server using T-SQL

When using SQL Server, if you want to change the name of a database, you can do this by using a GUI (like SSMS) or by using Transact-SQL.

If using a GUI, you can right-click on the database name and click Rename to rename the database (you might need to set the database to single-user mode first).

If you’re doing this using T-SQL, the examples on this page should help you out.

Continue reading

How to Remove a Column in SQL Server using T-SQL

In SQL Server, you can create, modify, and remove database objects using a graphical user interface (like using the GUI tools in SQL Server Management Studio), or you can use Transact-SQL to do the same tasks. In fact, the GUI tools actually use T-SQL behind the scenes to perform these tasks.

When using Transact-SQL, you can remove a column from a table by using the ALTER TABLE statement. This statement allows you to change a table’s definition by specifying exactly what changes you require. In our case, we require the removal of a column.

Continue reading

How to Add a Foreign Key Constraint to an Existing Table in SQL Server (T-SQL)

In database terms, a foreign key is a column that is linked to another table‘s primary key field in a relationship between two tables.

A foreign key is a type of constraint, and so if you want to create a foreign key in SQL Server, you’ll need to create a foreign key constraint.

This article demonstrates how to create a foreign key constraint in SQL Server, using Transact-SQL.

Continue reading

How to Change a Column’s Data Type in SQL Server (T-SQL)

When you create a database table, you specify all columns along with their data types. Once created, there’s not normally any intention of changing these data types. After all, whoever designed the schema would have put a lot of thought into what data type each and every column should accept.

However, we all know that things can change. Despite our best efforts at trying to foresee every possible scenario that may hit our database, sometimes that’s not enough.

So what do we do if we need to use Transact-SQL to change the data type of a column in SQL Server?

We use the ALTER TABLE statement to change it of course.

Continue reading

How to Drop a Constraint in SQL Server (T-SQL)

In SQL Server, a constraint defines rules that data in a database must comply with. For example, you could have a UNIQUE constraint applied to a column to ensure that any value inserted into that column is unique (i.e. no other row shares the same value).

If later on you need to remove that constraint, here’s how to do it using Transact-SQL.

Continue reading