Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SQL Tarek El-Shishtawy Professor Ass. Of Computer Engineering.

Similar presentations


Presentation on theme: "1 SQL Tarek El-Shishtawy Professor Ass. Of Computer Engineering."— Presentation transcript:

1 1 SQL Tarek El-Shishtawy Professor Ass. Of Computer Engineering

2 2 SQL - Advantages Stands for Structured Query Language Stands for Structured Query Language Low training cost – simple language Low training cost – simple language Fixed rigid simple syntax Fixed rigid simple syntax Independent of Operating system Independent of Operating system Independent of Data Base Independent of Data Base Provides interconnection between forms, reprts and all applications with the DB Provides interconnection between forms, reprts and all applications with the DB

3 3 SQL Categories DDL DDL Data Definition Language Data Definition Language Used to create data base elements such as tables, views Used to create data base elements such as tables, views DML DML Data Manipulation Language Data Manipulation Language Used to process data such as selecting, Inserting, deleting data Used to process data such as selecting, Inserting, deleting data DCL DCL Data Control Language Data Control Language Used by database administrator to give previelages to users, back up, tuning performance, Used by database administrator to give previelages to users, back up, tuning performance,

4 4 DDL, DML, DCL, and the database development process

5 5 Relational Tables Table Name Table Name COURSE COURSE Field Names Field Names CRS : Code for the course CRS : Code for the course CTITLE Course Title CTITLE Course Title CHRSCourse Hours CHRSCourse Hours CHRSWCourse Hours per week CHRSWCourse Hours per week

6 6 Relational Model

7 7 Note the following Note the following The intersection between each column and row has only one value The intersection between each column and row has only one value No two rows has identical values No two rows has identical values No order is assumed. No order is assumed. When inserting new row, its location is not known When inserting new row, its location is not known The table can be represented as The table can be represented as COURSE (CRS, CTITLE, CHRS, CHRSW) COURSE (CRS, CTITLE, CHRS, CHRSW)

8 8 ORACLE Basic data types String types String types CHAR(n) CHAR(n) fixed-length character data fixed-length character data n characters long n characters long VARCHAR2(n) VARCHAR2(n) variable length character data variable length character data maximum 4000 bytes in oracle 9 I maximum 4000 bytes in oracle 9 I LONG LONG variable-length character data variable-length character data up to 4GB up to 4GB Maximum 1 per table Maximum 1 per table

9 9 ORACLE Basic data types Numeric types Numeric types NUMBER(p,q) NUMBER(p,q) general purpose numeric data type general purpose numeric data type Total width = p, out of which q decimal numbers Total width = p, out of which q decimal numbers INTEGER(p) INTEGER(p) signed integer, p digits wide signed integer, p digits wide FLOAT(p) FLOAT(p) floating point in scientific notation with p binary digits precision floating point in scientific notation with p binary digits precision Date/time type Date/time type DATE DATE length date/time in dd-mm-yyyy/hh:mm:ss form length date/time in dd-mm-yyyy/hh:mm:ss form

10 10 Table Keys Primary Keys Primary Keys Uniqueness Uniqueness NOT NULL NOT NULL Minimum Number of columns Minimum Number of columns Foreign Keys Foreign Keys Used to relate rows of two or more tables 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 Foreign key at the child table should be of the same type (and values) of the primary key at the parent table

11 11 Example of relationships OFFLOC 01RIYAD 02JEDDAH Table LOCATION Parent CRSOFFODATE 7010121-SEP-91 7010216-NOV-91 7020126-NOV-91 7020201-FEB-92 Table Offer Child Primary Key Foreign Key Comes from LOCATION.OFF

12 12 Example of Database Tables COURSE (CRS, CTITLE, CHRS, CHRSW) COURSE (CRS, CTITLE, CHRS, CHRSW) OFFER (CRS, OFF, ODATE) OFFER (CRS, OFF, ODATE) LOCATION (OFF, LOC) LOCATION (OFF, LOC)

13 13 Questions Determine suitable data types and lengths for fields in previous tables 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? 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. Note data of persons are ID, Name, and address. Determine also primary and foreign key for new tables and suitable field types and widths. Determine also primary and foreign key for new tables and suitable field types and widths.

14 14 Selecting data from the table General Form General Form Select Column Names From Table Name [ Where Conditions] /* Optional where */ Examples for Examples for COURSE (CRS, CTITLE, CHRS, CHRSW) COURSE (CRS, CTITLE, CHRS, CHRSW) SELECT CRS, CTITLE FROM COURSE;

15 15 Example Continued SELECT CTITLE, CHRSW FROM COURSE; SELECT * FROM COURSE; SELECT CTITLE, CHRSW FROMCOURSE WHERECRS = 706;

16 16 Example Continued SELECT * FROMCOURSE WHERECHRS >= 60 AND CHRSW = 15; SELECT CTITLE, CHRSW FROMCOURSE WHERE CHRS >= 60 and CHRS = 60 and CHRS < =100;

17 17 Continued SELECT CTITLE, CHRSW FROMCOURSE WHERE CHRS BETWEEN 60 and 100; SELECT CTITLE, CHRSW FROMCOURSE WHERE CHRS >= 60 OR CRS = 706;

18 18


Download ppt "1 SQL Tarek El-Shishtawy Professor Ass. Of Computer Engineering."

Similar presentations


Ads by Google