MS SQL系統資料表內容

2021-09-05 20:18:44 字數 3164 閱讀 6413

1.今天聽到有人問:怎麼用sql語句取指定列的資料型別,覺得很無聊,但也許是我還不知道有什麼用的原因吧.

查了一下,這些東西都是存於每乙個資料庫的syscolumns表裡面得,name就是列名,xtype就是資料型別,但是這個xtype是數字的,下面是數字和資料型別對應的關係;

xtype=34 'image'

xtype=35 'text'

xtype=36 'uniqueidentifier'

xtype=48 'tinyint'

xtype=52 'smallint'

xtype=56 'int'

xtype=58 'smalldatetime'

xtype=59 'real'

xtype=60 'money'

xtype=61 'datetime'

xtype=62 'float'

xtype=98 'sql_variant'

xtype=99 'ntext'

xtype=104 'bit'

xtype=106 'decimal'

xtype=108 'numeric'

xtype=122 'smallmoney'

xtype=127 'bigint'

xtype=165 'varbinary'

xtype=167 'varchar'

xtype=173 'binary'

xtype=175 'char'

xtype=189 'timestamp'

xtype=231 'nvarchar'

xtype=239 'nchar'

xtype=241 'xml'

xtype=231 'sysname'

2.經常我們要查詢表的索引,約束,相關性,觸發器的屬性,那麼要知道sysobjects這個表的字段的意思,那麼不管要查什麼都沒有問題!

sysobjects:sql-server的每個資料庫內都有此系統表,它存放該資料庫內建立的所有物件,如約束、預設值、日誌、規則、儲存過程等,每個物件在表中佔一行。以下是此系統表的欄位名稱和相關說明。

name,id,xtype,uid,status:分別是物件名,物件id,物件型別,所有者物件的使用者id,物件狀態。

物件型別(xtype)。可以是下列物件型別中的一種:

c = check 約束

d = 預設值或 default 約束

f = foreign key 約束

l = 日誌

fn = 標量函式

if = 內嵌表函式

p = 儲存過程

pk = primary key 約束(型別是 k)

rf = 複製篩選儲存過程

s = 系統表

tf = 表函式

tr = 觸發器

u = 使用者表

uq = unique 約束(型別是 k)

v = 檢視

x = 擴充套件儲存過程

當xtype='u' and status>0代表是使用者建立的表,物件名就是表名,物件id就是表的id值。

用: select * from misa.dbo.sysobjects where xtype='u'and status>0 就可以列出庫misa中所有的使用者建立的表名。

select * from sysobjects where parent_obj = object_id( 'cs') and xtype='tr'

列出表cs的所有屬性,上面是trigger!

object_id

傳回資料庫物件識別碼

語法object_id ( 'object' )

use master

select object_id('pubs..authors')

以下為結果集:

-----------

1977058079  

(1 row(s) affected)

object_name

傳回資料庫物件的名稱。

語法object_name ( object_id )

use pubs

select table_catalog, table_name

from information_schema.tables

where table_name = object_name(1977058079)

以下為結果集:

table_catalog                  table_name  

------------------------------ --------------

pubs                           authors      

(1 row(s) affected)

objectproperty

傳回有關目前資料庫中物件的資訊。

語法objectproperty ( id , property )

if objectproperty ( object_id('authors'),'istable') = 1

print 'authors is a table'

col_length

傳回定義的資料行長度(以位元組為單位)。

語法col_length ( 'table' , 'column' )

use pubs

gocreate table t1

( c1 varchar (40), c2 nvarchar (40))

go以下為結果集:

select col_length('t1','c1')as 'varchar',

col_length('t1','c2')as 'nvarchar'

godrop table t1

以下為結果集。

varchar     nvarchar

40          80

col_name

傳回提供對應的資料表識別碼與資料行識別碼的資料庫資料行的名稱。

語法col_name ( table_id , column_id )

use northwind

set nocount off

select col_name(object_id('employees'), 1)

以下為結果集:

employeeid

操作MSSQL資料表

表是資料庫的主要物件,也是資料庫其他物件的基礎。表分為永久性表和臨時性表。通過sql語句可實現建立 修改和刪除表,以及查詢表的資料。我的理解 列對應表的結構,行對應表的資料。建立表需要使用create關鍵字 create table student uid varchar 20 not null n...

MSSQL加密資料表 sql2005

建立資料庫主秘鑰 use adb go create master key encryption by password p8ssw0rd go 建立存放加密資料的表 create table dbo.sectable id int identity primary key,data nvarcha...

學生管理系統資料表

學生表 tb student 欄位名字段型別 字段說明 是否為空 其他id int學生表主鍵否自增 name char 32 學生姓名 否innerno int班內序號 否pwd char 32 密碼否 gradeid int年級 外來鍵 否classid int班級 外來鍵 否 char 32 性...