SQL in Oracle
Set up Oracle access at IU You need to install Oracle Client: – – For windows: Connecting to Oracle with SQL*Plus - -Go to Start->All programs->Oracle->Application Development->SQL*Plus -Your username should be:
Set up Oracle access at IU Or you can use Aqua Data Studio to access Oracle
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
Relational Model Data stored in relations (tables) course attributes (or columns) tuples (or rows) course_IDcourse_namedepartment_ID CIS3100DatabaseCIS CIS4500NetworkCIS MKT3400AdvertisingMKT MKT4100MarketingMKT
SQL Data Definition Language (DDL) – CREATE TABLE – ALTER TABLE – DROP TABLE Data Manipulation Language (DML) – INSERT INTO – SELECT – UPDATE – DELETE
SQL Basic structure – query block – SELECT – FROM – WHERE clauses – GROUP BY clause – HAVING clause – ORDER BY clause – Aggregate functions COUNT, MIN, MAX, AVG, SUM
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:
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. HDGEEFE
Example student_idnamemajorGPA 101BillCIS MaryCIS SueMKT3.90 course_idnamedepartment_id CIS3100DatabaseCIS CIS3400Network ICIS CIS3500Network IICIS MKT3000AdvertisingMKT MKT3200Marketing IMKT MKT4200Marketing IIMKT student_idcourse_idgrade 101CIS3100A 101CIS3500B+ 102CIS3100A- 102CIS3400A 103MKT3000A 103MKT3200B 103MKT4200B+
DDL in Oracle Basic data types – CHAR(size) – VARCHAR2(size) – NUMBER(p, s) – DATE – BLOB/CLOB See:
DDL in Oracle CREATE TABLE ALTER TABLE DROP TABLE CREATE TABLE student ( student_idNUMBER(10), nameVARCHAR2(25), majorVARCHAR2(15), CONSTRAINT pk_students PRIMARY KEY (student_id) ); ALTER TABLE student ADD (GPA NUMBER(6,3)); DROP TABLE student;
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;
Create tables
Queries SELECT * FROM course WHERE rownum<=3; SELECT * FROM enroll WHERE grade=‘A’;
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’;
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;
Joining tables SELECT student.name, enroll.course_id, enroll.grade FROM student INNER JOIN enroll ON student.student_id=enroll.student_id;
Joining tables SELECT * FROM student LEFT JOIN enroll ON student.student_id=enroll.student_id;
Joining tables SELECT * FROM student RIGHT JOIN enroll ON student.student_id=enroll.student_id;
References Oracle tutorial: