|
|
8000XXXX Errors ASP.NET 2.0 Classic ASP 1.0 Databases Access DB & ADO General SQL Server & Access Articles MySQL Other Articles Schema Tutorials Sql Server 2000 Sql Server 2005 General Concepts Search Engine Optimization (SEO)Search | ASP FAQ Tutorials :: Databases :: Schema Tutorials :: Schema: How do I list all the indexes in a database? Schema: How do I list all the indexes in a database?SQL Server 2000 Yes, you can use the following for a specific table:
This returns index_name, index_description, and index_keys. The index_description column tells whether or not the index is clustered, and which filegroup it resides on. The index_keys column tells you the column names that participate in the index, and from what I can tell, these are always in the order they are created (a negative symbol (-) denotes that the column is in DESC order). This is great, but does not provide all of the information I'm often looking for. In order to return everything I wanted to know about the indexes in my database, I needed to create a couple of extra helper functions. (Unfortunately, indexes are not covered in the INFORMATION_SCHEMA views, so we need to rely on system tables like sysindexes and sysfilegroups, and system functions like INDEXPROPERTY() and INDEX_COL().) The first function is not required, but makes the second function quite tidier, IMHO:
These functions are based largely on sp_helpindex, and while they avoid cursors, they are still not likely to be very efficient as the functions will need to be called multiple times. Now that we have these functions, we can create this view:
This will give you a handy resultset, but does not specify whether the index is a PRIMARY KEY CONSTRAINT. You can do that by joining against INFORMATION_SCHEMA.TABLE_CONSTRAINTS:
This doesn't take into account same-named tables owned by different users, but if you look at the alternative (see the source code for sp_pkeys), it is probably a valid solution for most of us, where dbo is the de facto owner of all objects. With that limitation in mind, we can take it one step further by generating the CREATE INDEX / ADD CONSTRAINT statements:
This is what I have to offer for now, and I realize it is pretty quick and dirty. I'll be working on a similar script using the SQL Server 2005 catalog views, but I'll save that for another day. MS Access Since Access stopped storing object names in its MSys* tables, it is nearly impossible to perform administrative tasks within the database itself, unless you want to point and click through a GUI. So I developed the following script with ADOX:
Related Articles Schema: How do I list the databases on my server? Schema: How do I show all the primary keys in a database? Schema: How do I show all the triggers in a database? Schema: How do I show the columns for a table? Schema: How do I show the description property of a column? Schema: How do I show the parameters for a function or stored procedure? Schema: How do I show the stored procedures in a database? Schema: How do I show the tables in a database? Schema: How do I show the user-defined functions (UDFs) in a database? Schema: How do I show the views in a SQL Server database? |