Difference between revisions of "Import data"

From SQLZOO
Jump to: navigation, search
(Created page with "Import someone else's data. <div class='ht'> <div class=params>schema:scott</div> <source lang=sql class='tidy'>DROP TABLE employeeParking; DROP TABLE employeeCopy;</source> <...")
 
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Import someone else's data.
+
<p>Import someone else's data.</p>
 +
<p>In this example you are shown how to make a mimic which essentially makes a copy of the database table so it can be freely edited</p>
 +
<p>and then the newly fresh one when finished can be used to replace the out of date existing table.</p>
 +
<p>Table 1 and 2 show the two separate tables and table 3 displays the results that should be inside the mimic in this example.</p>
 +
<div class="ref_section">
 +
<table class= "db_ref">
 +
<caption>Table 1</caption>
 +
<tr><th align='center'>'''id'''</th><th>'''parkingSpace'''</th></tr>
 +
<tr><td align='left'>E01</td><td align='left'>F8</td></tr>
 +
<tr><td align='left'>E02</td><td align='left'>G22</td></tr>
 +
<tr><td align='left'>E03</td><td align='left'>F7</td></tr>
 +
</table>
 +
<table class="db_ref">
 +
<caption>Table 2</caption>
 +
<tr><th align='center'>'''id'''</th><th>'''name'''</th><th align='center'>'''phone'''</th></tr>
 +
<tr><td align='left'>E01</td><td align='left'>Harpo</td><td align='left'>2753</td></tr>
 +
<tr><td align='left'>E02</td><td align='left'>Zeppo</td><td align='left'>2754</td></tr>
 +
<tr><td align='left'>E03</td><td align='left'>Groucho</td><td align='left'>2755</td></tr>
 +
</table>
 +
<table class="db_ref">
 +
<caption>Table 3</caption>
 +
<tr><th align='center'>'''id'''</th><th>'''name'''</th><th align='center'>'''phone'''</th><th align='center'>'''parkingSpace'''</th></tr>
 +
<tr><td align='left'>E01</td><td align='left'>Harpo</td><td align='left'>2753</td><td align='left'>F8</td></tr>
 +
<tr><td align='left'>E02</td><td align='left'>Zeppo</td><td align='left'>2754</td><td align='left'>G22</td></tr>
 +
<tr><td align='left'>E03</td><td align='left'>Groucho</td><td align='left'>2755</td><td align='left'>F7</td></tr>
 +
</table>
 +
</div>
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
 
<source lang=sql class='tidy'>DROP TABLE employeeParking;
 
<source lang=sql class='tidy'>DROP TABLE employeeParking;
DROP TABLE employeeCopy;</source>
+
DROP TABLE employeeCopy;
 +
DROP VIEW mimic;</source>
 
<source lang=sql class='setup'> CREATE TABLE employeeParking(
 
<source lang=sql class='setup'> CREATE TABLE employeeParking(
 
   id TEXT,
 
   id TEXT,
Line 19: Line 46:
 
</source>
 
</source>
 
<div>
 
<div>
 +
<p>To allow data to be imported into your system a mimic of your system can be made
 +
and then all that needs to be done is the rows from the old copy have to be deleted
 +
and then the table can be refilled with the fresh imported copy.</p>
 +
<p>This method is not completely ideal but is efficient as a temporary
 +
measure until an up to date version of the table is made.</p>
 
</div>
 
</div>
  
<source lang='sql' class='def'>CREATE VIEW mimic AS
+
<source lang='sql' class='def'>
SELECT employeeParking.id, COALESCE(name, employeeParking.id) AS name,
+
CREATE VIEW mimic AS
COALESCE(phone, 'Not Available') AS phone, parkingSpace
+
    SELECT employeeParking.id, COALESCE(name, employeeParking.id) AS name,
FROM employeeParking LEFT OUTER JOIN
+
    COALESCE(phone, 'Not Available') AS phone, parkingSpace
employeeCopy ON (employeeParking.id = employeeCopy.id)</source>
+
      FROM employeeParking LEFT OUTER JOIN
 +
employeeCopy ON (employeeParking.id = employeeCopy.id);
 +
      SELECT * FROM mimic;</source>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
</div>
 
</div>
  
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 16:15, 2 August 2012

Import someone else's data.

In this example you are shown how to make a mimic which essentially makes a copy of the database table so it can be freely edited

and then the newly fresh one when finished can be used to replace the out of date existing table.

Table 1 and 2 show the two separate tables and table 3 displays the results that should be inside the mimic in this example.

Table 1
idparkingSpace
E01F8
E02G22
E03F7
Table 2
idnamephone
E01Harpo2753
E02Zeppo2754
E03Groucho2755
Table 3
idnamephoneparkingSpace
E01Harpo2753F8
E02Zeppo2754G22
E03Groucho2755F7
schema:scott
DROP TABLE employeeParking;
DROP TABLE employeeCopy;
DROP VIEW mimic;
 CREATE TABLE employeeParking(
  id TEXT,
  parkingSpace TEXT );
INSERT INTO employeeParking VALUES ('E01','F8');
INSERT INTO employeeParking VALUES ('E02','G22');
INSERT INTO employeeParking VALUES ('E03','F7');
CREATE TABLE employeeCopy (
  id TEXT,
  name VARCHAR(20),
  phone INTEGER );
INSERT INTO employeeCopy VALUES ('E01','Harpo',2753);
INSERT INTO employeeCopy VALUES ('E02','Zeppo',2754);
INSERT INTO employeeCopy VALUES ('E03','Groucho',2755);

To allow data to be imported into your system a mimic of your system can be made and then all that needs to be done is the rows from the old copy have to be deleted and then the table can be refilled with the fresh imported copy.

This method is not completely ideal but is efficient as a temporary measure until an up to date version of the table is made.

CREATE VIEW mimic AS
     SELECT employeeParking.id, COALESCE(name, employeeParking.id) AS name,
     COALESCE(phone, 'Not Available') AS phone, parkingSpace
       FROM employeeParking LEFT OUTER JOIN
employeeCopy ON (employeeParking.id = employeeCopy.id);
       SELECT * FROM mimic;
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense