How to choose the clustered index in SQL Server?

According to The Queen Of Indexing – Kimberly Tripp – what she looks for in a clustered index is primarily: Unique Narrow Static And if you can also guarantee: Ever-increasing pattern then you’re pretty close to having your ideal clustering key! Check out her entire blog post here, and another really interesting one about clustering … Read more

Store and reuse value returned by INSERT … RETURNING

… that can be used to insert values into other tables? You can even do that in a single SQL statement using a data-modifying CTE: WITH ins1 AS ( INSERT INTO tbl1(txt) VALUES (‘foo’) RETURNING tbl1_id ) INSERT INTO tbl2(tbl1_id) SELECT * FROM ins1 Requires PostgreSQL 9.1 or later. db<>fiddle here (Postgres 13) Old sqlfiddle … Read more

Retrieve Oracle last inserted IDENTITY

Well. Oracle uses sequences and default values for IDENTITY functionality in 12c. Therefore you need to know about sequences for your question. First create a test identity table. CREATE TABLE IDENTITY_TEST_TABLE ( ID NUMBER GENERATED ALWAYS AS IDENTITY , NAME VARCHAR2(30 BYTE) ); First, lets find your sequence name that is created with this identity … Read more

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

It is not possible. Relations in EF follows exactly same rules as in the database. It means that principal table must have unique identifier which is referenced by dependent table. In case of database the identifier can be either primary key or unique column(s) of principal table. Otherwise it is not valid relation. Entity framework … Read more

sql swap primary key values

Let’s for the sake of simplicity assume you have two records id name ——— 1 john id name ——— 2 jim both from table t (but they can come from different tables) You could do UPDATE t, t as t2 SET t.id = t2.id, t2.id = t.id WHERE t.id = 1 AND t2.id = 2 … Read more