Welcome to Day 13 of my “A Month of PowerShell” series. This series will use the series landing page on this blog at //blog.waynesheffield.com/wayne/a-month-of-powershell/. Please refer to this page to see all of the posts in this series, and to quickly go to them.

We have created a database named PoSh to play around with. But what use is an empty database? So, we’re going to start adding different objects. Today, we’re starting a mini-series of working with tables, where the next several days will be about tables.

Schemas

Unless you’re planning on putting everything into the default dbo schema, you’ll need to add a schema. Let’s start off by adding one named Common.

Tables

Now that we have a schema, it’s time to create a table. In this example, we’ll add to the above script to create a table in the “Common” schema named “TestTable”, with a few columns including an identity column and a clustered primary key on this column.

Most of the SMO classes have different overloaded constructors that allow for various parameters being supplied. For instance, this line from the above script:

This is equivalent to:

This is also equivalent to:

Note the data type designation for the columns in this table. The data type needs to come from the Microsoft.SqlServer.Management.Smo.Datatypes class. As demonstrated when creating a new column object, they can be put into a variable to use the variable, or you can specify the class. A complete list of available data types for use is available at http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.datatype.aspx.