· 4 years ago · Feb 04, 2021, 11:28 AM
1USE actalispec
2
3BEGIN TRANSACTION
4
5if not exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME= 'prodotto_categorie_aggiuntive')
6BEGIN
7 CREATE TABLE prodotto_categorie_aggiuntive (
8 id int IDENTITY(0,1) NOT NULL,
9 id_categoria int NOT NULL,
10 id_prodotto int NOT NULL
11 CONSTRAINT prodotto_categoria_aggiuntive_PK PRIMARY KEY (id), CONSTRAINT UQ_categoria_aggiuntiva unique (id_categoria,id_prodotto)
12 )
13
14 ALTER TABLE prodotto_categorie_aggiuntive ADD CONSTRAINT id_categoria_FK FOREIGN KEY (id_categoria) REFERENCES categoria_prodotto(id)
15 ALTER TABLE prodotto_categorie_aggiuntive ADD CONSTRAINT id_prodotto_FK FOREIGN KEY (id_prodotto) REFERENCES prodotto(id_prodotto)
16END
17
18IF @@ERROR != 0
19 BEGIN
20 PRINT @@ERROR
21 PRINT 'ERROR IN SCRIPT'
22 ROLLBACK TRANSACTION
23 RETURN
24 END
25ELSE
26 BEGIN
27 COMMIT TRANSACTION
28 PRINT 'COMMITTED SUCCESSFULLY'
29 END