SQL – IF EXISTS UPDATE ELSE INSERT INTO

Create a UNIQUE constraint on your subs_email column, if one does not already exist: ALTER TABLE subs ADD UNIQUE (subs_email) Use INSERT … ON DUPLICATE KEY UPDATE: INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE subs_name = VALUES(subs_name), subs_birthday = VALUES(subs_birthday) You can use the VALUES(col_name) function in the … Read more

What are practical differences between `REPLACE` and `INSERT … ON DUPLICATE KEY UPDATE` in MySQL?

REPLACE internally performs a delete and then an insert. This can cause problems if you have a foreign key constraint pointing at that row. In this situation the REPLACE could fail or worse: if your foreign key is set to cascade delete, the REPLACE will cause rows from other tables to be deleted. This can … Read more

MySQL Conditional Insert

If your DBMS does not impose limitations on which table you select from when you execute an insert, try: INSERT INTO x_table(instance, user, item) SELECT 919191, 123, 456 FROM dual WHERE NOT EXISTS (SELECT * FROM x_table WHERE user = 123 AND item = 456) In this, dual is a table with one row only … Read more

How to insert current datetime in postgresql insert query [duplicate]

timestamp (or date or time columns) do NOT have “a format”. Any formatting you see is applied by the SQL client you are using. To insert the current time use current_timestamp as documented in the manual: INSERT into “Group” (name,createddate) VALUES (‘Test’, current_timestamp); To display that value in a different format change the configuration of … Read more

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