SELECT 'ALTER TABLE '+TABLE_NAME + ' ADD CONSTRAINT '+ ' PK_'+TABLE_NAME+' PRIMARY KEY ('+COLUMN_NAME+');'
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME LIKE 'PK%';
SELECT 'ALTER TABLE ' + M_TAB +' ADD CONSTRAINT ' + F_NAME + ' FOREIGN KEY ('+M_CLON+') REFERENCES ' +F_TAB +'('+F_CLON+');'
FROM (SELECT O3.NAME F_NAME,O1.NAME M_TAB,O2.NAME F_TAB,
L1.NAME M_CLON,L2.NAME F_CLON
FROM SYSFOREIGNKEYS A, SYSOBJECTS O1,SYSOBJECTS O2 ,SYSOBJECTS O3,SYSCOLUMNS L1 ,SYSCOLUMNS L2
WHERE
A.CONSTID=O3.ID
AND A.FKEYID=O1.ID
AND A.RKEYID=O2.ID
AND L1.ID=O1.ID
AND L2.ID=O2.ID
AND A.FKEY=L1.COLID
AND A.RKEY=L2.COLID
AND O1.XTYPE='U'
AND O2.XTYPE='U')M ;
查询SqlServer下查询所有指定用户下的主外键,索引
最新推荐文章于 2023-06-26 18:57:12 发布