21
SQL in Oracle

SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: – – For windows:

Embed Size (px)

Citation preview

Page 1: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

SQL in Oracle

Page 2: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Set up Oracle access at IU• You need to install Oracle Client:

– http://kb.iu.edu/data/anhl.html– For windows: http://kb.iu.edu/data/aznp.html

• Connecting to Oracle with SQL*Plus- http://kb.iu.edu/data/aznp.html#connecting- Go to Start->All programs->Oracle->Application

Development->SQL*Plus- Your username should be: [email protected]

Page 3: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Set up Oracle access at IU• Or you can use Aqua Data Studio to access Oracle

Page 4: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Aqua Data Studio

• Server connection:– host: dbserv.uits.indiana.edu– port: 1521– username: your username– passcode: your password– System Identifier of the database (SID): oed1prd

Page 5: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Relational Model

• Data stored in relations (tables)

course

attributes(or columns)

tuples(or rows)

course_ID course_name

department_ID

CIS3100 Database CIS

CIS4500 Network CIS

MKT3400 Advertising MKT

MKT4100 Marketing MKT

Page 6: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

SQL

• Data Definition Language (DDL)– CREATE TABLE– ALTER TABLE– DROP TABLE

• Data Manipulation Language (DML)– INSERT INTO – SELECT– UPDATE– DELETE

Page 7: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

SQL

• Basic structure – query block– SELECT – FROM – WHERE clauses– GROUP BY clause– HAVING clause– ORDER BY clause– Aggregate functions

• COUNT, MIN, MAX, AVG, SUM

Page 8: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

SQL in Oracle

• SQL*Plus– Command line interface to access Oracle database– Enter, edit, store, retrieve, and run SQL statements

• Start SQL*Plus - Go to Start->All programs->Oracle->Application

Development->SQL*Plus- Your username should be:

[email protected]

Page 9: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

SQL*Plus Commands• DESCRIBE: list the columns with data types of a table• EXIT: exit the SQL*Plus program• GET: load a SQL statement into the buffer• LIST: list the current statement in the buffer• RUN: execute the current SQL statement in the buffer• SAVE: save the current SQL statement to a script file• SPOOL: send the output from a SQL statement to a file• START: load a SQL statement located in a script file and then

run that SQL statement• Commit: save your input from buffer to disk.

http://download.oracle.com/docs/cd/B10501_01/server.920/a90842/ch13.htmhttp://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch4.htm#CHDGEEFE

Page 10: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Examplestudent

PK student_id

name major GPA

course

PK course_id

name department_id

enroll

PK,FK1 student_idPK,FK2 course_id

grade

student_id name major GPA

101 Bill CIS 3.45

102 Mary CIS 3.10

103 Sue MKT 3.90

course_id name department_id

CIS3100 Database CIS

CIS3400 Network I CIS

CIS3500 Network II CIS

MKT3000 Advertising MKT

MKT3200 Marketing I MKT

MKT4200 Marketing II MKT

student_id course_id grade

101 CIS3100 A

101 CIS3500 B+

102 CIS3100 A-

102 CIS3400 A

103 MKT3000 A

103 MKT3200 B

103 MKT4200 B+

Page 11: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

DDL in Oracle

• Basic data types– CHAR(size)– VARCHAR2(size)– NUMBER(p, s)– DATE– BLOB/CLOBSee: http://www.techonthenet.com/oracle/datatypes.php

Page 12: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

DDL in Oracle

• CREATE TABLE

• ALTER TABLE

• DROP TABLE

CREATE TABLE student (student_id NUMBER(10),name VARCHAR2(25),major VARCHAR2(15),CONSTRAINT pk_students PRIMARY KEY (student_id)

);

ALTER TABLE student ADD (GPA NUMBER(6,3));

DROP TABLE student;

Page 13: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

DML in Oracle

• INSERT

• UPDATE

• DELETE

• SELECT

INSERT INTO student VALUES (101, 'Bill', 'CIS', 3.45);

UPDATE student SET GPA=3.55 where student_id=101;

DELETE FROM student where student_id=101;

SELECT * FROM student;

Page 14: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Create tables

Page 15: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

QueriesSELECT * FROM course WHERE rownum<=3;

SELECT * FROM enroll WHERE grade=‘A’;

Page 16: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Queries

SELECT * FROM student WHERE student.student_id=(SELECT enroll.student_id FROM enroll WHERE grade='A-');

SELECT * FROM student WHERE student.student_id IN (SELECT enroll.student_id FROM enroll WHERE grade='A’);

SELECT student.name FROM student, enroll WHERE student.student_id=enroll.student_id AND enroll.grade=‘A’;

Page 17: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Sorting and Grouping

SELECT * FROM enroll ORDER BY grade, course_id;

SELECT major, max(gpa) FROM student GROUP BY major HAVING max(gpa)>3.40;

SELECT DISTINCT grade FROM enroll;

Page 18: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Joining tablesSELECT student.name, enroll.course_id, enroll.grade FROM

student INNER JOIN enroll ON student.student_id=enroll.student_id;

Page 19: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Joining tables

• SELECT * FROM student LEFT JOIN enroll ON student.student_id=enroll.student_id;

Page 20: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

Joining tables

• SELECT * FROM student RIGHT JOIN enroll ON student.student_id=enroll.student_id;

Page 21: SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: –   – For windows:

References• www.oracle.com

Oracle tutorial:• http://dbis.ucdavis.edu/courses/sqltutorial/tutorial.pdf• http://cisnet.baruch.cuny.edu/holowczak/oracle/sqlplus/