开发者

Could someone help me understand and convert this MySql statement into Postgresql?

开发者 https://www.devze.com 2023-04-01 03:50 出处:网络
so the code I am working on has this statement executed by PHP (note:This is taken from the PostgreSQL log file so it doesn\'t include any PHP stuff):

so the code I am working on has this statement executed by PHP (note:This is taken from the PostgreSQL log file so it doesn't include any PHP stuff):

CREATE temporary table IF NOT EXIST temp tablename(id int primary key,
shared int default 0) replace select 1, userid as id 
from tablefoo where sharedid = 1337

I don't quite understand what's going on here exactly, I know what a temporary table is, and I can quite accurately guestimate what IF NOT EXIST does, but what is replace doing here? I know replace is like insert but it replaces stuff as well, but in this case, nothing is specified for it to replace with, so does it just replace something with nothing and why the Select 1, I know that pretty much just tells you if your table has rows or something, but what is the point of using it here?

After some research, I found that IF NOT EXIST and replace do not exist in PostgreSQL. Most online sources suggest that SQL functions be used to replace them.

Should 开发者_运维知识库I use an SQL function to emulate IF NOT EXIST? If so, what would I write (sorry, I am pretty new to SQL) or should I just use a PHP function. What about replace?

Sorry for the trouble, thanks for your time, oh and if you guys aren't busy or anything, you could also tell me about how to emulate "ignore", my current solution involves arbitrarily removing it.


Many uses in MySQL for temporary tables can be replaced in PostgreSQL with common table expressions or ordinary subselects.

WITH someCTE AS (
    SELECT 
     ...
) SELECT/UPDATE/DELETE ... WHERE sometable.column = someCTE.another_column;


Look into CREATE TABLE documentation. Temporary tables are just as name suggests not permanent:

If specified, the table is created as a temporary table. Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction (see ON COMMIT below). Existing permanent tables with the same name are not visible to the current session while the temporary table exists, unless they are referenced with schema-qualified names. Any indexes created on a temporary table are automatically temporary as well.

In particular temp tables are stored in diffrent (non-public) schema, e.g.:

=> Create Temporary Table someTempTable (value integer);
CREATE TABLE
=> \dt someTempTable
              List of relations
  Schema   |     Name      | Type  |  Owner
-----------+---------------+-------+----------
 pg_temp_2 | sometemptable | table | postgres
(1 row)

PostgreSQL doesn't have IF NOT EXISTS like in MySQL's CREATE TABLE, so you can't use it. If you want to create some table you need to firstly drop existing one (if it exists). Fortunately you could use SQL command DROP TABLE IF EXISTS to handle this:

=> Drop Table If Exists someTempTable;
DROP TABLE
=> Drop Table If Exists someTempTable;
NOTICE:  table "sometemptable" does not exist, skipping
DROP TABLE
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号