Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SQL and iSeries Computing These notes are being compiled as a supplement to the required labs for the DBS201 course. There will be no attempt to unravel the intricacies of the iSeries; rather, these notes will focus on the SQL scripts that can be written on the ‘green screen’ without the use of any additional assistance from the system itself. With the assistance of instructions from the introductory lab, students should be able to start SQL, and reach a screen headed ‘Enter SQL Statements’. This is the screen that we will work from. Using SQL code, we are able to formulate queries or write commands that will: a) retrieve data from existing databases b) create and update the tables of a new or existing database. The iSeries contains a library of database collections or schema. Each schema or collection contains tables. We can retrieve data from an existing database collection by using a SQL query in the following form: SELECT <attributes> FROM <schema.entity (or schema.entities)> WHERE <certain conditions exist> …this clause is optional. Note that the query is directed at a particuar table (or tables) of a schema. When we are querying the system, the attributes that we select must be written in exactly the same form as exist in the table. Alternately, if we are writing these commands on paper, any the attributes can be written in any short form that is representative of the attributes in question. To create database tables in your account, you use the SQL CREATE TABLE command to create the table and describe its layout. As is shown below, tables that are created require that you list a column name, define its data type, and indicate whether a value must be entered for that attribute, or if it can be left empty. Column Name Data Type Allow Nulls O The Column Name column lists the name of the table attribute(s). The data types that you will most often encounter are listed below: INTEGER - uses integers, numbers without a decimal part. You can use the DECIMAL(p,q) – Stores a decimal number p digits long with q of those digits being contents of INTEGER fields for calculations. decimal places. For example, DECIMAL(5,2) represents a number with 3 places to the left of the decimal point, and 2 places top the right of the decimal point. CHAR - Stores a character string n characters long. You use the CHAR type for DATE – Stores dates in the form DD-MM-YYYY or MM/DD/YYYY. To enter date fields that contain letters and other special characters and for numbers that will not be used for calculations. For example, since neither the sales rep number nor the customer number will be used in any calculations, it is best to assign both of them the CHAR data type. data , key in the form of YYYY_MM_DD. Allowing NULLS means that it is possible for the field to be EMPTY and not destroy the integrity of the database. A NULL does not mean that the value of the field is 0 (zero). CREATE TABLE Premiere.Rep (RepNum CHAR(2) not null, LastName CHAR(15) not null, FirstName Street City State ZipCode Commission Rate CHAR(15) not null, CHAR(15) not null, CHAR(15) not null, CHAR(2) not null, CHAR(5) not null, DECIMAL(7,2) not null, DECIMAL(3,2) not null) (Note: there is no rule that says you must write the SQL in the format given above. It is simply written that way to make the command more readable.) Examples of Simple Retrieval Recall that the basic form of an SQL retrieval command is SELECT-FROM-WHERE. There are no special formatting rules in SQL; putting the SELECT, FROM, and WHERE clauses on separate lines is done so as to make the commands more readable. Also, so that you can try these queries on your own, the attributes of the Customer table will be listed here as they are written in the Premiere database : CUSTOMER [customer_number, last_name, first_name, street, city, state, zip_code, balance, credit_limit, sales_rep_number] Consider the following examples: a)List the number, name and balance of all customers. SELECT customer_number, last_name, first_name, balance FROM premiere.customer (there is no WHERE clause because no conditions have to be met) b)List the complete PART table SELECT * FROM premiere.part c)List the name of every customer with a $1000 credit limit SELECT first_name, last_name FROM premiere.customer WHERE credit_limit=1000 d)Find the name of customer 124 SELECT first_name, last_name FROM premiere.customer Where customer_number = ‘124’ Note that the customer number is enclosed by single parentheses because it was defined as a CHAR field. e)Find the customer name for every customer located in Lansing SELECT first_name, last_name FROM premiere.customer WHERE city = ‘Lansing’ f)List the number, name, credit limit and balance for all customers with credit limits that exceed their balances. SELECT customer_number, first_name, last_name, balance FROM Customer WHERE credit_limit > balance SQL Commands with Compound Conditions A compound condition is formed by connecting two or more simple conditions using one both of the following operators: AND and OR. You can also precede a single condition with the NOT operator to negate the condition. When you connect simple conditions using the AND operator, all simple conditions must be true for the compound condition to be true. When you connect simple conditions using the OR operator, the compound condition will be true whenever any of the simple conditions are true. Preceding a condition with the NOT operator reverses the truth or falsity of the original condition. Further Examples: g)List the descriptions of all parts that are located in warehouse 3 and for which there are more than 20 units on hand. SELECT part_description FROM premiere.part WHERE warehouse = ‘3’ and on hand>20 h)List the description of all parts that are located in warehouse 3 or for which there are more than 20 units on hand SELECT part_description FROM premiere.part WHERE warehouse = ‘3’ or on_hand>20 i)List the description of all parts that are not in warehouse 3 SELECT part_description FROM premiere.part WHERE NOT Warehouse =’3’ j)List the number, name, and balance of all customers with balances greater than or equal to $500 and less than or equal to $2,000 SELECT customer_number, last_name, first_name, balance FROM premiere.customer WHERE balance BETWEEN 500 and 2000 COMPUTED FIELDS You can include fields in queries that are not in a database, but whose values you can compute from existing database fields. A field whose values you derive from existing fields is called a computed or calculated field. Computed fields can include addition (+), subtraction (-), multiplication (*), or division(/). More Examples: k)List the number, name and available credit for all customers. SELECT customer_number, last_name, first_name, credit_limit-balance as available credit FROM premiere.customer There is no field in the database that stores available credit, but you can compute it using two fields that are available in the database: Credit Limit and Balance. l)List the number, name, and available credit for all customers with credit limits that exceed their balances SELECT customer_number, last_name, first_name, credit_limit-balance as available credit FROM premiere.customer WHERE credit_limit > balance