530 likes | 642 Views
Oracle Structured Query Language Examples. Lloyd D. Brooks, Professor Management Information Systems MIS 3775. Basic SQL Commands. SELECT FROM WHERE Appendix. SQL Example. SELECT First, Last FROM Student WHERE Num = ’23’; Num Last First Phone
E N D
Oracle Structured Query Language Examples Lloyd D. Brooks, Professor Management Information Systems MIS 3775
Basic SQL Commands • SELECT • FROM • WHERE Appendix
SQL Example • SELECT First, Last • FROM Student • WHERE Num = ’23’; • Num Last First Phone • 18 Doe John 2382 • 23 Brown Betty 4722 • 27 Carlson Daniel 4920 Appendix
Creating a table • Create Table Inventory • (PackID Varchar2(4), • PackName Varchar2(20), • PackVer Number(3,2), • PackType Varchar2(15), • PackCost Number(5,2)); • Note: Mark there is no comma after the last field/Column description Appendix
Text Data Defined • Note: Oracle defines text data as follows: • Packid Varchar2(4) Appendix
Decimal Data Defined • Note: Oracle defines decimal data as follows: • Salary Number(8,2) Appendix
Null Constraints • Note: Null constraints can be entered as follows: • Quantity INTEGER NOT NULL • Order_Date DATE NOT NULL Appendix
Primary Key Definition Note: The primary key can be defined at table definition end as follows: • CONSTRAINT Inventory_PK PRIMARY KEY (PackID) • Notes: Inventory_PK is a arbitrary name assigned to the constraint. PackID is the name of the primary key field in the table. • Primary key can also be created as: • PackID varchar2[10] primary key Appendix
Boolean Operators and Algebra • AND OR NOT • * Multiply • / Divide • + Add • - Subtract Appendix
SQL Comparison Operators • = Equal to • > Greater than • >= Greater than or equal to • < Less than • <= Less than or equal to • <> Not equal to • != Not equal to Appendix
Data Type Samples in SQL • Order_Date DATE • Last_Name VARCHAR2(18) • Quantity INTEGER • Amount Number(7,2) Appendix
Data Query – No Restrictions • SELECT PackID, PackName, PackCost • FROM Inventory; Appendix
Data Query – Wild Card • SELECT * • FROM Inventory; Appendix
Data Query – Value Condition • SELECT EMP_Name • FROM Employee • WHERE Salary > 50000; Appendix
Data Query - Not Equal • SELECT Compid, Mfgname • FROM Computer • WHERE Mfgname <> ‘Bantam’; • Note: Alternative for the criteria command: • WHERE Mfgname != ‘Bantam’ • Note : SQL data are case sensitive. Appendix
Data Query – Text Condition • SELECT PackID, PackName • FROM Inventory • WHERE PackType = ‘Database’; Appendix
Data Query – Compound Condition • SELECT PackName • FROM Inventory • WHERE PackType = ‘Database’ • and PackCost > 400; Appendix
Data Query – NOT Command • SELECT PackName • FROM Package • WHERE NOT (PackType = ‘Database’); Appendix
Sorting Records – Single Field • SELECT * • FROM Employee • ORDER BY Last_Name; • Note: The default order is ascending. Add DESC after the attribute to change the order to descending. Appendix
Sorting Records – Compound Fields • SELECT * • FROM Employee ORDER BY Last_Name, First_Name; Note: Indicate the major sort field first and then the minor sort field next with a comma between the fields. Appendix
Built-In Functions - Count • SELECT COUNT(PackID) • FROM Inventory; • Note: Oracle has numerous built-in functions for a variety of statistical, financial, and other applications. Appendix
Count Function With a Condition • SELECT COUNT(Tagnum) • FROM PC • WHERE Location = ‘Home’; • Note: COUNT is a function. Do not space after the function name. Appendix
Count and Sum Functions Combined • SELECT COUNT(PackID), Sum(PackCost) • FROM Package; • Note: Count is a function that determines how many. Sum is a function that determines how much. Appendix
Average Function • SELECT AVG(Packcost) • FROM Package • WHERE Packtype = ‘Database’; • Note: AVG will initiate the function to compute the average for the values for the field indicated in parenthesis. Appendix
Grouping by a Control Field • SELECT TagNum, Sum(SoftCost) • FROM Software • GROUP BY TagNum • ORDER BY TagNum; Appendix
Grouping With a Condition • SELECT TagNum, Sum(SoftCost) • FROM Software • GROUP BY TagNum • HAVING Sum(SoftCost) > 600 • ORDER BY TagNum; • Note: The Having command takes the place of the Where command for groups. Appendix
Joining Tables • SELECT Software.Packid, Package.Packname, Package.Packver, Software.Tagnum, Software.Instdate, Software.Softcost • FROM Software, Package • WHERE Software.Packid = Package.Packid; • Note: The WHERE command determines if a value exists in both tables and is used when tables are being joined. Appendix
Alias in Oracle • SELECT pt_fname “First Name”, pt_lname “Last Name”, ptdob “BirthDate” • FROM Patient; • Note: Alias must include double quotes • Note: Omission of quotes will result in an all uppercase heading • Note: Quotes required, regardless, for headings requiring a space Appendix
Null Value Criteria in Oracle • SELECT Doc_ID FROM Doctor WHERE Annual_Bonus IS NULL; • Note: Use “IS NULL” and not “=NULL” in the criteria expression. Appendix
Like Command • SELECT * FROM Billing where Addr LIKE ‘%N. Allen%’; • Note: LIKE should be used for criteria comparisons whenever a wild card is used within the criteria. Appendix
Table Name Designation in SQL • Note: When the same attribute is included in two or more tables, it is necessary to indicate both the table name and the attribute name • WHERE Adviser.AdvID = Student.AdvID • Note: The period separates the table and attribute names. Note that AdvID appears in both tables in this example. Appendix
Updating Data in a Table • UPDATE Inventory • SET PackName = ‘Manta’ • WHERE PackID = ‘DB33’; • Note: Revise name for ID DB33 Package Appendix
Adding a Record to a Table • INSERT INTO Employee • VALUES (‘402’, ‘Sanders, Robert’, ‘2056’); • Note: The order of values must match the table structure. If only data for certain fields are to be entered, list the fields in parenthesis between the table name and the word VALUES. Appendix
Updating Selected Records • UPDATE Package • SET Packcost = Packcost + 10 • WHERE Packtype = ‘Word Processing’; • Note: Records that do not meet the criteria will not be included in the update process. Appendix
SQL Wildcard Examples in Oracle • % Takes the place of all characters before or after it. • ‘%ville’ All cities ending in ville • ‘C%’ All last names beginning with C • _ (underline) takes the place of 1 character • ‘T_M’ Will match any 3-character values beginning with a “T” and ending with a” M” Appendix
Date Comparisons and Like Values • WHERE Order_Date > ’24-OCT-99’ • Note: This will be a criteria match for order dates that occurred later than 10/24/99. Notice that the date has an apostrophe before and after it. • WHERE Last_Name LIKE ‘%son’ Appendix
Deleting a Record • DELETE FROM Employee • WHERE EmpPhone = ‘8716’; • Note: Deletes record(s) matching the • condition. Values must be • unique or multiple records will • be deleted. Appendix
Column Deletion • ALTER TABLE Employee • DROP Address; Appendix
Column Width Revision • ALTER TABLE Employee • CHANGE Column Last_Name • TO VarChar2(22); Appendix
Column Addition • ALTER TABLE Employee • ADD Address Varchar2(20); Appendix
Table Deletion • DROP TABLE Employee; Appendix
View Creation • CREATE VIEW Brown • AS SELECT EmpID, EmpName • FROM Employee; • Note: This view will permit the user to view data from the EmpID and EmpName columns in the Employee table. Appendix
View Deletion • DROP VIEW Brown; Appendix
Updating All Records – No Condition • UPDATE Employee • SET Salary = Salary * 1.05; Appendix
Averaging With a Condition • SELECT AVG(Salary) • FROM Employee • WHERE Dept = ‘Accounting’; Appendix
Averaging With Grouping • SELECT DeptName, Avg(Salary) • FROM Employee • GROUP BY DeptName • ORDER BY DeptName; Appendix
Major SQL System Commands • Saving a script • Save a:\filename.ext • Saving a session to a file • Spool a:\filename.txt • Running a script stored as a .txt file • @a:\filename.txt • Exit from session • Exit • Access program text editor • Edit Appendix
Null Values in a Field • UPDATE PC • SET EmpNum to Null • WHERE TagNum = ‘37691’; Appendix
Commands – Oracle • DESCRIBE Student; • Note: Provides a listing of fields and attribute types for the Student table. • RUN • Note: Executes a program. • SELECT * FROM TABS; • Note: Gets a listing of tables in the database • SAVE C:\Filename.ext • Note: Saves a program Appendix
Comments - Oracle • - - • Note: Place at the beginning of the comment line for one-line comments • /* */ • Note: Place multi-line comment between these notations. Appendix