MSDE Altre istruzioni per l'uso (parte 5) - Creare/Eliminare Vincoli su una Tabella
a cura di Sabrina Cosolo (requisiti: Livello intemedio di conoscenza generale)

Creare Vincoli e Relazioni su una tabella Via Codice SQL
La creazione di vincoli e relazioni sulle tabelle è un comando SQL che fa parte di quello che viene chiamato DDL (Data Definition Language). Deve essere effettuate collegandosi a SQL Server con un utente che abbia diritti di amministrazione sul database in cui si vuole generare il vincolo; è opportuno utilizzare sempre l'utente SA o un utente Windows del gruppo Builtin\Administrator per questo tipo di istruzioni.

Di seguito il codice SQL Per la creazione dei vincoli sulle tabelle del nostro database di esempio. Li genereremo per tipologia di vincolo e tabella e poi spiegheremo uso e sintassi dei vari vincoli.

  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbCatMusicali]
    FOREIGN KEY ([idCatMuscale]) 
    REFERENCES [dbo].[tbCatMusicali] ([idCatMusicale]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbCd] 
    FOREIGN KEY ([idcd]) 
    REFERENCES [dbo].[tbCd] ([idcd]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbInterpreti] 
    FOREIGN KEY ([idInterprete]) 
    REFERENCES [dbo].[tbInterpreti] ([idInterprete]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbRitmi] 
    FOREIGN KEY ([idRitmo]) 
    REFERENCES [dbo].[tbRitmi] ([idRitmo]) 
    NOT FOR REPLICATION

  ALTER TABLE [dbo].[tbCd] 
    ADD CONSTRAINT [FK_tbCd_tbCaseDisc] 
    FOREIGN KEY([idCasaDisc]) 
    REFERENCES [dbo].[tbCaseDisc] ([idCasaDisc]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbCd] 
    ADD CONSTRAINT [FK_tbCd_tbCate] 
    FOREIGN KEY ([idCate]) 
    REFERENCES [dbo].[tbCate] ([idCate]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbCd] 
    ADD CONSTRAINT [FK_tbCd_tbInterpreti] 
    FOREIGN KEY ([idInterprete]) 
    REFERENCES [dbo].[tbInterpreti] ([idInterprete]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbCatMusicali] 
    ADD CONSTRAINT [Uq_tbCatMusicali_Nome]
    UNIQUE CLUSTERED (Nome)
 
  ALTER TABLE [dbo].[tbCate] 
    ADD CONSTRAINT [Uq_tbCate_Nome]
    UNIQUE CLUSTERED (Nome)
 
  ALTER TABLE [dbo].[tbRitmi] 
    ADD CONSTRAINT [Uq_tbRitmi_Nome]
    UNIQUE CLUSTERED (Nome)

Diamo un significato agli elementi usati nella sintassi dei comandi utilizzati:

Eliminare un vincolo o una relazione via codice SQL
Come sempre eliminare è più facile che creare perciò ecco i comandi per eliminare i vincoli da noi costruiti:

  ALTER TABLE [dbo].[tbBrani] DROP CONSTRAINT FK_tbBrani_tbCatMusicali
  ALTER TABLE [dbo].[tbBrani] DROP CONSTRAINT FK_tbBrani_tbCd
  ALTER TABLE [dbo].[tbBrani] DROP CONSTRAINT FK_tbBrani_tbInterpreti
  ALTER TABLE [dbo].[tbBrani] DROP CONSTRAINT FK_tbBrani_tbRitmi
  ALTER TABLE [dbo].[tbCaseDisc] DROP CONSTRAINT UQ_tbCaseDisc_Nome
  ALTER TABLE [dbo].[tbCatMusicali] DROP CONSTRAINT UQ_tbCatMusicali_Nome
  ALTER TABLE [dbo].[tbCate] DROP CONSTRAINT UQ_tbCate_Nome
  ALTER TABLE [dbo].[tbCd] DROP CONSTRAINT FK_tbCd_tbCaseDisc
  ALTER TABLE [dbo].[tbCd] DROP CONSTRAINT FK_tbCd_tbCate
  ALTER TABLE [dbo].[tbCd] DROP CONSTRAINT FK_tbCd_tbInterpreti
  ALTER TABLE [dbo].[tbInterpreti] DROP CONSTRAINT FK_tbInterpreti_tbInterpreti
  ALTER TABLE [dbo].[tbInterpreti] DROP CONSTRAINT CK_tbInterpreti_TiPers
  ALTER TABLE [dbo].[tbInterpreti] DROP CONSTRAINT UQ_tbInterpreti_Nome
  ALTER TABLE [dbo].[tbRitmi] DROP CONSTRAINT UQ_tbRitmi_Nome
 
  ALTER TABLE [dbo].[tbBrani] DROP CONSTRAINT PK_tbBrani
  ALTER TABLE [dbo].[tbCaseDisc] DROP CONSTRAINT PK_tbCaseDisc
  ALTER TABLE [dbo].[tbCate] DROP CONSTRAINT PK_tbCate
  ALTER TABLE [dbo].[tbCatMusicali] DROP CONSTRAINT PK_tbCatMusicali
  ALTER TABLE [dbo].[tbCd] DROP CONSTRAINT PK_tbCd
  ALTER TABLE [dbo].[tbInterpreti] DROP CONSTRAINT PK_tbInterpreti

La sintassi di base del comando è:

  ALTER TABLE [[owner].[tablename] DROP CONSTRAINT [NomeConstraint]

Dove owner e tablename corrispondono alle definizioni date in precedenza, e NomeConstraint è il nome che abbbiamo assegnato al vincolo.
Faccio notare come nella precedente serie di comandi sql l'eliminazione dei vincoli di chiave primaria costituisca l'ultima operazione, infatti non è possibile eliminare un vincolo di chiave primaria da una tabella se questa tabella ha una relazione di FOREIGN KEY con un'altra tabella sulla propria chiave primaria.

Anche per questi comandi SQL si può utilizzare OSQL.EXE (vedi, anche per i parametri).

La sintassi per chiamare un comando SQL usando OSQL è la seguente:

  osql -S Localhost -U SA -P as -i vincolisql.sql

un possibile file di input da utilizzare per realizzare uno degli esempi vincolisql.sql è un file di testo contenente il comando da eseguire che è leggermente diverso da quello sopra descritto, riporto la sintassi per la creazione delle chiavi primarie:

  USE MusicBox
  GO
 
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbCatMusicali]
    FOREIGN KEY ([idCatMuscale]) 
    REFERENCES [dbo].[tbCatMusicali] ([idCatMusicale]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbCd] 
    FOREIGN KEY ([idcd]) 
    REFERENCES [dbo].[tbCd] ([idcd]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbInterpreti] 
    FOREIGN KEY ([idInterprete]) 
    REFERENCES [dbo].[tbInterpreti] ([idInterprete]) 
    NOT FOR REPLICATION
  ALTER TABLE [dbo].[tbBrani] 
    ADD CONSTRAINT [FK_tbBrani_tbRitmi] 
    FOREIGN KEY ([idRitmo]) 
    REFERENCES [dbo].[tbRitmi] ([idRitmo]) 
    NOT FOR REPLICATION 
  GO
 
  QUIT

Il comando USE MUSICBOX serve a dire al server che dobbiamo lavorare sul Database Musicbox database di esempio creato nel precedente articolo), Il comando GO esegue i comandi richiesti, il comando QUIT esce da OSQL.EXE che altrimenti si comporta come un Interprete comandi e rimane attivo all'interno della finestra console.

La sintassi da usare con OSQL per trasportarvi tutti gli script da noi generati in questa pagina è la seguente:

  USE Musicbox
  GO 
  
  script da eseguire
 
  GO
 
  QUIT
<<< Creare/Eliminare Tabelle