-2

Table details(image)

Given 2 column X and Y. How to create a primary key on the table?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
viv
  • 1
  • 2
  • Hi Viv, Microsoft has some good documentation on that subject you should give a gander: https://learn.microsoft.com/en-us/sql/relational-databases/tables/create-primary-keys?view=sql-server-2017 – Cowthulhu Apr 29 '19 at 17:11
  • 1
    Looking at your data, I'm sure it's just test data, but creating a composite primary key would fail because of the `1 / B` combination, there are 2 rows with the duplicate data. – SS_DBA Apr 29 '19 at 17:18
  • (Obviously) This is a faq. Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. In code questions give a [mcve]--cut & paste & runnable code & desired output & clear specification & explanation. Please [use text, not images/links, for text--including tables & ERDs.](https://meta.stackoverflow.com/q/285551/3404097) PS This is unclear. What PK? – philipxy Apr 29 '19 at 18:24

1 Answers1

0

Easy:

CREATE TABLE SAMPLE_TABLE
(X integer,
Y varchar(1),
PRIMARY KEY (X, Y));

The term you are looking for is composite or compound key. I got this answer by referencing this link and this post, so please research other posts before asking here.

  • This is obviously a duplicate so should be closed & not answered. See [ask], [answer] & the voting arrow mouseover texts. Neither the question nor this answer are 'useful'. – philipxy Apr 29 '19 at 18:25
  • Noted. Thanks seinfeld – KevinThePepper23 Apr 29 '19 at 19:13
  • I don't understand the 2nd sentence of your comment. If it's negative, that is not welcome here. – philipxy Apr 29 '19 at 19:20
  • Nope, my bad. My colleague's name is seinfeld. Thanks phil. I'll note it for next time. The links and the explanation are helpful, but your attitude is not. I ask that you [Be Nice](https://stackoverflow.com/help/behavior) lol. – KevinThePepper23 Apr 29 '19 at 21:27
  • I do not have a bad attitude & I have no idea what you think my attitude is or why. I stated some helpful things. It would be helpful if you just wrote clearly instead of cryptically. – philipxy Apr 29 '19 at 23:38