SQL Server : does NEWID() always gives a unique ID?

Both NEWID() and NEWSEQUENTIALID() give globally unique values of type uniqueidentifier. NEWID() involves random activity, thus the next value is unpredictable, and it’s slower to execute. NEWSEQUENTIALID() doesn’t involve random activity, thus the next generated value can be predicted (not easily!) and executes faster than NEWID(). So, if you’re not concerned about the next value … Read more

Will multiple calls to `now()` in a single PostgreSQL query always give the same result?

The documentation says about now(): now() is a traditional PostgreSQL equivalent to transaction_timestamp() And about transaction_timestamp(): These SQL-standard functions all return values based on the start time of the current transaction So within one SQL statement, now() will always return the same value.

Unit-Testing Databases

There’s no real way to unit test a database other than asserting that the tables exist, contain the expected columns, and have the appropriate constraints. But that’s usually not really worth doing. You don’t typically unit test the database. You usually involve the database in integration tests. You typically use your favourite automated unit testing … Read more

In SQL Server, how can I lock a single row in a way similar to Oracle’s “SELECT FOR UPDATE WAIT”?

You’re probably looking forwith (updlock, holdlock). This will make a select grab an exclusive lock, which is required for updates, instead of a shared lock. The holdlock hint tells SQL Server to keep the lock until the transaction ends. FROM TABLE_ITEM with (updlock, holdlock)

SQL – How to transpose?

MySQL doesn’t support ANSI PIVOT/UNPIVOT syntax, so that leave you to use: SELECT t.userid MAX(CASE WHEN t.fieldname=”Username” THEN t.fieldvalue ELSE NULL END) AS Username, MAX(CASE WHEN t.fieldname=”Password” THEN t.fieldvalue ELSE NULL END) AS Password, MAX(CASE WHEN t.fieldname=”Email Address” THEN t.fieldvalue ELSE NULL END) AS Email FROM TABLE t GROUP BY t.userid As you can see, … Read more

Laravel join with 3 Tables

I believe your join is wrong: $shares = DB::table(‘shares’) ->join(‘users’, ‘users.id’, ‘=’, ‘shares.user_id’) ->join(‘followers’, ‘followers.user_id’, ‘=’, ‘users.id’) ->where(‘followers.follower_id’, ‘=’, 3) ->get(); I also suggest you to name your table as follows instead, it feels a bit more natural to say user has many followers through follows and user has many followees through follows. Example $shares … Read more

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