Getting Started
The secret of becoming an expert in PeopleSoft or in any of the other front-end products is in understanding very clearly what goes on in the background. Understand this well, any you will find that there is a great deal that you can do with the product. And here, you will revise all that you need to know about Structured Query Language (pronounced sequel). There is no real difference from one product’s implementation of SQL to another. At best, there are extensions to the language to give greater functionality. Any information is useless all by itself. Hence, to make sense of information, one needs to arrange it in some meaningful manner or be able to order it somehow. And this is where computers can play a great role. Computers are able to store information, and retrieve information pretty quick. However, there are rules that one must observe in storing the data so that it can be accessed fast. There are also some commonly used terms that one follows so as not to confuse others who work with computers as well. Information when stored on the computer is put together as a collection of smaller individual units. One could refer to each individual bit as a cell or a field, much like the cells and fields that one sees in spreadsheets. Put these cells together in an orderly fashion and you have a whole set of rows and columns that make up the worksheet — terms that are also used when working with the database. A row or a record contains different data types, all of which refer to a single item in the list. A column however, will contain the same data type all throughout. For example, a row or record could contain a mix of character information as well as fields that contain numeric information too. However, a column once defined will only contain either character information or numeric information. (There are other data types that will be explained as we go along.) A collection of these rows and columns create what is called a Table in computerese. And a database can have multiple tables just like an Excel or Lotus 1-2-3 spreadsheet file can have multiple worksheets. Each sheet is unique but yet has some relation to the next which will why they are stored together. To prevent each table from growing too large in size, which would hamper handling and other operations, table definitions are kept to a bare minimum with data that is likely to be repeated many times over pushed into another table. For example, if you had to write a letter that contained a list of names of students, ages and schools that students came from, when the students came from just three schools, you would be better of doing a substitution. Like Jack, 12, B Jill,15,A Mahesh,8,C

Tom. It lets the SQL engine know that the command is completed. all one needs to do is to write out the name of the school once and replace the text accordingly.C Writing out just an A. You will have created another table with four fields this time. Two fields can store numerical values and two fields will be able to store non-numerical values like text. In fact. This table would only have a single column VNO. This would require one to analyse the situation in depth and design the overall database before starting. all the Bs with Christ Church School and similarly for C.12. Once the list is complete. With multiple lines of code this could get a bit more confusing. This will become clearer as you key in longer lines of code that will not fit in a single line or that require to be spread over several lines simple make understanding easier. To execute the statement. In the feedback window. all one needs to do is to separate each column definition with a comma.C Umesh. press Shift+F2 or Session | Execute Command. you will attempt to create a simple one field table. To create multiple fields or columns in one go. . it would be simplicity itself in locating all the As and replacing them with St. You need to pay attention to what you are doing as you are not told which table was created. you will be informed that a table was created. CCODE CHAR(2). the less space will it occupy when stored and the faster will work be done on any individual set of columns. being able to define a table that does not require major modification is another matter. The better the database design.B Angel. Hence. TOTAL NUMBER). Try the next command CREATE TABLE SALES(VNO NUMBER. PROD CHAR(2). a single command can create a rather complex table.Mary’s High School. How to start and use SQLTalk Creating tables Making a table is not difficult at all. B or C is far easier than writing the entire name of the school. What you did with the last statement was to request the SQL engine to create a table in the currently active database which would be called or referred to as TEST. However. In the SQL command window. Do not forget the semi-colon at the end of the line as this important. which will hold only numeric data. This is also possible in database operations. key in the following command CREATE TABLE TEST(VNO NUMBER).13.9. But for now. Press Shift+F2 or click on Session and then Execute Command to execute the line of code.

SELECT VNO. But this is not all that a programmer can do. SELECT * FROM SALES. ‘C3’. The values must match the specified data type for that cell. ‘C1’.Filling up the table Unlike Excel. You now have four records stored away in the SALES table. To do this the programmer has to specify the records that he requires. PROD. This is obviously not the best way to display the data required from a particular record or set of records. Use the following statements so that you have the same results that you see on the screen. 40). the data is then available for further processing. the process of placing values into a database table is slightly different. INSERT INTO SALES VALUES (3. You will need to use some commands to assign values to each cell. Where as the values inserted into the other two fields are character type. Once a selection is made. The only difference in this example is that the CUST field values for each record will not be displayed at all. ‘P1’. This is done so INSERT INTO SALES VALUES (1. ‘P2’. A SELECT command defines which records should be pulled out from a table and which not. Add a few more rows to the table so that there are sufficient records to experiment with. 20). Refining the SELECT command. Execute the above statement and as there are no conditions set all the fields and all the rows of the SALES table are selected. The simplest form of using this command is in the following manner. where one can see the cells and directly enter values to each. TOTAL FROM SALES. Do note the method in which the command was issued. 30). By adding on a couple of other options to the SELECT statement. as the first and the last fields of the SALES table have been defined as NUMBER. ‘C2’. it is possible to narrow the search to locate what you want rather quickly. ‘P2’. the values that are entered into these fields are numeric. one can . INSERT INTO SALES VALUES (2. there is no direct graphical representation of each table as in the case of a spreadsheet. This is because in SQL. to display only the VNO. Hence. ‘P1’. You will see them in the response window. ‘C1’. But this data is of no use unless it can be used or displayed in some manner that makes sense to the user. 10). you will have to execute the following command. The simplest form of doing this is by using the INSERT command. Do not forget the semi-colon at the end and to execute the command by pressing Shift+F2. For instance. This is done by SELECTing. You need to specify in which table to wish to add a row. you will get a feed back of all the records selected from the table SALES. and the values that should be filled in. As always in the interactive mode. INSERT INTO SALES VALUES (4. PROD and TOTAL fields. The response will inform you that the values have been successfully entered.

A command like SELECT * FROM SALES WHERE 2=2. The second condition further filters out from these three records any record that does not contain the text P1 in the field PROD. Narrowing down the selection even further. The next command shows you how this can be done by using the WHERE clause. Therefore .e. Do remember while combining the different conditions. This allows the total size of the database to be ultimately smaller than one large table that has all the values stored in it. In this case. All rows where the value of VNO is greater than two are selected. There are no limitations to the number of conditions that can be applied with the SELECT command except that the condition must be equated as true. and the PROD = ‘P1’ selects the first and third records.request the SQL engine to perform a small test on the data stored in a particular field on the table. Two or more… Being able to narrow down the records that one can see or use is what makes SQL so powerful. These two refer to a customer code and a product code respectively. then either of the conditions require to be met before a row is selected. Is a valid command that will select all records in the table and SELECT * FROM SALES WHERE 1=2. Would select no records as the condition will never be fulfilled or equated as true. all the rows are selected as the VNO >= 2 condition passes the last three records. a database usually has multiple tables that can be linked together in some way. In our example. This happens because the AND keyword requires both conditions to be met before passing or selecting a row. The first condition defines that the value defined in the field VNO must be equal to or greater than the value 2. Try the following: SELECT * FROM SALES WHERE VNO >= 2 AND PROD = ‘P1’ Such a command will only select a single record from out sample table SALES. you will have to make use of brackets to define the order in which the conditions must be applied. there are a couple of fields that are represented by code values i. before selected the record. This will be explained in depth later. This gives us three records which could potentially be selected. CCODE and PROD. For example. only the last two records have such a value and hence. . SELECTing does not in any way change the values stored in each field but it does make it easier to work with a huge database by eliminating records that one does not require. A programmer can set a number of conditions that have to be met before a record or row is chosen. SELECT * FROM SALES WHERE VNO > 2. in our SALES table. If the AND was an OR. they will be the only ones selected. There are two conditions that must be met for a row to be selected. requires a little bit more of work.

If for some reason. PNAME CHAR(8)). modifications are also simplified. John and Jill appear more than once in the table. ‘John’). as the SALES table is in no way modified. you will need to create another three tables thus: CREATE TABLE CUST(CCODE CHAR(2). ‘S2’). one required to store further details on each customer. INSERT INTO SALESMAN VALUES(‘S2’. INSERT INTO PRODUCT VALUES(‘P2’. assigning another salesman to a customer. INSERT INTO CUST VALUES(‘C3’. This also allows one to keep the older transactions unchanged. For instance. To do this. multiplying into a few thousand records (which is what a normal database would contain) the number of bytes saved will keep increasing. You will now have three other tables in addition to SALES. SNAME CHAR(8)). ‘S1’). needs only a small modification on one table CUST. While the number of bytes saved may not seem much with this limited data available. CREATE TABLE SALESMAN (SCODE CHAR(2). And fill in the following values: INSERT INTO CUST VALUES(‘C1’. SCODE CHAR(2)). One will be able to generate all transactions with the correct salesman being identified. ‘Jane’. ‘Jill’. INSERT INTO PRODUCT VALUES(‘P1’. the amount of repetition would automatically increase and so would the amount of space ‘wasted’. ‘Copper’). INSERT INTO CUST VALUES(‘C2’. ‘S1’). rather than searching and modifying the entire database. SQL therefore gives the programmer the option of breaking up a single table into more manageable bits and yet be able to present them all together. ‘Tom’). one can see that the values Silver. Further. CNAME CHAR(8).VOUCHER 1 2 3 4 CUSTOMER Jill Jane Mary Jill PRODUCT Copper Silver Copper Silver SALESMAN John Tom John John TOTAL 10 20 30 40 Even with such a brief description. INSERT INTO SALESMAN VALUES(‘S1’. ‘Silver’). product and salesman. CREATE TABLE PRODUCT (PCODE CHAR(2). Copper. ‘Mary’. .

you now have only those records where PROD=PCODE. The value found in PROD must match the value in PCODE. Try SELECT * FROM SALES. This could be represented thus: VNO 1 2 3 4 1 2 3 4 CCODE C1 C2 C3 C1 C1 C2 C3 C1 PROD P1 P2 P1 P2 P1 P2 P1 P2 TOTAL 10 20 30 40 10 20 30 40 PCODE P1 P1 P1 P1 P2 P2 P2 P2 PNAME Copper Copper Copper Copper Silver Silver Silver Silver As you will have noticed. In the table SALES. This is obviously not what one would like to do. For this to work. for every row table SALES is added to every row in table PRODUCT. the VNO is the primary. each table must have at least one primary key — where the contents of a field are not repeated. the condition to be satisfied is defined. it is called a Foreign key. Using the same commands that were used earlier. Therefore from the earlier set of eight rows or records that were selected. one now can modify the earlier command so that the rows selected match the maximum number of records that are there in the table SALES. Try the following command: SELECT * FROM SALES. Since values in the field PROD of table SALES is not unique. What one needs to do is to pay attention to the manner in which the fields are merged. Such a command will result in a selection of rows which equals to the total number of rows of the table SALES multiplied into the total number of rows in table PRODUCT.Learning to merge two or more tables together is therefore of prime importance in database administration. And the number of columns that the table would have is the sum of both tables SALES and PRODUCT. . In this form. PRODUCT. Two or more tables are always ‘joined’ in this manner — a foreign key of one table is set to match a primary key of another table. The command set that one needs to use is no different than what one has already used so far. PRODUCT WHERE PROD = PCODE. In the table PRODUCT. You will have noticed that there is an overlap as far as the product code is concerned and this is what one uses to link the two tables together. PCODE is the primary key. which in this case is 8.

All one has to remember to do is to identify the correct primary and secondary key in the tables to be joined. PNAME.PROD. Continuing to join… There is a lot more to the SELECT command than we have covered so far. And in the same manner. CCODE. To identify the right PCODE for each table. it is possible to link or merge the other two tables as well. Add in one more table that will take care of the individual items on the voucher that is stored in sales.PCODE.PCODE = PRODUCT. INO NUMBER. it is possible to display only the fields one wishes to see or use and not any other.). PRODUCT WHERE PROD = PCODE. One could therefore use PCODE in both the SALES and the PRODUCT tables.VNO 1 3 2 4 CUST C1 C3 C2 C1 PROD P1 P1 P2 P2 TOTAL 10 30 20 40 PCODE P1 P1 P2 P2 PNAME Copper Copper Silver Silver Using this same method. By default. each field name when used is preceded by its table name but separated by a period (. TOTAL FROM SALES. PCODE CHAR(2). This we will place in a table called SALESI for sales items. the field name PROD is actually SALES. the previous command line would have to read as: SELECT VNO. While care has been taken not to repeat field or column names in these examples for clarity. PRODUCT WHERE SALES. PNAME. Insert the following records into the table VNO 1 1 2 2 INO 1 2 1 2 PCODE P1 P2 P1 P2 ITOTAL 6 4 10 5 . ITOTAL NUMBER). displaying the product code is of little consequence and one could just as well select only the following fields in the following manner: SELECT VNO. In the current example. it is possible that each individual table uses the same field name. Hence. CCODE. Do this in the following manner CREATE TABLE SALESI(VNO NUMBER. TOTAL FROM SALES.

CUSTOMER. PRODUCT.SCODE=SALESMAN. This order is in no way affected by the order in which the table names are specified.SCODE.2 3 3 3 3 1 2 3 P3 P1 P2 P3 5 17 12 1 Displaying all the columns in a single statement would require the use of an asterisk (*). You could even use words and values that are not column names in the following manner: SELECT ‘X’ FROM SALES. all other fields which have matching contents use the same field name. CUSTOMER. one could also specify which columns should be displayed.CCODE AND CUSTOMER. PRODUCT. one would first have to define how the records from different tables should be matched up. you will bring down the number of records listed. SALESMAN. . Do remember to bear in mind the column names that you have used. Yet the total number of records from all tables just adds up to around sixteen records. SALESMAN WHERE SALES. To keep the result more precise. but it also display a column from the each table in the order defined. The result will be a whole list of records each joined with every other record from every other table. A short note on what each table contains will help you in not generating errors. It is good programming practice to also keep the column type and length of same named columns the same. Besides displaying all the columns by using the asterisk (*). program maintenance becomes simpler too just because one will no longer have to scratch one’s head to remember what different columns are called. SALESI. If you will notice.CCODE=CUSTOMER. The above statement not only joins the different tables together. except for the different product code columns. This clause defines which fields from one table are linked to which fields on another table. SALESMAN. A command like SELECT VNO. SNAME FROM SALES.VNO AND PROD=PCODE AND SALES. Try the following SELECT * FROM SALES.Using this without actually qualifying how the rows are to be joined would display over 200 records.VNO=SALESI. One therefore requires to use the WHERE clause. CNAME. You will find that using the next statement. SELECT * FROM SALES. While it may sound easier here to use different column names in different tables.CCODE=CUSTOMER. SALESI. CUSTOMER WHERE SALES.CCODE. Modify the earlier statement so that the links between one file and the next are clearly defined. in the long run.

then the following would work without a problem: SELECT VNO. Adding it all up! So far life was rather simple. SUM adds up all the values of the column TOTAL from the table SALES and then displays it in a single line. For example. . This option only applies to numeric information. one needs to issue a single command like so: SELECT SUM(TOTAL) FROM SALES. To do this in SQL. For instance. It is also possible to mix numerics with columns to display a result directly. TOTAL*25/100 FROM SALES. SELECT * FROM SALESI WHERE PCODE IN (‘P1’. one would normally code in the following way: SELECT * FROM SALES WHERE TOTAL BETWEEN 11 AND 30. Even if the table had another thousand records. This clause helps the programmer to identify a set of records that fall on or between to end values. so that you can customise the output that is displayed. you could also use both these command together too by joining them with a AND or OR. Used thus. This would also work across different tables. The command used this way will display only those rows of the table SALESI where the value in the column PCODE matches any of the strings listed between the brackets. which in this case is 3. save that they do not required to be shielded by quotation marks. SQL has a number of functions and operators that allow a programmer to directly manipulate the data and display results.if 25 per cent of each sale was your profit margin and you would like to see how much you made per bill. Not here or there… Another clause that makes selection easier is the BETWEEN clause. ‘P3’) AND ITOTAL BETWEEN 6 AND 20. ‘P3’). There is nothing that prevents one from using the operators in the WHERE clause as well. To select all the rows in the table SALES where TOTAL is equal to or greater than 11 and equal to or less than 30. displaying the total sales done to date. there would be only a single line result.This will generate a result set that will only contain a single column labeled ‘X’ and it will be repeated as many times as there are records in the table SALES. The arithmetic operators can be used in any part of the command. SQL will perform the calculation on the fly and the result is displayed at once. would require one to add up all the values in the column TOTAL. a good deal of database operations has to do with numbers. All that we had covered till now was how to join to tables and then select a particular set of records. One makes use of a lookup table against which a column is checked as in the next command SELECT * FROM SALESI WHERE PCODE IN (‘P1’. One could do the same with numerics. Hence the next command is also a valid one: SELECT * FROM SALES WHERE TOTAL*12/100 >=15. However. The situation is slightly different for character strings. And as usual.

What database management software does is to make some sense of the mess by applying simple commands. This makes no difference to the end result as neither of these functions exclude any record. Functions abound in SQL. Some functions like MIN(). Require you to try the functions so that you fully understand what they can do. SELECT ‘Average sale has been’. This gives a quick tally of the number of vouchers that have been entered into the SALES table. SUM(TOTAL) FROM SALES. SUM(TOTAL) FROM SALES GROUP BY SCODE. it is also possible to display reports across tables. Forming groups… With newer records always being entered into a table. a manager would like to know how much of which product was sold. And by joining two or more tables together. They simply operate on the data sets that are presented to them. Every DBMS has some functions that allow the database to be reordered and then displayed. Another function that is used in the same manner as SUM is COUNT. SELECT ‘Average sale has been’. SELECT COUNT(VNO) FROM SALES. needs to learn to use them correctly. it counts the number of occurrences of a particular field in a column.It is not possible to display a column at while using the SUM function. Mixing this with the last function. It is possible to classify or group like rows together. Others like …. however. With the sample tables for instance. MAX() need no explanation at all. The only difference is that in this case. it would be close to impossible to keep reordering the database. SUM(TOTAL)/COUNT(TOTAL) FROM SALES. It is possible to qualify the data sets that get passes on by including a WHERE clause in the command line. These results can be displayed using the following command SELECT SCODE. the following join will display the name of the salesman rather than let one guess the name from the salesman’s code. one can generate more meaningful result sets as in the following manner. . One could display a text string with the function so that the displayed line makes more sense. All the rules for SUM applies to COUNT. SELECT ‘Total sales to date is Rs’. You will have noticed that COUNT was used with TOTAL. SUM(TOTAL)/COUNT(TOTAL) FROM SALES WHERE TOTAL >= 20. One. Or possibly how much revenue each salesman generated over a period. While this will display the amount each salesman made.

which we will come to later. Hence any attempt to say group products and display all the customers would fail (unless one chooses to display the customers for a particular product as a sub group.SELECT SNAME.) SELECT SCODE. The above statement would fail and generate an error message as TOTAL is not qualified in any manner. But this by itself is of no use.SCODE GROUP BY SNAME. or sum up fields. You will want to do something with the groups that you create. . You are not allowed to specify any options that requires SQL to list the entire contents of a table as except as a part of the group row that will be displayed. SALESMAN WHERE SALES. TOTAL FROM SALES GROUP BY SCODE. or find the maximum and minimum values in a certain column for that group or perform some other operation. This is what you can do now. The most important of these is that you have to define the column that you would like to group on.SCODE=SALESMAN. Either you wish to count them. The column name will have to be specified as one of the columns to display and also will be the key on which the grouping takes place. When GROUPing rows together. SUM(TOTAL) FROM SALES. there are a few rules that you have to bear in mind.

Sign up to vote on this title
UsefulNot useful