Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Using the Set Operators
1 Term 2, 2004, Lecture 6, Views and SecurityMarian Ursu, Department of Computing, Goldsmiths College Views and Security 3.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
Introduction to SQL Tuning Brown Bag Three essential concepts.
SQL Tuning Briefing Null is not equal to null but null is null.
© Abdou Illia MIS Spring 2014
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Yong Choi School of Business CSU, Bakersfield
Data Definition Language (DDL)
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
SQL Rohit Khokher.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Virtual training week 4 structured query language (SQL)
Structured Query Language and its components. SQL SQL stands for Structured Query Language. There is a standard SQL called the American National Standards.
Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Introduction to Structured Query Language (SQL)
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
Database Lecture # 1 By Ubaid Ullah.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL.
ASP.NET Programming with C# and SQL Server First Edition
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
ORACLE SQL. Overview Personal DBMS Vs Client/Server DBMS Oracle 8 Environment SQL – syntax and examples PL/SQL-introduction.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
Introduction to SEQUEL. What is SEQUEL? Acronym for Structural English Query Language Acronym for Structural English Query Language Standard language.
Oracle Database Administration Lecture 2 SQL language.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
SQL FUNDAMENTALS SQL ( Structured Query Language )
Quick review of SQL And conversion to Oracle SQL.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 CONACT UC:  Magnific training   
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
 Reviewing basic architecture concepts  Oracle 10g Architecture  Main features of 9i and 10g
1 Copyright © 2005, Oracle. All rights reserved. Oracle Database Administration: Overview.
CS 3630 Database Design and Implementation
Interacting with the Oracle Server
“Introduction To Database and SQL”
DATABASE MANAGEMENT SYSTEM
مقدمة في قواعد البيانات
SQL Fundamentals in Three Hours
SQL .. An overview lecture3.
Introduction To Structured Query Language (SQL)
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Lecuter-1.
Presentation transcript:

Introduction to SQL Ahmet Oezcimen

Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP command structures 6. SELECT, INSERT, UPDATE & DELETE command structures 7. Operators 8. Functions 9. Basic SQL statements 10. Advanced SQL statements 11. Tips and techniques for better SQL 12. Tools using SQL 13. SQL*Plus 14. Literature 15. Questions

1. What is SQL? SQL stands for Structured Query Language SQL is an ANSI (American National Standards Institute) standard language SQL works with Oracle, Sybase, Informix, MS SQL Server, DB2, MySQL, etc.. SQL is easy to learn

2. SQL data types VARCHAR2 – contains text string up to 4,000b NUMBER - contains numeric data DATE – contains date data BLOB – Large binary object up to 4G CLOB – Large character-based object uo to 4G BFILE – Large external file Etc …

3. Statement categories in SQL DDL ( Data Definition Language ) –CREATE, ALTER, DROP, etc… DML ( Data Manipulation Language ) –SELECT, UPDATE, DELETE, INSERT Transaction Control Commands –COMMIT, ROLLBACK, SAVEPOINT, etc… Session Control Commands –ALTER SESSION DCL (Data Control Language) –GRANT, REVOKE, etc… Etc…

4. Data modelling example Department (deptno) Job (jobno) Interview (intno) Candidate (cno) Explanation: A deparment might have more than one job therefore also might invite more than one candidate and for one job might more than one interview and one candidate might be invited several times.

4. Data modelling example (cont.) Deparment : deptno number(5), deptname varchar2(25), depthead varchar2(35) Job : jobno number(5), jobtitle varchar2(25),salary number(5), deptno number(5) Candidate: cno number(5), cname varchar2(25), deptno number(5), ctel varchar2(20), position varchar(25) Interview: intno number(5), intdate date, jobno number(5), cno number(5)

5. CREATE & DROP command structures CREATE TABLE (,.., ) TABLESPACE ; DROP TABLE ;

6. SELECT, INSERT, UPDATE & DELETE command structures SELECT [distinct],.., FROM [alias],.., [alias] WHERE GROUP BY,.., HAVING CONNECT BY PRIOR START WITH ORDER BY,.., [ASC or DESC]; DELETE FROM WHERE ; INSERT INTO [( )] VALUES (value list); UPDATE SET = [WHERE ];

7. Operators Arithmetical operators –/, *, +, -, =, !=, <>, >, =, <= Boolean operators –NOT, NOR, AND, OR Logical operators –IN, EXISTS, LIKE

8. Functions Text functions –Lpad(x,y [,z]), rpad(x,y [,z]), lower(x), upper(x), initcap(x), length(x), substr(x,y[,z]),instr(x,y), trim(), replace(x,y [,z]) Math functions –Abs(x), ceil(x), floor(x), mod(x,y), round(x,y), sign(x), sqrt(x), trunc(x,y), vsize(x), Nvl(), decode(), greatest(x,y,…), least(x,y,…) Date functions –Sysdate, add_months(x,y), last_day(x), months_between(x,y), new_time(x,y,z), next_day(x) Conversion functions –To_char(x), to_number(x), to_date(x), etc… Group functions –avg(x), count(x), max(x), min(x), sum(x), etc…

9. Basic SQL statement The most easiest SQL is just to select a table –Select * from department; –select deptname, depthead from department; Conditional Selection –Select * from department where depthead=‘Alvis Brazma’; Relational operators –Select * from job where salary > 2000; More complex conditions –Select * from job where deptno=1 or deptno=2; –Select * from job where deptno=3 and salary > 2000; Using IN & BETWEEN –Select * from job where deptno in (1,2); –Select * from job where deptno between 1 and 3; Using LIKE –Select * from department where depthead like ‘Alvis%’;

10. Advanced SQL statements Joins & outer joins –Select * from department, job where department.deptno=job.deptno; –select * from department a, job b where a.deptno=b.deptno(+); Group by –Select max(salary) from job; –Select jobtitle,max(salary) from job group by jobtitle; Subqueries –select * from job where deptno in (select deptno from department); Exists & all –select * from job where exists (select * from department); –select * from job where salary >= all (select salary from job); Union –select deptno,deptname from department union select jobno,jobtitle from job; Intersect & minus –select deptno from job minus select deptno from job where jobbtitle=‘DBA’; –select jobtitle from job where deptno=1 intersect select jobtitle from job where deptno=2;

11. Tips and techniques for better SQL Index will not be used when a function is performed on an indexed field. SELECT * FROM department WHERE SUBSTR(DEPTNAME, 1, 3) = ‘Arr’

11. Tips and techniques for better SQL (cont.) If you cannot avoid using a function: –Use the INDEX hint. SELECT /*+ INDEX(DEPARTMENT DEPT_IDX_1) */ * FROM DEPARTMENT WHERE SUBSTR(DEPTNAME, 1, 3) = ‘Arr’

Replace SELECT * FROM job WHERE SALARY = :NEWSALARY with SELECT * FROM job WHERE SALARY = :NEWSALARY Tips and techniques for better SQL (cont.)

Indexes are not used, and a full table scan is done when the WHERE clause contains: != (NOT EQUALS) LIKE '%SA%'

11. Tips and techniques for better SQL (cont.) Depending on the range of the numbers in a BETWEEN, the optimizer will choose to do a full table scan or use the index. SELECT * FROM job WHERE salary BETWEEN 2500 AND 3500

11. Tips and techniques for better SQL (cont.) When retrieving a large portion of the table’s data, full table scans are likely to offer the best performance. When retrieving small number of rows, avoid full table scans.

11. Tips and techniques for better SQL (cont.) Sometimes DO disable the index. SELECT * FROM job WHERE SALARY + 0 = '10000' SELECT * FROM department WHERE deptname || '' = ‘A'

11. Tips and techniques for better SQL (cont.) Select the smallest table or smallest result set first. SELECT a.deptname FROM department a, job b WHERE a.deptno=b.deptno (assume a.deptno and b.deptno are indexed)

11. Tips and techniques for better SQL (cont.) Small table should drive the large table. If job is a large table and department is a small one. Disable index on department. This changes the table driving path. Replace SELECT * FROM job large, department small WHERE large.deptno = small.deptno With SELECT * FROM job large, department small WHERE large.deptno = small.deptno || ''

11. Tips and techniques for better SQL (cont.) A table join is normally better than sub-query Replace: SELECT * FROM job a WHERE a.deptno IN (SELECT b.deptno FROM department b) With: SELECT A.* FROM job A, department B WHERE A.job = B.department

These statements have the same result. Assume table job, department relationship is one to many. SELECT * FROM job WHERE deptno IN (SELECT deptno FROM department) SELECT * FROM job WHERE EXISTS (SELECT deptno FROM department WHERE job.deptno = department.deptno) 11. Tips and techniques for better SQL (cont.)

Use IN operator IN checks all rows. Only use IN if the table in the subquery is extremely small. SELECT * FROM job WHERE deptno IN (SELECT deptno FROM department)

11. Tips and techniques for better SQL (cont.) Use EXISTS operator EXISTS makes use of the index on the deptno column in the department table. For large tables, it may be faster. SELECT * FROM job WHERE EXISTS (SELECT deptno FROM department WHERE job.deptno = department.deptno)

11. Tips and techniques for better SQL (cont.) If A.STATE and B.STATE have a unique index, Replace: SELECT a.deptno, b.deptno FROM department a, job b WHERE a.deptno = b.deptno (+) With: SELECT a.deptname, b.jobtitle FROM department a, job b WHERE a.deptno = b.deptno UNION ALL SELECT c.deptname, NULL FROM department c WHERE NOT EXISTS (SELECT 'X' FROM job d WHERE c.deptno = d.deptno)

11. Tips and techniques for better SQL (cont.) Replace: SELECT * FROM department a, job b, candidate c WHERE a.deptno=b.deptno and b.deptno=c.deptno and c.deptno=1 With: SELECT * FROM A,B,C FROM department a, job b, candidate c WHERE a.deptno=b.deptno and b.deptno=c.deptno and a.deptno=1 and b.deptno=1 and c.deptno=1

12. Tools using SQL Oracle SQL*Plus Oracle PL/SQL Oracle Forms Oracle Reports Oracle ProC Java-JDBC Perl-CGI etc …

13. SQL*Plus Starting SQL*Plus –Sqlplus [ / ] ] ] Running a script –SQL>Start Exiting SQL*Plus –exit Some important SQL*Plus commands –Host or !, set, spool, rem, accept, or start, commit, rollback, help, desc, ttitle, btitle, break, etc …

14. Literature Oracle8i The complete reference Autors:Kevin Loney, George Koch Learning SQL Autors: Richard W.Earp,Sikha S.Bagui Oracle SQL and PL/SQL Handbook Autors: John Adolph Palinski

Glossary –Table: department DeptnoDeptnameDepthead 2MSDKim Henrick 3Swiss-ProtRolf Apweiler 4TrEMBLRolf Apweiler 1ArrayExpressAlvis Brazma 5PersonnelKeith Wiliamson

Glossary - Table: job JobnoJobtitleSalaryDeptno 6Database coordinator Database curator Software Engineer WEB Developer DBA Database coordinator Database curator Software Engineer WEB Developer DBA Database coordinator Database curator Software Engineer WEB Developer DBA Database coordinator Database curator Software Engineer WEB Developer DBA20001

Glossary - Table: candidate CnoCnameCtelPositionDeptno 1Richard Database coordinator 1 2Don Database coordinator 1 3Donald Database coordinator 1 4Marie Database coordinator 1 5Elisa Database coordinator 1

Glossary - Table: interview IntnoIntdateJobnoCno :00:00.011

Exercises 1.Very simple queries 2.Queries by using WHERE condition 3.Queries by using ORDER BY 4.Queries by using functions 5.Queries by using joins 6.Queries by using subqueries 7.Queries by using GROUP BY 8.Using queries for any other database (ARRAYS, EXPERIMENTS, PROTOCOLS)

Some AE examples - Show me arrays that use PCR products: Select i.identifier as array, o.value as technology From tt_identifiable i, tt_physicalarraydesign a, tt_featuregroup f, tt_ontologyentry o where i.id = a.id and a.id = f.t_arraydesign_id and f.technologytype_id = o.id and lower( o.value ) like '%pcr%'

Some AE examples - Show me all the bibliographic references that are books: select b.title as Title, b.authors as Author from tt_bibliographicreference b, tt_parameters_t_bibliogra l, tt_ontologyentry o where b.id = l.t_bibliographicreference_id and l.parameters_id = o.id and lower( o.value ) like '%book%'

Some AE examples - Produce a table of all the experiments and the arrays: select distinct ie.identifier as experiment, ia.identifier as arraydesign from tt_identifiable ie, tt_experiment e, tt_bioassays_t_experiment eb, tt_physicalbioassay pba, tt_bioassaycreation bac, tt_array a, tt_identifiable ia where ie.id = e.id and e.id = eb.t_experiment_id and eb.bioassays_id = pba.id and pba.bioassaycreation_id = bac.id and bac.array_id = a.id and ia.id = a.arraydesign_id order by ie.identifier asc

Some AE examples -Produce a table of all the experiments and the species they study: select distinct i.identifier as experiment, o.value as species from tt_identifiable i, tt_biomaterials_experiments eb, tt_characteris_t_biomateri bo, tt_ontologyentry o where i.id = eb.experiments_id and eb.biomaterials_id = bo.t_biomaterial_id and bo.characteristics_id = o.id and o.category like '%species%' order by i.identifier