Structured Data: Create A Table To Store Information About Weather Observation Stations
Structured Data: Create A Table To Store Information About Weather Observation Stations
Accessing structured data with SQL is quite different from the full text search of documents on
the Web. Structured data in the relational model means data that can be represented in tables --
rows and columns. Each row in a table represents a different object, and the columns represent
various "attributes" of the object. The columns have names and integrity constraints that specify
valid values.
Since the column values are named and are represented in a consistent format, you can select
rows very precisely, based on their contents. This is especially helpful in dealing with numeric
data. You can also join together data from different tables, based on matching column values.
You can do useful types of analysis, listing objects that are in one table and missing (or present,
or have specific attributes) from a related table. You can extract from a large table precisely
those rows of interest, regrouping them and generating simple statistics on them.
creating a table
creating a view
inserting rows
updating rows
deleting rows
commit -- making changes permanent
rollback -- undoing temporary changes
enforcing integrity constraints
using an Embedded C program
ID CITY STATE
13 Phoenix AZ
44 Denver CO
66 Caribou ME
ID CITY STATE
44 Denver CO
66 Caribou ME
populate the table STATS with some statistics for January and July:
query to look at table STATS, picking up location information by joining with table
STATION on the ID column:
-- matching two tables on a common column is called a "join".
-- the column names often match, but this is not required.
-- only the column values are required to match.
query to look at the table STATS, ordered by month and greatest rainfall, with columns
rearranged:
query to look at temperatures for July from table STATS, lowest temperatures first,
picking up city name and latitude by joining with table STATION on the ID column:
query to show MAX and MIN temperatures as well as average rainfall for each station:
query (with subquery) to show stations with year-round average temperature above 50
degrees:
-- rows are selected from the STATION table based on related values in the STATS table.
create a view (derived table or persistent query) to convert Fahrenheit to Celsius and
inches to centimeters:
query to look at table STATS in a metric light (through the new view):
another metric query restricted to January below-freezing (0 Celsius) data, sorted on
rainfall:
update one row, Denver's July temperature reading, to correct a data entry error:
COMMIT WORK;
Oops! We meant to update just the July reading! Undo that update:
-- undoes only updates since the last COMMIT WORK.
ROLLBACK WORK;
COMMIT WORK;
delete July data and East Coast data from both tables:
-- note that we use longitude values from the related STATION table to determine which STAT
stations were east of 90 degrees.
COMMIT WORK;
SQL Constraints
SQL enforces data integrity constraints.
error message
violation of constraint STATS_FOREIGN1 caused operation to fail
Attempt to update a row with a temperature below the range -80 TO 150.
error message
violation of constraint STATS_CHECK2 caused operation to fail
Attempt to insert a row with negative rainfall measurement, outside the range 0 to 100.
error message
violation of constraint STATS_CHECK3 caused operation to fail
Attempt to insert a row with month 13, outside the range of 1 to 12.
error message
violation of constraint STATS_CHECK1 caused operation to fail
Attempt to insert a row with a temperature above the range -80 TO 150.
error message
violation of constraint STATS_CHECK2 caused operation to fail
status message
1 row inserted
error message
violation of constraint STATS_PRIMARY_ID_MONTH caused operation to fail
#include<stdio.h>
#include<string.h>
EXEC SQL BEGIN DECLARE SECTION;
long station_id;
long mon;
float temp;
float rain;
char city_name[21];
long SQLCODE;
EXEC SQL END DECLARE SECTION;
main()
{
/* the CONNECT statement, if needed, goes here */
strcpy(city_name,"Denver");
EXEC SQL SELECT ID INTO :station_id
FROM STATION
WHERE CITY = :city_name;
if (SQLCODE == 100)
{
printf("There is no station for city %s\n",city_name);
exit(0);
}
printf("For the city %s, Station ID is %ld\n",city_name,station_id);
printf("And here is the weather data:\n");
EXEC SQL DECLARE XYZ CURSOR FOR
SELECT MONTH, TEMP_F, RAIN_I
FROM STATS
WHERE ID = :station_id
ORDER BY MONTH;
EXEC SQL OPEN XYZ;
while (SQLCODE != 100) {
EXEC SQL FETCH XYZ INTO :mon, :temp, :rain;
if (SQLCODE == 100)
printf("end of list\n");
else
printf("month = %ld, temperature = %f, rainfall = %f\n",mon,temp,rain);
}
EXEC SQL CLOSE XYZ;
exit(0);
}
Execution log:
For the city Denver, Station ID is 44
And here is the weather data:
month = 1, temperature = 27.299999, rainfall = 0.180000
month = 7, temperature = 74.800003, rainfall = 2.110000
end of list
return to SQL Table of Contents