0

I'm using Microsoft SQL Server Management Studio. I am trying to create this database, but I can not figure out what I'm doing wrong with the timestamp; it has to default now.

This is the Create Table query:

create table users(
    id int identity primary key,
    username varchar(255) unique,
    created_at date, TIMESTAMP default)
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 31 '22 at 10:44
  • 1
    `created_at date, TIMESTAMP default` This is not valid tsql - and we cannot know what your actual goal is. As a general suggestion, you should always include any error message your encountered. Unfortunately that message won't help readers here since no one knows your goal. TIMESTAMP is a deprecated datatype and shouldn't be used. But I suggest you leave database design and creation to others until AFTER you master tsql. Then learn the fundamentals of relational database design. – SMor May 31 '22 at 10:46
  • 1
    `timestamp`, in T-SQL, is a (as @SMor mentioned) is deprecated and is a synonym for `rowversion` (which is *not* deprecated); both are a `binary(8)` value and have **nothing** to do with dates and times. If you want a `rowversion` value then the correct syntax is `{Column Name} rowversion}`. If you are actually after some kind of date and time value, then you several [date and time data types](//docs.microsoft.com/en-us/sql/t-sql/data-types/date-and-time-types) to pick from. If you want a `DEFAULT` value, you need to *define* that value too. What are you *actually* trying to achieve here. – Thom A May 31 '22 at 11:54
  • Please see : https://stackoverflow.com/questions/17116334/sql-server-2008-row-insert-and-update-timestamps/17116641#17116641 – granadaCoder May 31 '22 at 13:03

1 Answers1

0

For a column date use default getdate()
for a column datetime use default CURRENT_TIMESTAMP

create table users(
    id int identity primary key,
    username varchar(255) unique,
    created_date date DEFAULT GETDATE(),
    created_date_time datetime default CURRENT_TIMESTAMP)
GO
insert into users ( username) values ('new user');
GO

1 rows affected

select * from users
GO
id | username | created_date | created_date_time      
-: | :------- | :----------- | :----------------------
 1 | new user | 2022-05-31   | 2022-05-31 13:19:41.830

db<>fiddle here