Let’s dive into subqueries for a moment. Specifically, how to use a SQL subquery in the SELECT
list. It’s a handy little trick that can help us fetch related data without resorting to complex joins.
Tag: how to
How to Enable a Primary Key in SQL Server
If you’ve got a primary key in SQL Server that you need to enable, you’ll need to enable its index. The index is how we enable and disable primary key constraints in SQL Server.
More specifically, we enable a primary key index (and other indexes) by rebuilding them.
Below is an example of enabling a primary key in SQL Server.
Continue readingFix Error 1913 “The operation failed because an index or statistics with name … already exists on table” in SQL Server
If you’re getting SQL Server error 1913 that reads something like “The operation failed because an index or statistics with name ‘IX_Employees_HireDate’ already exists on table ‘Employees’“, it appears that you’re trying to create an index with the same name of one that already exists.
In SQL Server, index names must be unique within a table or view, although they don’t have to be unique within a database. Also, index names must follow the rules of identifiers.
Continue readingHow to Update an Array Slice in PostgreSQL
When we update an array in PostgreSQL, we can update one specific element or we can update a whole slice (a range of elements).
To update a slice, we use a syntax that references the lower and upper bounds of the slice, separated by a colon. We also provide the new values that will be assigned to each element within that slice.
Continue readingCreate an ENUM Type in PostgreSQL
Some RDBMSs support an enum
type, which comprise a static, ordered set of values. Also known as enumerated types, enum
types can be handy for making a column accept just a small set of values, such as days in the week, predefined clothing sizes, or any number of other preset values.
In PostgreSQL, if we want to create a table that uses an enum
type, we need to create the enum
type first, then apply it against the table. This is a bit different to other RDBMSs such as MySQL, where we don’t need to create the enum
type first.
Fix Error “The configuration option ‘fill factor’ does not exist, or it may be an advanced option” in SQL Server
If you’re getting an error that reads “The configuration option ‘fill factor’ does not exist, or it may be an advanced option” in SQL Server, it appears that you’re trying to view or set the default fill factor
configuration option.
In SQL Server, fill factor
is considered an advanced configuration option. By default, advanced options aren’t available for viewing and changing. However, we can use the following technique to make them available.
5 Ways to Return UNIQUE Constraints in SQL Server
In SQL Server, we can use UNIQUE
constraints to ensure that a column (or columns) contain only unique values. When we have a UNIQUE
constraint against a column, the system will prevent any duplicate values are entered into that column.
Sometimes we need to return a list of UNIQUE
constraints, so that we simply know what we’re working with. Other times we might want to create scripts for all of our UNIQUE
constraints so that we can recreate the constraints later.
Regardless of the reason, here are five methods for returning UNIQUE
constraints in a SQL Server database.
Fix Error 3723 “An explicit DROP INDEX is not allowed on index” in SQL Server
If you’re getting SQL Server error 3723 that reads “An explicit DROP INDEX is not allowed on index…” it’s probably because you’re trying to drop a PRIMARY KEY
or UNIQUE
constraint by dropping its index.
How to Extract Fields from Composite Values in PostgreSQL
In PostgreSQL, a composite value is stored as a comma separated list of values, surrounded by parentheses.
When we have a table that contains composite values, we can extract individual fields by using dot notation to refer to the specific field that we want to extract.
Continue readingPossible Reason for Error 1914 “object is not a user table or view” in SQL Server
If you’re getting an error that reads something like “Index cannot be created on object ‘Customers’ because the object is not a user table or view” it could be that you’re trying to create an index, but there’s also a synonym of the same name (but in a different schema). It would appear that SQL Server thinks that you’re trying to create the index on the synonym instead of the table or view of the same name.
Continue reading