SQL更改创建以更改语法错误
我收到了一个sql代码:SQL更改创建以更改语法错误,sql,sql-server,create-table,alter,Sql,Sql Server,Create Table,Alter,我收到了一个sql代码: 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 - leping
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时,会出现错误:
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.dokumenid alter列
错误:
如果要内联添加外键,请执行以下操作:
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):
您可以在CREATETABLE语句上面使用以下代码:DROP table IF EXISTS dbo.Dokumendid Go我认为您正在尝试向现有表添加其他列,否则您可以使用alter table add Column语句将列添加到现有表中。
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
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