Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1.  
  2. /* Création des tables */
  3. CREATE TABLE Employe (
  4. Id_Emp int NOT NULL,
  5. Nom_Emp varchar(20),
  6. Prenom_Emp varchar(20),
  7. PRIMARY KEY (Id_Emp)
  8. );
  9.  
  10. ALTER TABLE Employe
  11. ADD UNIQUE (Id_Emp);
  12.  
  13. CREATE TABLE Secretaire (
  14. Id_Emp int NOT NULL,
  15. e_mail varchar(100),
  16. telephone varchar(10),
  17. PRIMARY KEY (Id_Emp),
  18. FOREIGN KEY (Id_Emp) REFERENCES Employe(Id_Emp)
  19. );
  20.  
  21.  
  22. /* Insertion des données */
  23.  
  24. INSERT INTO Employe VALUES (1,'Chirac','Bernadette');
  25. INSERT INTO Employe VALUES (2,'Veil','Simone');
  26. INSERT INTO Employe VALUES (3,'Kardashian','Kim');
  27. INSERT INTO Employe VALUES (4,'Granger','Hermione');
  28. INSERT INTO Employe VALUES (5,'Merkel','Angela');
  29.  
  30. INSERT INTO Secretaire VALUES (1,'Bernadettechirac@hotmail.fr','0123456789');
  31. INSERT INTO Secretaire VALUES (2,'Simoneveil@hotmail.fr','0223456788');
  32. INSERT INTO Secretaire VALUES (3,'Kimkardashian@hotmail.fr','0323456787');
  33. INSERT INTO Secretaire VALUES (4,'Hermionegranger@hotmail.fr','0423456786');
  34. INSERT INTO Secretaire VALUES (5,'Angelamerkel@hotmail.fr','0523456785');
  35. SELECT * From Employe
  36. SELECT * from Secretaire
  37. /* Création de la vue */
  38.  
  39. CREATE VIEW vue_secretaire
  40. AS
  41. SELECT e.Id_Emp, e.Nom_Emp, e.Prenom_Emp, s.e_mail, s.telephone
  42. FROM Employe e, Secretaire s
  43. WHERE e.Id_Emp = s.Id_Emp
  44.  
  45. select * FROM vue_secretaire
  46.  
  47. /* Create Tr_Stop_Insert*/
  48.  
  49. CREATE TRIGGER Tr_stop_insert
  50. ON Secretaire
  51. Instead of Insert
  52. AS
  53. BEGIN
  54. RAISERROR('Insertion impossible',10,1)
  55. ROLLBACK TRANSACTION
  56. END
  57.  
  58. /* Test */
  59. Insert Into Secretaire Values(8,'Medecis','Catherine');
  60. /* Drop du trigger Tr_stop_insert si nécessaire */
  61. DROP Trigger Tr_stop_insert
  62.  
  63. CREATE Trigger Tr_inserevue
  64. on vue_secretaire
  65. instead of insert
  66. as
  67. BEGIN
  68. INSERT INTO Employe
  69. SELECT i.Id_Emp,i.Nom_Emp, i.Prenom_Emp
  70. FROM inserted i
  71.  
  72. Insert into Secretaire
  73. Select i.Id_Emp,i.e_mail, i.telephone
  74. FROM inserted i
  75. END
  76.  
  77. SELECT * from vue_secretaire
  78.  
  79. DISABLE TRIGGER Tr_stop_insert on Secretaire
  80. ENABLE TRIGGER Tr_stop_insert on Secretaire
  81. INSERT INTO vue_secretaire VALUES (9,'Bernard','Laurence','Bernardlaurence@hotmail.fr','0601020304')
  82.  
  83. Select * from Secretaire
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement