Using Temporary Tables : Temporary Table « Table « Oracle PL / SQL






Using Temporary Tables

    
SQL>
SQL>
SQL>
SQL> CREATE TABLE emp (
  2     empID INT NOT NULL PRIMARY KEY,
  3     Name      VARCHAR(50) NOT NULL);

Table created.

SQL> INSERT INTO emp (empID,Name) VALUES (1,'Tom');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (2,'Jack');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (3,'Mary');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (4,'Bill');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (5,'Cat');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (6,'Victor');

1 row created.

SQL>
SQL>
SQL> CREATE TABLE empExam (
  2     empID  INT NOT NULL,
  3     ExamID     INT NOT NULL,
  4     Mark       INT,
  5     Taken   SMALLINT,
  6     Comments   VARCHAR(255),
  7     CONSTRAINT PK_empExam PRIMARY KEY (empID, ExamID));

Table created.

SQL>
SQL>
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (1,1,55,1,'Satisfactory');

1 row created.

SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (1,2,73,1,'Good result');

1 row created.

SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (2,3,44,1,'Hard');

1 row created.

SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (2,5,39,0,'Simple');

1 row created.

SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken) VALUES (2,6,63,1);

1 row created.

SQL>
SQL>
SQL> CREATE GLOBAL TEMPORARY TABLE tmp
  2  AS
  3  SELECT emp.Name AS empName, AVG(Mark) AS AverageMark
  4  FROM empExam
  5     INNER JOIN emp
  6     ON empExam.empID = emp.empID
  7  GROUP BY emp.Name;



SQL>
SQL> INSERT INTO tmp
  2     SELECT emp.Name AS empName, AVG(Mark) AS AverageMark
  3     FROM empExam
  4        INNER JOIN emp
  5        ON empExam.empID = emp.empID
  6     GROUP BY emp.Name;

2 rows created.




SQL>
SQL>
SQL> drop table empExam;

Table dropped.

SQL> drop table emp;

Table dropped.

   
    
    
    
  








Related examples in the same category

1.Create temporary table
2.Create global temporary table from existing table
3.Create global temporary table with 'on commit delete rows' option
4.Temporary tables cannot be forced into logging.
5.Temporary tables support primary keys.
6.Temporary tables do not support foreign keys
7.You cannot alter a temporary table to change its data duration.(drop and recreate)
8.ORA-14452: attempt to create, alter or drop an index on temporary table already in use
9.Update a TEMPORARY TABLE and check the table it based on
10.create global temporary table on commit delete rows
11.Temporary table on commit preserce and delete
12.drop global temporary table
13.Insert into a temporary with select statement
14.Insert data into the temporary table.
15.truncate a global temporary table
16.Create global temporary table working_emps on commit preserve rows