5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping
5. Simple SQL using Oracle2 SQL DML DML data manipulation language –insert –update –delete –select
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
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. 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]
Sequences (cont’d) Examples CREATE SEQUENCE major_majorid_seq INCREMENT BY 10 START WITH 610 MAXVALUE 999 NOCACHE; 5. Simple SQL using Oracle6
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
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!
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
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 +, -, *, /
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
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;
5. Simple SQL using Oracle13 Built-in functions Functions on a single row –Character functions –Number functions –Date functions select sysdate from dual –current time –dual is a dummy table, it doesn’t exist –Conversion functions
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