5. simple sql using oracle1 simple sql using oracle 5. working with tables: data management and...

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

Upload: elinor-hall

Post on 04-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

5. Simple SQL using Oracle 1

Simple SQL using Oracle

5. Working with Tables: Data management and Retrieval

6. Working with Tables: Functions and Grouping

Page 2: 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 Oracle 2

SQL DML

• DML data manipulation language– insert– update– delete– select

Page 3: 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 Oracle 3

insert, 98• insert one new row into a table• 2 syntaxes

– insert into student values ('Christian', 'Roskilde', 4)

• values must come in same order as attributes in table definition

– insert into student (name, semester) values ('Christian', 4)

• values come in the stated order• missing values use

– default value– null

Page 4: 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 Oracle 4

insert dates, 99

• Attributes with data type date requires special attention– default format

• DD-MM-YY using 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') )

Page 5: 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 Oracle 5

Sequences, 196

• Primary keys are often simple ID's– integers, not carrying data

• MS Access: auto number• MS SQL Server: identity• Oracle: sequence

– generates a sequence of unique numbers– page 196-197– Fig. 9-7 + 9-8, page 197– Fig. 9-9, page 198

Page 6: 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 Oracle 6

Update, 103

• Update an existing row - or more rows.

• Exampleupdate student

set semester = semester + 1

where semester < 5

no where clause ⇒ updates every row

Page 7: 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 Oracle 7

Delete, 105

• deletes one or more rows from a table.

• Exampledelete from student

where grade <= 3– no where clause ⇒ delete every row!

Page 8: 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 Oracle 8

Select, 105

• 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

Page 9: 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 Oracle 9

Select II, 106

• Concatenation– String columns may be concatenated using ||

• select firstname || ' ' || lastname …

• Arithmetic operations– Number columns may be used in arithmetic

operations +, -, *, /

Page 10: 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 Oracle 10

select … where, 114• 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

Page 11: 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 Oracle 11

Sorting, 122

• The DBMS may order the result of a select• Syntax

– select … order by col1, col2 asc / desc• asc ascending• desc descending

– col1 primary order– col2 secondary order– Eample fig. 5-38 – 5-40, page 124

• Never sort the result in an application program!

Page 12: 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 Oracle 12

Built-in functions, 133• Functions on a single row

– Character functions• Fig. 6-1 + 6-2, page 134

– Number functions• Fig. 6-6 + 6-7, page 138

– Date functions• Fig. 6-8 + 6-9, page 139 + 6-10, page 140• select sysdate from dual

– current time– dual is a dummy table, it doesn’t exist

– Conversion functions• Fig. 6-12, page 144 + Figure 6-15, page 145

Page 13: 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 Oracle 13

Group functions, 147• Functions on a table

– Fig. 6-19, page 147– 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(*) > 4 small groups excludes