Lecture: SQL1
Download
Report
Transcript Lecture: SQL1
SQL
Tarek El-Shishtawy
Professor Ass. Of Computer Engineering
1
SQL - Advantages
Stands for Structured Query Language
Low training cost – simple language
Fixed rigid simple syntax
Independent of Operating system
Independent of Data Base
Provides interconnection between forms,
reprts and all applications with the DB
2
SQL Categories
DDL
DML
Data Definition Language
Used to create data base elements such as tables,
views
Data Manipulation Language
Used to process data such as selecting, Inserting,
deleting data
DCL
Data Control Language
Used by database administrator to give previelages to
3
users, back up, tuning performance,
DDL, DML, DCL, and the database development
process
4
Relational Tables
Table Name
COURSE
Field Names
CRS
:
CTITLE
CHRS
CHRSW
Code for the course
Course Title
Course Hours
Course Hours per week
5
Relational Model
CRS
701
702
703
704
705
706
707
708
709
710
711
CTITLE
CHRS
INTRO TO COMPUTERS
45
COBOL PROGRAMMING
100
C PROGRAMMING
90
SYSTEM ANALYSIS AND DESIGN
90
FUNDAMENTALS OF DATABASES
45
CASE TOOLS
60
DESIGN OF DATABASES
75
SQL QUERY LANGUAGE
60
NATURAL PROGRAMMING
60
SOFTWARE ENGINEERING
60
SOFTWARE QUALITY ASSURANCE
45
CHRSW
15
20
15
15
15
15
15
15
15
15
15
6
Relational Model
Note the following
The
intersection between each column and
row has only one value
No two rows has identical values
No order is assumed.
When inserting new row, its location is not
known
The table can be represented as
COURSE
(CRS, CTITLE, CHRS, CHRSW)
7
ORACLE Basic data types
String types
CHAR(n)
fixed-length
character data
n characters long
VARCHAR2(n)
variable
length character data
maximum 4000 bytes in oracle 9 I
LONG
variable-length
character data
up
to 4GB
Maximum 1 per table
8
ORACLE Basic data types
Numeric types
NUMBER(p,q)
general purpose numeric data type
Total width = p, out of which q decimal numbers
INTEGER(p)
signed integer, p digits wide
FLOAT(p)
floating point in scientific notation with p binary digits
precision
Date/time type
DATE
length date/time in dd-mm-yyyy/hh:mm:ss form
9
Table Keys
Primary Keys
Uniqueness
NOT
NULL
Minimum Number of columns
Foreign Keys
Used
to relate rows of two or more tables
Foreign key at the child table should be of the
same type (and values) of the primary key at
the parent table
10
Example of relationships
Primary Key
Foreign Key
Comes from
LOCATION.OFF
OFF
LOC
01
RIYAD
02
JEDDAH
Table LOCATION
Parent
CRS
OFF
ODATE
701
01
21-SEP-91
Table Offer
701
02
16-NOV-91
Child
702
01
26-NOV-91
702
02
01-FEB-92
11
Example of Database Tables
COURSE (CRS, CTITLE, CHRS, CHRSW)
OFFER (CRS, OFF, ODATE)
LOCATION (OFF, LOC)
LOCATION
COURSE
OFF
LOC
CRS
CTITLE
CHRS
CHRSW
OFFER
CRS
OFF
ODATE
12
Questions
Determine suitable data types and lengths
for fields in previous tables
If we want to add data for PERSONS who
ATTEND offer of courses. How many
tables should be added? What relations
are required?
Note
data of persons are ID, Name, and
address.
Determine also primary and foreign key for
new tables and suitable field types and
widths.
13
Selecting data from the table
General Form
Select Column Names
From Table Name
[Where Conditions]
/* Optional where */
Examples for
COURSE
(CRS, CTITLE, CHRS, CHRSW)
SELECT CRS, CTITLE
FROM COURSE;
14
Example Continued
SELECT CTITLE, CHRSW
FROM COURSE;
SELECT * FROM COURSE;
SELECT CTITLE, CHRSW
FROM
COURSE
WHERE CRS = 706;
15
Example Continued
SELECT *
FROM
COURSE
WHERE CHRS >= 60 AND CHRSW = 15;
SELECT CTITLE, CHRSW
FROM
COURSE
WHERE CHRS >= 60 and CHRS < =100;
16
Continued
SELECT CTITLE, CHRSW
FROM
COURSE
WHERE CHRS BETWEEN 60 and 100;
SELECT CTITLE, CHRSW
FROM
COURSE
WHERE CHRS >= 60 OR CRS = 706;
17
18