NOT IN vs NOT EXISTS

I always default to NOT EXISTS. The execution plans may be the same at the moment but if either column is altered in the future to allow NULLs the NOT IN version will need to do more work (even if no NULLs are actually present in the data) and the semantics of NOT IN if … Read more

SQLite – UPSERT *not* INSERT or REPLACE

Assuming three columns in the table: ID, NAME, ROLE BAD: This will insert or replace all columns with new values for ID=1: INSERT OR REPLACE INTO Employee (id, name, role) VALUES (1, ‘John Foo’, ‘CEO’); BAD: This will insert or replace 2 of the columns… the NAME column will be set to NULL or the … Read more

Is it possible to insert multiple rows at a time in an SQLite database?

update As BrianCampbell points out here, SQLite 3.7.11 and above now supports the simpler syntax of the original post. However, the approach shown is still appropriate if you want maximum compatibility across legacy databases. original answer If I had privileges, I would bump river’s reply: You can insert multiple rows in SQLite, you just need … Read more

How do I restore a dump file from mysqldump?

If the database you want to restore doesn’t already exist, you need to create it first. On the command-line, if you’re in the same directory that contains the dumped file, use these commands (with appropriate substitutions): C:\> mysql -u root -p mysql> create database mydb; mysql> use mydb; mysql> source db_backup.dump;

Insert text with single quotes in PostgreSQL

String literals Escaping single quotes ‘ by doubling them up → ” is the standard way and works of course: ‘user’s log’ — incorrect syntax (unbalanced quote) ‘user”s log’ Plain single quotes (ASCII / UTF-8 code 39), mind you, not backticks `, which have no special purpose in Postgres (unlike certain other RDBMS) and not … Read more

Fetch the rows which have the Max value for a column for each distinct value of another column

I see many people use subqueries or else window functions to do this, but I often do this kind of query without subqueries in the following way. It uses plain, standard SQL so it should work in any brand of RDBMS. SELECT t1.* FROM mytable t1 LEFT OUTER JOIN mytable t2 ON (t1.UserId = t2.UserId … Read more

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