首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

创建新表时多列为主键该如何做

2012-01-19 
创建新表时多列为主键该怎么做Createtable#temp(Noint,Namechar(10))No,Name为主键[解决办法]Create table

创建新表时多列为主键该怎么做
Create   table   #temp(
  No   int     ,
  Name     char(10)
)
No,Name为主键

[解决办法]
Create table #temp(
No int ,
Name char(10)
Primary Key(No, Name)
)
Insert #temp Select 1, 'A ' --OK
Insert #temp Select 1, 'A ' --Error
Insert #temp Select 1, 'B ' --OK

Select * From #temp
Drop Table #temp
[解决办法]
Create table #temp(
No int ,
Name char(10)
)
go
ALTER TABLE dbo.#temp ADD CONSTRAINT
PK_#temp PRIMARY KEY CLUSTERED
(
No,
Name
) ON [PRIMARY]

[解决办法]
Create table #temp(
No int ,
Name char(10) Primary Key( No,Name )
)
[解决办法]
Create table #temp(
No int ,
Name char(10),
primary key(No,Name))

热点排行