{"id":4326,"date":"2013-01-31T17:20:22","date_gmt":"2013-01-31T17:20:22","guid":{"rendered":"http:\/\/craftydba.com\/?p=4326"},"modified":"2013-02-01T20:39:59","modified_gmt":"2013-02-01T20:39:59","slug":"schema-design-with-ssms","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=4326","title":{"rendered":"Schema Design with SSMS"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/objects_icon.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/objects_icon.jpg\" alt=\"\" title=\"objects_icon\" width=\"128\" height=\"128\" class=\"alignleft size-full wp-image-4330\" \/><\/a><\/p>\n<p>Most of the articles that I presented so far for database development have been centered around the exact Microsoft T-SQL syntax to perform a certain operation.  At the <a href=\"http:\/\/www.cs.uri.edu\/\">University of Rhode Island<\/a>, I learnt how to program in Pascal before typing in my first ANSI SQL statement.  In short, I will always be some type of developer in my heart.  <\/p>\n<p>However, many people do not come from a computer science and math background.  The SQL Server Management Studio (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb934498.aspx\">SSMS<\/a>) has menus and dialog boxes to achieve many of the same results.  <\/p>\n<p>Today, I am going to continue the series of articles that will recreate the [AUTOS] sample database while demonstrating how common T-SQL tasks in my <a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/02\/designing-with-ssms.sql_.txt\">script<\/a> can be done with SSMS.  I will be going over sections 1E in the sample code.<\/p>\n<p>I want to explore how the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189462.aspx\">CREATE SCHEMA<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173423.aspx\">ALTER SCHEMA<\/a>, and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186751.aspx\">DROP SCHEMA<\/a> data definition language (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Data_definition_language\">DDL<\/a>) constructs can be achieved with SSMS.<\/p>\n<p>A user defined schema allows a database administrator (DBA) to define security on groups of objects.  I think this is a very powerful but under utilized technique.<\/p>\n<p>The security node under the [AUTOS] database in the object explorer tree shows the default schema for any database.  As we know, every newly created object defaults to the [dbo] schema if not explicitly defined.  Exceptions to this rule are when a default schema other than [dbo] is selected when creating a database user.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-BEFORE.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-BEFORE.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-VIEW-SCHEMAS-BEFORE\" width=\"396\" height=\"522\" class=\"aligncenter size-full wp-image-4334\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-BEFORE.jpg 396w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-BEFORE-227x300.jpg 227w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><\/a><\/p>\n<p>I want to execute the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189462.aspx\">CREATE SCHEMA<\/a> statement twice in the [AUTOS] database: one for [ACTIVE] data and one for [STAGE] data.  <\/p>\n<p>The <span style=\"color: #FF0000;\">first step<\/span> is to right click the security node in the object explorer and select the New Schema menu option.  <\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-1.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-CREATE-SCHEMA-1\" width=\"400\" height=\"391\" class=\"aligncenter size-full wp-image-4338\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-1.jpg 400w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-1-300x293.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>The <span style=\"color: #FF0000;\">second step<\/span> is to choose a name and owner for the new schema.  I usually default schema ownership to [dbo].<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-2.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-CREATE-SCHEMA-2\" width=\"704\" height=\"633\" class=\"aligncenter size-full wp-image-4340\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-2.jpg 704w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-2-300x269.jpg 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/a><\/p>\n<p>Repeat these steps again for the [STAGE] schema.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-31.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-31.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-CREATE-SCHEMA-3\" width=\"705\" height=\"632\" class=\"aligncenter size-full wp-image-4344\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-31.jpg 705w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-CREATE-SCHEMA-31-300x268.jpg 300w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/a><\/p>\n<p>Taking another look at the security node under the [AUTOS] database in the object explorer show our two new SCHEMA.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-AFTER.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-AFTER.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-VIEW-SCHEMAS-AFTER\" width=\"401\" height=\"556\" class=\"aligncenter size-full wp-image-4346\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-AFTER.jpg 401w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-VIEW-SCHEMAS-AFTER-216x300.jpg 216w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><\/a><\/p>\n<p>What is the use of a schema without declaring a member object?<\/p>\n<p>I am jumping ahead in our series of SSMS talks to table creation.  Just run the T-SQL code in Section 1L to create the [MODELS] table on the [STAGE] schema.  Please see screen shot of table below.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-STAGE-SCHEMA-W-TABLE.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-STAGE-SCHEMA-W-TABLE.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-STAGE-SCHEMA-W-TABLE\" width=\"422\" height=\"249\" class=\"aligncenter size-full wp-image-4349\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-STAGE-SCHEMA-W-TABLE.jpg 422w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-STAGE-SCHEMA-W-TABLE-300x177.jpg 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/a><\/p>\n<p>Now, let&#8217;s see what happens when we try to execute the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186751.aspx\">DROP SCHEMA<\/a> statement on a schema that is bound to an object.  <\/p>\n<p>Find the security node in the object explorer and select the [STAGE] schema.  Right click and choose the delete menu option.  This will bring up the delete object dialog box.  Clicking OKAY will try to remove the schema.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-DROP-SCHEMA\" width=\"704\" height=\"631\" class=\"aligncenter size-full wp-image-4351\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA.jpg 704w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA-300x268.jpg 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/a><\/p>\n<p>The following error occurs when we try this action.  In short, the schema must be empty before we try this operation.  I will leave the task of dropping the [MODELS] table then the [STAGE] schema as an exercise that you can do.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA-2.jpg\" alt=\"\" title=\"SSMS-AUTOS-DB-DROP-SCHEMA-2\" width=\"706\" height=\"635\" class=\"aligncenter size-full wp-image-4358\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA-2.jpg 706w, https:\/\/craftydba.com\/wp-content\/uploads\/2013\/01\/SSMS-AUTOS-DB-DROP-SCHEMA-2-300x269.jpg 300w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/a><\/p>\n<p>In summary, we can do many of the Data Definition Language (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Data_definition_language\">DDL<\/a>) tasks that can be done in T-SQL. The cool script button shows you what SQL Server Management Studio (SSMS) will use for code to accomplish a particular task. Next time, I will be talking about creating server logins and database users with SSMS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the articles that I presented so far for database development have been centered around the exact Microsoft T-SQL syntax to perform a certain operation. At the University of Rhode Island, I learnt how to program in Pascal before typing in my first ANSI SQL statement. In short, I will always be some type of developer in my heart. However, many people do not come from a computer science and math background. The SQL Server Management Studio (SSMS) has menus and dialog boxes to achieve many of the same&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[55,53,31,391,748,54,12,15,28,741,470],"class_list":["post-4326","post","type-post","status-publish","format-standard","hentry","category-db-dev","tag-alter-schema","tag-create-schema","tag-database-developer","tag-ddl","tag-designing-schemas","tag-drop-schema","tag-free-code","tag-john-f-miner-iii","tag-sql-server","tag-sql-server-management-studio","tag-ssms"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/4326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4326"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/4326\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}