Download presentation
Presentation is loading. Please wait.
Published byJunior Francis Modified over 8 years ago
1
Introduction to Database SEM I, AY 2011-12 Department of Information Technology Salalah College of Technology Chapter No.3 SQL
2
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.
3
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
4
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)
5
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,477.5808 and 922,337,203,685,477.5807. DATETIME8 bytesA date or time value between the years 100 and 9999. REAL4 bytesA single-precision floating-point value with a range of – 3.402823E38 to – 1.401298E-45 for negative values, 1.401298E-45 to 3.402823E38 for positive values, and 0. FLOAT8 bytesA double-precision floating-point value with a range of – 1.79769313486232E308 to – 4.94065645841247E-324 for negative values, 4.94065645841247E-324 to 1.79769313486232E308 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.
6
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…],.... );
7
DDL (Data Definition Language) CREATE TABLE Persons ( P_ID integer, LastName char(50), FirstName char(50), Address char(255), City char(255) );
8
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 );
9
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) );
10
DDL (Data Definition Language) DROP COMMAND SYNTAX DROP TABLE table EXAMPLE drop table student Drop table Employee
11
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
12
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’);
13
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;
14
DML ( Data Manipulation Language) DELETE SYNTAX: DELETE FROM table WHERE condition; Example: Delete from TBLDEPT Where dept_id = 213;
15
DQL (Data Query Language) SELECT Syntax: SELECT fields… FROM table WHERE condition; Example: SELECT * FROM tblDept; SELECT department, DateofStart FROM TBLDEPT;
16
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
17
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
18
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
19
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;
20
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;
21
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 ;
22
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;
23
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
24
Introduction to SQL SQL SELECT Statement Another SQL SELECT Examples SELECT * FROM Persons SELECT DISTINCT Example SELECT DISTINCT City FROM Persons 24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.