{"id":987,"date":"2004-12-03T18:18:00","date_gmt":"2004-12-03T18:18:00","guid":{"rendered":"\/blogs\/bobb\/post\/Schemas-Users-and-Objects-III.aspx"},"modified":"2004-12-03T18:18:00","modified_gmt":"2004-12-03T18:18:00","slug":"schemas-users-and-objects-iii","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/","title":{"rendered":"Schemas, Users, and Objects &#8211; III"},"content":{"rendered":"<p>\nNow, back to our regularly scheduled technical content. About schemas, users, and owners.\n<\/p>\n<p>\nAlthough Ed originally created the table, since Fred is the schema owner, Fred owns to table. Ed can get ownership of the table in either of two ways.\n<\/p>\n<p>\n1. Someone with authority can alter the table&#39;s owner<br \/>\n2. Someone with authority can give Ed &quot;take ownership&quot; permission on the table\n<\/p>\n<p>\nUntil Ed has &quot;take ownership&quot; permission, he does not and cannot &quot;own&quot; the table he just created.\n<\/p>\n<p>\nThere are two ways to tell who owns a table. If you know who the schema owner is, &quot;select * from sys.tables&quot; produces a column named principal_id. If the principal_id is NULL, then the table is owned by the schema owner. If the principal_id is not NULL, the table has a specific owner. The other way is to use the OBJECTPROPERTY function and look for the property &#39;OwnerId&#39;. This gives the exact owner, whether or not it&#39;s the schema owner.\n<\/p>\n<p>\nThis matters because, if you change the schema owner, the owner of the tables with NULL in prinipal_id changes. The owner of &quot;specific-owner&quot; tables does NOT change. So if the schema owner changes, say, to DBO, then DBO owns all the tables in the schema. BUT does not own Ed&#39;s table.\n<\/p>\n<p>\nThis whole thing is made possible because of the interesting meld that had to happen between a SQL Server-specific feature (ownership chains) and a new SQL2003-compliant feature (separation of users and schemas). Is this clear as a bell, now?\n<\/p>\n<p>\nJust in case you don&#39;t believe it, code below (picks up where other code left off):\n<\/p>\n<p>\n&#8212; snip (when I left off, I was Ed)<br \/>\n&#8212; ed cannot get ownership of table<br \/>\n&#8212; this fails<br \/>\nalter authorization on object::fredstuff.edtab to ed<br \/>\ngo\n<\/p>\n<p>\n&#8212; back to dbo<br \/>\nsetuser <br \/>\ngo\n<\/p>\n<p>\n&#8212; dbo can give the table to ed<br \/>\n&#8212; alter authorization on object::fredstuff.edtab to ed<br \/>\n&#8212; go\n<\/p>\n<p>\n&#8212; or dbo can give ed &#39;take ownership&#39; permission<br \/>\ngrant take ownership on fredstuff.edtab to ed<br \/>\ngo\n<\/p>\n<p>\nsetuser &#39;ed&#39;<br \/>\ngo\n<\/p>\n<p>\n&#8212; now this works for ed, because he has &#39;take ownership&#39;<br \/>\nalter authorization on object::fredstuff.edtab to ed<br \/>\ngo\n<\/p>\n<p>\n&#8212; now ed can SELECT the table<br \/>\nselect * from fredstuff.edtab<br \/>\ngo\n<\/p>\n<p>\n&#8212; ed creates another table in the schema<br \/>\ncreate table fredstuff.table1 (id int)<br \/>\ngo\n<\/p>\n<p>\nsetuser <br \/>\ngo\n<\/p>\n<p>\n&#8212; note that edtab has a principal_id (ed&#39;s)<br \/>\n&#8212; note that table1 (owned by schema owner) has NULL principal_id<br \/>\nselect * from sys.database_principals<br \/>\nselect * from sys.tables<br \/>\ngo\n<\/p>\n<p>\n&#8212; owned by &#39;fred&#39; (schema owner)<br \/>\nselect objectproperty(object_id(&#39;fredstuff.table1&#39;), &#39;OwnerId&#39;)<br \/>\n&#8212; owned by &#39;ed&#39;<br \/>\nselect objectproperty(object_id(&#39;fredstuff.edtab&#39;), &#39;OwnerId&#39;)<br \/>\ngo\n<\/p>\n<p>\nsetuser &#39;fred&#39;<br \/>\ngo\n<\/p>\n<p>\n&#8212; so can fred SELECT both tables<br \/>\n&#8212; because fred is the schema owner<br \/>\nselect * from fredstuff.edtab<br \/>\nselect * from fredstuff.table1<br \/>\ngo\n<\/p>\n<p>\nsetuser <br \/>\ngo\n<\/p>\n<p>\nalter authorization on schema::fredstuff to dbo<br \/>\ngo\n<\/p>\n<p>\nsetuser &#39;fred&#39;<br \/>\ngo<br \/>\n&#8212; no access for fred on this table<br \/>\nselect * from fredstuff.edtab<br \/>\n&#8212; access for fred on this table<br \/>\nselect * from fredstuff.table1<br \/>\ngo\n<\/p>\n<p>\nsetuser <br \/>\ngo<br \/>\nsetuser &#39;ed&#39;<br \/>\ngo<br \/>\n&#8212; access for ed, he&#39;s still the owner<br \/>\nselect * from fredstuff.edtab<br \/>\n&#8212; never had access to this table<br \/>\nselect * from fredstuff.table1<br \/>\ngo\n<\/p>\n<p>\nsetuser <br \/>\ngo\n<\/p>\n<p>\n&#8212; note that edtab has a principal_id (ed&#39;s)<br \/>\n&#8212; note that table1 (owned by schema owner) has NULL principal_id<br \/>\nselect * from sys.tables<br \/>\ngo\n<\/p>\n<p>\nselect * from sys.database_principals<br \/>\n&#8212; owned by &#39;dbo&#39; (schema owner), this changed<br \/>\nselect objectproperty(object_id(&#39;fredstuff.table1&#39;), &#39;OwnerId&#39;)<br \/>\n&#8212; owned by &#39;ed&#39;, this did not change\n<\/p>\n<p>\nselect objectproperty(object_id(&#39;fredstuff.edtab&#39;), &#39;OwnerId&#39;)<br \/>\ngo\n<\/p>\n<p>\n&#8212; snip &#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now, back to our regularly scheduled technical content. About schemas, users, and owners. Although Ed originally created the table, since Fred is the schema owner, Fred owns to table. Ed can get ownership of the table in either of two ways. 1. Someone with authority can alter the table&#39;s owner 2. Someone with authority can [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,28,34],"tags":[],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-security","category-sql-server-2005","category-sql-server-schemas"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Schemas, Users, and Objects - III - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Schemas, Users, and Objects - III - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"Now, back to our regularly scheduled technical content. About schemas, users, and owners. Although Ed originally created the table, since Fred is the schema owner, Fred owns to table. Ed can get ownership of the table in either of two ways. 1. Someone with authority can alter the table&#039;s owner 2. Someone with authority can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2004-12-03T18:18:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/\",\"name\":\"Schemas, Users, and Objects - III - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2004-12-03T18:18:00+00:00\",\"dateModified\":\"2004-12-03T18:18:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Security\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/security\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Schemas, Users, and Objects &#8211; III\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Schemas, Users, and Objects - III - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/","og_locale":"en_US","og_type":"article","og_title":"Schemas, Users, and Objects - III - Bob Beauchemin","og_description":"Now, back to our regularly scheduled technical content. About schemas, users, and owners. Although Ed originally created the table, since Fred is the schema owner, Fred owns to table. Ed can get ownership of the table in either of two ways. 1. Someone with authority can alter the table&#39;s owner 2. Someone with authority can [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/","og_site_name":"Bob Beauchemin","article_published_time":"2004-12-03T18:18:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/","name":"Schemas, Users, and Objects - III - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2004-12-03T18:18:00+00:00","dateModified":"2004-12-03T18:18:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/schemas-users-and-objects-iii\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Security","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/security\/"},{"@type":"ListItem","position":3,"name":"Schemas, Users, and Objects &#8211; III"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=987"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}