What is a One-To-Many Relationship?

When working with relational databases, we tend to create a lot of relationships between tables. But not all relationships are created equal – there are different types of relationships. The one-to-many relationship is probably the most common type of relationship in relational database design.

A one-to-many relationship is a type of association between two tables where a record in one table (the “one” side) can be related to multiple records in another table (the “many” side). For example, in an e-commerce database, a single customer can place many orders. In this scenario, the Customers table would have a one-to-many relationship with the Orders table.

Continue reading

Fix “Cannot create index on view because its select list does not include a proper use of COUNT_BIG” in SQL Server (Error 10138)

If you’re getting an error that reads something like “Cannot create index on view … because its select list does not include a proper use of COUNT_BIG…” and so on in SQL Server, it could be that you’re trying to create an index on a view that uses the GROUP BY clause, but doesn’t have the COUNT_BIG() function.

If the view contains a GROUP BY clause, then it must also have COUNT_BIG(*).

To fix this issue, try adding COUNT_BIG(*) to your SELECT list.

Continue reading

What is a Many-To-Many Relationship?

In SQL, a many-to-many relationship occurs when multiple records in one table can be associated with multiple records in another table.

To represent this type of relationship, a third table—often referred to as a “junction table” or “associative entity”—is used. This junction table typically contains foreign keys that reference the primary keys of the two related tables, allowing for the connection between multiple records.

Continue reading

Explanation of ON DELETE NO ACTION in SQL Server

In SQL Server, ON DELETE NO ACTION is an option that we can apply to foreign key constraints to prevent the deletion of a row in the parent table if there are related rows in the child table.

Unlike ON DELETE CASCADE, which would delete the related rows in the child table, NO ACTION enforces that if a deletion would result in orphaned records in the child table, the deletion operation is prohibited, and an error is raised.

Continue reading

Fix Error 7999 “Could not find any index…” in SQL Server

If you’re getting SQL Server error 7999 that reads something like “Could not find any index named ‘IX_LastName’ for table ‘Employees’“, it appears that you’re trying to create an index with the DROP_EXISTING argument ON, but there is no existing index of that name.

When we set the DROP_EXISTING argument to ON, SQL Server tries to look for the index to drop before it recreates it with the new definition. If it doesn’t exist, then we get the error.

Continue reading

Compound Keys Explained

In SQL databases, a compound key is a type of primary key that consists of two or more columns combined to uniquely identify each row in a table. The key columns are used together as a single unit to ensure uniqueness.

Some within the SQL community define compound keys as composite primary keys comprising of foreign keys from other tables, so there doesn’t seem to be an agreed consensus on the precise definition.

With that in mind, let’s explore these definitions of compound keys.

Continue reading

Fix “Incorrect syntax near the keyword ‘Order'” in SQL Server (Error 156)

If you’re getting an error in SQL Server that reads “Incorrect syntax near the keyword ‘Order’“, it could be because you’re using the word Order (or another reserved keyword) in your SQL code.

The 156 error itself merely indicates a syntax error, so there could be a multitude of reasons you might be getting this error. But when it refers to the keyword 'Order', this could suggest that you’re trying to use the word Order as an identifier (such as a column name, table name, etc).

Continue reading