一.查旬乙個表有哪些主鍵:
(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) --刪除原主鍵
end2.把所有主鍵設為不能為空
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
sqlserver修改主鍵為自增
使用powerdesigner建立一張表,拷貝建表語句發現id不是自增的,以下是修改語句 alter table user job exe rec drop column id alter table user job exe rec add id int identity 1,1 注 這只適用於剛...
sqlserver 自增字段修改為普通主鍵字段
增加備份字段 alter table tablename add columnnamebak bigint 將主鍵自增字段 賦值到備份字段 update tablenameset columnnamebak columnname 刪除主鍵備份字段約束 alter table tablenamedro...
sql server 定義主鍵
drop table father create table father id int identity 1,1 primary key,name varchar 20 not null,age int not null drop table mother create table mother ...