Computed / calculated / virtual / derived / generated columns in PostgreSQL

Postgres 12 or newer STORED generated columns are introduced with Postgres 12 – as defined in the SQL standard and implemented by some RDBMS including DB2, MySQL, and Oracle. Or the similar “computed columns” of SQL Server. Trivial example: CREATE TABLE tbl ( int1 int , int2 int , product bigint GENERATED ALWAYS AS (int1 … Read more

Find all tables containing column with specified name

Search Tables: SELECT c.name AS ‘ColumnName’ ,(SCHEMA_NAME(t.schema_id) + ‘.’ + t.name) AS ‘TableName’ FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id WHERE c.name LIKE ‘%MyName%’ ORDER BY TableName ,ColumnName; Search Tables and Views: SELECT COLUMN_NAME AS ‘ColumnName’ ,TABLE_NAME AS ‘TableName’ FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE ‘%MyName%’ ORDER BY TableName ,ColumnName;

MySQL procedure vs function, which would I use when?

The most general difference between procedures and functions is that they are invoked differently and for different purposes: A procedure does not return a value. Instead, it is invoked with a CALL statement to perform an operation such as modifying a table or processing retrieved records. A function is invoked within an expression and returns … Read more

Declare a variable in a PostgreSQL query

I accomplished the same goal by using a WITH clause, it’s nowhere near as elegant but can do the same thing. Though for this example it’s really overkill. I also don’t particularly recommend this. WITH myconstants (var1, var2) as ( values (5, ‘foo’) ) SELECT * FROM somewhere, myconstants WHERE something = var1 OR something_else … Read more

SQL JOIN: what is the difference between WHERE clause and ON clause?

They are not the same thing. Consider these queries: SELECT * FROM Orders LEFT JOIN OrderLines ON OrderLines.OrderID=Orders.ID WHERE Orders.ID = 12345 and SELECT * FROM Orders LEFT JOIN OrderLines ON OrderLines.OrderID=Orders.ID AND Orders.ID = 12345 The first will return an order and its lines, if any, for order number 12345. The second will return … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)