常用的SQL更改和查询语句
select *,(case when type=1 then
(select typename from TableaType where type=1)
when type=2 then
(select typename from TableaType where type=2)
when type=3 then
(select typename from TableaType where type=3)
else
(select typename from TableaType where type=4)
end
) as TypeTmp from Tablea
=================================
select *,(select typename from tableType where type=t.type) as type1
from tablea t
==================================
--修改表添加主键
ALTER TABLE [tablea] WITH NOCHECK ADD
PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
--修改表添加默认值和唯一约束
ALTER TABLE [tablea] WITH NOCHECK ADD
CONSTRAINT [DF__tableb__TypeN__6346339D] DEFAULT ('') FOR [TypeName],
CONSTRAINT [DF__tableb__TypeR__643A57D6] DEFAULT ('') FOR [TypeRemark],
CONSTRAINT [UQ__tablea__Type] UNIQUE NONCLUSTERED
(
[Type]
) ON [PRIMARY]
GO
--修改系表的Type字段添加外键约束到另一个表
ALTER TABLE [tablec] ADD
CONSTRAINT [FK_tablec_tablea] FOREIGN KEY
(
[Type]
) REFERENCES [tablea] (
[Type]
)
GO
select *,(case when type=1 then
(select typename from TableaType where type=1)
when type=2 then
(select typename from TableaType where type=2)
when type=3 then
(select typename from TableaType where type=3)
else
(select typename from TableaType where type=4)
end
) as TypeTmp from Tablea
=================================
select *,(select typename from tableType where type=t.type) as type1
from tablea t
==================================
--修改表添加主键
ALTER TABLE [tablea] WITH NOCHECK ADD
PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
--修改表添加默认值和唯一约束
ALTER TABLE [tablea] WITH NOCHECK ADD
CONSTRAINT [DF__tableb__TypeN__6346339D] DEFAULT ('') FOR [TypeName],
CONSTRAINT [DF__tableb__TypeR__643A57D6] DEFAULT ('') FOR [TypeRemark],
CONSTRAINT [UQ__tablea__Type] UNIQUE NONCLUSTERED
(
[Type]
) ON [PRIMARY]
GO
--修改系表的Type字段添加外键约束到另一个表
ALTER TABLE [tablec] ADD
CONSTRAINT [FK_tablec_tablea] FOREIGN KEY
(
[Type]
) REFERENCES [tablea] (
[Type]
)
GO