Skip to content
Advertisement

How to assign the IDs to the referring table and how to display this correctly? (SSMS)

I am in the process of creating an audit plan using ERD, going off the below image you can see that there’s a permissions table with four FK columns referring to the other four tables PK column. I am just confused as to how the IDs will relate to the other tables and how will it show up correctly in the permissions table?

For the Users table, I imported the data from ‘master.sys.server_principals. For the Instance table, I imported the data by using @@SERVERNAME. For the Databases table, I imported the data from master.sys.databases. For the Object Types table, I imported the data from master.sys.objects.

Now, I am currently on the permissions table and stuck at this point because I am wondering how will the IDs match from the four other tables (mentioned above and shown in the image link below) to this permissions table. I know I need to query from master.sys.database_permissions to get the information for both columns ‘Permissions_Permission_Name’ and ‘Permissions_Object_Name’ but it’s just the other four ID columns which I am confused about…(you can ignore the column Permissions_ID)

Image of ERD of Audit Plan

Advertisement

Answer

I’m going to use the Answer field, because there is no space in the Comment editor. This answer is an answer to only part your question, two of the four tables (Databases and Users) I can relate to system tables.

First and foremost: when filling in Id’s, you would generate the other table records first, keep the Identity Id’s generated, and finally create a new Permission record and fillin the correct indexes there, in each Id field. That counts for any such change when a table contains indexes to other tables. Suppose you know.

Issue is, your structure differs from the system tables. You will need more “permission” records than master.sys.database_permissions, because MsSQL registers these as permissions per principal (role) not permissions per user.

I solved two of the four:

  • The user is connected to a principal role via master.sys.database_role_members. The Id of the user role can be found in your source as master.sys.database_permissions.grantee_principal_id and the corresponding users that have this principal_id are listed in master.sys.database_role_members.

  • Your permission a database (ONE database) is defined in your Permission record. The database name in this database record should map to a database on your server. In that database, you will find database_permissions.sys.server_principals. users that have the permissions are (again) found in master.sys.database_role_members.

I’m not sure what you intend to do with the other 2 tables, Instances and Object Types.

Refer ms-docs about the subject at https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-permissions-transact-sql?view=sql-server-ver15

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement