The \di
command can be used to list indexes in the current database when using psql
(PostgreSQL’s command-line tool). This meta-command can display all indexes, or just indexes that match certain patterns. Here’s an in-depth explanation of how \di
works, its options, and examples of its various uses.
Quick Query to Identify Unused Indexes in SQL Server
Below is a query that we can use to quickly identify unused indexes in SQL Server. More specifically, it returns indexes that are updated, but not used.
Continue reading4 Ways to Get the Last SQL Server Startup Time using T-SQL
Occasionally we might want to check to see how long SQL Server has been running uninterrupted. For example, how long has SQL Server been running? Or when was the last restart?
Fortunately there are a multitude of ways we can go about this. Some of these methods involve checking the event viewer or going through the SMSS GUI. But here are four ways we can do it with a T-SQL query.
Continue readingA Possible Cause for the 102 Error When Running a Subquery in SQL Server
If you’re running a subquery in SQL Server, but you’re getting error 102 that reads something like “Incorrect syntax near ‘;’“, there could be any number of reasons, because this is a generic error that simply means wrong syntax.
But one possible cause could be that you haven’t declared an alias for the subquery.
Continue reading7 SQL Statement Examples for Beginners
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. Whether you’re just starting your journey in data management or looking to refresh your skills, understanding the basic SQL statements is crucial.
This article will walk you through seven fundamental SQL statement examples that are pretty much standard across most major Relational Database Management Systems (RDBMSs).
Continue readingHow to List all Domains in PostgreSQL
In PostgreSQL, domains are basically data types with optional constraints. We can create them as a kind of user-defined data type, and then reuse them in our columns going forward.
As with any user-created object, we sometimes need to see a list of existing domains in a PostgreSQL database. Listing all domains can be useful for database management, documentation, or troubleshooting.
This article will show you how to retrieve a list of all domains in your PostgreSQL database.
Continue readingHow to Tell Whether a Given Row was Inserted, Updated, or Deleted During a MERGE in SQL Server
If you’re using the MERGE
statement in SQL Server to merge data between tables, you may or may not be aware that the statement gives us the ability to check which rows were affected by the MERGE
operation, as well as how they were affected.
To get this info, we can use the OUTPUT
clause along with the $action
argument.
3 Ways to Find a Table’s Primary Key Constraint Name in SQL Server
Primary keys are fundamental to relational database design, ensuring each row in a table can be uniquely identified. They help to maintain data integrity in our databases.
There are many ways to find the primary key column in SQL Server, but sometimes we might need to identify the name of the primary key constraint itself.
Below are three examples of how we can do this.
Continue readingPossible Reason You’re Getting an Error When Using HANDLER … LAST or PREV in MySQL
If you’re getting an error when specifying LAST
or PREV
for MySQL’s HANDLER
statement, it could be that you’re trying to do a table scan using the index syntax.
While the HANDLER
statement does accept the LAST
and PREV
options, we can only use them with an index.
So to fix this issue, be sure to specify an index when using the LAST
and PREV
options.
Understanding the Various ON DELETE Options in SQL Server Foreign Keys
It’s widely understood among SQL Server developers that foreign keys can prevent us from deleting a row from the parent table if there’s child table that references the key in that row.
But did you know that we can specify a different course of action in such cases?
For example, we could configure our foreign key to delete the child as well. Or we could set it to NULL
, or to its default value.
These options are all available, thanks to the ON DELETE
clause.