Docsity

Basi di dati - Microsoft SQL Server, Esercizi di Basi di Dati

Toggle sidebar
bg1
1
Microsoft SQL Server
2
Microsoft SQL Server
Che cos'è SQL Server
•E' il DBMS commercializzato da Microsoft.
•Principali concorrenti:
–Prodotti commerciali: Oracle, IBM DBII;
–Prodotti "free": MySQL, Postgres.
•Requisiti tipici per l'installazione della versione Enterprise:
–S.O. Windows 2000 Server o Windows NT4 Server;
–128 MB RAM;
–250 MB HD.
•Costo della licenza: circa 1500€ (Windows 2000 Server + SQL
Server).
•Esiste una versione Evaluation, scaricabile gratuitamente dal
sito Microsoft ed utilizzabile per 120 giorni.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Anteprima parziale del testo

Scarica Basi di dati - Microsoft SQL Server e più Esercizi in PDF di Basi di Dati solo su Docsity!

Microsoft SQL Server

Che cos'è SQL Server

  • E' il DBMS commercializzato da Microsoft.
  • Principali concorrenti:
    • Prodotti commerciali: Oracle, IBM DBII;
    • Prodotti "free": MySQL, Postgres.
  • Requisiti tipici per l'installazione della versione Enterprise:
    • S.O. Windows 2000 Server o Windows NT4 Server;
    • 128 MB RAM;
    • 250 MB HD.
  • Costo della licenza: circa 1500€ (Windows 2000 Server + SQL Server).
  • Esiste una versione Evaluation, scaricabile gratuitamente dal sito Microsoft ed utilizzabile per 120 giorni.

3

Le componenti principali di SQL Server

  • La componente fondamentale è il Relational Database Engine : supporta lo standard SQL92; il "dialetto" usato si chiama Transact-SQL (T-SQL).
  • Analysis Services: supporta la creazione di Data Warehouse, utilizzati per l'esecuzioni di query particolarmente lunghe e complesse, destinate ad estrarre da grosse quantità di dati informazioni di supporto ai processi decisionali aziendali.
  • Replication: gestisce il mantenimento di copie multiple del DB, al fine di migliorare le prestazioni in un ambiente distribuito.
  • Supporto alla creazione di applicazioni tramite ODBC , ADO , OLE DB, ADO.NET.

Microsoft SQL Server

I servizi Windows

  • SQL Server Service : è il gestore vero e proprio del DB; risponde alle query, gestisce le transazioni, la concorrenza, la sicurezza.
  • SQL Server Agent Service : esegue le operazioni schedulate (Job), ad esempio i backup, e le azioni da intraprendere quando avvengono particolari eventi, ad esempio crash del sistema.
  • Microsoft Search Service : permette di effettuare query sofisticate di ricerca di parole all'interno del testo contenuto nelle varie tabelle (una specie di motore di ricerca).
  • MSSQLServerADHelper Service : gestisce l'integrazione con Active Directory.
  • MS DTC Service : gestisce transazioni che utilizzano DB localizzati su vari server.

7

La console di gestione

  • L‘amministrazione di SQL Server avviene tramite Enterprise Manager , che è il lato "client" di SQL Server.
  • Da un singolo client possiamo gestire più server, effettuando più registrazioni. Per effettuare una registrazione dovremo fornire: - Il server al quale vogliamo connetterci (nome o indirizzo IP); - Il tipo di autenticazione usata (Windows o SQL Server); - Nel caso di autenticazione SQL Server, Username e Password.

Microsoft SQL Server

All'interno del server

9

All'interno di un Database

Microsoft SQL Server

Creare una tabella

  • Alla creazione di una tabella si definiscono:
    • Il nome della tabella;
    • Il nome e il tipo dei vari campi;
    • La primary key;
    • Le foreign key;
    • I check constraint;
    • Gli indici;
    • I trigger.
  • Queste operazioni possono essere fatte graficamente o scrivendo comandi DDL, che vengono eseguiti tramite SQL Query Analyzer ; allo stesso modo è possibile successivamente modificare la struttura di una tabella.

13

Creare una tabella: la sintassi

CREATE TABLE [ database_name.[ owner ]. | owner. ] table_name ( { < column_definition > | column_name AS computed_column_expression | < table_constraint > ::= [ CONSTRAINT constraint _name ] } | [ { PRIMARY KEY | UNIQUE } [ ,...n ] ) [ ON { filegroup | DEFAULT } ] [ TEXTIMAGE_ON { filegroup | DEFAULT } ] < column _constraint > ::= [ CONSTRAINT constraint_name ] { [ NULL | NOT NULL ] | [ { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor ] [ON { filegroup | DEFAULT} ] ] ] | [ [ FOREIGN KEY ] REFERENCES ref_table [ ( ref_column ) ] [ ON DELETE { CASCADE | NO ACTION } ] [ ON UPDATE { CASCADE | NO ACTION } ] [ NOT FOR REPLICATION ] ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) }

< column _definition > ::= { _column name data_type } [ COLLATE < collation _name > ] [ [ DEFAULT constant_expression ] | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ] ] [ ROWGUIDCOL] [ < column _constraint > ] [ ... n ]

< table_constraint > ::= [ CONSTRAINT constraint_name ] { [ { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] { ( column [ ASC | DESC ] [ , ... n ] ) } [ WITH FILLFACTOR = fillfactor ] [ ON { filegroup | DEFAULT } ] ] | FOREIGN KEY [ ( column [ , ... n ] ) ] REFERENCES ref_table [ ( ref_column [ , ... n ] ) ] [ ON DELETE { CASCADE | NO ACTION } ] [ ON UPDATE { CASCADE | NO ACTION } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( search conditions_ ) }

Microsoft SQL Server

Creare una tabella: esempio

CREATE TABLE "Esame" ( "ID" smallint IDENTITY PRIMARY KEY, "StudenteID" smallint NOT NULL REFERENCES Studente(ID), "CorsoID" smallint NOT NULL REFERENCES Corso(ID), "DataEsame" datetime NOT NULL DEFAULT (getdate()), "Voto" tinyint NOT NULL CONSTRAINT ck_Esame_Voto CHECK (Voto BETWEEN 18 AND 30), "Lode" bit NOT NULL DEFAULT 0, "Note" varchar(255) NULL )

15

Creare un indice: l'interfaccia grafica

Microsoft SQL Server

Creare un indice: la sintassi

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) [ WITH < index_option > [ ,...n] ] [ ON filegroup ]

< index_option > :: = { PAD_INDEX | FILLFACTOR = fillfactor | IGNORE_DUP_KEY | DROP_EXISTING | STATISTICS_NORECOMPUTE | SORT_IN_TEMPDB }

Esempio: CREATE INDEX IX_Esame_Studente ON Esame (StudenteID ASC)

19

Creare una vista: l'interfaccia grafica

Microsoft SQL Server

Creare una vista: la sintassi

CREATE VIEW [ < database_name >. ] [ < owner >. ] _view name [ ( column [ , ... n ] ) ] [ WITH < view_attribute > [ , ...n ] ] AS select_statement [ WITH CHECK OPTION ] < view_attribute > ::= { ENCRYPTION | SCHEMABINDING | VIEW_METADATA }

CREATE VIEW dbo.v w_EsamiPositivi AS SELECT dbo.Corso.Nome, dbo.Esame. StudenteID, dbo.Esame. DataEsame FROM dbo.Corso INNER JOIN dbo.Esame ON dbo.Corso.ID = dbo.Esame. CorsoID WHERE (dbo.Esame.Voto > 28)

21

Creare un tipo: l'interfaccia grafica

Microsoft SQL Server

Creare un tipo: la sintassi

EXEC sp_addtype [ @typename = ] type, [ @phystype = ] system_data_type [ , [ @nulltype = ] 'null_type' ] [ , [ @owner = ] 'owner_name' ]

EXEC sp_addtype udt_CodiceFiscale, 'CHAR(15)', 'NULL'

25

Creare un diagramma: l'interfaccia grafica

Microsoft SQL Server

Grant: l'interfaccia grafica

27

Grant: la sintassi

GRANT { ALL [ PRIVILEGES ] | permission [ ,...n ] } { [ ( column [ ,...n ] ) ] ON { table | view } | ON { table | view } [ ( column [ ,...n ] ) ] | ON { stored_procedure | extended_procedure } | ON { user_defined_function } } TO security _account [ ,...n ] [ WITH GRANT OPTION ] [ AS { group | role } ]

Esempio1: GRANT SELECT ON Corsi TO Marco WITH GRANT OPTION Esempio2: GRANT CREATE TABLE TO Giovanni, Francesco

Si possono concedere grant di sistema (creazione tabelle, creazione Database ecc.) o grant su un singolo oggetto (select, update ecc.)

Microsoft SQL Server

Per qualsiasi dubbio…