首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >SQL change create以更改语法错误

SQL change create以更改语法错误
EN

Stack Overflow用户
提问于 2020-04-30 14:55:57
回答 1查看 33关注 0票数 0

我收到了一个sql代码:

代码语言:javascript
代码运行次数:0
运行
复制
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

但是我们已经有了表dbo.Dokumendid,所以当我将create改为alter时,我会得到错误:

代码语言:javascript
代码运行次数:0
运行
复制
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near '('.

我将其更改为: alter table dbo.Dokumendid alter column error:

代码语言:javascript
代码运行次数:0
运行
复制
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 '('.
EN

回答 1

Stack Overflow用户

发布于 2020-04-30 15:10:42

如果要添加内联外键:

代码语言:javascript
代码运行次数:0
运行
复制
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

此外,您还可以在线添加索引创建(来自SQL Server 2014):

代码语言:javascript
代码运行次数:0
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61517548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档