Download presentation
Presentation is loading. Please wait.
Published byVernon Stanley Modified over 8 years ago
1
Lecture 2 21/1/16
2
Putting the output in order Oracle can sort a column, or multiple columns select emp_no, emp_name from employee order by emp_name; emp_no emp_name 4BYRNE 8HARTE 3HEARNE 6WALSH select * from customer order by name; Name phone address CHARLES KERRY 87676456 CIARA CORK 4567890 MARY CORK 67834534 2
3
Where Select emp_name, sal, dept_no from employee Where job= 'ANALYST'; emp_name sal dept_no HEARNE8004 Select * from customer where name='CIARA'; NAME ADDRESS PHONE CIARA CORK 4567890 3
4
More… Select emp_name, sal from employee where job='CLERK'; emp_name sal BYRNE100 4
5
Operator Precedence select emp_name, sal, 12*(sal+100) from employee; 5
6
Null Values NULL values represent unavailable, unassigned, unknown or inapplicable data A NULL is not the same as a zero or a blank space. 6
7
Null Values Name Null Type -------- -------- ------------ EMP_NO NOT NULL NUMBER(4) EMP_NAME VARCHAR2(20) JOB VARCHAR2(20) DEPT_NO NUMBER(2) SAL VARCHAR2(20) COMM VARCHAR2(20) 7
8
Note VARCHAR2 is a data type used to store variable-length character data. A varchar2 value can contain up to 4000 bytes of data (limit in Oracle 7 was 2000 bytes). The NUMBER datatype stores fixed and floating-point numbers. Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision. http://docs.oracle.com/cd/B28359_01/server.111/b28318/dat atype.htm#CNCPT313
9
Null Values 3HEARNEANALYST4800100 4BYRNECLERK3100 6WALSHMANAGER4350 8HARTEACCOUNTANT3700300 9DOHERTYCLERK3150 10MARTINMANAGER3900190 9
10
Null Values select COMM from employee where emp_name='DOHERTY'; 10 COMM – commission awarded to employees
11
Concatenation Operator Concatenation means to join, or link together In Oracle we use two bars|| This joins two, or more, columns together select EMP_NAME || ' IS A '|| JOB from EMPLOYEE; 11
12
12
13
select NAME || ' CONTACT DETAILS '|| PHONE from CUSTOMER; 13
14
Literal Character Strings Character, number or date that is included in a SELECT Date and character literals must be enclosed by single quotes Each string is output once for each row Example select EMP_NAME || ' IS A '|| JOB from EMPLOYEE; 14
15
Note A string is a sequence of bytes or characters, enclosed within either single quote (“'”) or double quote (“"”) characters. Examples: 'a string' "another string“ https://docs.oracle.com/cd/E17952_01/refman-5.5-en/string- literals.html
16
Comparison Operators OperatorMeaning = (Equals)Equal to > (Greater Than)Greater than < (Less Than)Less than >= (Greater Than or Equal To)Greater than or equal to <= (Less Than or Equal To)Less than or equal to <> (Not Equal To)Not equal to != (Not Equal To)Not equal to (not ISO standard) !< (Not Less Than)Not less than (not ISO standard) !> (Not Greater Than)Not greater than (not ISO standard) 16
17
Comparison Operators… select * from customer where name ='CIARA'; select emp_name, sal from employee where sal >300 order by emp_name; select emp_name, sal from employee where sal <300 order by emp_name; select * from employee where dept_no >=4 order by emp_name; 17
18
What if? select * from customer where name ='ciara';
19
Comparison Operators select * from employee where dept_no <=4 order by emp_name; 19
20
Comparison Operators select * from employee where dept_no <>4; 20
21
More Examples select * from emp where deptno <>20; 7934MILLERCLERK778223-JAN-821300 10 7900JAMESCLERK769803-DEC-81950 30 7844TURNERSALESMAN769808-SEP-8115000 30 7839KINGPRESIDENT17-NOV-815000 10 7782CLARKMANAGER783909-JUN-812450 10 7698BLAKEMANAGER783901-MAY-812850 30 7654MARTINSALESMAN769828-SEP-8112501400 30 7521WARDSALESMAN769822-FEB-811250500 30 7499ALLENSALESMAN769820-FEB-811600300 30
22
More examples select * from emp where deptno <>20 and deptno <>10; 7900JAMESCLERK769803-DEC-8195030 7844TURNERSALESMAN769808-SEP-811500030 7698BLAKEMANAGER783901-MAY-81285030 7654MARTINSALESMAN769828-SEP-811250140030 7521WARDSALESMAN769822-FEB-81125050030 7499ALLENSALESMAN769820-FEB-81160030030
23
Displaying Table Structure DESC Displays the structure of the table including column names and data types 23
24
Datatypes The VARCHAR2 datatype stores variable-length character strings The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings. The NUMBER datatype stores fixed and floating-point numbers. Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision The following numbers can be stored in a NUMBER column: Positive numbers in the range 1 x 10 -130 to 9.99...9 x 10 125 with up to 38 significant digits Negative numbers from -1 x 10 -130 to 9.99...99 x 10 125 with up to 38 significant digits Zero Positive and negative infinity 24
25
Datatypes – Number continued… For numeric columns, you can specify the column as: column_name NUMBER Optionally, you can also specify a precision (total number of digits) and scale (number of digits to the right of the decimal point): column_name NUMBER (precision, scale) 25
26
Datatypes The DATE datatype stores point-in-time values (dates and times) in a table. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight) Oracle Database uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second. For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows: '13-NOV-92' 26
27
Datatypes The VARCHAR2 datatype stores variable-length character strings The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings. The NUMBER datatype stores fixed and floating-point numbers. Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision The following numbers can be stored in a NUMBER column: Positive numbers in the range 1 x 10 -130 to 9.99...9 x 10 125 with up to 38 significant digits Negative numbers from -1 x 10 -130 to 9.99...99 x 10 125 with up to 38 significant digits Zero Positive and negative infinity 27
28
Datatypes The DATE datatype stores point-in-time values (dates and times) in a table. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight) Oracle Database uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second. For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows: '13-NOV-92' 28
29
Commands can be divided into four categories Query. Data Manipulation Language (DML) commands Data Definition Language (DDL) commands Security 29
30
Entering SQL commands SQL has a buffer which stores the current command SQL commands can span lines Although not obligatory, clauses should be put on separate lines to improve readablity Tabs should be used to make a command more readable SQL commands are not case sensitive 30
31
Like % Select ename, job from emp where job like ‘C%'; ENAMEJOB MILLERCLERK JAMESCLERK ADAMSCLERK SMITHCLERK Select ename, job from emp where job like ‘c%'; Result??? Another example: select * from emp where job like '%NALYST'; 3HEARNEANALYST4800 31
32
More Examples Select ename, job from emp where job like 'CL%'; Select ename, job from emp where job like 'CL%RK'; Select ename, job from emp where job like 'CL%%'; Select ename, job from emp where job like 'CL%%'; Select ename, job from emp where job like '%%K';
34
Error? Select * from customer Select name address from customer; Select name, address from customer where name =mary; Select name CUS NAME from customer; 34
35
SQL Questions cust_codecust_namecust_city working_a rea cust_count ry grade opening_a mt receive_a mt payment_ amt outstandin g_amt phone_no agent_cod e C00013HolmesLondon UK26000.005000.007000.004000.00BBBBBBBA003 C00001MichealNew York USA23000.005000.002000.006000.00CCCCCCCA008 C00020AlbertNew York USA35000.007000.006000.00 BBBBSBBA008 C00025RavindranBangalore India25000.007000.004000.008000.00AVAVAVAA011 C00024CookLondon UK24000.009000.007000.006000.00FSDDSDFA006 C00015StuartLondon UK16000.008000.003000.0011000.00GFSGERSA003 C00002BoltNew York USA35000.007000.009000.003000.00DDNRDRHA008 C00018FlemingBrisban Australia27000.00 9000.005000.00NHBGVFCA005 C00021JacksBrisban Australia17000.00 WERTGDFA005 C00019 Yearannaid u Chennai India18000.007000.00 8000.00ZZZZBFVA010 35 Create a report to list: 1.All customer names in alphabetical order 2.Customer names and phone numbers of those living in London 3.Customer countries – avoid duplicates 4.Customer names and codes where their outstanding amount is less than 5000.00 5.Customer countries with ‘U’ in the name 6.All customer details where the sum of 'opening_amt' and 'receive_amt' is greater than 15000
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.