CSE 4701 Chapter 10-1 Chapter 10 6e: Oracle Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT (860) n All of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech.
CSE 4701 Chapter 10-2 Oracle by Example n Oracle architecture n Online book store implementation l define a simple data model l use SQL*Plus to create the DB in Oracle l overview of commands to see structure l insert, update, and delete data l more advanced Oracle SQL l SQL*Plus shell features
CSE 4701 Chapter 10-3 Oracle Architecture
CSE 4701 Chapter 10-4 Data storage in Oracle n Database l tablespaces à schema objects F data files
CSE 4701 Chapter 10-5 Logging in n Log in to acme n Add to your.profile file export FMHOME=/usr/local/lib/frame export ORACLE_HOME=/usr/local/oracle/8.1.7 export ORACLE_SID=ccdb export LD_LIBRARY_PATH=$ORACLE_HOME/lib On the prompt, l {acme:gte113x:145}. oraenv l ORACLE_SID = [ccdb] ? ccdb l {acme:gte113x:145} sqlplus l will ask for your username and password à Default password is gtxxxxxDDMM à Should change the password after first use
CSE 4701 Chapter 10-6
CSE 4701 Chapter 10-7 Things to remember n Always terminate your command with a n Semicolon (;) n Exit sqlplus by entering quit or exit at the SQL> prompt n Commands can be in lower or upper case n To execute a host operating system command without leaving SQL*Plus, type host l SQL> HOST ls *.sql l SQL> sample1.sql sample2.sql
CSE 4701 Chapter 10-8
CSE 4701 Chapter 10-9 Help Index
CSE 4701 Chapter n Book n Customer n Order n LineItem Relational Model ISBNAuthorTitlePublisherDatePrice Cust #Name StreetCityStateZip Order #Cust#CardExp.Auth Order#Book# Date
CSE 4701 Chapter Create Table
CSE 4701 Chapter Schema Creation n Oracle schema’s are based on user-name l You only get one schema per user using other schema’s requires permission ( GRANT ’s)
CSE 4701 Chapter Schema Creation - book
CSE 4701 Chapter Schema Creation - customer
CSE 4701 Chapter Schema Creation - order
CSE 4701 Chapter Schema Creation - line_item
CSE 4701 Chapter Oracle Data types n character data types : l char(x), max 256 bytes l varchar2(x), max 2000 bytes n numerical data: l number [(x, y)], length and precision l float[(b)], binary precision (up to 126) n long: up to 2GB l restricted: one long per table;no join n Date l century year month day hour min sec
CSE 4701 Chapter Browsing the catalog n view table structure l DESC book n view your tables: l SELECT table_name from user_tables;
CSE 4701 Chapter Browsing the catalog n view all objects l SELECT object_name, object_type, created from user_objects;
CSE 4701 Chapter Insert n Insert command l insert into book values (‘ ’, ‘Smith’, ‘Oracle Basics’, ‘McNeil’, sysdate, 14.99); l insert into book (isbn, author, title, publisher, pub_date, price) values (‘ ’, ‘Smith’, ‘Oracle Basics’, ‘McNeil’, sysdate, 14.99); n What is the difference?
CSE 4701 Chapter Inserted data
CSE 4701 Chapter Updating data
CSE 4701 Chapter Delete n Delete table with drop table l DROP TABLE customer; n Delete rows with delete from l DELETE FROM customer WHERE name = “Smith”; l DELETE FROM customer; n Permanently delete rows with truncate l TRUNCATE TABLE customer; à Faster: no DELETE triggers fired; no rollback available.
CSE 4701 Chapter Commit and Rollback n Modifications of tuples are temporarily stored in the database n They become permanent after commit;statement has been issued n You can undo all modifications since last commit using rollback n Any data definition command like create results in implicit commit n Termination of Oracle session also executes implicit commit
CSE 4701 Chapter Bulk Loading data Create a.sql file with insert statements or start filename.sql n Use SQL*Loader % SQLLOAD / CONTROL=BOOK.CTL LOG=BOOK.LOG LOAD DATA INFILE book.dat APPEND INTO TABLE book FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' (isbn, author, title, publisher, pub_date DATE "DD-Month-YYYY", price)
CSE 4701 Chapter Data file Example “ ”, “Smith”, “Oracle Basics”, “McNeil”, 17-April-2000, “ ”, “Smith”, “Java Basics”, “AW”, 12-March-1998, 35
CSE 4701 Chapter Log File
CSE 4701 Chapter View Create a new view of data l CREATE VIEW clerk (id_number, person, department, position) AS SELECT empno, ename, deptno, job FROM emp WHERE job = 'CLERK’; n Why?
CSE 4701 Chapter View Command
CSE 4701 Chapter Constraints n Column constraints: l primary key l foreign key l NULL values l create table CUSTOMER (id NUMBER CONSTRAINT pk_cust PRIMARY KEY, name VARCHAR2(250), VARCHAR2(60) CONSTRAINT nn_ NOT NULL, addr NUMBER CONSTRAINT fk_addr REFERENCES address(id));
CSE 4701 Chapter Table Constraint Clause
CSE 4701 Chapter Column Constraint Clause
CSE 4701 Chapter Alter Table n Add a column, or integrity constraint n Remove an integrity constraint n Cannot rename or drop a column n Keep a copy of the SQL statements you use to create and alter tables l ALTER TABLE book MODIFY (price NUMBER(7,2)); l ALTER TABLE customer DROP PRIMARY KEY CASCADE;
CSE 4701 Chapter Alter Table command
CSE 4701 Chapter SQL*Plus Shell n Features: l scripting l variables l report formatting n Basics l commands end with a semi-colon, a single ‘/’, or two blank lines. l Repeat last command: ‘/’ l Comments begin with /* and end with */ l comments and semi-colons don’t co-exist
CSE 4701 Chapter Formatting l SQL> SELECT * from user_tables;
CSE 4701 Chapter Formatting l SQL> SELECT table_name, pct_free from user_tables;
CSE 4701 Chapter SQL*Plus Display commands l COLUMN table_name HEADING table l COLUMN pct_free HEADING free l COLUMN table_name FORMAT A10 l SET UNDERLINE = l TTITLE CENTER ‘TABLE FREE SPACE’
CSE 4701 Chapter Running commands from a file n SQL> GET filename loads a command file into the buffer n SQL> RUN executes the buffer n loads and executes a command file n % sqlplus start sqlplus and immediately runs file n login.sql like.cshrc, configures environment with SQL*Plus shell commands
CSE 4701 Chapter Oracle Errors n Oracle only provides an error number and a short message n To get an explanation: use oerr