Beruflich Dokumente
Kultur Dokumente
Vandana Janeja 10th February 2004 For : Database Systems 22:198:603 Prof. Vijay Atluri Adapted from material by Dr.Richard Holowczak and Dr.Soon Chun
Outline
Background SQL*PLUS SQL
Components
Oracle Server Front end application VB, Access, Excel etc.
ODBC Driver
ORACLE RDBMS (Oracle Universal server) Integrated Data Dictionary: manage tables owned by all users in a system SQL: language to access and manipulate data PL/SQL: a procedural extension to SQL language
Connectivity
SQL*Net and Net8: allow a Oracle client machine to communicate with Oracle data server SQL*Connect and Oracle Gateways: communicate a client machine to access nonoracle data on server machine Oracle Server: receives requests from client machines and submits them to Oracle RDBMS Oracle ODBC drivers: connect software to Oracle databases
6
SQL*Plus
Command line tool that process users SQL statements Requires Oracle account
DDL SQL Data Definition
DML
DCL
Password: MBAgrad2004
Getting help
Help command
SQL*Plus Commands
Example of SQL stmt: select * from tab; describe <table name> list : list current sql stmt edit : edit current sql statement (or ! Editorname <filename>) where editor name is vi or pico input : add one or more lines to sqlplus buffer spool : start directing output of sql statements to a file spool off : turn the spool off run (or /): execute the statement in the current buffer save <filename.sql> : save current sql stmt to a file get <filename.sql> : load sql statements into buffer spool <filename>: send output from sql stmt to a file start <filename.sql>: load script file with sql stmts and run them help
10
11
vi Editor
14
MS Access, Oracle, Sybase, Informix, etc. specified by a command-line tool or is embedded into a general purpose programming language, C, Pascal, Java, Perl...
Data Definition Language (DDL) Used to create (define) data structures such as tables, indexes, clusters
Data Manipulation Language (DML) is used to store, retrieve and update data from tables
Data Control Language used to control the access to the database objects created using DDL and DML
15
Create, Drop or Alter a table Create or Drop an Index Define Integrity constraints Define access privileges to users Define access privileges on objects
16
Create Table
17
Data Types
A table is made up of one or more columns Each column is given a name and a data type that reflects the kind of data it will store. Oracle supports four basic data types
CHAR NUMBER DATE RAW. There are also a few additional variations on the RAW and CHAR data types.
19
VARCHAR2
Character data type. Can contain letters, numbers and punctuation. The syntax : VARCHAR2(size) where size is the maximum number of alphanumeric characters the column can hold. In Oracle8, the maximum size of a VARCHAR2 column is 4,000 bytes. Numeric data type. Can contain integer or floating point numbers only. The syntax : NUMBER(precision, scale) where precision is the total size of the number including decimal point and scale is the number of places to the right of the decimal. For example, NUMBER(6,2) can hold a number between 999.99 and 999.99.
20
NUMBER
DATE
Date and Time data type. Can contain a date and time portion in the format: DD-MON-YY HH:MI:SS. No additional information needed when specifying the DATE data type. the time of 00:00:00 is used as a default. The output format of the date and time can be modified Free form binary data. Can contain binary data up to 255 characters. Data type LONG RAW can contain up to 2 gigabytes of binary data. RAW and LONG RAW data cannot be indexed and can not be displayed or queried in SQL*Plus. Only one RAW column is allowed per table. Large Object data types. These include BLOB (Binary Large OBject) and CLOB (Character Large OBject). More than one LOB column can appear in a table. These data types are the prefferred method for storing large objects such as text documents (CLOB), images, or video (BLOB).
21
RAW
LOB
Foreign Key
23
Primary key
CREATE TABLE order_header ( order_number NUMBER(10,0) NOT NULL, order_date DATE, sales_person VARCHAR(25), bill_to VARCHAR(35), bill_to_address VARCHAR(45), bill_to_city VARCHAR(20), bill_to_state VARCHAR(2), bill_to_zip VARCHAR(10), PRIMARY KEY (order_number) );
24
Foreign key
CREATE TABLE order_items ( order_number NUMBER(10,0) NOT NULL, line_item NUMBER(4,0) NOT NULL, part_number VARCHAR(12) NOT NULL, quantity NUMBER(4,0), PRIMARY KEY (order_number, line_item), FORIEGN KEY (order_number) REFERENCES order_header (order_number), FOREIGN KEY (part_number) REFERENCES parts (part_number) );
25
Creating indexes/views/sequences
CREATE INDEX items_index ON order_items (order_number, line_item) ASC ;
drop index index_name create view emp_dno1 as select fname, lname, deptno from emp where deptno=4001
26
Other Constraints
28
NULL - Orphans.
30
Example
CREATE TABLE order_items ( order_number NUMBER(10,0) NOT NULL, line_item NUMBER(4,0) NOT NULL, part_number VARCHAR(12) NOT NULL, quantity NUMBER(4,0), PRIMARY KEY (order_number, line_item), FORIEGN KEY (order_number) REFERENCES order_header (order_number) ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY (part_number) REFERENCES parts (part_number) ); 31
bill_to_state VARCHAR(2),
bill_to_zip VARCHAR(10), CONSTRAINT order_header_pk PRIMARY KEY (order_number) );
32
Drop
33
Alter
35
36
Syntax of commands
alter table add/modify column create table tname ( colname, datatype, not null, ...); create table .. as <sql stmt> drop table tname create index <indexname> on tname (colname, colname); drop index <indexname> create sequence <seqname> increament by .. start with maxvalue cycle; drop sequence create view <vname> as <sql select stment = select <colnames> from tname where <condition> (read only, no insert, update or delete) drop view
38
DML
39
SQL DML
commit delete insert rollback select update
40
INSERT INTO tablename (column1, column2, ... columnX) VALUES (val1, val2, ... valX);
Examples: INSERT INTO employee (first_name, last_name, street, city, state, zip) VALUES ("Buddy", "Rich", "123 Sticks Ln.", "Fillville", "TN", "31212");
INSERT INTO stocks (symbol, close_date, close_price) VALUES ("IBM", "03-JUN-94", 104.25); INSERT INTO student_grades (student_id, test_name, score, grade) VALUES (101, "Quiz 1", 88, "B+");
41
If the WHERE clause is omitted, all rows in the table will be deleted.
UPDATE employee SET salary = salary * 1.03 WHERE dno = (SELECT dno FROM department WHERE dname = 'MARKETING');
42
GRANT INSERT, UPDATE, DELETE ON Student TO Mary GRANT <privileges> ON <object name> TO <grantee> [ <comma> <grantee> ... ] [ WITH GRANT OPTION ] WITH GRANT OPTION: allows the grantee to further grant privileges Can be limited to a column of a table, Ex: GRANT UPDATE(name) ON Student TO Mary To revoke privileges : REVOKE
43
A Bank Example
44
CUSTOMERS Customer_Id, Name, Street, City, State, Zip ACCOUNTS Customer_Id, Account_Number, Account_Type, Date_Opened, Balance
Customer_Id is the key for the CUSTOMERS table. Account_Number is the key for the ACCOUNTS table. Customer_Id in the ACCOUNTS table is called a Foreign Key
45
Customer
Column Customer_Id (Key) Name Street City State Zip Data Type Integer Character Character Character Character Character Size 20 30 30 25 2 5
46
Accounts
Column Customer_Id (FK) Account_Number (Key) Account_Type Date_Opened Balance
20 15 2 12,2
47
48
Balance
4000.00
2000.00
1000.00
1003
1004 1004
4433
3322 1122
Savings
Savings Checking
12/01/94
08/22/94 11/13/88
9000.00
49
Business Rules
Business rules allow us to specify constraints on what data can appear in tables and what operations can be performed on data in tables. For example:
An account balance can never be negative.
existing (open) account. Money can only be transferred from a "Savings" account to a "Checking" account. Savings accounts with less than a $500 balance incur a service charge.
52
CREATE TABLE students (studentid NUMBER(5,0), name VARCHAR2(25), major VARCHAR2(15), gpa NUMBER(6,3), tutorid NUMBER(5,0)); INSERT INTO students VALUES (101, 'Bill', 'CIS', 3.45, 102); INSERT INTO students VALUES (102, 'Mary', 'CIS', 3.10, NULL); INSERT INTO students VALUES (103, 'Sue', 'Marketing', 2.95, 102); INSERT INTO students VALUES (104, 'Tom', 'Finance', 3.5, 106); INSERT INTO students VALUES (105, 'Alex', 'CIS', 2.75, 106); INSERT INTO students VALUES (106, 'Sam', 'Marketing', 3.25, 103); INSERT INTO students VALUES (107, 'Jane', 'Finance', 2.90, 102); Example table COURSES: Create table courses(studentid NUMBER(5,0) NOT NULL, coursenumber VARCHAR2(15) NOT NULL, coursename VARCHAR2(25), semester VARCHAR2(10), year NUMBER(4,0), grade VARCHAR2(2)); INSERT INTO courses VALUES (101, 'CIS3400', 'DBMS I', 'FALL', 1997, 'B+'); INSERT INTO courses VALUES (101, 'CIS3100', 'OOP I', 'SPRING', 1999, 'A-'); INSERT INTO courses VALUES (101, 'MKT3000', 'Marketing', 'FALL', 1997, 'A'); INSERT INTO courses VALUES (102, 'CIS3400', 'DBMS I', 'SPRING', 1997, 'A-'); INSERT INTO courses VALUES (102, 'CIS3500', 'Network I', 'SUMMER', 1997, 'B'); INSERT INTO courses VALUES (102, 'CIS4500', 'Network II', 'FALL', 1997, 'B+'); INSERT INTO courses VALUES (103, 'MKT3100', 'Advertizing', 'SPRING', 1998, 'A'); INSERT INTO courses VALUES (103, 'MKT3000', 'Marketing', 'FALL', 1997, 'A'); INSERT INTO courses VALUES (103, 'MKT4100', 'Marketing II', 'SUMMER', 1998, 'A-');
53
GPA TutorId
102
102
54
Some queries
Average GPA of all students
SELECT AVG(gpa) FROM students; AVG(GPA) ---------- 3.12857143
Average GPA of Finance and CIS students: SELECT AVG(gpa) FROM students WHERE major = 'CIS' OR major =
'Finance'; AVG(GPA) ---------- 3.14
56