Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 630

In relational database design, a primary key can uniquely identify each row in a table. A primary key comprises a single column or a set of columns.

5 votes

Why do primary keys have names of their own?

I would say it's an implementation detail for human readability. Constraint names are optional (in SQL Server at least), but I'd like to have PK_MyTable for MyTable rather than PK_MyTabl___16feb6d8a
gbn's user avatar
  • 70.1k
5 votes
Accepted

performance of multi field primary key or 'contrived' 'semi artificial' key

I'll answer obliquely... The natural key is always the natural key and should be enforced with a unique constraint or index. This is the "primary key" that flows from your modelling phase. The choic …
gbn's user avatar
  • 70.1k
7 votes
Accepted

Is it require to fill all tables inside of the database?

BAD PRACTICE ALERT! You need to delimit each string. Not the whole lot. Note where the single quotes are: ("Insert Into dbo.PrnInf(PrnName, PrnSurName, PrnEgn ) Values ('"+txtPrnName.Text+"', …
gbn's user avatar
  • 70.1k
3 votes
Accepted

Does the order of keys in compound Primary key make any difference?

The order of keys can make a difference depending on your access pattern. Example... You want to find MAX(idPackageVariant) for a singleidHolidayPackage With idHolidayPackage as the second column, …
gbn's user avatar
  • 70.1k
5 votes

Sort order specified in primary key, yet sorting is executed on SELECT

I'm speculating that the SORT is needed because of parallel plan. I base this on some dim and distant blog article: but I found this on MSDN which may or may not justify this So, try with MAXDOP 1 and …
gbn's user avatar
  • 70.1k
11 votes

What are negative keys used for?

If we're on about identity or autonumber columns, the value itself should have no meaning. (sometimes it does, as per SO's chat users nentioned by drachenstern, which I've done before myself) However …
gbn's user avatar
  • 70.1k
4 votes
Accepted

Can I use a Clob as a table key (from Java)?

The simple version would be to generate a hash from the CLOB and use this as a key. This key will fit into the allowed key width for your engine (900 SQL Server, 767 InnoDB, 1000 MyISAM etc). The has …
gbn's user avatar
  • 70.1k
7 votes

Differences between "Unique Key" and "Primary Key"

One major difference Primary key disallows nullable columns Unique key allows nullable columns Otherwise, there isn't much difference...
gbn's user avatar
  • 70.1k
12 votes

Choosing primary keys: Scientific names of species or system-assigned numeric identifiers?

I would use my own identifier. The species name may be unique but - it's too long - it's a string For example, in SQL Server, if used as the clustered primary key it will be used in non-clustered i …
gbn's user avatar
  • 70.1k
14 votes

Should every table have a single-field surrogate/artificial primary key?

"it depends" Yes: Surrogate IDENTITY/AUTONUMBER fields are good when the natural key is wide and non-numeric. Note: this assumes the conflation of "PK" and clustered index that occurs by default in S …
gbn's user avatar
  • 70.1k
35 votes

How to promote an existing index to primary key in PostgreSQL

You'd use ALTER TABLE to add the primary key constraint. In Postgres you can "promote" an index using the "ALTER TABLE .. ADD table_constraint_using_index" form Note, the index need to be unique of c …
gbn's user avatar
  • 70.1k
3 votes

Can I have create an InnoDB table without a PK? If so, would a secondary index be used as th...

Yes, on any RDBMS, if you still have a unique index with non-nullable columns to identify individual rows. The difference between a "unique key" and a "primary key" is a PK does not allow NULLs. NULL …
gbn's user avatar
  • 70.1k
11 votes
Accepted

What are the performance considerations between using a broad PK vs a separate synthetic key...

There is no significant disadvantage using the natural key as the clustered index there are no non-clustered indexes no foreign keys referencing this table (it is a parent row) The downside would …
gbn's user avatar
  • 70.1k
8 votes
Accepted

How can an identity primary key index become fragmented?

Updates on data already there causes rows to be moved and forward pointers added In this test, we can get 65% fragmentation on 115k densely packed rows CREATE TABLE #FragTest ( FragTestID int NO …
gbn's user avatar
  • 70.1k
7 votes
Accepted

Need Help Troubleshooting Sql Server 2005 Deadlock Scenario

Three things leap out: Your DELETE is on the 2nd column (RespondentID) of the current PK which means a scan, not a seek. Pointless ROWLOCK hint Your "UPSERT" pattern is not concurrency safe. The tes …
gbn's user avatar
  • 70.1k