SqlServer創(chuàng)建數(shù)據(jù)表描述及列描述信息
Intro
Q: 為什么要?jiǎng)?chuàng)建描述信息?
A: 鼠標(biāo)懸停在對(duì)應(yīng)表和列上時(shí),會(huì)出現(xiàn)描述信息,可以提高工作的效率,借助工具我們根據(jù)數(shù)據(jù)表可以生成Model,可以將描述信息。


添加描述語(yǔ)法
添加描述存儲(chǔ)過(guò)程【sp_addextendedproperty】語(yǔ)法
sp_addextendedproperty
[ @name = ] { 'property_name' }
[ , [ @value = ] { 'value' }
[ , [ @level0type = ] { 'level0_object_type' }
, [ @level0name = ] { 'level0_object_name' }
[ , [ @level1type = ] { 'level1_object_type' }
, [ @level1name = ] { 'level1_object_name' }
[ , [ @level2type = ] { 'level2_object_type' }
, [ @level2name = ] { 'level2_object_name' }
]
]
]
]
[;]
參數(shù)說(shuō)明
[ @name ] = { 'property_name' }
Is the name of the property to be added. property_name is sysname and cannot be NULL. Names can also include blank or non-alphanumeric character strings, and binary values.
[ @value= ] { 'value'}
Is the value to be associated with the property. value is sql_variant, with a default of NULL. The size of value cannot be more than 7,500 bytes.
[ @level0type= ] { 'level0_object_type' }
Is the type of level 0 object. level0_object_type is varchar(128), with a default of NULL.
Valid inputs are ASSEMBLY, CONTRACT, EVENT NOTIFICATION, FILEGROUP, MESSAGE TYPE, PARTITION FUNCTION, PARTITION SCHEME, REMOTE SERVICE BINDING, ROUTE, SCHEMA, SERVICE, USER, TRIGGER, TYPE, PLAN GUIDE, and NULL.
[ @level0name= ] { 'level0_object_name' }
Is the name of the level 0 object type specified. level0_object_name is sysname with a default of NULL.
[ @level1type= ] { 'level1_object_type' }
Is the type of level 1 object. level1_object_type is varchar(128), with a default of NULL. Valid inputs are AGGREGATE, DEFAULT, FUNCTION, LOGICAL FILE NAME, PROCEDURE, QUEUE, RULE, SYNONYM, TABLE, TABLE_TYPE, TYPE, VIEW, XML SCHEMA COLLECTION, and NULL.
[ @level1name= ] { 'level1_object_name' }
Is the name of the level 1 object type specified. level1_object_name is sysname, with a default of NULL.
[ @level2type= ] { 'level2_object_type' }
Is the type of level 2 object. level2_object_type is varchar(128), with a default of NULL. Valid inputs are COLUMN, CONSTRAINT, EVENT NOTIFICATION, INDEX, PARAMETER, TRIGGER, and NULL.
[ @level2name= ] { 'level2_object_name' }
Is the name of the level 2 object type specified. level2_object_name is sysname, with a default of NULL.
[ @name = ] { 'property_name' }
要添加的屬性名稱。property_name 的數(shù)據(jù)類型為 sysname,它不能是 NULL。名稱可能還包括空白或非字母數(shù)字字符串和二進(jìn)制值。
[ @value = ] { 'value' }
將要與屬性相關(guān)聯(lián)的值。value 的數(shù)據(jù)類型為 sql_variant,帶有默認(rèn)設(shè)置 NULL。value 的大小不能超過(guò) 7,500 字節(jié);否則 SQL Server 會(huì)產(chǎn)生錯(cuò)誤。
[ @level0type = ] { 'level0_object_type' }
用戶或用戶定義類型。level0_object_type 的數(shù)據(jù)類型為 varchar(128),其默認(rèn)值為 NULL。有效的輸入是 ASSEMBLY, CONTRACT, EVENT NOTIFICATION, FILEGROUP, MESSAGE TYPE, PARTITION FUNCTION, PARTITION SCHEME, REMOTE SERVICE BINDING, ROUTE, SCHEMA, SERVICE, USER, TRIGGER, TYPE, PLAN GUIDE 和 NULL
[ @level0name = ] { 'level0_object_name' }
指定的 0 級(jí)對(duì)象類型的名稱。level0_object_name 的數(shù)據(jù)類型為 sysname,其默認(rèn)值為 NULL。
[ @level1type = ] { 'level1_object_type' }
1 級(jí)對(duì)象的類型。level1_object_type 的數(shù)據(jù)類型為 varchar(128),其默認(rèn)值為 NULL。有效的輸入是 AGGREGATE, DEFAULT, FUNCTION, LOGICAL FILE NAME, PROCEDURE, QUEUE, RULE, SYNONYM, TABLE, TABLE_TYPE, TYPE, VIEW, XML SCHEMA COLLECTION 和 NULL。
[ @level1name = ] { 'level1_object_name' }
指定的 1 級(jí)對(duì)象類型的名稱。level1_object_name 的數(shù)據(jù)類型為 sysname,其默認(rèn)值為 NULL。
[ @level2type = ] { 'level2_object_type' }
2 級(jí)對(duì)象的類型。level2_object_type 的數(shù)據(jù)類型為 varchar(128),其默認(rèn)值為 NULL。有效的輸入是 COLUMN, CONSTRAINT, EVENT NOTIFICATION, INDEX, PARAMETER, TRIGGER 和 NULL。
[ @level2name = ] { 'level2_object_name' }
指定的 2 級(jí)對(duì)象類型的名稱。level2_object_name 的數(shù)據(jù)類型為 sysname,其默認(rèn)值為 NULL。
返回值為0則成功,1則失敗
更新描述
sp_updateextendedproperty
[ @name = ]{ 'property_name' }
[ , [ @value = ]{ 'value' }
[, [ @level0type = ]{ 'level0_object_type' }
, [ @level0name = ]{ 'level0_object_name' }
[, [ @level1type = ]{ 'level1_object_type' }
, [ @level1name = ]{ 'level1_object_name' }
[, [ @level2type = ]{ 'level2_object_type' }
, [ @level2name = ]{ 'level2_object_name' }
]
]
]
]
更新描述 和 添加的語(yǔ)法差不多,在此就不再贅述
USER 還是 SCHEMA
msdn 給出的文檔里這么說(shuō):
The ability to specify USER as a level 0 type in an extended property of a level 1 type object will be removed in a future version of SQL Server. Use SCHEMA as the level 0 type instead. For example, when defining an extended property on a table, specify the schema of the table instead of a user name. The ability to specify TYPE as level-0 type will be removed in a future version of SQL Server. For TYPE, use SCHEMA as the level 0 type and TYPE as the level 1 type.
在級(jí)別1類型對(duì)象的擴(kuò)展屬性中將USER指定為級(jí)別0類型的功能將在未來(lái)版本的SQL Server中刪除。
使用SCHEMA作為0級(jí)類型。 例如,在表上定義擴(kuò)展屬性時(shí),請(qǐng)指定表的模式,而不是用戶名。
將在未來(lái)版本的SQL Server中刪除將TYPE指定為級(jí)別0類型的能力。
對(duì)于TYPE,使用SCHEMA作為0級(jí)類型,使用TYPE作為1級(jí)類型。
We do not recommend specifying USER as a level 0 type when you apply an extended property to a database object, because this can cause name resolution ambiguity. For example, assume user Mary owns two schemas (Mary and MySchema) and these schemas both contain a table named MyTable. If Mary adds an extended property to table MyTable and specifies @level0type = N'USER', @level0name = Mary, it is not clear to which table the extended property is applied. To maintain backward compatibility, SQL Server will apply the property to the table that is contained in the schema named Mary.
當(dāng)將擴(kuò)展屬性應(yīng)用于數(shù)據(jù)庫(kù)對(duì)象時(shí),我們不建議將USER指定為級(jí)別0類型,因?yàn)檫@可能會(huì)導(dǎo)致名稱解析模糊。
例如,假設(shè)用戶Mary擁有兩個(gè)模式(Mary和MySchema),這些模式都包含一個(gè)名為MyTable的表。
如果Mary向表MyTable添加擴(kuò)展屬性并指定@ level0type = N'USER',@ level0name = Mary,則不清楚擴(kuò)展屬性應(yīng)用于哪個(gè)表。
為了保持向后兼容性,SQL Server會(huì)將該屬性應(yīng)用于包含在名為Mary的模式中的表。
總結(jié)來(lái)說(shuō),當(dāng)將擴(kuò)展屬性應(yīng)用于數(shù)據(jù)庫(kù)對(duì)象時(shí),微軟推薦使用 SCHEMA,不推薦 USER,但是 USER 還是會(huì)保留,還是可以使用但是在某些情況下(如上所述)可能會(huì)出現(xiàn)自己意料之外的事情。
示例
-- 查詢
SELECT [TypeId] , [TypeName]
FROM [dbo].[tabBlockType];
-- USER (不推薦)
-- 添加表 tabBlockType 描述
EXECUTE sp_addextendedproperty N'MS_Description', N'黑名單類型表', N'USER', N'dbo',
N'table', N'tabBlockType';
-- 添加列 TypeId 描述
EXECUTE sp_addextendedproperty N'MS_Description', N'黑名單類型id', N'USER', N'dbo',
N'TABLE', N'tabBlockType', N'COLUMN', N'TypeId';
-- 添加 列 TypeName 描述
EXECUTE sp_addextendedproperty N'MS_Description', N'黑名單類型名稱', N'USER', N'dbo',
N'TABLE', N'tabBlockType', N'COLUMN', N'TypeName';
-- 更新列 TypeName 描述
EXECUTE sp_updateextendedproperty N'MS_Description', N'黑名單類型', N'USER', N'dbo',
N'TABLE', N'tabBlockType', N'COLUMN', N'TypeName';
-- SCHEMA (推薦)
-- 添加表 tabBlockType 描述
EXECUTE sp_addextendedproperty N'MS_Description', N'黑名單類型表', N'SCHEMA', N'dbo',
N'table', N'tabBlockType';
-- 添加列 TypeId 描述
EXECUTE sp_addextendedproperty N'MS_Description', N'黑名單類型id', N'SCHEMA', N'dbo',
N'TABLE', N'tabBlockType', N'COLUMN', N'TypeId';
-- 添加 列 TypeName 描述
EXECUTE sp_addextendedproperty N'MS_Description', N'黑名單類型名稱', N'SCHEMA', N'dbo',
N'TABLE', N'tabBlockType', N'COLUMN', N'TypeName';
-- 更新列 TypeName 描述
EXECUTE sp_updateextendedproperty N'MS_Description', N'黑名單類型', N'SCHEMA', N'dbo',
N'TABLE', N'tabBlockType', N'COLUMN', N'TypeName';
-- 創(chuàng)建 Create sql 腳本 生成的添加描述 腳本
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'黑名單類型id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tabBlockType', @level2type=N'COLUMN',@level2name=N'TypeId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'黑名單類型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tabBlockType', @level2type=N'COLUMN',@level2name=N'TypeName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'黑名單類型表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tabBlockType'
GO
sql 總覽

創(chuàng)建create 語(yǔ)句時(shí) 描述信息也會(huì)導(dǎo)出

More
參考資料
- https://msdn.microsoft.com/en-us/library/ms180047.aspx
- https://msdn.microsoft.com/en-us/library/ms186885.aspx
- http://www.cnblogs.com/wangshenhe/p/3178039.html (注:這篇博客中有幾點(diǎn)錯(cuò)誤的地方,推薦看上面兩篇,官方文檔最準(zhǔn))
In the end
筆者水平有限,如果發(fā)現(xiàn)有什么錯(cuò)誤的地方,歡迎指出,歡迎與我聯(lián)系 ben121011@126.com