When creating a foreign key constraint in SQL Server, we have the option of including ON DELETE SET NULL
in the definition. When we use this option in a foreign key, it tells SQL Server to automatically set the foreign key column values to NULL
in the child table when the corresponding primary key record in the parent table is deleted.
Author: Ian
Fix Error 2714 “There is already an object named ‘…’ in the database” in SQL Server
If you’re getting SQL Server error 2714 that reads something like “There is already an object named ‘Actors’ in the database” it seems that you’re trying to create an object that already exists.
It could be that you didn’t know that the object had already been created. Or it could be that you’re trying to create a different object, but you’re inadvertently using the same name that another object has. Or it could be that you want both objects to have the same name, but a different schema. In this case, perhaps you inadvertently omitted the schema name from the object.
Continue readingUnderstanding PostgreSQL’s FILTER Clause
When using aggregation functions in PostgreSQL, we have the option of including a FILTER
clause. This clause allows us to narrow down – or filter – the rows that are fed to the aggregate function.
This can be be a handy way to provide functionality that we might otherwise need to use a CASE
statement or perhaps a CTE.
In this article, we’ll take a look at PostgreSQL’s FILTER
clause, and see how it can simplify our SQL queries by replacing CASE
statements with more concise code.
What is Entity Integrity?
When working with relational databases, data accuracy and consistency are paramount. When we talk about maintaining the integrity of the data within a relational database, several types of integrity come into play. One of the most fundamental is entity integrity.
This concept is crucial for ensuring that each record within a database table is uniquely identifiable, which in turn supports the reliability of the data. In this article, we will delve into what entity integrity is, why it matters, and how it is implemented in practice. We’ll also look at an example to clarify the concept further.
Continue reading“Incorrect syntax” Error When Creating a Filtered Index in SQL Server due to an Unsupported Operator
If you’re getting the “Incorrect syntax…” error in SQL Server when trying to create a filtered index, it could be that you’re using an unsupported operator.
Filtered indexes in SQL Server only support simple comparison operators. If you use an unsupported operator or expression, you’ll likely get the dreaded “Incorrect syntax” error.
To fix, either use a supported operator, or create a view.
Continue readingSome Handy T-SQL String Manipulation Techniques
Transact-SQL (T-SQL) offers a nice set of functions and methods for manipulating strings. Let’s look at some handy techniques that can help us work more efficiently with string data in SQL Server.
Continue readingHow to Identify All Temporal Tables in Your SQL Server Database
Temporal tables can be a powerful way to track historical data changes over time in our SQL Server databases. When we’re working with a database that uses temporal tables, we may need to identify all such tables quickly. Or we may simply want to check to see if it has any temporal tables.
Either way, the following SQL queries can be used to return a list of all temporal tables in the database.
Continue readingFix Error “cannot truncate a table referenced in a foreign key constraint” in PostgreSQL
When attempting to truncate a table in PostgreSQL, you might encounter the error “cannot truncate a table referenced in a foreign key constraint“. This is the default behaviour for the TRUNCATE
statement whenever you try to truncate a table that is referenced by another table through a foreign key relationship.
If you want to truncate both tables, you can use the CASCADE
option of the TRUNCATE
statement. Alternatively, you could truncate both tables at the same time.
2 Ways to Delete Rows from Parent and Child Tables in SQL Server when there’s a Foreign Key Relationship
When working with relational databases, it’s common to encounter situations where you need to delete data from both parent and child tables that are linked by foreign key relationships. This article explores two methods for accomplishing this task in SQL Server, providing detailed examples and explanations for each approach.
Continue readingUsing ON DELETE CASCADE When Creating a Foreign Key in SQL Server
By default, if we try to delete a row in the parent table of a referential relationship in SQL Server, we’ll get an error. That’s because the default action is NO ACTION
. This means that the delete doesn’t happen, and an error is raised.
But we’re not quite doomed yet. We can use the ON DELETE CASCADE
option to ensure that the delete operation does happen, and that no error is returned. This option automatically deletes related records in the child table when a record in the parent table is deleted.
If we’re going to use this option, we need to define it when creating the foreign key. That’s because ON DELETE CASCADE
is an optional argument that we can provide when creating the foreign key.