SQLServer中一些查看表信息的sql语句

1、查看指定库下的所有表名:

use 库名;
SELECT name FROM sysobjects WHERE xtype = 'u';

2、查看库中与某个表有外键关联的表:

USE 库名;
SELECT    
     f.name AS foreignkeyName  --外键名
    ,OBJECT_NAME(f.parent_object_id) AS foreignkeyTable --外键表名
    ,OBJECT_NAME (f.referenced_object_id) AS primarykeyTable   --主键表名
    ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS columnname  --外键字段名
FROM sys.foreign_keys AS f 
INNER JOIN sys.foreign_key_columns AS fc  
ON f.object_id = fc.constraint_object_id  
WHERE f.parent_object_id = OBJECT_ID('表名');

3、查看库中包含某个字段的表:

select name from 库名.dbo.sysobjects where id in(select id from 库名.dbo.syscolumns Where name='字段名');

4、查看某个库下是否有指定表:

USE 库名;
IF Exists(select top 1 1 from sysObjects where Id=OBJECT_ID('表名') and xtype='U')
    print '存在'
else 
    print '不存在'

5、查询所有的系统基础表

SELECT name,type_desc FROM sys.objects WHERE type_desc = 'SYSTEM_TABLE'ORDER BY name

你可能感兴趣的:(SQL,Server)