Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQLPlus Commands. Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –http://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htmhttp://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htm.

Similar presentations


Presentation on theme: "SQLPlus Commands. Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –http://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htmhttp://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htm."— Presentation transcript:

1 SQLPlus Commands

2 Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –http://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htmhttp://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htm Basic Unix commands: –http://www.sfsu.edu/~helpdesk/docs/using/unixcmd.htmhttp://www.sfsu.edu/~helpdesk/docs/using/unixcmd.htm –PICO text editor

3 Commands at Oracle SQL> To list all commands: –SQL>help index To list all tables: –Select * from Cat; To display a table structure: –Describe tableName To run an Unix command from Oracle: –Host unixCommand Use Column command to change output format: –SQL>column empid format a5; –SQL>column salary format $9,999.99; –SQL>column empid heading EmpID; Commands related to SQL buffer: –List, Run, Clear, Del, Change

4 Working with SQL Buffer SQL Buffer remembers the last SQL command executed. List: list the SQL statement with line number correct the line with error: line number correct statement Run

5 Creating SQL Command Batch File Creating a batch file with.sql extension –Use Unix text editor (pico) to create the batch file, or –At SQL>Edit filename Ex: SQL>edit batch1.sql Use START command to execute the batch file. –SQL>START batch1.sql See the batch file contents at SQL> –SQL>host cat batch1.sql

6 Batch File Example Drop table hotel; Create table hotel (hotelno char(3), hotelname varchar(2 Insert into hotel values('h1', 'Grosvenor','CHICAGO'); Insert into hotel values('h2', 'Westin','SAN FRANCISCO') Insert into hotel values('h3', 'Hilton','London'); Drop table room; Create table room(roomno char(3),hotelno char(3),type char(6), price number(7,2)); Insert into room values('r1','h1','single', 200.00); Insert into room values('r2','h1','single', 300.00); Insert into room values('r3','h1','double', 440.00); Insert into room values('r1','h2','single', 200.00); Insert into room values('r2','h2','single', 300.00); Insert into room values('r3','h2','double', 400.00); Insert into room values('r1','h3','single', 200.00); Insert into room values('r2','h3','single', 250.00); Insert into room values('r3','h3','double', 300.00);

7 To display complete field name: –Ex. –column sex format a3 –Select * from student; To print output: –In computer lab: Spool anyFileName Run sql statements Spool off –Screen capture –Telnet window: Click the computer icon on the upper left corner Edit/Mark Edit/Copy Enter a date value: assume emp table has: eid, ename, hiredate and salary field: –Insert into emp values(‘e1’, ‘peter’,’10-Feb-04’,5000.00);

8 SQL Select

9 Selection SELECT * FROM tableName WHERE criteria; Criteria: –=, <>,, = –(), NOT, AND, OR –BETWEEN WHERE salary BETWEEN 1000 AND 10000; –LIKE, NOT LIKE: % WHERE ename LIKE ‘C%’ –Cannot say: WHERE ename = ‘C%’ –IN, NOT IN WHERE eid IN (‘e1’,’e3’) –IS NULL WHERE rating IS NULL –Arithmetic expression in criteria: WHERE salary*0.1 < 500

10 ROWNUM Field ROWNUM field is a pseudocolumn that applies to every table even though it is not displayed through a SELECT * command. SELECT ROWNUM, empid FROM emp; SELECT * FROM emp WHERE ROWNUM < = 3;

11 Projection SELECT fields FROM tableName WHERE criteria; SELECT DISTINCT fields FROM tableName WHERE criteria; Field name alias: AS Ex: SELECT eid AS empID, ename AS empName FROM emp; Calculated fields: –SELECT empid,ename,salary*0.1 AS tax FROM emp; Note: UNIQUE –SELECT UNIQUET fields FROM tableName WHERE criteria;

12 Oracle Functions Text functions: –UPPER, LOWER, INITCAP –SUBSTR(text,starting position, # of chars) –LPAD(text,length of text,padding symbol) LPAD(salary,10,’*”) –CONCAT(text1,text2) We can also use concatenation operator, || Date functions: –SYSDATE,NEXT_DAY Ex. NEXT_DAY(SYSDATE,’MONDAY’) –MONTHS_BETWEEN(date1,date2) –ADD_MONTHS(date,# of months added)

13 Product SELECT fields FROM table1, table2; table name alias: Ex: SELECT s.*, f.* FROM student s, faculty f; Take the product of a table itself. –SELECT * FROM emp e1, emp e2;

14 CROSS JOIN = Product select * from student cross join course;

15 Natural Join SELECT * FROM table1 NATURAL JOIN table2; –SELECT * FROM student NATURAL JOIN faculty; SELECT * FROM table1, table2 WHERE table1.JoinAttribute = table2.JoinAttribute; –SELECT * FROM student, faculty –WHERE student.fid = faculty.fid; Table name alias: –SELECT * FROM student s, faculty f WHERE s.fid = f.fid; Other forms: –FROM student s JOIN faculty f ON s.fid=f.fid; –FROM student s INNER JOIN faculty f ON s.fid=f.fid; –FROM student JOIN faculty USING fid; Not supported by Oracle Note: What if the FID in the student table is named AdvisorID?

16 Outer Join SELECT s.*, f.* FROM –student s FULL JOIN faculty f ON s.fid = f.fid; SELECT s.*, f.* FROM –student s RIGHT JOIN faculty f ON s.fid = f.fid; SELECT s.*, f.* FROM –student s LEFT JOIN faculty f ON s.fid = f.fid; Demo: Access’ SQL view.

17 Theta Join SELECT * FROM table1, table2 WHERE criteria; Example: match male students with female students who have higher GPA: –select m.sid, m.sname,f.sid,f.sname –from malestudent m, female f –where m.gpa < f.gpa;

18 Theta Join Example: match male employees with female employees who have higher salary: –SELECT e1.empid,e1.ename,e2.empid,e2.ename –FROM emp e1, emp e2 –WHERE e1.sex=‘M’ and e2.sex=‘F’ –AND e1.salary < e2.salary;

19 Sorting ORDER BY fieldName [DESC] –SELECT * FROM student ORDER BY sname; –SELECT * FROM student ORDER BY sname DESC; More than one field: –SELECT * FROM student ORDER BY major, sname; Note 1: Don’t name a table “ORDER” because ORDER is a SQL keyword. Note 2: If there is a WHERE clause, then the ORDER BY clause should be placed after the WHERE clause.

20 Set Operators Union compatible Union: –(SELECT * FROM table1) UNION (SELECT * FROM table2); Intersect: (SELECT * FROM table1) INTERSECT (SELECT * FROM table2); Minus: (SELECT * FROM table1) MINUS (SELECT * FROM table2);

21 Find students taking 263 and acct 101 select sid from registration where cid='ISYS263' intersect select sid from registration where cid='acct101‘; Note: How to get student name?

22 Aggregates SELECT AVG(fieldName) FROM tableName; –COUNT(fieldName), COUNT(*) –COUNT(DISTINCT fieldName) –MAX(fieldName) –MIN(fieldName) –SUM(fieldName) More than one aggregate: SELECT AVG(fieldName), MAX(fieldName), MIN(fieldName) FROM tableName; With alias: –SELECT AVG(gpa) AS avggpa, COUNT(sid) AS scount FROM student;

23 GROUP BY SELECT groupingFields, function(fieldname) FROM tablename GROUP BY groupingFields; –SELECT major, count(sid) FROM student GROUP BY major; –SELECT sex, major, count(sid) FROM student GROUP BY sex, major; Compute more than one subtotals: –SELECT major, count(sid), avg(gpa) FROM student –GROUP BY major Compute subtotals from a join Compute the number of courses taken by each student: –SELECT sid, sname, COUNT(cid) FROM student NATURAL JOIN registration GROUP BY sid, sname; Note 1: All grouping fields in the SELECT clause must be included in the GROUP BY clause). Note 2:WHERE clause must come before the GROUP BY: –SELECT major, count(sid) FROM student WHERE GPA > 3.0 GROUP BY major;

24 Adding a Criteria for the Sub Totals with HAVING SELECT major, count(sid) FROM student –GROUP BY major –HAVING count(sid) > 5; Sometime the aggregates are not required to display: –Find majors that have more than 5 students: –SELECT major FROM student GROUP BY major HAVING count(sid) > 5;

25 Sub (or Nested ) Query Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student WHERE gpa < (SELECT AVG(gpa) FROM student); Q: Find employees with higher than average salary.

26 Sub Query with IN Q: Display faculty’s ID and name if the faculty advises at least one student. –SELECT fid, fname FROM faculty –WHERE fid IN (SELECT DISTINCT fid FROM student); Q: Display faculty’s name and phone if the faculty does not advise any student. –SELECT fid, fname FROM faculty –WHERE fid NOT IN (SELECT DISTINCT fid FROM student);

27 Sub Query with IN Q: Find students who take at least one course and display their ID and name. –SELECT sid, sname FROM student NATURAL JOIN enroll GROUP BY sid HAVING COUNT(cid) > 1; –SELECT sid, sname FROM student WHERE sid IN (SELECT DISTINCT sid FROM enroll); Q: Find students who take more than 5 courses and display their ID and name. –SELECT sid, sname FROM student WHERE sid IN (SELECT sid FROM enroll GROUP BY sid –HAVING COUNT(cid) > 5);

28 Sub Query with Join Display students’ ID and name who owe university more than $2000. select sid,sname from student natural join (select * from account where balance > 2000);

29 Sub Query with ALL/SOME/ANY Q: Find students whose gpa is greater than all/some bus majors’ gpa: –SELECT sid, sname FROM student WHERE gpa > ALL(SELECT gpa FROM student WHERE major=‘bus’); –SELECT sid, sname FROM student WHERE gpa > SOME (SELECT gpa FROM student WHERE major=‘bus’); –SELECT sid, sname FROM student WHERE gpa > ANY (SELECT gpa FROM student WHERE major=‘bus’);

30 Comparing the Two SQL Statements Compute the number of courses taken by each student: (1) SELECT sid, sname, courses FROM student NATURAL JOIN (SELECT sid, COUNT(cid) AS courses FROM registration GROUP BY sid); (2) SELECT sid, sname, COUNT(cid) AS courses FROM student NATURAL JOIN registration GROUP BY sid);

31 Examples University database: –Student: SID, Sname, Sex, Major, GPA, FID –Account: SID, Balance –Faculty: FID, Fname –Course: CID, Cname, Credits –StudentCourse: SID, CID

32 Questions Q1: Display College of Business students’ ID and name. Q2: Display students’ ID and name who owe university more than $2000. Q3: Display faculty’s name and phone if the student’s GPA is lower than 2.0. Q4: Display faculty’s name and phone if the faculty advises at least one student. Q5: Display faculty’s name and phone if the faculty does not advise any student. Q6: Display students’ ID and name who are taking at least one course. Q7: Display students’ ID and name who do not take any course. Q8: Display students’ ID and name who are taking 464 and GPA < 2.0 Q9: Display students’ ID and name who are taking Chao’s courses. Q10: Display students’ ID and name who are taking 464 and 363.


Download ppt "SQLPlus Commands. Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –http://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htmhttp://www.sfsu.edu/~helpdesk/docs/using/usingoracle.htm."

Similar presentations


Ads by Google