Listing indexes and constraints












7















I am looking at a SQL Server database for an application I have inherited. I have not looked into SQL Server for approximately 10 years, so please bear with me.



The database table I am looking at has a bigint NOT NULL column called id, yet, when I check for constraints, I don't see any, and the same holds true for all database tables.



Am I right in assuming that there is no primary key & no indexing (clustered or nonclustered) on these tables?



I ran the following queries and the results appear to confirm my suspicion:



//**returns 0**
select count(*) from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;

//**returns no rows**
select * from sys.indexes
where object_id = (select object_id from sys.objects where name = 'NAME-OF-TABLE');

//**returns all tables in database**
SELECT name
FROM sys.tables
WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0;









share|improve this question





























    7















    I am looking at a SQL Server database for an application I have inherited. I have not looked into SQL Server for approximately 10 years, so please bear with me.



    The database table I am looking at has a bigint NOT NULL column called id, yet, when I check for constraints, I don't see any, and the same holds true for all database tables.



    Am I right in assuming that there is no primary key & no indexing (clustered or nonclustered) on these tables?



    I ran the following queries and the results appear to confirm my suspicion:



    //**returns 0**
    select count(*) from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;

    //**returns no rows**
    select * from sys.indexes
    where object_id = (select object_id from sys.objects where name = 'NAME-OF-TABLE');

    //**returns all tables in database**
    SELECT name
    FROM sys.tables
    WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0;









    share|improve this question



























      7












      7








      7


      3






      I am looking at a SQL Server database for an application I have inherited. I have not looked into SQL Server for approximately 10 years, so please bear with me.



      The database table I am looking at has a bigint NOT NULL column called id, yet, when I check for constraints, I don't see any, and the same holds true for all database tables.



      Am I right in assuming that there is no primary key & no indexing (clustered or nonclustered) on these tables?



      I ran the following queries and the results appear to confirm my suspicion:



      //**returns 0**
      select count(*) from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;

      //**returns no rows**
      select * from sys.indexes
      where object_id = (select object_id from sys.objects where name = 'NAME-OF-TABLE');

      //**returns all tables in database**
      SELECT name
      FROM sys.tables
      WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0;









      share|improve this question
















      I am looking at a SQL Server database for an application I have inherited. I have not looked into SQL Server for approximately 10 years, so please bear with me.



      The database table I am looking at has a bigint NOT NULL column called id, yet, when I check for constraints, I don't see any, and the same holds true for all database tables.



      Am I right in assuming that there is no primary key & no indexing (clustered or nonclustered) on these tables?



      I ran the following queries and the results appear to confirm my suspicion:



      //**returns 0**
      select count(*) from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;

      //**returns no rows**
      select * from sys.indexes
      where object_id = (select object_id from sys.objects where name = 'NAME-OF-TABLE');

      //**returns all tables in database**
      SELECT name
      FROM sys.tables
      WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0;






      sql-server index primary-key constraint






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 14 '14 at 20:51









      Paul White

      50.2k14269435




      50.2k14269435










      asked Apr 14 '14 at 19:11









      ali haiderali haider

      138115




      138115






















          4 Answers
          4






          active

          oldest

          votes


















          7














          These two queries may help you. The first will list all of the tables and indexes on those tables in your database. If the table does not appear in the list is does not have any indexes defined on it. These queries assume SQL Server version 2005 or newer.



          SELECT 
          IndexName = QUOTENAME(I.name),
          TableName =
          QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
          N'.' + QUOTENAME(T.name),
          IsPrimaryKey = I.is_primary_key
          FROM sys.indexes AS I
          INNER JOIN sys.tables AS T
          ON I.[object_id] = T.[object_id]
          WHERE
          I.type_desc <> N'HEAP'
          ORDER BY
          TableName ASC,
          IndexName ASC;


          The second query will report for each table the identity column, if any on each table in your database.



          SELECT
          TableName =
          QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
          N'.' + QUOTENAME(T.name),
          IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
          FROM sys.tables AS T
          LEFT OUTER JOIN sys.columns AS C
          ON T.[object_id] = C.[object_id]
          AND C.is_identity = 1
          ORDER BY
          TableName ASC;


          To limit the queries to a specific table add a WHERE clause similar to:



          WHERE T.name = N'NAME-OF-TABLE'





          share|improve this answer

































            2














            No, something is incorrect.



            The check on sys.indexes should return a row even if your table has no indexes. The heap still has a record in sys.indexes with a type_desc of 'HEAP' and type of 0.



            I think you probably need to make sure you are in the right database context since OBJECT_ID() and sys.objects are database-specific.



            Try this:



            USE MyDatabase

            SELECT *
            FROM sys.indexes
            WHERE object_id = OBJECT_ID('schema.MyTableName')





            share|improve this answer































              1














              I am not sure if you're interested in all constraints but INFORMATION_SCHEMA.TABLE_CONSTRAINTS doesn't seem to return the DEFAULT constraints -- TABLE_CONSTRAINTS (Transact-SQL)




              CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY




              This query will do a simple count against the sys.objects DMV:



              select COUNT(*)
              from sys.objects o
              where o.type_desc like '%CONSTRAINT%';


              If you are interested in listing the tables, you could run something like this:



              select distinct
              o.object_id
              , QUOTENAME(s.name) + '.' + QUOTENAME(o.name) as [object_name]
              , o.type_desc
              , case when dc.parent_object_id is null then 'No' else 'Yes' end as has_default_constraint
              , case when cc.parent_object_id is null then 'No' else 'Yes' end as has_check_constraint
              , case when fk.parent_object_id is null then 'No' else 'Yes' end as has_foreing_key
              , case when kc.parent_object_id is null then 'No' else 'Yes' end as has_primary_key
              from sys.objects o
              inner join sys.schemas s on s.schema_id = o.schema_id
              left outer join sys.default_constraints dc on dc.parent_object_id = o.object_id and dc.schema_id = o.schema_id
              left outer join sys.check_constraints cc on cc.parent_object_id = o.object_id and cc.schema_id = o.schema_id
              left outer join sys.foreign_keys fk on fk.parent_object_id = o.object_id and fk.schema_id = o.schema_id
              left outer join sys.key_constraints kc on kc.parent_object_id = o.object_id and kc.schema_id = o.schema_id
              where o.is_ms_shipped = 0
              and o.type = 'U'
              order by [object_name];


              This one should give you the info on your indexes:



              select o.name
              , i.*
              from sys.objects o
              inner join sys.indexes i on i.object_id = o.object_id
              where o.is_ms_shipped = 0
              and i.object_id > 100
              and i.index_id > 0
              order by o.name
              , i.index_id;



              • Index_Id = 0 -- HEAP (won't show up)

              • Index_Id = 1 -- CLUSTERED

              • Index_Id > 1 -- NONCLUSTERED






              share|improve this answer
























              • could you explain why you have object_id > 100 ?

                – brianc
                Oct 5 '17 at 20:55











              • @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                – DenisT
                Nov 1 '17 at 11:53



















              0














              Well Explained Above Article
              Keep Posting such Good Things



              https://www.zonalstudy.com/download-electrical-power-by-w-j-r-h-pooler-pdf/





              share








              New contributor




              Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "182"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f63185%2flisting-indexes-and-constraints%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                7














                These two queries may help you. The first will list all of the tables and indexes on those tables in your database. If the table does not appear in the list is does not have any indexes defined on it. These queries assume SQL Server version 2005 or newer.



                SELECT 
                IndexName = QUOTENAME(I.name),
                TableName =
                QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                N'.' + QUOTENAME(T.name),
                IsPrimaryKey = I.is_primary_key
                FROM sys.indexes AS I
                INNER JOIN sys.tables AS T
                ON I.[object_id] = T.[object_id]
                WHERE
                I.type_desc <> N'HEAP'
                ORDER BY
                TableName ASC,
                IndexName ASC;


                The second query will report for each table the identity column, if any on each table in your database.



                SELECT
                TableName =
                QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                N'.' + QUOTENAME(T.name),
                IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
                FROM sys.tables AS T
                LEFT OUTER JOIN sys.columns AS C
                ON T.[object_id] = C.[object_id]
                AND C.is_identity = 1
                ORDER BY
                TableName ASC;


                To limit the queries to a specific table add a WHERE clause similar to:



                WHERE T.name = N'NAME-OF-TABLE'





                share|improve this answer






























                  7














                  These two queries may help you. The first will list all of the tables and indexes on those tables in your database. If the table does not appear in the list is does not have any indexes defined on it. These queries assume SQL Server version 2005 or newer.



                  SELECT 
                  IndexName = QUOTENAME(I.name),
                  TableName =
                  QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                  N'.' + QUOTENAME(T.name),
                  IsPrimaryKey = I.is_primary_key
                  FROM sys.indexes AS I
                  INNER JOIN sys.tables AS T
                  ON I.[object_id] = T.[object_id]
                  WHERE
                  I.type_desc <> N'HEAP'
                  ORDER BY
                  TableName ASC,
                  IndexName ASC;


                  The second query will report for each table the identity column, if any on each table in your database.



                  SELECT
                  TableName =
                  QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                  N'.' + QUOTENAME(T.name),
                  IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
                  FROM sys.tables AS T
                  LEFT OUTER JOIN sys.columns AS C
                  ON T.[object_id] = C.[object_id]
                  AND C.is_identity = 1
                  ORDER BY
                  TableName ASC;


                  To limit the queries to a specific table add a WHERE clause similar to:



                  WHERE T.name = N'NAME-OF-TABLE'





                  share|improve this answer




























                    7












                    7








                    7







                    These two queries may help you. The first will list all of the tables and indexes on those tables in your database. If the table does not appear in the list is does not have any indexes defined on it. These queries assume SQL Server version 2005 or newer.



                    SELECT 
                    IndexName = QUOTENAME(I.name),
                    TableName =
                    QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                    N'.' + QUOTENAME(T.name),
                    IsPrimaryKey = I.is_primary_key
                    FROM sys.indexes AS I
                    INNER JOIN sys.tables AS T
                    ON I.[object_id] = T.[object_id]
                    WHERE
                    I.type_desc <> N'HEAP'
                    ORDER BY
                    TableName ASC,
                    IndexName ASC;


                    The second query will report for each table the identity column, if any on each table in your database.



                    SELECT
                    TableName =
                    QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                    N'.' + QUOTENAME(T.name),
                    IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
                    FROM sys.tables AS T
                    LEFT OUTER JOIN sys.columns AS C
                    ON T.[object_id] = C.[object_id]
                    AND C.is_identity = 1
                    ORDER BY
                    TableName ASC;


                    To limit the queries to a specific table add a WHERE clause similar to:



                    WHERE T.name = N'NAME-OF-TABLE'





                    share|improve this answer















                    These two queries may help you. The first will list all of the tables and indexes on those tables in your database. If the table does not appear in the list is does not have any indexes defined on it. These queries assume SQL Server version 2005 or newer.



                    SELECT 
                    IndexName = QUOTENAME(I.name),
                    TableName =
                    QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                    N'.' + QUOTENAME(T.name),
                    IsPrimaryKey = I.is_primary_key
                    FROM sys.indexes AS I
                    INNER JOIN sys.tables AS T
                    ON I.[object_id] = T.[object_id]
                    WHERE
                    I.type_desc <> N'HEAP'
                    ORDER BY
                    TableName ASC,
                    IndexName ASC;


                    The second query will report for each table the identity column, if any on each table in your database.



                    SELECT
                    TableName =
                    QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
                    N'.' + QUOTENAME(T.name),
                    IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
                    FROM sys.tables AS T
                    LEFT OUTER JOIN sys.columns AS C
                    ON T.[object_id] = C.[object_id]
                    AND C.is_identity = 1
                    ORDER BY
                    TableName ASC;


                    To limit the queries to a specific table add a WHERE clause similar to:



                    WHERE T.name = N'NAME-OF-TABLE'






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 26 '16 at 13:53









                    Paul White

                    50.2k14269435




                    50.2k14269435










                    answered Apr 14 '14 at 20:40









                    JeremyJeremy

                    41026




                    41026

























                        2














                        No, something is incorrect.



                        The check on sys.indexes should return a row even if your table has no indexes. The heap still has a record in sys.indexes with a type_desc of 'HEAP' and type of 0.



                        I think you probably need to make sure you are in the right database context since OBJECT_ID() and sys.objects are database-specific.



                        Try this:



                        USE MyDatabase

                        SELECT *
                        FROM sys.indexes
                        WHERE object_id = OBJECT_ID('schema.MyTableName')





                        share|improve this answer




























                          2














                          No, something is incorrect.



                          The check on sys.indexes should return a row even if your table has no indexes. The heap still has a record in sys.indexes with a type_desc of 'HEAP' and type of 0.



                          I think you probably need to make sure you are in the right database context since OBJECT_ID() and sys.objects are database-specific.



                          Try this:



                          USE MyDatabase

                          SELECT *
                          FROM sys.indexes
                          WHERE object_id = OBJECT_ID('schema.MyTableName')





                          share|improve this answer


























                            2












                            2








                            2







                            No, something is incorrect.



                            The check on sys.indexes should return a row even if your table has no indexes. The heap still has a record in sys.indexes with a type_desc of 'HEAP' and type of 0.



                            I think you probably need to make sure you are in the right database context since OBJECT_ID() and sys.objects are database-specific.



                            Try this:



                            USE MyDatabase

                            SELECT *
                            FROM sys.indexes
                            WHERE object_id = OBJECT_ID('schema.MyTableName')





                            share|improve this answer













                            No, something is incorrect.



                            The check on sys.indexes should return a row even if your table has no indexes. The heap still has a record in sys.indexes with a type_desc of 'HEAP' and type of 0.



                            I think you probably need to make sure you are in the right database context since OBJECT_ID() and sys.objects are database-specific.



                            Try this:



                            USE MyDatabase

                            SELECT *
                            FROM sys.indexes
                            WHERE object_id = OBJECT_ID('schema.MyTableName')






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 14 '14 at 19:46









                            JNKJNK

                            15.7k55091




                            15.7k55091























                                1














                                I am not sure if you're interested in all constraints but INFORMATION_SCHEMA.TABLE_CONSTRAINTS doesn't seem to return the DEFAULT constraints -- TABLE_CONSTRAINTS (Transact-SQL)




                                CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY




                                This query will do a simple count against the sys.objects DMV:



                                select COUNT(*)
                                from sys.objects o
                                where o.type_desc like '%CONSTRAINT%';


                                If you are interested in listing the tables, you could run something like this:



                                select distinct
                                o.object_id
                                , QUOTENAME(s.name) + '.' + QUOTENAME(o.name) as [object_name]
                                , o.type_desc
                                , case when dc.parent_object_id is null then 'No' else 'Yes' end as has_default_constraint
                                , case when cc.parent_object_id is null then 'No' else 'Yes' end as has_check_constraint
                                , case when fk.parent_object_id is null then 'No' else 'Yes' end as has_foreing_key
                                , case when kc.parent_object_id is null then 'No' else 'Yes' end as has_primary_key
                                from sys.objects o
                                inner join sys.schemas s on s.schema_id = o.schema_id
                                left outer join sys.default_constraints dc on dc.parent_object_id = o.object_id and dc.schema_id = o.schema_id
                                left outer join sys.check_constraints cc on cc.parent_object_id = o.object_id and cc.schema_id = o.schema_id
                                left outer join sys.foreign_keys fk on fk.parent_object_id = o.object_id and fk.schema_id = o.schema_id
                                left outer join sys.key_constraints kc on kc.parent_object_id = o.object_id and kc.schema_id = o.schema_id
                                where o.is_ms_shipped = 0
                                and o.type = 'U'
                                order by [object_name];


                                This one should give you the info on your indexes:



                                select o.name
                                , i.*
                                from sys.objects o
                                inner join sys.indexes i on i.object_id = o.object_id
                                where o.is_ms_shipped = 0
                                and i.object_id > 100
                                and i.index_id > 0
                                order by o.name
                                , i.index_id;



                                • Index_Id = 0 -- HEAP (won't show up)

                                • Index_Id = 1 -- CLUSTERED

                                • Index_Id > 1 -- NONCLUSTERED






                                share|improve this answer
























                                • could you explain why you have object_id > 100 ?

                                  – brianc
                                  Oct 5 '17 at 20:55











                                • @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                                  – DenisT
                                  Nov 1 '17 at 11:53
















                                1














                                I am not sure if you're interested in all constraints but INFORMATION_SCHEMA.TABLE_CONSTRAINTS doesn't seem to return the DEFAULT constraints -- TABLE_CONSTRAINTS (Transact-SQL)




                                CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY




                                This query will do a simple count against the sys.objects DMV:



                                select COUNT(*)
                                from sys.objects o
                                where o.type_desc like '%CONSTRAINT%';


                                If you are interested in listing the tables, you could run something like this:



                                select distinct
                                o.object_id
                                , QUOTENAME(s.name) + '.' + QUOTENAME(o.name) as [object_name]
                                , o.type_desc
                                , case when dc.parent_object_id is null then 'No' else 'Yes' end as has_default_constraint
                                , case when cc.parent_object_id is null then 'No' else 'Yes' end as has_check_constraint
                                , case when fk.parent_object_id is null then 'No' else 'Yes' end as has_foreing_key
                                , case when kc.parent_object_id is null then 'No' else 'Yes' end as has_primary_key
                                from sys.objects o
                                inner join sys.schemas s on s.schema_id = o.schema_id
                                left outer join sys.default_constraints dc on dc.parent_object_id = o.object_id and dc.schema_id = o.schema_id
                                left outer join sys.check_constraints cc on cc.parent_object_id = o.object_id and cc.schema_id = o.schema_id
                                left outer join sys.foreign_keys fk on fk.parent_object_id = o.object_id and fk.schema_id = o.schema_id
                                left outer join sys.key_constraints kc on kc.parent_object_id = o.object_id and kc.schema_id = o.schema_id
                                where o.is_ms_shipped = 0
                                and o.type = 'U'
                                order by [object_name];


                                This one should give you the info on your indexes:



                                select o.name
                                , i.*
                                from sys.objects o
                                inner join sys.indexes i on i.object_id = o.object_id
                                where o.is_ms_shipped = 0
                                and i.object_id > 100
                                and i.index_id > 0
                                order by o.name
                                , i.index_id;



                                • Index_Id = 0 -- HEAP (won't show up)

                                • Index_Id = 1 -- CLUSTERED

                                • Index_Id > 1 -- NONCLUSTERED






                                share|improve this answer
























                                • could you explain why you have object_id > 100 ?

                                  – brianc
                                  Oct 5 '17 at 20:55











                                • @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                                  – DenisT
                                  Nov 1 '17 at 11:53














                                1












                                1








                                1







                                I am not sure if you're interested in all constraints but INFORMATION_SCHEMA.TABLE_CONSTRAINTS doesn't seem to return the DEFAULT constraints -- TABLE_CONSTRAINTS (Transact-SQL)




                                CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY




                                This query will do a simple count against the sys.objects DMV:



                                select COUNT(*)
                                from sys.objects o
                                where o.type_desc like '%CONSTRAINT%';


                                If you are interested in listing the tables, you could run something like this:



                                select distinct
                                o.object_id
                                , QUOTENAME(s.name) + '.' + QUOTENAME(o.name) as [object_name]
                                , o.type_desc
                                , case when dc.parent_object_id is null then 'No' else 'Yes' end as has_default_constraint
                                , case when cc.parent_object_id is null then 'No' else 'Yes' end as has_check_constraint
                                , case when fk.parent_object_id is null then 'No' else 'Yes' end as has_foreing_key
                                , case when kc.parent_object_id is null then 'No' else 'Yes' end as has_primary_key
                                from sys.objects o
                                inner join sys.schemas s on s.schema_id = o.schema_id
                                left outer join sys.default_constraints dc on dc.parent_object_id = o.object_id and dc.schema_id = o.schema_id
                                left outer join sys.check_constraints cc on cc.parent_object_id = o.object_id and cc.schema_id = o.schema_id
                                left outer join sys.foreign_keys fk on fk.parent_object_id = o.object_id and fk.schema_id = o.schema_id
                                left outer join sys.key_constraints kc on kc.parent_object_id = o.object_id and kc.schema_id = o.schema_id
                                where o.is_ms_shipped = 0
                                and o.type = 'U'
                                order by [object_name];


                                This one should give you the info on your indexes:



                                select o.name
                                , i.*
                                from sys.objects o
                                inner join sys.indexes i on i.object_id = o.object_id
                                where o.is_ms_shipped = 0
                                and i.object_id > 100
                                and i.index_id > 0
                                order by o.name
                                , i.index_id;



                                • Index_Id = 0 -- HEAP (won't show up)

                                • Index_Id = 1 -- CLUSTERED

                                • Index_Id > 1 -- NONCLUSTERED






                                share|improve this answer













                                I am not sure if you're interested in all constraints but INFORMATION_SCHEMA.TABLE_CONSTRAINTS doesn't seem to return the DEFAULT constraints -- TABLE_CONSTRAINTS (Transact-SQL)




                                CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY




                                This query will do a simple count against the sys.objects DMV:



                                select COUNT(*)
                                from sys.objects o
                                where o.type_desc like '%CONSTRAINT%';


                                If you are interested in listing the tables, you could run something like this:



                                select distinct
                                o.object_id
                                , QUOTENAME(s.name) + '.' + QUOTENAME(o.name) as [object_name]
                                , o.type_desc
                                , case when dc.parent_object_id is null then 'No' else 'Yes' end as has_default_constraint
                                , case when cc.parent_object_id is null then 'No' else 'Yes' end as has_check_constraint
                                , case when fk.parent_object_id is null then 'No' else 'Yes' end as has_foreing_key
                                , case when kc.parent_object_id is null then 'No' else 'Yes' end as has_primary_key
                                from sys.objects o
                                inner join sys.schemas s on s.schema_id = o.schema_id
                                left outer join sys.default_constraints dc on dc.parent_object_id = o.object_id and dc.schema_id = o.schema_id
                                left outer join sys.check_constraints cc on cc.parent_object_id = o.object_id and cc.schema_id = o.schema_id
                                left outer join sys.foreign_keys fk on fk.parent_object_id = o.object_id and fk.schema_id = o.schema_id
                                left outer join sys.key_constraints kc on kc.parent_object_id = o.object_id and kc.schema_id = o.schema_id
                                where o.is_ms_shipped = 0
                                and o.type = 'U'
                                order by [object_name];


                                This one should give you the info on your indexes:



                                select o.name
                                , i.*
                                from sys.objects o
                                inner join sys.indexes i on i.object_id = o.object_id
                                where o.is_ms_shipped = 0
                                and i.object_id > 100
                                and i.index_id > 0
                                order by o.name
                                , i.index_id;



                                • Index_Id = 0 -- HEAP (won't show up)

                                • Index_Id = 1 -- CLUSTERED

                                • Index_Id > 1 -- NONCLUSTERED







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Apr 14 '14 at 20:20









                                DenisTDenisT

                                580136




                                580136













                                • could you explain why you have object_id > 100 ?

                                  – brianc
                                  Oct 5 '17 at 20:55











                                • @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                                  – DenisT
                                  Nov 1 '17 at 11:53



















                                • could you explain why you have object_id > 100 ?

                                  – brianc
                                  Oct 5 '17 at 20:55











                                • @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                                  – DenisT
                                  Nov 1 '17 at 11:53

















                                could you explain why you have object_id > 100 ?

                                – brianc
                                Oct 5 '17 at 20:55





                                could you explain why you have object_id > 100 ?

                                – brianc
                                Oct 5 '17 at 20:55













                                @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                                – DenisT
                                Nov 1 '17 at 11:53





                                @bluevoodoo1 -- not mandatory but < 100 are the system objects but since using o.is_ms_shipped = 0, should not include them anyway. Just playing safe, that's all

                                – DenisT
                                Nov 1 '17 at 11:53











                                0














                                Well Explained Above Article
                                Keep Posting such Good Things



                                https://www.zonalstudy.com/download-electrical-power-by-w-j-r-h-pooler-pdf/





                                share








                                New contributor




                                Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.

























                                  0














                                  Well Explained Above Article
                                  Keep Posting such Good Things



                                  https://www.zonalstudy.com/download-electrical-power-by-w-j-r-h-pooler-pdf/





                                  share








                                  New contributor




                                  Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.























                                    0












                                    0








                                    0







                                    Well Explained Above Article
                                    Keep Posting such Good Things



                                    https://www.zonalstudy.com/download-electrical-power-by-w-j-r-h-pooler-pdf/





                                    share








                                    New contributor




                                    Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.










                                    Well Explained Above Article
                                    Keep Posting such Good Things



                                    https://www.zonalstudy.com/download-electrical-power-by-w-j-r-h-pooler-pdf/






                                    share








                                    New contributor




                                    Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.








                                    share


                                    share






                                    New contributor




                                    Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered 6 mins ago









                                    RajRaj

                                    1




                                    1




                                    New contributor




                                    Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Database Administrators Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f63185%2flisting-indexes-and-constraints%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        الفوسفات في المغرب

                                        Four equal circles intersect: What is the area of the small shaded portion and its height

                                        جامعة ليفربول