[Turn] SQL statement: get the user name of the table, get all the information listed in the table, get the primary key column names in the table contained ...

1, to obtain the user name of the table:

SELECT name FROM sysobjects WHERE type = 'U' AND sysstat = '83'

Note: usually you need only type = 'U', but sometimes there will be a system table mixed in with them (do not know why), plus the latter sentence will be able to delete these tables the system

2, to obtain all the information listed in the table (containing data type name):

SELECT syscolumns.name,systypes.name,syscolumns.isnullable,syscolumns.length FROM syscolumns, systypes WHERE syscolumns.xusertype = systypes.xusertype AND "syscolumns.id = object_id('tableName')

Note points:
(1) highlight some important here to focus on content, selected several of information output.
(2) syscolumns table contains only the data type number, to get the full name from the need to find systypes table, the data type is typically used by the user corresponds with xusertype better, many cases do not appear.
(3) syscolumns.length obtained is the length of the physical memory, the display and other types nvarchar and varchar in the database of the half.

3, to obtain the primary key column name table included:

SELECT syscolumns.name FROM syscolumns,sysobjects,sysindexes,sysindexkeys WHERE syscolumns.id = object_id('tablename') AND sysobjects.xtype = 'PK' AND sysobjects.parent_obj = syscolumns.id AND sysindexes.id = syscolumns.id AND sysobjects.name = sysindexes.name AND sysindexkeys.id = syscolumns.id AND sysindexkeys.indid = sysindexes.indid AND syscolumns.colid = sysindexkeys.colid

Note: This is looking at four tables in the system, the relationship is more complex, generally can be expressed as:
the syscolumns table and column information in there id the table, sysobjects table there primary key name (ie PK_Table similar) and Table id , sysindexes there in the primary key table name and id and index number, sysindexkeys there in the table id and index number and column number, one by one after you will find correspondence column names, and call ~

Reproduced in: https: //www.cnblogs.com/sofire/archive/2010/07/07/1772623.html

Guess you like

Origin blog.csdn.net/weixin_34273481/article/details/92638216