Presentation is loading. Please wait.

Presentation is loading. Please wait.

5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.

Similar presentations


Presentation on theme: "5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping."— Presentation transcript:

1 5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping

2 5. Simple SQL using Oracle2 SQL DML DML data manipulation language –insert –update –delete –select

3 5. Simple SQL using Oracle3 insert insert one new row into a table 2 syntaxes –insert into student values (‘Jørgen', 'Roskilde', 4) values must come in same order as attributes in table definition –insert into student (name, semester) values (‘Jørgen', 4) values come in the stated order missing values use –default value –null

4 5. Simple SQL using Oracle4 insert dates Attributes with data type date requires special attention –default format DD-MM-YYusing current century –use the built-in to_date function to convert a string to a date to_date('08 mar 1966', 'DD MON YYYY') insert into student (name, birthday) values ('Anders', to_date('08 mar 1966', 'DD MON YYYY') )

5 5. Simple SQL using Oracle5 Sequences Primary keys are often simple ID's –integers, not carrying data Oracle: sequence –generates a sequence of unique numbers CREATE SEQUENCE sequencename [INCREMENT BY n] [START WITH s] [MAXVALUE x | NOMAXVALUE] [MINVALUE m | NOMINVALUE] [CYCLE | NOCYLE] [CACHE c | NOCACHE] [ORDER | NOORDER]

6 Sequences (cont’d) Examples CREATE SEQUENCE major_majorid_seq INCREMENT BY 10 START WITH 610 MAXVALUE 999 NOCACHE; 5. Simple SQL using Oracle6

7 7 Update Update an existing row - or more rows. Example update student set semester = semester + 1 where semester < 5 no where clause ⇒ updates every row

8 5. Simple SQL using Oracle8 Delete deletes one or more rows from a table. Example delete from student where grade <= 3 –no where clause ⇒ delete every row!

9 5. Simple SQL using Oracle9 Select Simple select –select columns from tablename –variations wild card * –select all columns select distinct attribute … –excludes equal rows (equals attribute values) in the result column aliases –select columnname [as] alias, »You can use the keyword AS if you like »I always use AS

10 5. Simple SQL using Oracle10 Select II Concatenation –String columns may be concatenated using || select firstname || ' ' || lastname … Arithmetic operations –Number columns may be used in arithmetic operations +, -, *, /

11 5. Simple SQL using Oracle11 select … where The where clause restrict the number of rows in the result. Operators in where –AND, OR, NOT combining boolean statements –IS NULL … where column IS NUL ordinary equality (=) with NULL is always false, –NULL == NULL is false –BETWEEN … AND … where age between 45 and 78 –IN … where country in ('DK', 'SE', 'NO') –LIKE … where firstname like 'And%'wild card: 0 or more chars … where firstname like 'A_ders'wild card: 1 characters Don’t use == with wildcards, use like

12 5. Simple SQL using Oracle12 Sorting The DBMS may order the result of a select Syntax –select … order by col1, col2 asc / desc ascascending, desc descending col1 primary order, col2 secondary order Example SELECT Lname, Fname, Salary FROM employee WHERE DeptId = 30 ORDER BY Salary Desc;

13 5. Simple SQL using Oracle13 Built-in functions Functions on a single row –Character functions http://www.oradev.com/oracle_string_functions.jsp –Number functions http://www.oradev.com/numeric_functions.jsp –Date functions http://www.oradev.com/oracle_date_functions.jsp select sysdate from dual –current time –dual is a dummy table, it doesn’t exist –Conversion functions http://www.praetoriate.com/t_high_perform_conversion_functions.htm

14 5. Simple SQL using Oracle14 Group functions Functions on a table –SUM, AVG, MAX, MIN, COUNT –functions may be computed on groups of data select attribute functions … group by attribute select grade, count(*) from students group by grade select semester, avg(grade) from students group by semester –having clause restricts the groups select col functions … group by col having condition select grade, count(*) from students group by grade having count(*) > 4small groups excludes


Download ppt "5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping."

Similar presentations


Ads by Google