SQL Server主键的常用操作
时间:2010-10-29 来源:惟恐天下不乱哥
SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME<>'dtproperties'
二、获取指定表的信息
EXEC sp_pkeys @table_name='TableName'
三、获取指定表中主键的详细信息
select o.name as 表名,c.name as 字段名,k.colid as 字段序号,k.keyno as 索引顺序,t.name as 类型
from sysindexes i
join sysindexkeys k on i.id = k.id and i.indid = k.indid
join sysobjects o on i.id = o.id
join syscolumns c on i.id=c.id and k.colid = c.colid
join systypes t on c.xusertype=t.xusertype
where o.xtype = 'U' and o.name='TableName'
and exists(select 1 from sysobjects where xtype = 'PK' and parent_obj=i.id and name = i.name)
order by o.name,k.colid
四、更改当前表格主键
alter table TableName drop constraint PK_Comun_1
alter table TableName add constraint pk_column primary key(pk)
alter table TableName add columnname nvarchar(20) null
注:红色为需要修改的部分