Condividi tramite


Creazione, modifica e rimozione di chiavi esterne

In SMO (SQL Server Management Objects) le chiavi esterne sono rappresentate dall'oggetto ForeignKey.

Per creare una chiave esterna in SMO, è necessario specificare la tabella in cui la chiave esterna viene definita nel costruttore dell'oggetto ForeignKey. Dalla tabella, è necessario selezionare almeno una colonna che rappresenti la chiave esterna. A questo scopo, creare una variabile dell'oggetto ForeignKeyColumn e specificare il nome della colonna che rappresenta la chiave esterna. Specificare quindi la tabella e la colonna a cui si fa riferimento. Utilizzare il metodo Add per aggiungere la colonna alla proprietà dell'oggetto Columns.

Le colonne che rappresentano la chiave esterna sono elencate nella proprietà dell'oggetto Columns dell'oggetto ForeignKey. La chiave primaria a cui la chiave esterna fa riferimento è rappresentata dalla proprietà ReferencedKey che si trova nella tabella specificata nella proprietà ReferencedTable.

Esempio

Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente, il modello e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere Procedura: Creazione di un progetto Visual Basic SMO in Visual Studio .NET o Procedura: Creazione di un progetto Visual C# SMO in Visual Studio .NET.

Creazione, modifica e rimozione di una chiave esterna in Visual Basic

In questo esempio di codice viene illustrato come creare una relazione di chiave esterna tra una o più colonne in una tabella e una colonna chiave primaria in un'altra tabella.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Declare a Table object variable and reference the Employee table.
Dim tbe As Table
tbe = db.Tables("Employee", "HumanResources")
'Declare another Table object variable and reference the EmployeeAddress table.
Dim tbea As Table
tbea = db.Tables("EmployeeAddress", "HumanResources")
'Define a Foreign Key object variable by supplying the EmployeeAddress as the parent table and the foreign key name in the constructor.
Dim fk As ForeignKey
fk = New ForeignKey(tbea, "test_foreignkey")
'Add EmployeeID as the foreign key column.
Dim fkc As ForeignKeyColumn
fkc = New ForeignKeyColumn(fk, "EmployeeID", "EmployeeID")
fk.Columns.Add(fkc)
'Set the referenced table and schema.
fk.ReferencedTable = "Employee"
fk.ReferencedTableSchema = "HumanResources"
'Create the foreign key on the instance of SQL Server.
fk.Create()

Creazione, modifica e rimozione di una chiave esterna in Visual C#

In questo esempio di codice viene illustrato come creare una relazione di chiave esterna tra una o più colonne in una tabella e una colonna chiave primaria in un'altra tabella.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db; 
db = srv.Databases("AdventureWorks"); 
//Declare a Table object variable and reference the Employee table. 
Table tbe; 
tbe = db.Tables("Employee", "HumanResources"); 
//Declare another Table object variable and reference the EmployeeAddress table. 
Table tbea; 
tbea = db.Tables("EmployeeAddress", "HumanResources"); 
//Define a Foreign Key object variable by supplying the EmployeeAddress as the parent table and the foreign key name in the constructor. 
ForeignKey fk; 
fk = new ForeignKey(tbea, "test_foreignkey"); 
//Add EmployeeID as the foreign key column. 
ForeignKeyColumn fkc; 
fkc = new ForeignKeyColumn(fk, "EmployeeID", "EmployeeID"); 
fk.Columns.Add(fkc); 
//Set the referenced table and schema. 
fk.ReferencedTable = "Employee"; 
fk.ReferencedTableSchema = "HumanResources"; 
//Create the foreign key on the instance of SQL Server. 
fk.Create();
}