00-SQL NEEDED FOR DBS201 CONTENT: COLLECTION ADDING A COLLECTION DELETING A COLLECTION TABLE ADDING A TABLE CHANGING the characteristics or properties of the table DELETING A TABLE DATA ADDING DATA TO A TABLE CHANGING DATA IN A TABLE DELETING DATA FROM A TABLE OTHER IMPORTANT TOPICS CONSTRAINTS VIEWS JOINS Note: You can execute all these commands on DB2 on the iSeries (fmly: AS400). If you wanted to you can cut and paste them, but unless you typeor practice writing them out and also try ideas of your own how will you ever learn them for a test. Note: I use PREMTARR, but you should use some other collection name throughout the code. Do a global replace on all these commands. COLLECTIONS Creating a collection CREATE COLLECTION PREMTARR Deleting, Removing or Dropping a collection DROP COLLECTION PREMTARR Recreate the dropped collection Document1 by rt -- 24 March 2016 1 of 9 TABLES Creating a table What do you want to do? CREATE What do you want to create? TABLE What name? CUSTOMER What is the data called (columns), the type of data and the length of the data Finally, what constraints do you want to apply to ensure data integrity CREATE TABLE PREMTARR.CUSTOMER (CUSTOMER_NUMBER CHAR (3) NOT NULL WITH DEFAULT, LAST_NAME CHAR (15) NOT NULL WITH DEFAULT, FIRST_NAME CHAR (16) NOT NULL WITH DEFAULT, STREET CHAR (15) NOT NULL WITH DEFAULT, CITY CHAR (15) NOT NULL WITH DEFAULT, PROVINCE CHAR (2) NOT NULL WITH DEFAULT, POSTAL_CODE CHAR (6) NOT NULL WITH DEFAULT, CREDIT_LIMIT DECIMAL (5, 0) NOT NULL WITH DEFAULT, BALANCE DECIMAL (6 , 2) NOT NULL WITH DEFAULT, SALES_REP_NUMBER CHAR (2) NOT NULL WITH DEFAULT, CONSTRAINT PREMTARR.CUSTOMER_NUMBER_PK PRIMARY KEY (CUSTOMER_NUMBER) ) Simplified Form CREATE TABLE PREMTARR.CUSTOMER (CUSTOMER_NUMBER CHAR (3), LAST_NAME CHAR (15) NOT NULL, FIRST_NAME CHAR (16) NOT NULL, STREET CHAR (15) NOT NULL, CITY CHAR (15) NOT NULL, PROVINCE CHAR (2) NOT NULL, POSTAL_CODE CHAR (6) NOT NULL, CREDIT_LIMIT NUMERIC (5), not all columns have to be NOT NULL BALANCE DECIMAL (6, 2) NOT NULL WITH DEFAULT 0.00, SALES_REP_NUMBER CHAR (2) , say nothing and it allows NULLS CONSTRAINT PREMTARR.CUSTOMER_NUMBER_PK PRIMARY KEY (CUSTOMER_NUMBER) ) Only one of many constraints are shown Document1 by rt -- 24 March 2016 2 of 9 Removing a table Get rid of the table from the collection DROP TABLE PREMTARR.CUSTOMER Simplified DROP TABLE CUSTOMER Recreate the table again. You can do it quickly with the F9 key Create another table SALESREP Note the use of PK means it is NOT NULL and UNIQUE CREATE TABLE PREMTARR.SALESREP (SALES_REP_NUMBER CHAR (2), SALES_REP_NAME CHAR (15), CONSTRAINT PREMTARR.SALES_REP_NUMBER_PK PRIMARY KEY (SALES_REP_NUMBER)) Document1 by rt -- 24 March 2016 3 of 9 Changing a Table Remember this is about changing the structure of the table and the constraints and NOT the data. Now set up the FOREIGN KEY constraint. You can do this when the table is being created, or it can be added after. You have already created a table and deleted (dropped) a table, the only thing else you can do with a table is modify it. This next step modifies a table. Adding a constraint First say what you want to do alter a table Next say what altering are you going to do ADD or DROP constraint or column Here we are adding a foreign key constraint ALTER TABLE PREMTARR.CUSTOMER ADD CONSTRAINT PREMTARR.CUST_SALES_REP_FK FOREIGN KEY (SALES_REP_NUMBER) REFERENCES PREMTARR.SALESREP (SALES_REP_NUMBER) ON DELETE NO ACTION you can ignore these 2 lines ON UPDATE NO ACTION Adding a column ALTER TABLE PREMTARR.CUSTOMER ADD COLUMN COMM_RATE DECIMAL (3,2) When you add a column you need the type and length of the data to be stored just as in a create table Removing a column ALTER TABLE PREMTARR.CUSTOMER DROP COLUMN COMM_RATE You lose all the data in the column as well. Adding, changing and dropping tables is not something the company wants to do a lot of with its day-to-day data. That is why it is important to design it properly. However sometimes changes need to be made. Also, you will be working in the developing side of the business and you will need to add, change and delete tables all the time. It is good idea to do a SELECT * to look at what the table contains and what columns exist now. Insert data into table, but use your own name. INSERT INTO PREMTARR.CUSTOMER VALUES ('100', 'LAST', 'FIRST','100 STREET','TORONTO', 'ON', 'M9W1A4', 5000, 1234.56, '6') Insert these two rows into SALESREP INSERT INTO PREMTARR.SALESREP VALUES ('6', 'RON TARR') INSERT INTO PREMTARR.SALESREP VALUES ('99','RON TARR99') Document1 by rt -- 24 March 2016 4 of 9 DATA Adding data to a table Insert data into table, but use your own name. INSERT INTO PREMTARR.CUSTOMER VALUES ('100', 'LAST', 'FIRST','100 STREET','TORONTO', 'ON', 'M9W1A4', 5000, 1234.56, '6') Notice 1. That the first value is in single quotes. This is because the first column was defined as character data. Just like any other language character data is in quotes. For SQL data is in single quotes. Only alternate names for columns in the select clause has double quotes. 2. The values must be in the same order and data type as the table. 3. If the column allows NULL, then NULL must be used as in the example below. INSERT INTO PREMTARR.CUSTOMER VALUES ('100', 'LAST', 'FIRST', NULL,'TORONTO', NULL, 'M9W1A4', 5000, 1234.56, '6') NULL is not a character value and does not need quotes. This example will not work for 2 reasons 1) A primary key was defined and we already used ‘100’ 2) Last name and province were defined as not allowing NULL values. Insert these two rows into SALESREP INSERT INTO PREMTARR.SALESREP VALUES ('6', 'RON TARR') INSERT INTO PREMTARR.SALESREP VALUES ('99','RON TARR99') Deleting data from the table DELETE FROM PREMTARR.CUSTOMER Since there was no condition all the rows were deleted Insert the row back into CUSTOMER Insert it again. Note the error message. That is a check being done by the DBMS that you don't have to program to ensure the integrity of the data. Insert another row, so that you have 2 rows in the table CUSTOMER Delete only one of the 2 rows by adding a condition. DELETE FROM PREMTARR.CUSTOMER WHERE CUSTOMER_NUMBER = '100' Changing data in a table UPDATE PREMTARR.CUSTOMER SET STREET = 'UPDATED100' WHERE CUSTOMER_NUMBER ='100' If there is not a WHERE condition then all rows will be updated to the same address. Document1 by rt -- 24 March 2016 5 of 9 Working with data Now insert the following using a new PK and FK value INSERT INTO PREMTARR.CUSTOMER VALUES ('300', '300LAST', '300FIRST','300 STREET','TORONTO', 'ON', 'M9W1A4', 3000, 1234.56, '1') An error occurs as follows: Operation not allowed by referential constraint CUST_SALES_REP_FK in -- etc This is another check done by the DBMS Now add a column to the SALESREP table called COMM_RATE using this code ALTER TABLE PREMTARR.CUSTOMER ADD COLUMN COMM_RATE DECIMAL (3,2) Oops -- it was added to the wrong table. Leave it there and we will fix it later. Now add it to the correct table ALTER TABLE PREMTARR.SALESREP ADD COLUMN COMM_RATE DECIMAL (3,2) Now apply a constraint on the new field to allow only specified values. The constraint is called CHECK ALTER TABLE PREMTARR.SALESREP ADD CONSTRAINT PREMTARR.COMM_RATE CHECK (COMM_RATE IN (0.05, 0.06, 0.07) ) Add a UNIQUE constraint ALTER TABLE PREMTARR.SALESREP ADD CONSTRAINT PREMTARR.SALES_REP_NAME UNIQUE (SALES_REP_NUMBER) Test the constraints. Do a select so that you know what data you have already and insert more testing the CHECK and UNIQUE constraints. INSERT INTO PREMTARR.SALESREP VALUES ('7', '7RON TARR',.7) Did it work? INSERT INTO PREMTARR.SALESREP VALUES ('98','RON TARR99') Did it work and why if not why? INSERT INTO PREMTARR.SALESREP VALUES ('98','RON TARR99', .06) Did this correct the problem above? INSERT INTO PREMTARR.SALESREP VALUES ('98','RON TARR98', .06) Finally ---- You have tested the keys You have modified the table by adding columns and constraints, now remove the wrong column added to the customer table ALTER TABLE PREMTARR.CUSTOMER DROP COLUMN COMM_RATE Document1 by rt -- 24 March 2016 6 of 9 Drop a constraint. I have chosen the FK on CUSTOMER ALTER TABLE PREMTARR.CUSTOMER alter the table DROP FOREIGN KEY what action PREMTARR.CUST_SALES_REP_FK apply to what or name the constraint to drop Up to this point in time we have done reports from 1 table only. Suppose you wanted to produce an invoice. You would need data from CUSTOMER, INVOICE, INVOICELINE, PRODUCT and maybe several other tables, depending upon the complexity of the system (Tax, sales_reps, shipping data etc) For this example you will need to cut and paste the following commands to create a table CREATE TABLE PREMTARR.PRODUCT (PID NUMERIC (2), PDESC CHAR (15) NOT NULL, QOH NUMERIC (4) ) CREATE TABLE PREMTARR.INVOICE (INVID NUMERIC (4), CID CHAR (3) ) CREATE TABLE PREMTARR.INVOICELINE (INVID NUMERIC (4), PID NUMERIC (2), QOO NUMERIC (2) ) DATA - adding new data to a table uses the INSERT statement Now insert some data INSERT INTO PREMTARR.CUSTOMER VALUES ('100', 'LAST', 'FIRST','100 STREET','TORONTO', 'ON', 'M9W1A4', 5000, 1234.56, '6') INSERT INTO PREMTARR.INVOICE VALUES (1111, '100') INSERT INTO PREMTARR.INVOICE VALUES (2222, '100') INSERT INTO PREMTARR.PRODUCT VALUES (11, 'PRODUCT 11', 1011) INSERT INTO PREMTARR.PRODUCT VALUES (12, 'PRODUCT 12', 1212) INSERT INTO PREMTARR.PRODUCT VALUES (13, 'PRODUCT 13', 25) INSERT INTO PREMTARR.INVOICELINE VALUES (1111, 13, 2) INSERT INTO PREMTARR.INVOICELINE VALUES (1111, 12, 12) INSERT INTO PREMTARR.INVOICELINE VALUES (2222, 13, 12) To retrieve data for an invoice you need information from many tables. If you ran this it would not look like an invoice, because the data has not been re-arranged on an invoice form. However all the required data will be selected and displayed. Document1 by rt -- 24 March 2016 7 of 9 SELECT PREMTARR.INVOICE.INVID, PREMTARR.CUSTOMER.CUSTOMER_NUMBER, PREMTARR.CUSTOMER.LAST_NAME, PREMTARR.PRODUCT.PID, PREMTARR.PRODUCT.PDESC, PREMTARR.INVOICELINE.QOO, PREMTARR.SALESREP.SALES_REP_NAME FROM PREMTARR.CUSTOMER, PREMTARR.INVOICE,PREMTARR.INVOICELINE, PREMTARR.PRODUCT,PREMTARR.SALESREP WHERE PREMTARR.CUSTOMER.CUSTOMER_NUMBER = PREMTARR.INVOICE.CID AND PREMTARR.CUSTOMER.SALES_REP_NUMBER = PREMTARR.SALESREP.SALES_REP_NUMBER AND PREMTARR.INVOICE.INVID = PREMTARR.INVOICELINE.INVID AND PREMTARR.INVOICELINE.PID = PREMTARR.PRODUCT.PID ORDER BY PREMTARR.INVOICE.INVID What does the SQL look like if I didn’t have to qualify every name with the collection as required by AS400? SELECT I.INVID, we qualify with the table tame when a column is in 2 tables to avoid ambiguity CUSTOMER_NUMBER, no ambiguity here LAST_NAME, P.PID, P.PDESC, QOO, S.SALES_REP_NAME FROM CUSTOMER C, the C is an alias name, so that the full name doesn’t need to be used INVOICE I, INVOICELINE IL, PRODUCT P, SALESREP S WHERE C.CUSTOMER_NUMBER = I.CID AND C.SALES_REP_NUMBER = S.SALES_REP_NUMBER AND I.INVID = IL.INVOICELINE.INVID AND IL.PID = P.PID ORDER BY I.INVID VIEWS Creating a view. A view is LIKE a table. It has a structure, but unlike a table it has no data. The data that a view appears to have is loaded at run time. Views are used to 1 - provide a form of security in that only part or parts of tables will be shown and the user getes permission to access those parts and not permission on the tables. 2 - It can simplify so that users that have some SQL ability can do reports. For example a complicated join and other where conditions could be used in a view, but the user then needs only to use simple SQL Document1 by rt -- 24 March 2016 8 of 9 Examples follow. Creating the VIEW that will be called CR and gets the data from 2 tables CREATE VIEW PREMTARR.CR <== name the view (CUSTOMER_NUMBER, LAST_NAME, CREDIT_LIMIT,SALES_REP_NAME)<== Note 1 below AS SELECT PREMTARR.CUSTOMER.CUSTOMER_NUMBER, <== the columns to show PREMTARR.CUSTOMER.LAST_NAME, PREMTARR.CUSTOMER.CREDIT_LIMIT, PREMTARR.SALESREP.SALES_REP_NAME FROM PREMTARR.CUSTOMER, <== the tables where data exists PREMTARR.SALESREP WHERE PREMTARR.CUSTOMER.SALES_REP_NUMBER = PREMTARR.SALESREP.SALES_REP_NUMBER <== the join condition Note 1. On the AS400 it tells you what the column names in the CREATE VIEW will be. It took the names from the underlying tables. If you wanted you could have given the columns a different name such as CID instead of CUSTOMER_NUMBER. You would have specified them on that line. Query using the view is like any other table. You can see it has 4 columns so you can SELECT as though it was a table. At the time the SELECT is run the underlying data is retrieved from the original tables. Try this. SELECT * FROM PREMTARR.CR SELECT FROM LAST_NAME PREMTARR.CR SELECT FROM WHERE LAST_NAME PREMTARR.CR CUSTOMER_NUMBER > 'LZ' Notice how easy it was to get information. No join was needed. Notice that if a user is given permission to use CR then, they won't have access to other information about the customer that is not vital to their job. For example the clerk in payroll may get to see all employees data, except the executive salaries. Therefore the view will be created with a WHERE statement that excludes executive employees. A manager in Computer Studies may be able to see students and faculty in their department and not in any other department. This is the security aspect. Document1 by rt -- 24 March 2016 9 of 9