DROP TABLE IF EXISTS in SQL

In SQL, we can use the DROP TABLE IF EXISTS statement to drop a table only if it exists.

While it may seem obvious that we can only drop a table if it exists (i.e. we can’t drop a table that doesn’t exist), there’s a good reason for using this statement.

The reason we put an IF EXISTS clause into a DROP TABLE statement is to prevent any errors that would occur if the table doesn’t exist.

Read more

PostgreSQL SHOW TABLES Equivalent (psql)

MySQL and MariaDB have a SHOW TABLES statement, which outputs a list of tables and views in a database. PostgreSQL doesn’t have a SHOW TABLES statement, but it does have a command that produces a similar result.

In Postgres, you can use the \dt command to show a list of tables. This is a psql command (psql is the interactive terminal for PostgreSQL).

Read more

SQLite SHOW TABLES Equivalent

SQLite doesn’t have a SHOW TABLES statement like MySQL and MariaDB have, but it does have a similar command.

In SQLite, you can use the .tables command to show a list of tables. You can alternatively use the table_list pragma to do the job.

Read more

SQL Server SHOW TABLES Equivalent

Every now and then I find myself typing SHOW TABLES in SQL Server, expecting to get a list of tables.

That would make perfect sense if I was using MySQL or MariaDB. But SQL Server/T-SQL doesn’t have a SHOW TABLES statement like MySQL or MariaDB, so it never works. And I keep forgetting. But fortunately, SQL Server does have alternatives.

Here are five options for getting a list of tables in SQL Server. These can be used whenever you’re trying to find that elusive SHOW TABLES statement in SQL Server.

Read more

MySQL TABLE Statement

In MySQL, the TABLE statement returns rows and columns of the given table.

The TABLE statement is similar to the SELECT statement, and it can be used as a kind of shorthand version of the SELECT statement.

The TABLE statement was introduced in MySQL 8.0.19.

Read more