0

I was sent a sql code:

create table dbo.Dokumendid(
  id               int identity not Null primary key,
  idPersonal       int not Null references dbo.Personal on delete cascade,
  Liik             char(1) Not Null,                        -- Liik: L - lepingud, K - koolitused, T - tervisetõendid    
  FailiNimetus     nvarchar(200) not null,                  -- faili nimetus
  LaadimiseKpv     smalldatetime null,                      -- laadimise kpv
  Fail             varbinary(max) null,                     -- fail
  Markus           nvarchar(max) Null,                      -- märkus
  dtCreated        datetime default GetDate() Null,
  UserNameCreated  nvarchar(50) null,
  dtUpdated        datetime  null,
  UserNameUpdated  nvarchar(50) null
)
Go
create index IX_Personal on dbo.Dokumendid(idPersonal) on INDEXES
Go

But we already have table dbo.Dokumendid so when I change create to alter I get error:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near '('.

I change it to: alter table dbo.Dokumendid alter column error:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'identity'.
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near '('.
2
  • learn.microsoft.com/en-us/sql/t-sql/statements/… Commented Apr 30, 2020 at 7:04
  • You Can use this Code Above the Create table Statement: DROP TABLE IF EXISTS dbo.Dokumendid Go I think you are trying to add an additional Columns to an Existing Table Else you can use alter Table Add Column Statement to add the Columns to the Existing Table. Commented Apr 30, 2020 at 7:08

1 Answer 1

1

If you want to add the foreign key inline:

create table dbo.Dokumendid(
  id               int identity not Null primary key,
  idPersonal       int not Null, --references dbo.Personal on delete cascade,
  Liik             char(1) Not Null,                        -- Liik: L - lepingud, K - koolitused, T - tervisetõendid    
  FailiNimetus     nvarchar(200) not null,                  -- faili nimetus
  LaadimiseKpv     smalldatetime null,                      -- laadimise kpv
  Fail             varbinary(max) null,                     -- fail
  Markus           nvarchar(max) Null,                      -- märkus
  dtCreated        datetime default GetDate() Null,
  UserNameCreated  nvarchar(50) null,
  dtUpdated        datetime  null,
  UserNameUpdated  nvarchar(50) null,
  CONSTRAINT FK_Dokumendid_idPersonal FOREIGN KEY (idPersonal) REFERENCES dbo.Personal (idPersonal) on delete cascade
)
Go

Also, you can add the index creation online, too (from SQL Server 2014):

create table dbo.Dokumendid(
  id               int identity not Null primary key,
  ...
  CONSTRAINT FK_Dokumendid_idPersonal FOREIGN KEY (idPersonal) REFERENCES dbo.Personal (idPersonal) on delete cascade,
  index IX_Personal(idPersonal)
)
Go
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.