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()),
CodePudding user response:
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.
CodePudding user response:
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.)