select
table_name,column_name
from
information_schema.key_column_usagewhere table_name
<>
'dtproperties'2
:exec
sp_pkeys
@table_name='
表名'3:
select
o.name
as表名,c.name
as欄位名,k.colid
as欄位序號,k.keyno
as索引順序,t.name
as型別
from
sysindexes i
join
sysindexkeys k
oni.id
=k.id
andi.indid
=k.indid
join sysobjects o
oni.id
=o.id
join syscolumns c
oni.id
=c.id
andk.colid
=c.colid
join systypes t
onc.xusertype
=t.xusertype
where o.xtype ='
u'ando.name='
要查詢的表名
' and
exists
(select
1from
sysobjects
where
xtype ='
pk'andparent_obj
=i.id
andname
=i.name)
order
byo.name,k.colid
獲取所有select表名=
case
when
a.colorder=1
then
d.name
else
''end
, 欄位名
=a.name, 標識=
case
when
columnproperty
(a.id,a.name,
'isidentity')
=1then'√
'else
''end
, 主鍵
=case
when
exists
(select
1from
sysobjects
where
xtype='
pk'andname
in(
select
name
from
sysindexes
where
indid
in(
select
indid
from
sysindexkeys
where
id =
a.id
andcolid
=a.colid
)))
then'√
'else
''end
, 型別
=b.name,
占用位元組數
=a.length, 長度=
columnproperty
(a.id,a.name,
'precision
'),
小數字數
=isnull
(columnproperty
(a.id,a.name,
'scale'),
0),
允許空=
case
when
a.isnullable=1
then'√
'else
''end
, 預設值
=isnull
(e.text
, ''
), 字段說明
=isnull
(g.[
value],
'')
from
syscolumns a
left
join
systypes b
ona.xtype
=b.xusertype
inner
join
sysobjects d
ona.id
=d.id
andd.xtype='
u'andd.name
<>
'dtproperties
'and
d.name ='
要查詢的表名
'left
join
syscomments e
ona.cdefault
=e.id
left
join
sys.extended_properties g
ona.id
=g.major_id
anda.colid
=g.minor_id
andg.name='
ms_description
'order
bya.id,a.colorder
獲取表自增列名稱whereselect
column_name
as標識
from
information_schema.columns
table_name='
表名'andcolumnproperty
(object_id('
表名'),column_name,
'isidentity')
=1
select標識 =
case
when
columnproperty
( a.id,a.name,
'isidentity')
=1then
a.name
else
'null
'end
from
syscolumns a
inner
join
sysobjects d
ona.id
=d.id
andd.xtype='
u'andd.name
<>
'dtproperties
'where
d.name='
testtype
'order
bya.id,a.colorder
獲取字段預設值select
b.text
as欄位預設值
from
syscolumns a
left
join
syscomments b
ona.cdefault
=b.id
where
a.id
=object_id('
表名') and
a.name ='
欄位名稱'
移植表: select * into 庫2.dbo.表2 from 庫1.dbo.表1
資料庫獲取主鍵的SQL
好記憶力 不如爛筆頭 很多時候,要獲得資料庫的表的主鍵,但是一時間又想不起來用什麼,今天就做個筆記,記錄下 1.獲得 mysql 中某個表的主鍵的sql select table name,column name from information schema.key column usage wh...
PostgreSQL資料庫中獲取表主鍵名稱
一 如下表示,要獲取teacher表的主鍵資訊 select pg constraint.conname as pk name,pg attribute.attname as colname,pg type.typname as typename from pg constraint inner j...
資料庫表維護(sql語句)
新增字段 alter table 表名 add 欄位名 nvarchar 50 null 刪除字段 alter table 表名 drop column 欄位名 修改字段 alter table 表名 alter column 欄位名 nvarchar 50 null 重新命名表 access 重新...