Fix “The new name is already in use as a COLUMN name and would cause a duplicate that is not permitted” in SQL Server (Error 15335)

If you’re getting an error that reads something like “Error: The new name ‘c1’ is already in use as a COLUMN name and would cause a duplicate that is not permitted” in SQL Server, it appears that you’re trying to rename a column with a name that already exists in that table.

Basically, there’s already a column of that name in the table.

Continue reading

A Quick Look at the DROP_EXISTING Option in SQL Server

In SQL Server, we can use the DROP_EXISTING option of the CREATE INDEX statement to modify an existing index. It allows us to drop and rebuild an index in one atomic operation. This can be particularly useful when we need to make changes to an index, such as altering its columns, changing its properties, or rebuilding it with different options like fill factor or sort order.

Continue reading

What is User-Defined Integrity?

Maintaining data integrity is critical for ensuring the accuracy, consistency, and reliability of data inside a relational database. Among the various strategies to enforce data integrity, user-defined integrity plays a significant role, especially in customized applications and systems where standard integrity constraints may not suffice.

This article explores the concept of user-defined integrity, its importance, how it is implemented, and scenarios where it can become essential.

Continue reading

What is a One-To-One Relationship?

In relational databases, a one-to-one relationship is a relationship between two tables where each record in the first table corresponds to one and only one record in the second table, and vice versa.

This kind of relationship is less common compared to other relationship types, but it can be useful in certain scenarios. The one-to-many and many-to-many relationships are much more common than the one-to-one relationship in SQL.

Continue reading

Understanding the NOT NULL Constraint in SQL

In SQL, constraints are essential for maintaining data integrity and ensuring the accuracy and reliability of the data stored within a database. One of the most commonly used constraints in SQL databases is the NOT NULL constraint.

In this article, I provide a quick overview of the NOT NULL constraint, its purpose, syntax, usage, and practical examples.

Continue reading

Fix “Cannot create index on view. The function yields nondeterministic results” in SQL Server

If you’re getting an error that reads something like “Cannot create index on view ‘demo.dbo.v1’. The function ‘sysdatetime’ yields nondeterministic results…” and so on, in SQL Server, it looks like you’re trying to create an index on a view that returns nondeterministic results.

Indexes can only be created on views that return deterministic results.

Continue reading

How to View your Current SET Options in SQL Server

SQL Server provides various SET options that control the behavior of our session. These options can impact query execution and results. Also, some features in SQL Server rely on certain SET options to be configured in a certain way (for example indexed views).

Getting the current SET options configuration is not as straightforward as one might think. We need to do a bit of trickery.

Let’s look at how to view our current SET options.

Continue reading

Fix “Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause” in SQL Server (Error 144)

If you’re getting an error that reads something like “Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.” in SQL Server, it looks like you’re trying to use either an aggregate function or a subquery in the GROUP BY clause.

We can’t use aggregates or subqueries in the GROUP BY clause.

To fix this issue, remove any aggregate functions or subqueries from your GROUP BY clause.

Continue reading