Advertisement
Garey

OracleSHit

Sep 17th, 2019
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 20.44 KB | None | 0 0
  1. --------------------------------------------------------
  2. --  File created - Tuesday-September-17-2019  
  3. --------------------------------------------------------
  4. --------------------------------------------------------
  5. --  DDL for Table CLIENT
  6. --------------------------------------------------------
  7.  
  8.   CREATE TABLE "CLIENT"
  9.    (    "ID" NUMBER,
  10.     "NAME" VARCHAR2(32 BYTE)
  11.     "PHONE" VARCHAR2(32 BYTE)
  12.    ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  13.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  14.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  15.   TABLESPACE "USERS" ;
  16. --------------------------------------------------------
  17. --  DDL for Table DESTINATIONS
  18. --------------------------------------------------------
  19.  
  20.   CREATE TABLE "DESTINATIONS"
  21.    (    "ID" NUMBER,
  22.     "NAME" VARCHAR2(32 BYTE)
  23.     "TOTALRESERVATIONS" NUMBER,
  24.    ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  25.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  26.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  27.   TABLESPACE "USERS" ;
  28. --------------------------------------------------------
  29. --  DDL for Table POSITIONS
  30. --------------------------------------------------------
  31.  
  32.   CREATE TABLE "POSITIONS"
  33.    (    "ID" NUMBER,
  34.     "NAME" VARCHAR2(32 BYTE)
  35.    ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  36.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  37.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  38.   TABLESPACE "USERS" ;
  39. --------------------------------------------------------
  40. --  DDL for Table RESERVATIONS
  41. --------------------------------------------------------
  42.  
  43.   CREATE TABLE "RESERVATIONS"
  44.    (    "ID" NUMBER,
  45.     "DESTINATION_ID" NUMBER,
  46.     "CLIENT_ID" NUMBER,
  47.     "EMPLOYEE_ID" NUMBER,
  48.     "RDATE" DATE,
  49.     "RHOUR" TIMESTAMP (6),
  50.     "PRICE" FLOAT(126)
  51.    ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  52.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  53.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  54.   TABLESPACE "USERS" ;
  55. --------------------------------------------------------
  56. --  DDL for Table EMPLOYEE
  57. --------------------------------------------------------
  58.  
  59.   CREATE TABLE "EMPLOYEE"
  60.    (    "ID" NUMBER,
  61.     "POSITION_ID" NUMBER DEFAULT NULL,
  62.     "NAME" VARCHAR2(32 BYTE),
  63.     "PHONE" VARCHAR2(32 BYTE)
  64.    ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  65.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  66.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  67.   TABLESPACE "USERS" ;
  68. REM INSERTING INTO CLIENT
  69. SET DEFINE OFF;
  70. REM INSERTING INTO DESTINATIONS
  71. SET DEFINE OFF;
  72. REM INSERTING INTO POSITIONS
  73. SET DEFINE OFF;
  74. REM INSERTING INTO RESERVATIONS
  75. SET DEFINE OFF;
  76. REM INSERTING INTO EMPLOYEE
  77. SET DEFINE OFF;
  78. --------------------------------------------------------
  79. --  DDL for Index CLIENT_PK
  80. --------------------------------------------------------
  81.  
  82.   CREATE UNIQUE INDEX "CLIENT_PK" ON "CLIENT" ("ID")
  83.   PCTFREE 10 INITRANS 2 MAXTRANS 255
  84.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  85.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  86.   TABLESPACE "USERS" ;
  87. --------------------------------------------------------
  88. --  DDL for Index DESTINATIONS_PK
  89. --------------------------------------------------------
  90.  
  91.   CREATE UNIQUE INDEX "DESTINATIONS_PK" ON "DESTINATIONS" ("ID")
  92.   PCTFREE 10 INITRANS 2 MAXTRANS 255
  93.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  94.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  95.   TABLESPACE "USERS" ;
  96. --------------------------------------------------------
  97. --  DDL for Index POSITIONS_PK
  98. --------------------------------------------------------
  99.  
  100.   CREATE UNIQUE INDEX "POSITIONS_PK" ON "POSITIONS" ("ID")
  101.   PCTFREE 10 INITRANS 2 MAXTRANS 255
  102.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  103.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  104.   TABLESPACE "USERS" ;
  105. --------------------------------------------------------
  106. --  DDL for Index RESERVATIONS_PK
  107. --------------------------------------------------------
  108.  
  109.   CREATE UNIQUE INDEX "RESERVATIONS_PK" ON "RESERVATIONS" ("ID")
  110.   PCTFREE 10 INITRANS 2 MAXTRANS 255
  111.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  112.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  113.   TABLESPACE "USERS" ;
  114. --------------------------------------------------------
  115. --  DDL for Index EMPLOYEE_PK
  116. --------------------------------------------------------
  117.  
  118.   CREATE UNIQUE INDEX "EMPLOYEE_PK" ON "EMPLOYEE" ("ID")
  119.   PCTFREE 10 INITRANS 2 MAXTRANS 255
  120.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  121.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  122.   TABLESPACE "USERS" ;
  123. --------------------------------------------------------
  124. --  Constraints for Table CLIENT
  125. --------------------------------------------------------
  126.  
  127.   ALTER TABLE "CLIENT" ADD CONSTRAINT "CLIENT_PK" PRIMARY KEY ("ID")
  128.   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  129.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  130.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  131.   TABLESPACE "USERS"  ENABLE;
  132.  
  133.   ALTER TABLE "CLIENT" MODIFY ("ID" NOT NULL ENABLE);
  134. --------------------------------------------------------
  135. --  Constraints for Table DESTINATIONS
  136. --------------------------------------------------------
  137.  
  138.   ALTER TABLE "DESTINATIONS" ADD CONSTRAINT "DESTINATIONS_PK" PRIMARY KEY ("ID")
  139.   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  140.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  141.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  142.   TABLESPACE "USERS"  ENABLE;
  143.  
  144.   ALTER TABLE "DESTINATIONS" MODIFY ("ID" NOT NULL ENABLE);
  145. --------------------------------------------------------
  146. --  Constraints for Table POSITIONS
  147. --------------------------------------------------------
  148.  
  149.   ALTER TABLE "POSITIONS" ADD CONSTRAINT "POSITIONS_PK" PRIMARY KEY ("ID")
  150.   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  151.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  152.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  153.   TABLESPACE "USERS"  ENABLE;
  154.  
  155.   ALTER TABLE "POSITIONS" MODIFY ("ID" NOT NULL ENABLE);
  156. --------------------------------------------------------
  157. --  Constraints for Table RESERVATIONS
  158. --------------------------------------------------------
  159.  
  160.   ALTER TABLE "RESERVATIONS" ADD CONSTRAINT "RESERVATIONS_PK" PRIMARY KEY ("ID")
  161.   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  162.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  163.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  164.   TABLESPACE "USERS"  ENABLE;
  165.  
  166.   ALTER TABLE "RESERVATIONS" MODIFY ("ID" NOT NULL ENABLE);
  167.  
  168.   ALTER TABLE "RESERVATIONS" MODIFY ("DESTINATION_ID" NOT NULL ENABLE);
  169.  
  170.   ALTER TABLE "RESERVATIONS" MODIFY ("CLIENT_ID" NOT NULL ENABLE);
  171.  
  172.   ALTER TABLE "RESERVATIONS" MODIFY ("EMPLOYEE_ID" NOT NULL ENABLE);
  173. --------------------------------------------------------
  174. --  Constraints for Table EMPLOYEE
  175. --------------------------------------------------------
  176.  
  177.   ALTER TABLE "EMPLOYEE" ADD CONSTRAINT "EMPLOYEE_PK" PRIMARY KEY ("ID")
  178.   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  179.   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  180.   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  181.   TABLESPACE "USERS"  ENABLE;
  182.  
  183.   ALTER TABLE "EMPLOYEE" MODIFY ("ID" NOT NULL ENABLE);
  184.  
  185.   ALTER TABLE "EMPLOYEE" MODIFY ("POSITION_ID" NOT NULL ENABLE);
  186. --------------------------------------------------------
  187. --  Ref Constraints for Table RESERVATIONS
  188. --------------------------------------------------------
  189.  
  190.   ALTER TABLE "RESERVATIONS" ADD CONSTRAINT "RESERVATIONS_FK1" FOREIGN KEY ("DESTINATION_ID")
  191.       REFERENCES "DESTINATIONS" ("ID") ENABLE;
  192.  
  193.   ALTER TABLE "RESERVATIONS" ADD CONSTRAINT "RESERVATIONS_FK2" FOREIGN KEY ("CLIENT_ID")
  194.       REFERENCES "CLIENT" ("ID") ENABLE;
  195.  
  196.   ALTER TABLE "RESERVATIONS" ADD CONSTRAINT "RESERVATIONS_FK3" FOREIGN KEY ("EMPLOYEE_ID")
  197.       REFERENCES "EMPLOYEE" ("ID") ENABLE;
  198. --------------------------------------------------------
  199. --  Ref Constraints for Table EMPLOYEE
  200. --------------------------------------------------------
  201.  
  202.   ALTER TABLE "EMPLOYEE" ADD CONSTRAINT "EMPLOYEE_FK1" FOREIGN KEY ("ID")
  203.       REFERENCES "POSITIONS" ("ID") ENABLE;
  204.  
  205.  
  206. --------------------------------------------------------
  207. --  Auto Increment Triggers
  208. --------------------------------------------------------
  209.  
  210. CREATE SEQUENCE employee_id_seq;
  211. CREATE SEQUENCE client_id_seq;
  212. CREATE SEQUENCE positions_id_seq;
  213. CREATE SEQUENCE reservations_id_seq;
  214. CREATE SEQUENCE destinations_id_seq;
  215.  
  216. CREATE TRIGGER employee_bi
  217. BEFORE INSERT ON employee
  218. FOR EACH ROW
  219. BEGIN
  220.     SELECT employee_id_seq.NEXTVAL
  221.     INTO :NEW.id
  222.     FROM dual;
  223. END;
  224.  
  225. CREATE TRIGGER client_bi
  226. BEFORE INSERT ON client
  227. FOR EACH ROW
  228. BEGIN
  229.     SELECT client_id_seq.NEXTVAL
  230.     INTO :NEW.id
  231.     FROM dual;
  232. END;
  233.  
  234. CREATE TRIGGER positions_bi
  235. BEFORE INSERT ON positions
  236. FOR EACH ROW
  237. BEGIN
  238.     SELECT positions_id_seq.NEXTVAL
  239.     INTO :NEW.id
  240.     FROM dual;
  241. END;
  242.  
  243. CREATE TRIGGER reservations_bi
  244. BEFORE INSERT ON reservations
  245. FOR EACH ROW
  246. BEGIN
  247.     SELECT reservations_id_seq.NEXTVAL
  248.     INTO :NEW.id
  249.     FROM dual;
  250. END;
  251.  
  252. CREATE TRIGGER destinations_bi
  253. BEFORE INSERT ON destinations
  254. FOR EACH ROW
  255. BEGIN
  256.     SELECT destinations_id_seq.NEXTVAL
  257.     INTO :NEW.id
  258.     FROM dual;
  259. END;
  260.  
  261. --------------------------------------------------------
  262. --  PROCEDURES
  263. --------------------------------------------------------
  264.  
  265. DECLARE
  266.     mSelectedRows   NUMBER(2);
  267.     mClient         client%ROWTYPE;
  268. BEGIN
  269.    
  270.     ------------------
  271.     -- Select Rows
  272.     ------------------
  273.  
  274.     SELECT  id, name, phone
  275.     INTO    mClient
  276.     FROM    client
  277.     WHERE   phone LIKE '+359%'
  278.     AND     name LIKE 'Kol%';
  279.    
  280.  
  281.     --------------------------
  282.     -- If one row is found,
  283.     -- print it
  284.     --------------------------
  285.  
  286.     IF sql%found THEN
  287.         mSelectedRows := sql%rowcount;
  288.         DBMS_OUTPUT.put_line(mSelectedRows || ' row selected');
  289.         DBMS_OUTPUT.put_line('Name: ' || mClient.name || ' -- Phone: ' || mClient.phone);
  290.     END IF;
  291.  
  292.     ---------------
  293.     -- Exceptions
  294.     ---------------
  295.     EXCEPTION
  296.  
  297.     WHEN NO_DATA_FOUND THEN
  298.         DBMS_OUTPUT.put_line('No rows found!');
  299.  
  300.     WHEN TOO_MANY_ROWS THEN
  301.         mSelectedRows := sql%rowcount;
  302.         DBMS_OUTPUT.put_line(mSelectedRows || ' rows that match found');
  303. END;
  304.  
  305. CREATE OR REPLACE PROCEDURE createAutoIncrement(
  306.     tableName       VARCHAR2,
  307.     columnNameAI    VARCHAR2
  308. )
  309. IS
  310.     sequenceName            VARCHAR2(64);
  311.     sequenceStatement       VARCHAR2(64);
  312.     triggerStatement        VARCHAR2(256);
  313. BEGIN
  314.  
  315.     --------------------------------------------------
  316.     -- Check if table exists
  317.     --------------------------------------------------
  318.     FOR CUR_VAL IN (
  319.         SELECT  COUNT(*) AS tableCount
  320.         FROM    tab
  321.         WHERE   tname LIKE tableName
  322.     )
  323.     LOOP
  324.         ----------------------------------------------
  325.         -- If table does not exist, inform user
  326.         -- and exit procedure
  327.         ----------------------------------------------
  328.         IF CUR_VAL.tableCount < 1 THEN
  329.             DBMS_OUTPUT.put_line('Table ' || tableName || ' does not exist');
  330.             RETURN;
  331.         END IF;
  332.     END LOOP;
  333.  
  334.     --------------------------------------------------
  335.     -- Build sequence name
  336.     --------------------------------------------------
  337.     sequenceName        := tableName || '_id_seq';
  338.  
  339.     --------------------------------------------------
  340.     -- Check if sequence exists
  341.     --------------------------------------------------
  342.     FOR CUR_VAL IN (
  343.         SELECT  COUNT(*) AS seqCount
  344.         FROM    user_sequences
  345.         WHERE   sequence_name LIKE UPPER(sequenceName)
  346.     )
  347.     LOOP
  348.         ----------------------------------------------
  349.         -- If sequence exists, inform user
  350.         -- and exit procedure
  351.         ----------------------------------------------
  352.         IF CUR_VAL.seqCount > 0 THEN
  353.             DBMS_OUTPUT.put_line('Sequence already exists');
  354.             RETURN;
  355.         END IF;
  356.     END LOOP;
  357.  
  358.     --------------------------------------------------
  359.     -- Check if trigger already exists
  360.     --------------------------------------------------
  361.     FOR CUR_VAL IN (
  362.         SELECT  COUNT(*) AS trgCount
  363.         FROM    user_triggers
  364.         WHERE   trigger_Name LIKE UPPER(tableName || '_bi')
  365.     )
  366.     LOOP
  367.         ----------------------------------------------
  368.         -- If trigger exists, inform user
  369.         -- and exit procedure
  370.         ----------------------------------------------
  371.         IF CUR_VAL.trgCount > 0 THEN
  372.             DBMS_OUTPUT.put_line('Trigger already exists');
  373.             RETURN;
  374.         END IF;
  375.     END LOOP;
  376.  
  377.     --------------------------------------------------
  378.     -- Inform User and settle into local variable
  379.     --------------------------------------------------
  380.     DBMS_OUTPUT.put_line('Building sequence...!');
  381.     sequenceStatement := 'CREATE SEQUENCE ' || sequenceName;
  382.    
  383.     --------------------------------------------------
  384.     -- Execute Create sequence statement
  385.     --------------------------------------------------
  386.     DBMS_OUTPUT.put_line('Sequence built!');
  387.     EXECUTE IMMEDIATE sequenceStatement;
  388.    
  389.     --------------------------------------------------
  390.     -- Inform user about query execution
  391.     --------------------------------------------------
  392.     DBMS_OUTPUT.put_line('Sequence executed!');
  393.    
  394.     --------------------------------------------------
  395.     -- Building Trigger Query
  396.     -- And executing it to
  397.     -- create the trigger
  398.     -- for auto-incrementing
  399.     -- value
  400.     --------------------------------------------------
  401.    
  402.     DBMS_OUTPUT.put_line('Building trigger query...');
  403.     triggerStatement := 'CREATE TRIGGER ' || tableName || '_bi
  404.    BEFORE INSERT ON ' || tableName  || '
  405.    FOR EACH ROW
  406.    BEGIN
  407.        SELECT ' || sequenceName || '.nextval
  408.        INTO :new.' || columnNameAI || '
  409.        FROM dual;
  410.    END;';
  411.  
  412.     --------------------------------------------------
  413.     -- Debug
  414.     --------------------------------------------------
  415.     DBMS_OUTPUT.put_line(triggerStatement);
  416.    
  417.     --------------------------------------------------
  418.     -- Trigger query created and informed user
  419.     --------------------------------------------------
  420.     DBMS_OUTPUT.put_line('Trigger query created!');
  421.    
  422.     --------------------------------------------------
  423.     -- Executing create trigger statement and
  424.     -- informing the user
  425.     --------------------------------------------------
  426.     EXECUTE IMMEDIATE triggerStatement;
  427.    
  428.     DBMS_OUTPUT.put_line('Trigger query executed!');
  429. END;
  430.  
  431.  
  432. --------------------------------------------------------
  433. --  Free Reservations for destination
  434. --------------------------------------------------------
  435. DECLARE
  436.     destinationName destinations.name%TYPE;
  437.     freeSpots       destinations.totalreservations%TYPE;
  438.     totalSpots      destinations.totalreservations%TYPE;
  439. BEGIN
  440.     ------------------------------
  441.     -- Get user input
  442.     ------------------------------
  443.     destinationName := '&name';
  444.  
  445.     ------------------------------
  446.     -- Select total reservations
  447.     -- and free reservations
  448.     -- for the inputted
  449.     -- destination into
  450.     -- freeSpots and totalSpots
  451.     -- local variables
  452.     ------------------------------
  453.     SELECT
  454.         totalreservations,
  455.         (totalreservations - (SELECT COUNT(*)
  456.         FROM
  457.             reservations
  458.         LEFT JOIN
  459.             destinations
  460.         ON
  461.             reservations.destination_id = destinations.id
  462.         WHERE
  463.             destinations.name LIKE destinationName))
  464.         AS
  465.             FreeSlots
  466.     INTO
  467.         totalSpots,
  468.         freeSpots
  469.     FROM
  470.         destinations
  471.     WHERE
  472.         name LIKE destinationName;
  473.    
  474.     ------------------------------
  475.     -- See free spots
  476.     ------------------------------
  477.     DBMS_OUTPUT.put_line(freeSpots || ' out of ' || totalSpots || ' reservations are free for ' || destinationName);
  478.        
  479.     ------------------------------
  480.     -- Foolproof handling
  481.     -- and clairvoyance of
  482.     -- basic errors
  483.     ------------------------------
  484.     EXCEPTION
  485.    
  486.     WHEN NO_DATA_FOUND THEN
  487.         DBMS_OUTPUT.put_line('No rows found!');
  488.    
  489.     WHEN TOO_MANY_ROWS THEN
  490.         DBMS_OUTPUT.put_line('Too many rows found! Must be exactly one. Please check your SQL!');
  491.        
  492. END;
  493.  
  494. --------------------------------------------------------
  495. --  CURSORS
  496. --------------------------------------------------------
  497.  
  498. CURSOR returnClientType (mPhone IN VARCHAR2)
  499.     RETURN client%ROWTYPE
  500.  
  501. IS
  502.     SELECT  *
  503.     FROM    client
  504.     WHERE   phone LIKE mPhone;
  505.  
  506.  
  507. --------------------------------------------------------
  508. --  SELECT Statements
  509. --------------------------------------------------------
  510.  
  511. --------------------------------------
  512. -- Reservations between two dates
  513. --------------------------------------
  514.  
  515. SELECT
  516.     COUNT(*) AS reservationsCount
  517. FROM
  518.     reservations
  519. LEFT JOIN
  520.     employee
  521. ON
  522.     reservations.employee_id = employee.id
  523. WHERE
  524.     reservations.rdate
  525.     BETWEEN
  526.         TO_DATE('&date', 'dd/mm/yyyy')
  527.     AND
  528.         TO_DATE('&seconddate', 'dd/mm/yyyy');
  529.  
  530. --------------------------------------
  531. -- Tickets by date for client
  532. --------------------------------------
  533.  
  534. SELECT
  535.     *
  536. FROM
  537.     reservations
  538. LEFT JOIN
  539.     client
  540. ON
  541.     reservations.client_id = client.id
  542. ORDER BY
  543.     RDATE;
  544.  
  545. --------------------------------------
  546. -- Last 5 Reservations in DESC order
  547. --------------------------------------
  548.  
  549. SELECT
  550.     *
  551. FROM
  552.     (SELECT * FROM reservations ORDER BY id DESC) reservationsReversed
  553. RIGHT JOIN
  554.     employee
  555. ON
  556.     reservationsReversed.employee_id = employee.id
  557. WHERE
  558.     ROWNUM <= 5
  559. ORDER BY
  560.     ROWNUM DESC;
  561.  
  562.  
  563. -----------------------------------------
  564. -- Reservation by:
  565. --
  566. -- destination
  567. -- date
  568. -- time
  569. --
  570. -----------------------------------------
  571.  
  572. SELECT
  573.     *
  574. FROM
  575.     reservations
  576. LEFT JOIN
  577.     destinations
  578. ON
  579.     reservations.destination_id = destinations.id
  580. WHERE
  581.     name LIKE '&name'
  582.     AND
  583.         rdate LIKE TO_DATE('&date', 'dd/mm/yyyy')
  584.     AND
  585.         TO_CHAR(CAST(rhour AS DATE), 'HH24:MI')
  586.         LIKE
  587.             TO_CHAR(CAST(TO_TIMESTAMP('&hour', 'HH24:MI') AS DATE), 'HH24:MI');
  588.  
  589. --------------------------------------------------------
  590. --  INSERT Statements
  591. --------------------------------------------------------
  592.  
  593. INSERT INTO RESERVATIONS (ID,DESTINATION_ID,CLIENT_ID,EMPLOYEE_ID,RDATE,RHOUR,PRICE) VALUES (1,1,2,1,TO_DATE('2019-09-18','RRRR-MM-DD'),TO_TIMESTAMP('2019-09-18 11:24:25.338000000','RRRR-MM-DD HH24:MI:SSXFF'),500);
  594. INSERT INTO RESERVATIONS (ID,DESTINATION_ID,CLIENT_ID,EMPLOYEE_ID,RDATE,RHOUR,PRICE) VALUES (2,1,1,1,TO_DATE('2019-09-17','RRRR-MM-DD'),TO_TIMESTAMP('2019-09-07 11:38:24.341000000','RRRR-MM-DD HH24:MI:SSXFF'),340);
  595.  
  596.  
  597. INSERT INTO POSITIONS (ID,NAME) VALUES (1,'Manager');
  598. INSERT INTO POSITIONS (ID,NAME) VALUES (2,'Employee');
  599. INSERT INTO POSITIONS (ID,NAME) VALUES (3,'Example');
  600.  
  601.  
  602. INSERT INTO DESTINATIONS (ID,NAME, TOTALRESERVATIONS) VALUES (1,'Varna', 15);
  603. INSERT INTO DESTINATIONS (ID,NAME, TOTALRESERVATIONS) VALUES (2,'Sofia', 4);
  604. INSERT INTO DESTINATIONS (ID,NAME, TOTALRESERVATIONS) VALUES (3,'Moscow', 50);
  605. INSERT INTO DESTINATIONS (ID,NAME, TOTALRESERVATIONS) VALUES (4,'Shanghai', 12);
  606.  
  607.  
  608. INSERT INTO CLIENT (ID,NAME,PHONE) VALUES (1,'Kolev','+359876181955');
  609. INSERT INTO CLIENT (ID,NAME,PHONE) VALUES (2,'Ivan','+359876181954');
  610.  
  611.  
  612. INSERT INTO EMPLOYEE (ID,POSITION_ID,NAME,PHONE) VALUES (1,1,'Kolev','+359876181954');
  613.  
  614. --------------------------------------------------------
  615. --  UPDATE Statements
  616. --------------------------------------------------------
  617.  
  618. UPDATE
  619.     reservations
  620. SET
  621.     price = 890
  622. WHERE
  623.     id = 1;
  624.  
  625.  
  626. UPDATE
  627.     positions
  628. SET
  629.     name = 'Database Engineer'
  630. WHERE
  631.     id = 3;
  632.  
  633. --------------------------------------------------------
  634. --  DELETE Statements
  635. --------------------------------------------------------
  636.  
  637. DELETE FROM
  638.     destinations
  639. WHERE
  640.     id = 4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement