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

How to Add a New Column to an Existing Table in SQL Server (T-SQL)

When you create a table in SQL Server using T-SQL, you specify all the columns for that table, along with their data types, any constraints, etc.

But what happens if one day you decide to add a new column to that table? How do you add the new column to the existing table without creating the table again? After all, dropping the table and starting again is usually not an option, as the table will already contain data, and you probably don’t want to have to backup all that data and re-insert it after dropping and creating the table.

The answer is: The ALTER TABLE statement.

Continue reading

How to Add a DEFAULT Constraint to an Existing Column in SQL Server

When using SQL Server, sometimes you need to modify an existing table. For the purposes of this article, say you want to add a DEFAULT constraint to an existing column.

To add a DEFAULT constraint to an existing column, use the ALTER TABLE statement and specify the column and the specific constraint that you want to apply.

Continue reading

CONVERT() vs TRY_CONVERT in SQL Server: What’s the Difference?

You might have noticed that T-SQL includes both a CONVERT() function and a TRY_CONVERT() function that you can use in SQL Server to perform conversions between data types. But if you’re scratching your head over what the difference is between these two functions, read on!

The difference between CONVERT() and TRY_CONVERT() is in the way they handle data types that can’t be converted. One throws an error, while the other returns null. The following examples demonstrate this.

Continue reading