sql: Query to Display Foreign Key Relationships and Name of

作者:分分快三全天计划网站
--1第一种方法
--增加表描述 
EXECUTE sp_addextendedproperty N'MS_Description', '客户本人照片表', N'user', N'dbo', N'table', N'UserPhoto', NULL, NULL
GO
--删除
EXECUTE sp_dropextendedproperty N'MS_Description',  N'user', N'dbo', N'table', N'UserPhoto', DEFAULT, DEFAULT
GO

--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'UserPhoto', N'column', N'UserId'
--修改
EXECUTE sp_updateextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'UserPhoto', N'column', N'UserId'

EXECUTE sp_addextendedproperty N'MS_Description', '客户ID,外键', N'user', N'dbo', N'table', N'UserPhoto', N'column', N'UserToId'
EXECUTE sp_addextendedproperty N'MS_Description', '相片名称', N'user', N'dbo', N'table', N'UserPhoto', N'column', N'UserName'
EXECUTE sp_addextendedproperty N'MS_Description', '相片', N'user', N'dbo', N'table', N'UserPhoto', N'column', N'UserImage'
EXECUTE sp_addextendedproperty N'MS_Description', '生成日期', N'user', N'dbo', N'table', N'UserPhoto', N'column', N'UserDate'
GO

SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'TABLE', N'UserPhoto', default, default)
GO

SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'TABLE', N'UserPhoto','column', default)
GO

--第二种方法
--sp_updateextendedproperty 
--增加表描述 
EXEC sys.sp_addextendedproperty  @name = N'MS_Description', @value = N'客户本人照片表',  @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE',  @level1name = 'UserPhoto';  
GO 

--刪除表描述 
EXEC sp_dropextendedproperty  @name = N'MS_DescriptionExample', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE',  @level1name = 'UserPhoto';  
GO   

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单据唯一编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserPhoto', @level2type=N'COLUMN',@level2name=N'UserId'
GO
--修改
EXEC sys.sp_updateextendedproperty @name=N'MS_Description', @value=N'单据唯一编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserPhoto', @level2type=N'COLUMN',@level2name=N'UserId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单据明细内部编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserPhoto', @level2type=N'COLUMN',@level2name=N'UserToId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单据唯一编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserPhoto', @level2type=N'COLUMN',@level2name=N'UserName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单据明细内部编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserPhoto', @level2type=N'COLUMN',@level2name=N'UserImage'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单据唯一编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserPhoto', @level2type=N'COLUMN',@level2name=N'UserDate'
GO

 

 备注描述另种写法:

 

--增加表的描述
EXEC sys.sp_addextendedproperty  @name = N'MS_Description', @value = N'母件物料表',  @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE',  @level1name = 'MMBOMPairProduct';  
GO 



EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'母件物料编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MMBOMPairProduct', @level2type=N'COLUMN',@level2name=N'PMaterielNo'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'父BOM版次' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MMBOMPairProduct', @level2type=N'COLUMN',@level2name=N'PVersion'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'副产品物料编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MMBOMPairProduct', @level2type=N'COLUMN',@level2name=N'CMaterielNo'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MMBOMPairProduct', @level2type=N'COLUMN',@level2name=N'Dosage'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'说明' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MMBOMPairProduct', @level2type=N'COLUMN',@level2name=N'Remark'
GO

 

---PRIMARY 主鍵
SELECT  tab.name AS 'TableName',  idx.name AS 'PrimaryName',  col.name AS 'FieldName',c.name AS 'TypeName',col.max_length AS 'Length',b.value AS 'TableDescription',col.is_nullable as 'IS_NULL' FROM sys.indexes idx,sys.index_columns idxCol,sys.tables tab,sys.columns col,sys.systypes AS c,sys.extended_properties AS b
WHERE idx.object_id = idxCol.object_id AND idx.index_id = idxCol.index_id AND idx.is_primary_key = 1 AND idx.object_id = tab.object_id AND idx.object_id = col.object_id AND idxCol.column_id = col.column_id AND  col.user_type_id=c.xusertype AND tab.object_id=b.major_id AND b.minor_id=0
GO

---外键约束  FieldDescription            
select  oSub.name  AS  SubTableName, dbo.F_GetDescriptionTableName(osub.object_id) AS 'SubTableDescription', fk.name AS  ForeignName,  SubCol.name AS SubFieldName,  oMain.name  AS  MainTableName, dbo.F_GetDescriptionTableName(oMain.object_id) AS 'MainTableDescription', MainCol.name AS MainFieldName,c.name AS 'FieldType',SubCol.max_length AS 'FieldLength'
from  sys.foreign_keys fk,sys.all_objects oSub, sys.all_objects oMain, sys.foreign_key_columns fkCols ,sys.columns SubCol,sys.columns MainCol,sys.systypes AS c,sys.extended_properties AS b
WHERE  fk.parent_object_id = oSub.object_id AND  fk.referenced_object_id = oMain.object_id AND  fk.object_id = fkCols.constraint_object_id AND  oSub.object_id = SubCol.object_id  AND fkCols.parent_column_id = SubCol.column_id
AND oMain.object_id = MainCol.object_id  AND fkCols.referenced_column_id = MainCol.column_id AND SubCol.object_id=b.major_id AND SubCol.user_type_id=c.xusertype  AND b.minor_id=0 AND oMain.name='SendReceiveMailHost' 

  

 

 

--20170505
--塗聚文 Geovin Du


CREATE DATABASE DuMailSystem
GO
USE DuMailSystem
GO

--1查詢表的及备注说明
SELECT Sysobjects.name AS TABLE_NAME, syscolumns.Id, syscolumns.name AS COLUMN_NAME,
systypes.name AS DATA_TYPE, syscolumns.length as CHARACTER_MAXIMUM_LENGTH, 
sys.extended_properties.[value] AS COLUMN_DESCRIPTION,  syscomments.text as 
COLUMN_DEFAULT,syscolumns.isnullable as IS_NULLABLE FROM syscolumns 
INNER JOIN systypes 
    ON syscolumns.xtype = systypes.xtype 
    LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id 
  LEFT OUTER JOIN sys.extended_properties ON 
  ( sys.extended_properties.minor_id = syscolumns.colid 
    AND sys.extended_properties.major_id = syscolumns.id) 
  LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id 
  WHERE syscolumns.id IN 
    (SELECT id FROM SYSOBJECTS WHERE xtype = 'U') AND (systypes.name <> 'sysname')
   -- ORDER BY syscolumns.colid

--2.SQL查询表的所有字段的备注说明
SELECT 
(case when a.colorder=1 then d.name else '' end) N'表名', 
a.colorder N'字段序号', 
a.name N'字段名', 
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' 
end) N'标识', 
(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) N'主键', 
b.name N'类型', 
a.length N'占用字节数', 
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度', 
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数', 
(case when a.isnullable=1 then '√'else '' end) N'允许空', 
isnull(e.text,'') N'默认值', 
isnull(g.[value],'') AS N'字段说明' 
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' 
--AND d.name='MaterialOrderDetails'
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 
order by object_name(a.id),a.colorder   
GO

--造成此问题的原因是由于Sql 2005、2008 删除了系统表 sysproperties 而改用 sys.extended_properties 表所致         
--sql server 2008
SELECT a.[name] as '字段名称', (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) as '标识'
,b.name '类型'
,a.length '占用字节数'
,(case when a.isnullable = 1 then '√'else '' end) '允许空'
,isnull(e.text,'') '默认值'
,cast(isnull(g.[value],'') as varchar(100)) AS '字段说明'
FROM syscolumns  a
left join systypes b
on  a.xtype = b.xusertype
left join sys.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
--WHERE a.id =1173579219
--ORDER by a.colorder
GO


SELECT * FROM sys.extended_properties
SELECT * FROM syscolumns
SELECT * FROM sys.syscomments 

SELECT a.name,b.value AS 'Description',b.name,c.name AS 'typename',a.length,d.name as 'TableName' FROM syscolumns AS a,sys.extended_properties AS b,systypes AS c,sysobjects as d
WHERE a.id=b.major_id AND a.colid=b.minor_id
AND a.xtype=c.xusertype
AND  a.id=d.id 



--主鍵
SELECT a.name,b.value AS 'Description',b.name,c.name AS 'typename',a.length,d.name as 'TableName',dbo.F_GetDescriptionTableName(d.id) AS 'TableDescription' FROM syscolumns AS a,sys.extended_properties AS b,systypes AS c,sysobjects as d ,INFORMATION_SCHEMA.KEY_COLUMN_USAGE as e
WHERE a.id=b.major_id AND a.colid=b.minor_id
AND a.xtype=c.xusertype
AND  a.id=d.id 
AND d.xtype='U' and e.TABLE_NAME   = d.name and e.COLUMN_NAME=a.name

--表描述
SELECT * FROM sys.extended_properties  WHERE  minor_id=0

SELECT a.id,a.name,b.value AS 'Description' FROM  sysobjects AS a,sys.extended_properties b WHERE a.xtype='U' AND a.id=b.major_id AND minor_id=0




DECLARE @name NVARCHAR(100),@id INT
SET @id=21575115
SELECT @name=CONVERT(NVARCHAR(100),b.value)FROM  sysobjects AS a,sys.extended_properties as b WHERE a.xtype='U' AND a.id=b.major_id AND minor_id=0 AND a.id=@id
SELECT @name AS '表描述'


---表描述
if exists(select 1 from sysobjects where name = 'F_GetDescriptionTableName' and xtype = 'FN') 
drop function F_GetDescriptionTableName 
go 
CREATE function F_GetDescriptionTableName
(
 @id int
) 
returns nvarchar(400) 
as 
begin
declare @str nvarchar(100)
SELECT @str=CONVERT(NVARCHAR(100),b.value)FROM  sysobjects AS a,sys.extended_properties as b WHERE a.xtype='U' AND a.id=b.major_id AND minor_id=0 AND a.id=@id
return isnull(@str,'')
end 
GO



---
SELECT d.name as 'TableName',dbo.F_GetDescriptionTableName(d.id) AS 'TableDescription',a.name as 'FieldName',e.value AS 'FieldDescription',b.name as 'TypeName',a.length as 'Length',a.isnullable as 'IS_NULL' FROM  syscolumns  a,  systypes b,sysobjects d ,INFORMATION_SCHEMA.KEY_COLUMN_USAGE c,sys.extended_properties AS e 
WHERE  a.xtype=b.xusertype  and  a.id=d.id  and  d.xtype='U' and c.TABLE_NAME   = d.name and c.COLUMN_NAME=a.name
AND d.id=e.major_id
AND a.id=e.major_id AND a.colid=e.minor_id
GO

SELECT d.name as 'TableName',a.name as 'FieldName',b.name as 'TypeName',a.length as 'Length',a.isnullable as 'IS_NULL' FROM  syscolumns  a,  systypes b,sysobjects d ,INFORMATION_SCHEMA.KEY_COLUMN_USAGE c WHERE  a.xtype=b.xusertype  and  a.id=d.id  and  d.xtype='U' and c.TABLE_NAME   = d.name and c.COLUMN_NAME=a.name
GO

--Customer,可以加上描述
SELECT c.name   as   FieldName,t.name   as   FieldType,   c.length   as   FieldLength   FROM   SYSCOLUMNS   c   inner   join     systypes   t   on   c.xusertype=t.xusertype   WHERE   c.ID   =   OBJECT_ID('Customer')
GO

SELECT c.name   as   FieldName,t.name as   FieldType,e.value AS FieldDescription ,   c.length   as   FieldLength   FROM   SYSCOLUMNS as  c ,systypes  as t,sys.extended_properties AS e  
WHERE c.xusertype=t.xusertype 
AND c.id=e.major_id AND c.colid=e.minor_id
AND  c.ID   =   OBJECT_ID('Customer')
GO


--查询表
SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', default, default, default)

 --收件箱 Receive,發件箱 

IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].SendReceiveMailHost') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE SendReceiveMailHost 
GO
 CREATE TABLE SendReceiveMailHost
 (
 SendHostId INT IDENTITY(1,1) PRIMARY KEY,
 SendHostName VARCHAR(100) NOT NULL,
 PopHostName VARCHAR(100) NOT NULL,
 SendUserName VARCHAR(100) NOT NULL,
 SendRealName VARCHAR(100) NOT NULL,
 SendPassword VARCHAR(100) NOT NULL,
 SendPort INT DEFAULT(25) NOT NULL,
 PopPort INT DEFAULT(110) NOT NULL,
 SendEnableSsl BIT DEFAULT(0),
 SendTimeout INT DEFAULT(10000) 
 )
 GO 
 -- 
EXECUTE sp_addextendedproperty N'MS_Description', '收發郵件箱設置表', N'user', N'dbo', N'table', N'SendReceiveMailHost', NULL, NULL
--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendHostId'
EXECUTE sp_addextendedproperty N'MS_Description', '發郵件服務地址', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendHostName'
EXECUTE sp_addextendedproperty N'MS_Description', '收件郵件服務地址', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'PopHostName'
EXECUTE sp_addextendedproperty N'MS_Description', '發郵件箱', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendUserName'
EXECUTE sp_addextendedproperty N'MS_Description', '顯示姓名', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendRealName'
EXECUTE sp_addextendedproperty N'MS_Description', '發件箱密碼', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendPassword'
EXECUTE sp_addextendedproperty N'MS_Description', '发郵件服務端口', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendPort'
EXECUTE sp_addextendedproperty N'MS_Description', '收郵件服務端口', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'PopPort'
EXECUTE sp_addextendedproperty N'MS_Description', '是否安全SSL', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendEnableSsl'
EXECUTE sp_addextendedproperty N'MS_Description', '發件延長時間秒', N'user', N'dbo', N'table', N'SendReceiveMailHost', N'column', N'SendTimeout' 


SELECT * FROM SendReceiveMailHost;


--收郵件列表ReceiveMail
IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].ReceiveMail') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE ReceiveMail 
GO
CREATE TABLE ReceiveMail
(
 ReceiveId   INT IDENTITY(1,1) PRIMARY KEY,
 ReceiveHostId INT NOT NULL
  FOREIGN key references SendReceiveMailHost(SendHostId),
 ContentType VARCHAR(100) NOT NULL,
 ContentEncoding VARCHAR(100) NOT NULL,
 ReceiveTo VARCHAR(100), 
 ReceiveFrom VARCHAR(100),
 ReplyTo VARCHAR(100), 
 ReceiveSubject NVARCHAR(200),
 ReceiveBody NTEXT,
 ReceiveDate DATETIME 
)
GO

EXECUTE sp_addextendedproperty N'MS_Description', '收邮件表', N'user', N'dbo', N'table', N'ReceiveMail', NULL, NULL
--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveId'
EXECUTE sp_addextendedproperty N'MS_Description', '收件服务ID,外键', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveHostId'
EXECUTE sp_addextendedproperty N'MS_Description', '文件类型', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ContentType'
EXECUTE sp_addextendedproperty N'MS_Description', '邮件字符编码类型', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ContentEncoding'
EXECUTE sp_addextendedproperty N'MS_Description', '收件人', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveTo'

EXECUTE sp_addextendedproperty N'MS_Description', '发件人', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveFrom'
EXECUTE sp_addextendedproperty N'MS_Description', '回复邮件地址', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReplyTo'
EXECUTE sp_addextendedproperty N'MS_Description', '邮件标题', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveSubject'
EXECUTE sp_addextendedproperty N'MS_Description', '邮件内容', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveBody'
EXECUTE sp_addextendedproperty N'MS_Description', '收件日期', N'user', N'dbo', N'table', N'ReceiveMail', N'column', N'ReceiveDate'




---收邮件附件列表 ReceiveAttachments
IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].ReceiveAttachments') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE ReceiveAttachments 
GO
CREATE TABLE ReceiveAttachments
(
  AttachmentId INT IDENTITY(1,1) PRIMARY KEY,
  AttachmentIdKey  INT NOT NULL
  FOREIGN key references ReceiveMail(ReceiveId),
  AttachmentFileName NVARCHAR(100) NOT NULL,
  AttachmentFileUrl NVARCHAR(500) NOT NULL,
  AttachmentImage IMAGE NOT NULL,
  AttachmentType VARCHAR(100)  
)
GO
--來源地址,保存地址
EXECUTE sp_addextendedproperty N'MS_Description', '收邮件附件表', N'user', N'dbo', N'table', N'ReceiveAttachments', NULL, NULL
EXECUTE sp_updateextendedproperty N'MS_Description', '收邮件附件表', N'user', N'dbo', N'table', N'ReceiveAttachments', NULL, NULL
--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'ReceiveAttachments', N'column', N'AttachmentId'
EXECUTE sp_addextendedproperty N'MS_Description', '外鍵ID', N'user', N'dbo', N'table', N'ReceiveAttachments', N'column', N'AttachmentIdKey'
EXECUTE sp_addextendedproperty N'MS_Description', '文件名稱', N'user', N'dbo', N'table', N'ReceiveAttachments', N'column', N'AttachmentFileName'
EXECUTE sp_addextendedproperty N'MS_Description', '文件地址', N'user', N'dbo', N'table', N'ReceiveAttachments', N'column', N'AttachmentFileUrl'
EXECUTE sp_addextendedproperty N'MS_Description', '文件存儲數据庫', N'user', N'dbo', N'table', N'ReceiveAttachments', N'column', N'AttachmentImage'
EXECUTE sp_addextendedproperty N'MS_Description', '文件類型', N'user', N'dbo', N'table', N'ReceiveAttachments', N'column', N'AttachmentType'


--郵件聯系人(收件人)
IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].Customer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE Customer 
GO
CREATE TABLE Customer
(
 Id INT IDENTITY(1,1) PRIMARY KEY,
 RealName NVARCHAR(100) NOT NULL,
 Email NVARCHAR(100) NOT NULL,
 Sex INT NOT NULL, 
 Title NVARCHAR(50),
 Tel VARCHAR(50) 
)
GO

EXECUTE sp_addextendedproperty N'MS_Description', '郵件聯系人表', N'user', N'dbo', N'table', N'Customer', NULL, NULL
--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'Customer', N'column', N'Id'
EXECUTE sp_addextendedproperty N'MS_Description', '真实姓名', N'user', N'dbo', N'table', N'Customer', N'column', N'RealName'
EXECUTE sp_addextendedproperty N'MS_Description', '邮件地址', N'user', N'dbo', N'table', N'Customer', N'column', N'Email'
EXECUTE sp_addextendedproperty N'MS_Description', '性别', N'user', N'dbo', N'table', N'Customer', N'column', N'Sex'
EXECUTE sp_addextendedproperty N'MS_Description', '称呼', N'user', N'dbo', N'table', N'Customer', N'column', N'Title'
EXECUTE sp_addextendedproperty N'MS_Description', '电话', N'user', N'dbo', N'table', N'Customer', N'column', N'Tel'




--發郵件列表 SendMail
IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].SendMail') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE SendMail 
GO
CREATE TABLE SendMail
(
 SendId INT IDENTITY(1,1) PRIMARY KEY,
 SendHostId INT NOT NULL 
   FOREIGN key references SendReceiveMailHost(SendHostId),    --
 SendToId INT NOT NULL
        FOREIGN key references Customer(Id),
 SendSubject NVARCHAR(500) NOT NULL,
 SendBody NTEXT NULL,
 SendIs BIT DEFAULT(1),              --1,ok.0,no.
 SendDate DATETIME DEFAULT(GETDATE()) 
)
GO

EXECUTE sp_addextendedproperty N'MS_Description', '發件郵列表', N'user', N'dbo', N'table', N'SendMail', NULL, NULL
--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendId'
EXECUTE sp_addextendedproperty N'MS_Description', '發郵件箱ID,外鍵', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendHostId'
EXECUTE sp_addextendedproperty N'MS_Description', '收件人ID,外鍵', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendToId'
EXECUTE sp_addextendedproperty N'MS_Description', '發件標題', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendSubject'
EXECUTE sp_addextendedproperty N'MS_Description', '發件內容', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendBody'
EXECUTE sp_addextendedproperty N'MS_Description', '發件是否成功', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendIs'
EXECUTE sp_addextendedproperty N'MS_Description', '發件時間', N'user', N'dbo', N'table', N'SendMail', N'column', N'SendDate'



--发邮件附件列表 SendAttachments

IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].SendAttachments') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE SendAttachments 
GO
CREATE TABLE SendAttachments
(
    AttachmentId INT IDENTITY(1,1) PRIMARY KEY,
 AttachmentIdKey INT
  FOREIGN key references SendMail(SendId),
 AttachmentFileName NVARCHAR(100) NOT NULL,
 AttachmentFileUrl NVARCHAR(500) NOT NULL,
 AttachmentImage IMAGE NOT NULL,
 AttachmentType VARCHAR(100) 
)
GO
--來源地址,保存地址
EXECUTE sp_addextendedproperty N'MS_Description', '发邮件附件表', N'user', N'dbo', N'table', N'SendAttachments', NULL, NULL
--为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '自动增长ID号', N'user', N'dbo', N'table', N'SendAttachments', N'column', N'AttachmentId'
EXECUTE sp_addextendedproperty N'MS_Description', '外鍵ID', N'user', N'dbo', N'table', N'SendAttachments', N'column', N'AttachmentIdKey'
EXECUTE sp_addextendedproperty N'MS_Description', '文件名稱', N'user', N'dbo', N'table', N'SendAttachments', N'column', N'AttachmentFileName'
EXECUTE sp_addextendedproperty N'MS_Description', '文件地址', N'user', N'dbo', N'table', N'SendAttachments', N'column', N'AttachmentFileUrl'
EXECUTE sp_addextendedproperty N'MS_Description', '文件存儲數据庫', N'user', N'dbo', N'table', N'SendAttachments', N'column', N'AttachmentImage'
EXECUTE sp_addextendedproperty N'MS_Description', '文件類型', N'user', N'dbo', N'table', N'SendAttachments', N'column', N'AttachmentType'
GO

--主键约束
SELECT
  tab.name AS [表名],
  idx.name AS [主键名称],
  col.name AS [主键列名]
FROM
  sys.indexes idx
    JOIN sys.index_columns idxCol 
      ON (idx.object_id = idxCol.object_id 
          AND idx.index_id = idxCol.index_id 
          AND idx.is_primary_key = 1)
    JOIN sys.tables tab
      ON (idx.object_id = tab.object_id)
    JOIN sys.columns col
      ON (idx.object_id = col.object_id
          AND idxCol.column_id = col.column_id);
GO

---唯一约束
SELECT
  tab.name AS [表名],
  idx.name AS [约束名称],
  col.name AS [约束列名]
FROM
  sys.indexes idx
    JOIN sys.index_columns idxCol 
      ON (idx.object_id = idxCol.object_id 
          AND idx.index_id = idxCol.index_id 
          AND idx.is_unique_constraint = 1)
    JOIN sys.tables tab
      ON (idx.object_id = tab.object_id)
    JOIN sys.columns col
      ON (idx.object_id = col.object_id
          AND idxCol.column_id = col.column_id);
GO

--外键约束
select 
  oSub.name  AS  [子表名称],
  fk.name AS  [外键名称],
  SubCol.name AS [子表列名],
  oMain.name  AS  [主表名称],
  MainCol.name AS [主表列名]
from 
  sys.foreign_keys fk  
    JOIN sys.all_objects oSub  
        ON (fk.parent_object_id = oSub.object_id)
    JOIN sys.all_objects oMain 
        ON (fk.referenced_object_id = oMain.object_id)
    JOIN sys.foreign_key_columns fkCols 
        ON (fk.object_id = fkCols.constraint_object_id)
    JOIN sys.columns SubCol 
        ON (oSub.object_id = SubCol.object_id  
            AND fkCols.parent_column_id = SubCol.column_id)
    JOIN sys.columns MainCol 
        ON (oMain.object_id = MainCol.object_id  
            AND fkCols.referenced_column_id = MainCol.column_id)
GO

--Check约束
SELECT
  tab.name AS [表名],
  chk.name AS [Check约束名],
  col.name AS [列名],
  chk.definition
FROM
  sys.check_constraints chk
    JOIN sys.tables tab
      ON (chk.parent_object_id = tab.object_id)
    JOIN sys.columns col
      ON (chk.parent_object_id = col.object_id
          AND chk.parent_column_id = col.column_id)
GO


--外键约束
SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
---- optional:
GO

select 
    t.name as TableWithForeignKey, 
    fk.constraint_column_id as FK_PartNo, c.
    name as ForeignKeyColumn 
from 
    sys.foreign_key_columns as fk
inner join 
    sys.tables as t on fk.parent_object_id = t.object_id
inner join 
    sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_id
where 
    fk.referenced_object_id = (select object_id 
                               from sys.tables 
                               where name = 'SendMail')
order by 
    TableWithForeignKey, FK_PartNo
GO


SELECT  obj.name AS FK_NAME,
    sch.name AS [schema_name],
    tab1.name AS [table],
    col1.name AS [column],
    tab2.name AS [referenced_table],
    col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
    ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
    ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
    ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
    ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
    ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
    ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
GO

SELECT PKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()),
       PKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O1.SCHEMA_ID)),
       PKTABLE_NAME = CONVERT(SYSNAME,O1.NAME),
       PKCOLUMN_NAME = CONVERT(SYSNAME,C1.NAME),
       FKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()),
       FKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O2.SCHEMA_ID)),
       FKTABLE_NAME = CONVERT(SYSNAME,O2.NAME),
       FKCOLUMN_NAME = CONVERT(SYSNAME,C2.NAME),
       -- Force the column to be non-nullable (see SQL BU 325751)
       --KEY_SEQ             = isnull(convert(smallint,k.constraint_column_id), sysconv(smallint,0)),
       UPDATE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.OBJECT_ID,'CnstIsUpdateCascade') 
                                        WHEN 1 THEN 0
                                        ELSE 1
                                      END),
       DELETE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.OBJECT_ID,'CnstIsDeleteCascade') 
                                        WHEN 1 THEN 0
                                        ELSE 1
                                      END),
       FK_NAME = CONVERT(SYSNAME,OBJECT_NAME(F.OBJECT_ID)),
       PK_NAME = CONVERT(SYSNAME,I.NAME),
       DEFERRABILITY = CONVERT(SMALLINT,7)   -- SQL_NOT_DEFERRABLE
FROM   SYS.ALL_OBJECTS O1,
       SYS.ALL_OBJECTS O2,
       SYS.ALL_COLUMNS C1,
       SYS.ALL_COLUMNS C2,
       SYS.FOREIGN_KEYS F
       INNER JOIN SYS.FOREIGN_KEY_COLUMNS K
         ON (K.CONSTRAINT_OBJECT_ID = F.OBJECT_ID)
       INNER JOIN SYS.INDEXES I
         ON (F.REFERENCED_OBJECT_ID = I.OBJECT_ID
             AND F.KEY_INDEX_ID = I.INDEX_ID)
WHERE  O1.OBJECT_ID = F.REFERENCED_OBJECT_ID
       AND O2.OBJECT_ID = F.PARENT_OBJECT_ID
       AND C1.OBJECT_ID = F.REFERENCED_OBJECT_ID
       AND C2.OBJECT_ID = F.PARENT_OBJECT_ID
       AND C1.COLUMN_ID = K.REFERENCED_COLUMN_ID
       AND C2.COLUMN_ID = K.PARENT_COLUMN_ID
GO


SELECT
OBJECT_NAME(parent_object_id) 'Parent table',
c.NAME 'Parent column name',
OBJECT_NAME(referenced_object_id) 'Referenced table',
cref.NAME 'Referenced column name'
FROM 
sys.foreign_key_columns fkc 
INNER JOIN 
sys.columns c 
   ON fkc.parent_column_id = c.column_id 
      AND fkc.parent_object_id = c.object_id
INNER JOIN 
sys.columns cref 
   ON fkc.referenced_column_id = cref.column_id 
      AND fkc.referenced_object_id = cref.object_id  where   OBJECT_NAME(parent_object_id) = 'SendMail'
GO

/* CAST IS DONE , SO THAT OUTPUT INTEXT FILE REMAINS WITH SCREEN LIMIT*/
WITH   ALL_KEYS_IN_TABLE (CONSTRAINT_NAME,CONSTRAINT_TYPE,PARENT_TABLE_NAME,PARENT_COL_NAME,PARENT_COL_NAME_DATA_TYPE,REFERENCE_TABLE_NAME,REFERENCE_COL_NAME) 
AS
(
SELECT  CONSTRAINT_NAME= CAST (PKnUKEY.name AS VARCHAR(30)) ,
        CONSTRAINT_TYPE=CAST (PKnUKEY.type_desc AS VARCHAR(30)) ,
        PARENT_TABLE_NAME=CAST (PKnUTable.name AS VARCHAR(30)) ,
        PARENT_COL_NAME=CAST ( PKnUKEYCol.name AS VARCHAR(30)) ,
        PARENT_COL_NAME_DATA_TYPE=  oParentColDtl.DATA_TYPE,        
        REFERENCE_TABLE_NAME='' ,
        REFERENCE_COL_NAME='' 

FROM sys.key_constraints as PKnUKEY
    INNER JOIN sys.tables as PKnUTable
            ON PKnUTable.object_id = PKnUKEY.parent_object_id
    INNER JOIN sys.index_columns as PKnUColIdx
            ON PKnUColIdx.object_id = PKnUTable.object_id
            AND PKnUColIdx.index_id = PKnUKEY.unique_index_id
    INNER JOIN sys.columns as PKnUKEYCol
            ON PKnUKEYCol.object_id = PKnUTable.object_id
            AND PKnUKEYCol.column_id = PKnUColIdx.column_id
     INNER JOIN INFORMATION_SCHEMA.COLUMNS oParentColDtl
            ON oParentColDtl.TABLE_NAME=PKnUTable.name
            AND oParentColDtl.COLUMN_NAME=PKnUKEYCol.name
UNION ALL
SELECT  CONSTRAINT_NAME= CAST (oConstraint.name AS VARCHAR(30)) ,
        CONSTRAINT_TYPE='FK',
        PARENT_TABLE_NAME=CAST (oParent.name AS VARCHAR(30)) ,
        PARENT_COL_NAME=CAST ( oParentCol.name AS VARCHAR(30)) ,
        PARENT_COL_NAME_DATA_TYPE= oParentColDtl.DATA_TYPE,     
        REFERENCE_TABLE_NAME=CAST ( oReference.name AS VARCHAR(30)) ,
        REFERENCE_COL_NAME=CAST (oReferenceCol.name AS VARCHAR(30)) 
FROM sys.foreign_key_columns FKC
    INNER JOIN sys.sysobjects oConstraint
            ON FKC.constraint_object_id=oConstraint.id 
    INNER JOIN sys.sysobjects oParent
            ON FKC.parent_object_id=oParent.id
    INNER JOIN sys.all_columns oParentCol
            ON FKC.parent_object_id=oParentCol.object_id /* ID of the object to which this column belongs.*/
            AND FKC.parent_column_id=oParentCol.column_id/* ID of the column. Is unique within the object.Column IDs might not be sequential.*/
    INNER JOIN sys.sysobjects oReference
            ON FKC.referenced_object_id=oReference.id
    INNER JOIN INFORMATION_SCHEMA.COLUMNS oParentColDtl
            ON oParentColDtl.TABLE_NAME=oParent.name
            AND oParentColDtl.COLUMN_NAME=oParentCol.name
    INNER JOIN sys.all_columns oReferenceCol
            ON FKC.referenced_object_id=oReferenceCol.object_id /* ID of the object to which this column belongs.*/
            AND FKC.referenced_column_id=oReferenceCol.column_id/* ID of the column. Is unique within the object.Column IDs might not be sequential.*/

)

select * from   ALL_KEYS_IN_TABLE
where   
    PARENT_TABLE_NAME  in ('SendMail') 
    or REFERENCE_TABLE_NAME  in ('SendMail')
ORDER BY PARENT_TABLE_NAME,CONSTRAINT_NAME;
GO






SELECT 
   f.name AS 'Name of Foreign Key',
   OBJECT_NAME(f.parent_object_id) AS 'Table name',
   COL_NAME(fc.parent_object_id,fc.parent_column_id) AS 'Fieldname',
   OBJECT_NAME(t.object_id) AS 'References Table name',
   COL_NAME(t.object_id,fc.referenced_column_id) AS 'References fieldname',

   'ALTER TABLE ['   OBJECT_NAME(f.parent_object_id)   ']  DROP CONSTRAINT ['   f.name   ']' AS 'Delete foreign key',

   'ALTER TABLE ['   OBJECT_NAME(f.parent_object_id)   ']  WITH NOCHECK ADD CONSTRAINT ['   
        f.name   '] FOREIGN KEY(['   COL_NAME(fc.parent_object_id,fc.parent_column_id)   ']) REFERENCES '   
        '['   OBJECT_NAME(t.object_id)   '] (['  
        COL_NAME(t.object_id,fc.referenced_column_id)   '])' AS 'Create foreign key'
    -- , delete_referential_action_desc AS 'UsesCascadeDelete'
FROM sys.foreign_keys AS f,
     sys.foreign_key_columns AS fc,
     sys.tables t 
WHERE f.OBJECT_ID = fc.constraint_object_id
AND t.OBJECT_ID = fc.referenced_object_id
AND OBJECT_NAME(t.object_id) = 'SendMail'      --  Just show the FKs which reference a particular table
ORDER BY 2
GO

  

本文由分分快三计划发布,转载请注明来源

关键词: 分分快三计划