Exemples
CREATE VIEW ENTRY_LEVEL_JOBS AS
SELECT JOB_CODE, JOB_TITLE
FROM JOB
WHERE MAX_SALARY < 15000;
Créez une vue renvoyant les colonnes JOB_CODE et JOB_TITLE uniquement pour les emplois dont le MAX_SALARY est inférieur à 15 000 $.Lors de l’insertion d’une nouvelle entrée ou de la modification d’une entrée existante, la condition MAX_SALARY < 15000 sera vérifiée, si la condition n’est pas remplie, l’insertion/modification sera rejetée.
CREATE VIEW ENTRY_LEVEL_JOBS AS
SELECT JOB_CODE, JOB_TITLE
FROM JOB
WHERE MAX_SALARY < 15000
WITH CHECK OPTIONS;
CREATE VIEW PRICE_WITH_MARKUP (
CODE_PRICE,
COST,
COST_WITH_MARKUP
) AS
SELECT
CODE_PRICE,
COST,
COST * 1.1
FROM PRICE;
CREATE VIEW PRICE_WITH_MARKUP AS
SELECT
CODE_PRICE,
COST,
COST * 1.1 AS COST_WITH_MARKUP
FROM PRICE;
CREATE VIEW GOODS_PRICE AS
SELECT
goods.name AS goodsname,
price.cost AS cost,
b.quantity AS quantity
FROM
goods
JOIN price ON goods.code_goods = price.code_goods
LEFT JOIN sp_get_balance(goods.code_goods) b ON 1 = 1;
-- tables de base
RECREATE TABLE t_films(id INT PRIMARY KEY, title VARCHAR(100));
RECREATE TABLE t_sound(id INT PRIMARY KEY, audio BLOB);
RECREATE TABLE t_video(id INT PRIMARY KEY, video BLOB);
COMMIT;
-- création d'une vue non renouvelable
RECREATE VIEW v_films AS
SELECT f.id, f.title, s.audio, v.video
FROM t_films f
LEFT JOIN t_sound s ON f.id = s.id
LEFT JOIN t_video v ON f.id = v.id;
/* Pour que la vue puisse être mise à jour, créez un déclencheur qui manipulera les données sous-jacentes */
SET TERM ^;
CREATE OR ALTER TRIGGER v_films_biud FOR v_films
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 0 AS
BEGIN
IF (INSERTING) THEN
new.id = COALESCE(new.id, GEN_ID(g_films, 1));
IF (NOT DELETING) THEN
BEGIN
UPDATE OR INSERT INTO t_films(id, title)
VALUES(new.id, new.title)
MATCHING(id);
UPDATE OR INSERT INTO t_sound(id, audio)
VALUES(new.id, new.audio)
MATCHING(id);
UPDATE OR INSERT INTO t_video(id, video)
VALUES(new.id, new.video)
MATCHING(id);
END
ELSE
BEGIN
DELETE FROM t_films WHERE id = old.id;
DELETE FROM t_sound WHERE id = old.id;
DELETE FROM t_video WHERE id = old.id;
END
END^
SET TERM ;^
/* Nous pouvons maintenant manipuler cette vue comme si nous travaillions avec une table */
INSERT INTO v_films(title, audio, video)
VALUES('007 coordinates skyfall', 'pif-paf!', 'oh! waw!');