Posts Categorizados ‘Foreign Key

15
fev
12

Criando Constraints e Índices no SQL Server com PowerShell

Para continuar com o prometido no primeiro artigo desta série de artigos sobre SMO, teremos a versão em PowerShell do artigo desta semana, que ao meu ver não ficou tão simples quanto o mesmo código escrito em .NET, mas permite ver como PowerShell pode ser mais simples que .NET em alguns momentos, e em outros nem tanto.

Continuar lendo ‘Criando Constraints e Índices no SQL Server com PowerShell’

15
fev
12

Criando Constraints e Índices no SQL Server com .NET

No artigo da semana passada vimos como criar colunas e tabelas com .NET e com PowerShell, nesta semana veremos como é possível também criar Constraints (Foreign Key, Default, Unique Key, Primary Key) e Índices com SMO.

Inicialmente, criaremos duas tabelas bem simples:

const string serverName = @".\SQLEXPRESS";

var server = new Server(serverName);

var database = new Database(server, "Estoque");
database.Create();

// CREATE DATABASE [Estoque] ...
var table = new Table(database, "Produto", "dbo");
table.Columns.Add(new Column(table, "Id", DataType.Int) { Nullable = false, Identity = true });
table.Columns.Add(new Column(table, "Nome", DataType.VarChar(250)) { Nullable = false });
table.Columns.Add(new Column(table, "Quantidade", DataType.Int) { Nullable = false });
table.Columns.Add(new Column(table, "Valor", DataType.Money) { Nullable = false });
table.Columns.Add(new Column(table, "Peso", DataType.Decimal(2, 5)) { Nullable = false });
table.Columns.Add(new Column(table, "CategoriaId", DataType.Int) { Nullable = false });
table.Columns.Add(new Column(table, "DataCadastro", DataType.DateTime) { Nullable = false });
table.Create();

// CREATE TABLE [dbo].[Categoria] ...
var table2 = new Table(database, "Categoria", "dbo");
table2.Columns.Add(new Column(table2, "Id", DataType.Int) { Nullable = false, Identity = true });
table2.Columns.Add(new Column(table2, "Nome", DataType.VarChar(250)) { Nullable = false });
table2.Create();

E em seguida, criamos as Primary Keys destas tabelas, por meio de um objeto Index, especificado como DriPrimaryKey:

// ALTER TABLE [dbo].[Produto]
//   ADD CONSTRAINT [PK_Produto] PRIMARY KEY ([Id])
var pk = new Index(table, "PK_Produto") { IndexKeyType = IndexKeyType.DriPrimaryKey };
pk.IndexedColumns.Add(new IndexedColumn(pk, "Id"));
pk.Create();

// ALTER TABLE [dbo].[Categoria]
//   ADD CONSTRAINT [PK_Categoriao] PRIMARY KEY (Id)
var pk2 = new Index(table2, "PK_Categoria") { IndexKeyType = IndexKeyType.DriPrimaryKey };
pk2.IndexedColumns.Add(new IndexedColumn(pk2, "Id"));
pk2.Create();

No caso das Unique Keys, é possível utilizar também de um objeto Index, agora como DriUniqueKey:

// ALTER TABLE [dbo].[Produto]
//   ADD CONSTRAINT [UK_Produto_Nome] UNIQUE ([Nome])
var uk = new Index(table, "UK_Produto_Nome") { IndexKeyType = IndexKeyType.DriUniqueKey };
uk.IndexedColumns.Add(new IndexedColumn(uk, "Nome"));
uk.Create();

Para a criação de índices, criamos um objeto Index sem especificar a propriedade IndexKeyType, e definimos quais as colunas que o compõe, assim como as colunas inclusas (IsIncluded):

// CREATE INDEX [IX_Produto_NmQtVl]
//   ON [dbo].[Produto] ([Nome], [Quantidade], [Valor])
//   INCLUDE ([Peso])
var ix = new Index(table, "IX_Produto_NmQtV");
ix.IndexedColumns.Add(new IndexedColumn(ix, "Nome"));
ix.IndexedColumns.Add(new IndexedColumn(ix, "Quantidade"));
ix.IndexedColumns.Add(new IndexedColumn(ix, "Valor"));
ix.IndexedColumns.Add(new IndexedColumn(ix, "Peso") { IsIncluded = true });
ix.Create();

As Default Contraints são um pouco mais chatas de serem criadas, pois requerem a utilização de uma coluna (objeto Column) e a utilização do método AddDefaultConstraint para serem adicionadas:

// ALTER TABLE [dbo].[Produto]
//   ADD CONSTRAINT [DF_Produto_DataCadastro] DEFAULT (GETDATE()) FOR [DataCadastro]
var col = table.Columns["DataCadastro"];
var def = col.AddDefaultConstraint("DF_Produto_DataCadastro");
def.Text = "(GETDATE())";
def.Create();

As Foreign Keys, somente requerem a especificação da tabela a ser referenciada e das colunas que a referenciam.

// ALTER TABLE [dbo].[Produto]
//   ADD CONSTRAINT [PK_Categoriao] FOREIGN KEY ([CategoriaId])
//   REFERENCE [dbo].[Categoria] ([Id])
var fk = new ForeignKey(table, "FK_Produto_Categoria")
                {
                    ReferencedTableSchema = table2.Schema,
                    ReferencedTable = table2.Name
                };
fk.Columns.Add(new ForeignKeyColumn(fk, "CategoriaId", "Id"));
fk.Create();

Continuar lendo ‘Criando Constraints e Índices no SQL Server com .NET’




Sobre o blog

Blog que há três anos trata de SQL Server, .NET Framework, PowerShell, soluções para problemas comuns e não tão comuns assim, informações sobre ferramentas diversas e o que vier na cabeça do MCT Paulo R. Pereira.

Twitter


Seguir

Obtenha todo post novo entregue na sua caixa de entrada.

Junte-se a 349 outros seguidores

%d bloggers like this: