Introduction to Database SEM I, AY 2011-12 Department of Information Technology Salalah College of Technology Chapter No.3 SQL.

Slides:



Advertisements
Similar presentations
COMP 5531 Introduction to MySQL. SQL SQL is a standard language for accessing and managing databases. SQL stands for Structured Query Language.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
Virtual training week 4 structured query language (SQL)
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Sundara Ram Matta Apr 01 st, Sundara Ram Matta Apr 01 st, 2015
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
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.
SQL FUNDAMENTALS SQL ( Structured Query Language )
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Structured Query Language 1. Data Definition Language (DDL) is used to manage table and define data structure i.e. CREATE, ALTER, DROP Data Control.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating 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.
Fox MIS Spring 2011 Database Week 5 SQL basics SELECT, INSERT, UPDATE, DELETE.
Lab_03: Basic SQL.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
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.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Chapter 11 Database and SQL. Flat Files and Databases Flat files Databases Advantages Efficient use of resources Access control Disadvantages Security.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Web Systems & Technologies
From: SQL From:
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
مقدمة في قواعد البيانات
Introduction To Structured Query Language (SQL)
SQL Queries Chapter No 3.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Introduction To Structured Query Language (SQL)
Structured Query Language
PHP and MySQL.
The University of Akron College of Applied Science & Technology Dept
Structured Query Language Path from Unorganized to Organized….
Presentation transcript:

Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL

SQL (Structured Query Language) Definition: Structured Query Language is a database computer language designed for managing data in relational database management systems (RDBMS). SQL was developed at IBM in the early 1970s.

SQL (Structured Query Language) What Can SQL do? – SQL can execute queries against a database – SQL can retrieve data from a database – SQL can insert records in a database – SQL can update records in a database – SQL can delete records from a database – SQL can create new databases – SQL can create new tables in a database etc. 3

Languages of SQL SQL commands are divided into four Languages. 1. DDL ( Data Definition Language) (CREATE, ALTER, DROP) 2. DML (Data Manipulation Language) (INSERT, UPDATE, DELETE) 3. DQL ( Data Query Language) (SELECT) 4. DCL (Data Control Language) (COMMIT, ROLLBACK)

SQL Data Types Data typeStorage sizeDescription BIT1 byteYes and No values and fields that contain only one of two values. MONEY8 bytesA scaled integer between – 922,337,203,685, and 922,337,203,685, DATETIME8 bytesA date or time value between the years 100 and REAL4 bytesA single-precision floating-point value with a range of – E38 to – E-45 for negative values, E-45 to E38 for positive values, and 0. FLOAT8 bytesA double-precision floating-point value with a range of – E308 to – E-324 for negative values, E-324 to E308 for positive values, and 0. SMALLINT2 bytesA short integer between – 32,768 and 32,767. (See Notes) INTEGER4 bytesA long integer between – 2,147,483,648 and 2,147,483,647. (See Notes) TEXT2 bytes per character (See Notes) Zero to a maximum of 2.14 gigabytes.

DDL (Data Definition Language) (CREATE, ALTER, DROP) CREATE TABLE table_name ( column_name1 data_type [constraints…], column_name2 data_type [constraints…], column_name3 data_type [constraints…],.... );

DDL (Data Definition Language) CREATE TABLE Persons ( P_ID integer, LastName char(50), FirstName char(50), Address char(255), City char(255) );

DDL (Data Definition Language) Example: Create table tbldept ( Dept_id smallint primary key, Dept_Name text(50) not null, Dateofstart datetime ); Create table tblpos ( Pos_code smallint primary key, position_Name text(50) not null );

DDL (Data Definition Language) HOW TO ADD FOREIGN KEY (RELATIONSHIP) Create table empTbl ( emp_id smallint primary key, emp_Name text(50) not null, dept_id smallint references deptTbl, pos_code smallint references posTbl(pos_code) );

DDL (Data Definition Language) DROP COMMAND SYNTAX DROP TABLE table EXAMPLE drop table student Drop table Employee

DDL (Data Definition Language) ALTERCOMMAND SYNTAX ALTER TABLE table ADD COLUMN field type (size) DROP COLUMN field ALTER COLUMN field type (size) ADD PRIMARY KEY (field) Example ALTER TABLE Employees ADD COLUMN Notes TEXT(25) ALTER TABLE Employees DROP COLUMN Salary

DML ( Data Manipulation Language) (INSERT, UPDATE, DELETE) INSERT Syntax: INSERT INTO TABLE (FIELD1, FIELD2…….FIELDN) VALUES (VALUE1,VALUE2……..VALUEN); Example: INSERT INTO TBLDEPT (Dept_id, Dept_Name, Dateofstart) VALUES (212,’IT’,’12/12/2001’); INSERT INTO TBLDEPT (Dept_id, Dept_Name) VALUES (213,’bUSINESS’); SHORTCUT FOR: INSERT ALL FIELDS INSERT INTO TABLE VALUES (VALUE1,VALUE2……..VALUEN); INSERT INTO DEPTBL VALUES (212,’IT’,’12/12/2001’);

DML ( Data Manipulation Language) UPDATE TABLE Syntax: SET FIELDNAME1=VALE1, FIELDNAME2=VALE2,….. WHERE FIELDNAME=VALUE….; Example: UPDATE TBLDEPT SET DATEOSSTART=#21/08/2010# WHERE Dept_id =213;

DML ( Data Manipulation Language) DELETE SYNTAX: DELETE FROM table WHERE condition; Example: Delete from TBLDEPT Where dept_id = 213;

DQL (Data Query Language) SELECT Syntax: SELECT fields… FROM table WHERE condition; Example: SELECT * FROM tblDept; SELECT department, DateofStart FROM TBLDEPT;

DQL (Data Query Language) SELECT SELECT * FROM Persons WHERE City=“Sandnes” SELECT department, DateofStart FROM tblDept WHERE department like "M*" and department like "*S"; 16 SELECT DISTINCT Example SELECT DISTINCT City FROM Persons

OPERATORS IN SQL OPERATORS Allowed in the WHERE Clause OperatorDescription =Equal <>Not equal >Greater than <Less than >=Greater than or equal <=Less than or equal BETWEENBetween an inclusive range LIKESearch for a pattern INIf you know the exact value you want to return for at least one of the columns Note: In some versions of SQL the <> operator may be written as != 17

OPERATORS IN SQL SQL AND & OR Operators The AND & OR Operators The AND operator displays a record if both the first condition and the second condition is true. The OR operator displays a record if either the first condition or the second condition is true. AND Operator Example SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson' OR Operator Example SELECT * FROM Persons WHERE FirstName='Tove' OR FirstName='Ola' 18

ORDER BY CLAUSE Syntax: SELECT field_1, field_2….. FROM table WHERE condition Order by field [asc|desc] Example: SELECT department, DateofStart FROM tblDept WHERE department like "*S" Order by DateofStart;

Join Query Examples: – SELECT tblEmp.emp_id, tblEmp.name_1, tblEmp.name_4 FROM tblEmp, tblposWHERE tblEmp.pos_code=tblPos.pos_code; – SELECT tblEmp.emp_id, tblEmp.name_1, tblEmp.name_4, tblpos.position FROM tblEmp, tblposWHERE tblEmp.Gender and tblEmp.pos_code=tblPos.pos_code – SELECT tblEmp.emp_id, tblEmp.name_1, tblEmp.name_4, tblpos.position, tblDept.department FROM tblEmp, tblpos, tblDeptWHERE tblEmp.gender="m" And tblEmp.pos_code=tblPos.pos_code And tblEmp.dept_code=tblDept.dept_code;

GROUP BY CLAUSE Syntax: SELECT field_1, field_2,… AGG_Fun(field_n) FROM table WHERE Condition GROUP BY field_1, field_2,… Order by field [asc|desc],…. Example: Q.1 Display no. of Male employee in each department. Sort the result by department in Z-A order. SELECT tblDept.department, Count(tblEmp.emp_id) AS CountOfemp_id FROM tblemp, tblDept WHERE tblDept.dept_code=tblEmp.dept_code And tblEmp.gender="m“ GROUP BY tblDept.department ORDER BY tblDept.department DESC ;

GROUP BY CLAUSE (Continued) Q.2 Display no. of Male employee in each department having the number less than 5 SELECT tblDept.department, Count(tblEmp.emp_id) AS CountOfemp_id FROM tblemp, tblDept WHERE tblDept.dept_code=tblEmp.dept_code And tblEmp.gender="m" GROUP BY tblDept.department HAVING Count(tblEmp.emp_id)<5 ORDER BY tblDept.department DESC;

Introduction to SQL SQL CREATE TABLE Statement CREATE TABLE Example Now we want to create a table called "Persons" that contains five columns: P_ID, LastName, FirstName, Address, and City. We use the following CREATE TABLE statement: CREATE TABLE Persons ( P_ID integer, LastName char(50), FirstName char(50), Address char(255), City char(255) ) 23

Introduction to SQL SQL SELECT Statement Another SQL SELECT Examples SELECT * FROM Persons SELECT DISTINCT Example SELECT DISTINCT City FROM Persons 24