How to Find the Default File Location for Data Files and Log Files in SQL Server

Any time you create a database in SQL Server, two files are created. One is the data file, and the other is the transaction log file.

The location of these files will depend on whether or not you explicitly specify a location for these files when you create the database. If not, they will be created in the default location.

You can find the default location with the following code:

SELECT
  SERVERPROPERTY('InstanceDefaultDataPath') AS 'Data Files',
  SERVERPROPERTY('InstanceDefaultLogPath') AS 'Log Files'

Continue reading

How to Insert Values into an IDENTITY Column in SQL Server

If you’ve ever tried to insert values into an identity column in SQL Server, you might’ve seen an error like this:

Cannot insert explicit value for identity column in table ‘Artists’ when IDENTITY_INSERT is set to OFF.

This is normal. An identity column is there for a reason. It automatically populates the column with an incrementing value for each row that’s inserted. Therefore there’s no need for you to insert a value into that column.

However, sometimes you do need to insert a value into an identity column. For example, you could be populating the database with data that needs to retain its own identity values. If this is the case, you’ll need to override the IDENTITY property. Here’s how.

Continue reading

T-SQL vs SQL

Here’s a quick overview of the difference between SQL and T-SQL (Transact-SQL). Not so much the differences as such, but more an explanation of T-SQL and where it sits in relation to SQL.

If you’ve done any database development or administration, you’re probably familiar with SQL. SQL, which stands for Structured Query Language, is a standard query language for working with databases. Most of the major relational database management systems such as MySQL, Oracle, SQL Server, PostgreSQL, etc support SQL in one way or another.

However, while the SQL standard provides clear specifications, it also allows for database vendors to add their own extensions. This allows vendors to provide extra features and functionality for their customers that might not be offered by their competitors.

This is where T-SQL comes in.

Continue reading