-2

how can i replace columns with those in transact sql? I only have this code this way. I could do it directly in sms but I don't understand some things in this code so I prefer to do it directly in transact to be safer. For example I can make an Id column with int but I don't understand the "Identity" and (1,1)... the get date I have to put it where... so here it is Thanks

    [Id]        INT          IDENTITY (1, 1) NOT NULL,
 [DateCreated] DATETIMEOFFSET NOT NULL DEFAULT (getdate()),

  • identidy tell the sql server that it should increase the the id when you insert a new row. you date column will add the actual date if don't insert one in the insert clause – nbk Nov 18 '22 at 14:43

2 Answers2

1

These two fields (or columns) contain auto-generated data. So, let's say you have 3 fields; ID, DateCreated and Username. You will only ever enter data for Username. ID will auto-generate sequential numbers (the "(1,1)" means, "Begin with the number 1, add 1 to the previous number for each new record), and DateCreated will automatically fill with the date you add the new record.

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
  • Thank you for your answer but suddenly how can I implement that whether in transact sql or ssms? –  Nov 18 '22 at 14:34
  • @AdamD - I'm not sure what you mean by "implement that". What is it that you're trying to do? Script a table? Select data? – Johnny Bones Nov 18 '22 at 14:41
  • In fact I already have your columns Id, created and I want to replace them with those I showed you –  Nov 18 '22 at 15:02
  • @AdamD - Are you trying to do something like this? https://stackoverflow.com/questions/626899/how-do-you-change-the-datatype-of-a-column-in-sql-server – Johnny Bones Nov 18 '22 at 15:06
  • I have syntax errors for the identity keyword and the first 1 in (1,1) –  Nov 18 '22 at 15:22
  • How about this: https://stackoverflow.com/a/1049305/2174085 – Johnny Bones Nov 18 '22 at 15:27
0

The IDENTITY(1,1) creates an column that automatically increases based on the arguments. With the (1,1) the value of the column starts at 1 (first argument) and increases by 1 (second argument) for each new record (with a caveat or two).

For the rest of the question, what? You want to replace columns. What are you trying to replace? The DateCreated column looks fine. For both Id and DateCreated, they are tagged NOT NULL but with the IDENTITY and DEFAULT constraint, the columns will be automatically populated so you don't actually have to provide data for either column when doing an INSERT. You'll probably want to add another column that describes the thing you are inserting (i.e. Name, Description, etc.)

Tim Jarosz
  • 1,133
  • 1
  • 8
  • 15