Download Lecture Note 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Access wikipedia , lookup

DBase wikipedia , lookup

Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Btrieve wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Oracle Database wikipedia , lookup

Clusterpoint wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
ITEC 3220M
Using and Designing Database Systems
Instructor: Prof. Z. Yang
Course Website:
http://people.math.yorku.ca/~zyang/it
ec3220m.htm
Office: Tel 3049
SQL*Loader
SQL*Loader
• SQL*Loader is a bulk loader utility used
for moving data from external files into
an Oracle table.
• Tables must exist
• Loader works with the control file
(file.ctl)
3
Loader Files
• SQL*Loader takes either:
–
two input files – a control file and a data
file
or
–
a single input file – a control file that
contains a set of data and loads the data
into a single Oracle table
4
Loader Files
The data file contains data
– each record is placed in one row,
– fields can be delimited by ‘,’ or ‘|’
– can have extensions .csv or .dat
The control file contains information about the data
– specifies action: insert, replace, append
– describes the data, indicates which tables and
columns the data is to be loaded
– has extension .ctl
5
The SQL*Loader Environment
6
The Log, Bad and Discard Files
• The log file (.log)
– records SQL*Loader's activities during a load session
and contains statistics on the load, error messages for
records that cause errors
It is important to review the log file after a load to make
sure that no errors occurred!
• The bad file (.bad)
– shows database errors, e.g. insert failure due to
integrity constraint violation
• The discard file (.dis)
– holds records that do not meet selection criteria
specified in the SQL*Loader control file
7
The Data File (ship.csv) - example
“001”,”Century”,”1986”,”2000”
“002”,”Galaxy”,”1989”,”1500”
“003”,”Horizon”,”1992”,”1600”
“004”,”Infinity”,”1995”,”2500”
“005”,”Journey”,”1998”,”2500”
“006”, “Mercury”,”2001”,”3000”
8
The Control File (ship.ctl) - example
INFILE ‘ship.csv’
REPLACE INTO TABLE Ship
FIELDS TERMINATED By ‘,’ OPTIONALLY
ENCLOSED BY ‘”’
(shipNum, shipName, yearBuilt, capacity)
9
Data in the Control File - example
LOAD DATA
INFILE *
REPLACE INTO TABLE Ship
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
(shipNum, shipName, yearBuilt, capacity)
BEGINDATA
"001","Century","1986","2000"
"002","Galaxy","1989","1500"
"003","Horizon","1992","1600"
"004","Infinity","1995","2500"
"005","Journey","1998","2500"
"006","Mercury","2001","3000"
10
Execution of SQL*Loader
sqlldr login/password@studb10g
control=ship.ctl
11
SQL Queries
SQL Queries
• Single table query
• Multiple table query
– Nesting query
•Using IN
•Using EXISTS
– Join Table
13
Joining Database Tables
• Ability to combine (join) tables on common attributes
is most important distinction between a relational
database and other databases
• Join is performed when data are retrieved from more
than one table at a time
• Join is generally composed of an equality comparison
between the foreign key and the primary key of related
tables
14
Examples
• SELECT Order_Num FROM ORDERS
WHERE Order_Num IN
(SELECT Order_Num
FROM ORDER_LINE
WHERE Part_Num =1234;
15
Examples (Cont’d)
• SELECT Order_Num FROM ORDERS
WHERE EXISTS
(SELECT *
FROM ORDER_LINE
WHERE ORDERS.Order_Num
ORDERLINE.Order_Num AND
Part_Num =1234;
=
16
Examples (Cont’d)
• SELECT S.Last, S.First, C.Last, C.First
FROM SALES_REP S,
CUSTOMER C
WHERE S.Srep_Num =
C. Srep_Num
17
SQL Exercise
• Write SQL code that will create the relations
shown. Assume the following attribute data
types:
–
–
–
–
–
–
–
–
–
Student_ID: integer
Student_Name: 25 characters
Faculty_ID: integer
Faculty_Name: 25 characters
Course_ID: 25 characters
Course_Name: 15 characters
Date_Qualified: date
Section_ID: integer
Semester: 7 characters
18
SQL Exercise (Cont’d)
STUDENT (Primary
key: Student_ID)
Student_
ID
Student_
Name
38214
Letersky
54907
66324
70542
IS_QUALIFIED (Primary key:
Faculty_ID, Course_ID)
Faculty_
ID
2143
Course_I
D
ISM3112
Date_
Qualified
9/1988
3467
ISM4212
9/1995
3467
ISM4930
9/1996
4756
ISM3113
9/1991
4756
ISM3112
9/1991
Altvater
Aiken
Marra
19
SQL Exercise (Cont’d)
FACULTY (Primary
key: Faculty_ID)
SECTION (Primary
key: Section_ID)
Faculty_I
D
2143
Faculty_Name
Section_ID
Course_ID
Birkin
2712
ISM3113
3467
Berndt
2713
ISM3113
4756
Collins
2714
ISM4212
2715
ISM4930
20
SQL Exercise (Cont’d)
COURSE ((Primary
key: Course_ID)
IS_REGISTERED (Primary
key: Student_ID, Section_ID)
Student_I
D
38214
Section_I
D
2714
Semester
ISM3112
Course_
Name
Syst
Analysis
Syst Design
54907
2714
I - 2001
ISM4212
Database
54907
2715
I - 2001
ISM4930
Networking
66324
2713
I - 2001
Course_ID
ISM3113
I - 2001
21
SQL Exercise (Cont’d)
• Write SQL queries to answer the following
questions:
– Display the course ID and course name for all
courses with an ISM prefix.
– Display all the courses (Course_Name) for which
Professor Berndt has been qualified.
– Is any instructor qualified to teach ISM 3113 and
not qualified to teach ISM 4930?
– How many students are enrolled in section 2714
during semester I – 2001?
– Which students were not enrolled in any courses
during semester I – 2001?
22
Lab Instruction
• Login to sit.yorku.ca
• Start Oracle SQL*PLUS environment by
typing the following command: sqlplus
• When prompted for the username/password
enter your_username@studb10g (where
your_username is your AML username) at the
username prompt and your AML password at
the password prompt.
23
Lab Tips
• To list all tables you have in your Oracle account use the
following SQL command:
select table_name from user_tables;
• To describe a given Oracle table use the following
Oracle environment command (note that this is not an
SQL command):
desc tablename (where tablename is the name of the
table that you have in your account)
24
SQL Plus Editing Commands
• Add text at end of current line: A text
• Change current line: type the line
number
• Change text in current line: C/old/new
• Insert a line following current line: I
25