SqlServer 修改主键总结
一.查旬一个表有哪些主键:
(1)EXEC sp_pkeys @table_name='表名'
可以按数据库中表的顺序显示
(2)SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME='表名'
(3)查询数据库中所有表的主键
select o.name as 表名,c.name as 字段名,k.colid as 字段序号,k.keyno 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
where o.xtype = 'U'
and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)
order by o.name,k.colid
(4) exec sp_helpconstraint '表名'
二.查询一个表有哪些外键:
(1)select object_name(a.parent_object_id) 'tables'
from sys.foreign_keys a
where a.referenced_object_id=object_id('表名')
(2)select * from sysforeignkeys 获取表外键
三.查询表结构信息
(1)sp_help N'表名'
(2)SELECT (case when a.colorder=1 then d.name else null end) 表名,
a.colorder 字段序号,a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(case when (SELECT count(*) FROM sysobjects
WHERE (name in (SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then '√' else '' end) 主键,b.name 类型,a.length 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,(case when a.isnullable=1 then '√'else '' end) 允许空,
isnull(e.text,'') 默认值,isnull(g.[value], ' ') AS [说明]
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.class and f.minor_id=0
--WHERE b.name is not null
where d.name='表名' --如果只查询指定表,加上此条件
order by a.id,a.colorder
四.获取表字段信息
(1)SELECT syscolumns.name,systypes.name,syscolumns.isnullable, syscolumns.length
FROM syscolumns, systypes
WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id('表名')
五.查询某个数据库中所有的表名:
(1)SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
六.查询数据库中的所有数据库名
(1)SELECT Name FROM Master..SysDatabases ORDER BY Name
七.重建主键批处理
1.删除主键:
Declare @Pk varChar(100);
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('表名') and xtype='PK';
if @Pk is not null
begin
exec('Alter table abcd Drop '+ @Pk) --删除原主键
end
2.把所有主键设为不能为空
alter table abcd alter column c char(10) not null
3.重建主键:
ALTER Table abcd ADD CONSTRAINT pk_abcd PRIMARY KEY (a, b, c )
查询数据库中没有主键的表名并为其增加主键的方法:
declare @tablename sysname
declare @strsql nchar(500)
declare tableNameCursor cursor for
select b.name from sysobjects b where xtype='U' and b.name not in
(select object_name(a.parent_obj) from sysobjects a where xtype='PK' )
open tableNameCursor
fetch next from tableNameCursor into @tablename
while @@FETCH_STATUS = 0
begin
print @tablename
set @strsql= 'alter table ' + @tablename + ' add primary key (id) '
print @strsql
exec (@strsql)
fetch next from tableNameCursor into @tablename
end
close tableNameCursor
deallocate tableNameCursor
(1)EXEC sp_pkeys @table_name='表名'
可以按数据库中表的顺序显示
(2)SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME='表名'
(3)查询数据库中所有表的主键
select o.name as 表名,c.name as 字段名,k.colid as 字段序号,k.keyno 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
where o.xtype = 'U'
and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)
order by o.name,k.colid
(4) exec sp_helpconstraint '表名'
二.查询一个表有哪些外键:
(1)select object_name(a.parent_object_id) 'tables'
from sys.foreign_keys a
where a.referenced_object_id=object_id('表名')
(2)select * from sysforeignkeys 获取表外键
三.查询表结构信息
(1)sp_help N'表名'
(2)SELECT (case when a.colorder=1 then d.name else null end) 表名,
a.colorder 字段序号,a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(case when (SELECT count(*) FROM sysobjects
WHERE (name in (SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then '√' else '' end) 主键,b.name 类型,a.length 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,(case when a.isnullable=1 then '√'else '' end) 允许空,
isnull(e.text,'') 默认值,isnull(g.[value], ' ') AS [说明]
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.class and f.minor_id=0
--WHERE b.name is not null
where d.name='表名' --如果只查询指定表,加上此条件
order by a.id,a.colorder
四.获取表字段信息
(1)SELECT syscolumns.name,systypes.name,syscolumns.isnullable, syscolumns.length
FROM syscolumns, systypes
WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id('表名')
五.查询某个数据库中所有的表名:
(1)SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
六.查询数据库中的所有数据库名
(1)SELECT Name FROM Master..SysDatabases ORDER BY Name
七.重建主键批处理
1.删除主键:
Declare @Pk varChar(100);
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('表名') and xtype='PK';
if @Pk is not null
begin
exec('Alter table abcd Drop '+ @Pk) --删除原主键
end
2.把所有主键设为不能为空
alter table abcd alter column c char(10) not null
3.重建主键:
ALTER Table abcd ADD CONSTRAINT pk_abcd PRIMARY KEY (a, b, c )
查询数据库中没有主键的表名并为其增加主键的方法:
declare @tablename sysname
declare @strsql nchar(500)
declare tableNameCursor cursor for
select b.name from sysobjects b where xtype='U' and b.name not in
(select object_name(a.parent_obj) from sysobjects a where xtype='PK' )
open tableNameCursor
fetch next from tableNameCursor into @tablename
while @@FETCH_STATUS = 0
begin
print @tablename
set @strsql= 'alter table ' + @tablename + ' add primary key (id) '
print @strsql
exec (@strsql)
fetch next from tableNameCursor into @tablename
end
close tableNameCursor
deallocate tableNameCursor
版权声明:本文为博主原创文章,未经博主允许不得转载。