Conttent Database plays a very important role in SharePoint.There are some cases when we need to look into and read from the content databases.
NOTE: Never update any SharePoint database directly. Always use the SharePoint API (Object Model) for any updates.
Before beginning the postmortem of sharepoint content database, lets focused on basic sharepoint tables as how excately they function.
Features:Table that holds information about all the activated features for each site collection or site.
Sites:Table that holds information about all the site collections for this content database.
Webs:Table that holds information about all the specific sites (webs) in each site collection.
UserInfo:Table that holds information about all the users for each site collection.
Groups:Table that holds information about all the SharePoint groups in each site collection.
Roles:Table that holds information about all the SharePoint roles (permission levels) for each site.
AllLists:Table that holds information about lists for each site.
GroupMembership:Table that holds information about all the SharePoint group members.
AllUserData:Table that holds information about all the list items for each list.
AllDocs:Table that holds information about all the documents (and all list items) for each document library and list.
Here are some common queries that we can run against the content databases.
--Query to get all the top level site collections
SELECT SiteId AS SiteGuid, Id AS WebGuid, FullUrl AS Url, Title, Author, TimeCreatedFROM dbo.WebsWHERE (ParentWebId IS NULL)
--Query to get all the SharePoint groups in a site collection
SELECT dbo.Webs.SiteId, dbo.Webs.Id, dbo.Webs.FullUrl, dbo.Webs.Title, dbo.Groups.ID AS Expr1, dbo.Groups.Title AS Expr2, dbo.Groups.DescriptionFROM dbo.Groups INNER JOINdbo.Webs ON dbo.Groups.SiteId = dbo.Webs.SiteId
--Query to get all the users in a site collection
SELECT dbo.Webs.SiteId, dbo.Webs.Id, dbo.Webs.FullUrl, dbo.Webs.Title, dbo.UserInfo.tp_ID, dbo.UserInfo.tp_DomainGroup, dbo.UserInfo.tp_SiteAdmin, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_EmailFROM dbo.UserInfo INNER JOINdbo.Webs ON dbo.UserInfo.tp_SiteID = dbo.Webs.SiteId
--Query to get all the members of the SharePoint Groups
SELECT dbo.Groups.ID, dbo.Groups.Title, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_LoginFROM dbo.GroupMembership INNER JOINdbo.Groups ON dbo.GroupMembership.SiteId = dbo.Groups.SiteId INNER JOINdbo.UserInfo ON dbo.GroupMembership.MemberId = dbo.UserInfo.tp_ID
--Query to get all the sites where a specific feature is activated
SELECT dbo.Webs.Id AS WebGuid, dbo.Webs.Title AS WebTitle, dbo.Webs.FullUrl AS WebUrl, dbo.Features.FeatureId, dbo.Features.TimeActivatedFROM dbo.Features INNER JOINdbo.Webs ON dbo.Features.SiteId = dbo.Webs.SiteId AND dbo.Features.WebId = dbo.Webs.IdWHERE (dbo.Features.FeatureId = '00BFEA71-D1CE-42de-9C63-A44004CE0104')
-- Query to get all the users assigned to roles
SELECT dbo.Webs.Id, dbo.Webs.Title, dbo.Webs.FullUrl, dbo.Roles.RoleId, dbo.Roles.Title AS RoleTitle, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_LoginFROM dbo.RoleAssignment INNER JOINdbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId INNER JOINdbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId AND dbo.Roles.WebId = dbo.Webs.Id INNER JOINdbo.UserInfo ON dbo.RoleAssignment.PrincipalId = dbo.UserInfo.tp_ID
--Query to get all the SharePoint groups assigned to roles
SELECT dbo.Webs.Id, dbo.Webs.Title, dbo.Webs.FullUrl, dbo.Roles.RoleId, dbo.Roles.Title AS RoleTitle, dbo.Groups.Title AS GroupNameFROM dbo.RoleAssignment INNER JOINdbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId INNER JOINdbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId AND dbo.Roles.WebId = dbo.Webs.Id INNER JOINdbo.Groups ON dbo.RoleAssignment.SiteId = dbo.Groups.SiteId AND dbo.RoleAssignment.PrincipalId = dbo.Groups.ID
These are just some of the common queries that I have used against the content database.
Enjoy!!!