SlideShare a Scribd company logo
Page 1 of 10


                   Content Mapping of Database & SQL




                                                   DATABASE AND SQL




              Database Concepts                                       Structured Query Language




Relational Data Model         Relational Algebra      DDL                      DML                   DCL




                                   Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 2 of 10


                               Database Concepts
What is Database ?
A database is the collection of data in an organized manner.
Advantages of Database over file system
   • Data Independence can be achieved i.e. data and programs that manipulate the
       data are two different entities.
   • The amount of data redundancy(repetition of data) in stored data can be reduced.
   • Stored Data can be shared by single or multiple users.
   • Data Integrity (i.e. when database contains only accurate data) can be maintained
   • Data Security can be easily implemented.

Relational Data Model:
  Relational data model is introduced by C.F.Codd in 1970.
      The relational data model describes the world as “a collection of inter-related
relations (or tables) “.

 In other way ,in relational data model the data is organized into tables (i.e rows and
columns) these tables are called relations.

The relational model has provided basis for:

      Research on theory of data/relationship/constraint
      Numerous database design methodologies
      The standard database access language SQL
      Almost all modern commercial database management systems

Fundamental concepts in Relational Data Model

1. Domain: A domain D is the original sets of atomic values used to model data.
     By atomic, we mean that each value in the domain is indivisible as far as the
relational model is concerned.
For example:
     The domain of day shift is the set of all possible days : {Mon, Tue, Wed…}

2. Tuple : The row of tables (relations ) are generally referred as tuples.

3. Attribute : The columns of the table (relation) are generally referred as attributes

4. Relation (Relation state): Relation can be viewed as a “table”. In that table, each row
represents a tuple of data values and each column represents an attribute.
         A relation is also a subset of the Cartesian product of a list of domains
characterised by a name..
Given n domains denoted by D1, D2, …, Dn , r is a relation defined on these domains if
r⊆D1×D2×...×Dn



                            Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 3 of 10



           5. The degree of a relation is the number of attributes n of its relational schema.
           6. The cardinality of a relation is the number of tuples present in a relation.




     Figure : EMPLOYEE Relational Data Model

           Key:

           Key is able to specify how one row is distinguished from another row in a relation.

           Primary Key :

           It is one or more columns in a table used to uniquely identify each row in the table.A
           single column primary key is called as Simple Key.A multicolumn primary key is called
           Composite Key.

                Ex: student rollno in Student table(rollno,name,marks)

           Candidate Key:

            It is, a minimal set of columns that can be used to identify a single row.

           In Employee table(employeeID, Name, job, departmentID) would be {employeeID,
           Name}, {employeeID, Name, job}, and {employeeID, Name, job, departmentID}. So,
           employeeID is a candidate key..

           employeeID              Name                    job                     departmentID
           12460                   Pawan kumar             Mgr                     10
           23909                   Aarti Sharma            Clerk                   20
           65809                   Navneet Thakre          Asst. Mgr.              30
Candidate Key




                                       Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 4 of 10

    Alternate Key:

    In the candidate key the key which is not primary key is the alternate key.
    In above table (name,job) can be taken as an alternate key.
    Relational Algebra Concepts:

    Relational Algebra:
     It is a Relational Database language which provides notations for
    deriving information from the permanent relations in the database.
           Relational Algebra uses some specialized operators to extract data from database.
    The operators are :
    (sigma)
    (pi)
    (rho)
   X(Cartesian product)
   U (union)
    (intersection)
   - (difference)

 The SELECT Operation ( (sigma))
     SELECT operation is an unary operation and is used to select a subset of the tuples
  from a relation that satisfy the selection condition.
  Syntax : <selection condition> (R)
   -> used to denote the SELECT operator
  <selection condition> -> is a boolean expression.

    e.g.
    Retrieve from the STUDENT relation the tuples for those students having level >= 2
    Result : LEVEL>=2 (STUDENT)

      The PROJECT Operation ( (pi))
    It is a unary operator which projects out a vertical subset of a given relation
    Syntax : <attribute list> (R)

    Query : Retrieve the room numbers of lecturers
    Result : ROOM# (LECTURER)

     The RENAME Operation ( (rho))
    When sequence of operations are exercised on the relations, then in many cases, the
    relation names and attribute names are renamed
    Query : Rename the STUDENT(S#,SNAME,LEVEL) to LEARNER
    (ROLL,NAME,DEGREE)

    Result : LEARNER(ROLL,NAME,DEGREE) (STUDENT)




                                Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 5 of 10

•     UNION (U): The set of tuples that are in relation R or in relation S, or in both is
        denoted by R U S or (R union S)

     Roll      Name       Marks           Roll        Name       Marks
      11      Mounika      65              12         Pallavi     94
      14      Mousumi      89              14        Mousumi      89

    Relation R                                     Relation S

                       Roll   Name       Marks
                        11   Mounika       65
                        12    Pallavi      94
                        14   Mousumi       89       R US
•     INTERSECTION (): The set of tuples that are in R and also in S and is denoted by
      RS or (R intersect S)
                             Roll      Name       Marks
                              14      Mousumi      89
                               RS


•     SET DIFFERENCE (-) : The set of tuples that are in R but not in S is denoted by (R
         minus S) or R-S
                      Roll       Name        Marks
                       11       Mounika       65


     CARTESIAN PRODUCT (X)

  Let R and S be relations of degree n1 and n2 respectively. Then the Cartesian Product of
R and S, is of degree n1+n2. It is denoted by R X S

Suppose relation R is R(A1,A2,…,An) and relation S is S(B1,B2,….,Bm) then Q=R X S
is Q(A1,A2,…An,B1,B2,….Bm)

If R has k1 tuples and S has k2 tuples then R X S will have k1*k2 tuples.



Roll          Name       Marks                            Age             Class
12            Pallavi    94                               17               XII
 14           Mousumi 89                                  14               IX
                 Relation R                                         Relation S




                              Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 6 of 10



       Roll            Name                Marks                Age                Class
        12             Pallavi               94                 17                  XII
        12             Pallavi               94                 14                  IX
        14            Mousumi                89                 17                  XII
        14            Mousumi                89                 14                  IX
                                       Relation R X S

                     STRUCTURED QUERY LANGUAGE

SQL is a standardized query language that provides interface to relational database
system..The SQL has provided a set of commands to access data from the Relational
Database.

Basic structure of SQL

   •    SELECT Clause : It corresponds the Projection operation of the relational algebra.
        It is used to list the attributes required in the result of a query.

   •    FROM Clause : It corresponds to the Cartesian Product operation of the relational
        algebra. It lists the relations to be used for getting the result.

   •    WHERE Clause : It corresponds to the selection predicate of the relational
        algebra. It consists of a predicate involving attributes of the relations that appear
        in the from clause.
                  The syntax is
                        Select <Column list>
                           From <Table name>
                           Where <Conditions>

Advantages of SQL:

       It is a Non-procedural Language
       It reduces the amount of time required for creating and manipulating tables.
       It is a language for all users
       SQL provides easy to learn commands
       It is a common Language for all relational databases.

Components of SQL:
   DDL ( Data Definition Language)
   DML ( Data Manipulation Language)
   DCL ( Data Control Language)

   DDL :
   It is a set of SQL commands used to create,modify and delete database structures.
            Ex: Create, Alter, Drop, Truncate, Rename


                            Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 7 of 10

    DML:
    The Data Manipulation Language (DML) is used to retrieve, insert and modify database information.
                   Ex: Insert, Update, Delete, Select
    DCL :
    It is the component of SQL statement that control access to the data.
                Ex: Commit ,Rollback , Savepoint ,Grant ,Revoke


DATA TYPES IN SQL:
Data types come in several forms and sizes.
i.e. Char ,Varchar,date,number etc.

       •    NUMBER(p,s) : To store zero,positive and negative fixed and floating point
            numbers with magnitude between 1.0X10-130 and 9.9999…X 10125
       •    DATE Datatype : used to store date and time information. For each DATE
            value the following information are stored
                          • century
                          • Year
                          • Month
                          • Day
                          • Hour
                          • Minute
                          • Second
           CHAR Datatype : fixed length character string.
       •    VARCHAR2           : variable length character string
       •    VARCHAR            : synonymous with VARCHAR2 datatype

                                  DATA TYPES
Sl.No.          Datatype                                        Description
   1       VARCHAR2(size) Variable length character with maximum size 2000. Size is in bytes
   2       NUMBER(p,s)         Number having precision p and scale s. p from 1 to 38 and s from –84
                               to 127
   3       LONG                Character data type of variable length upto 2 GB or 231 - 1
   4       DATE                Valid range from January 1, 4721 BC to December 31, 4712 AD
   5       RAW(size)           Raw binary data length size bytes. Maximum 255 bytes
   6       ROWID               Hexadecimal string datatype returned by ROWID pseudocolumn.
   7       CHAR(size)          Fixed length character data of maximum 255

Constraints:
A constraint is a given restriction on a field or a set of fields of a table.

Types of Constraint :




                              Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 8 of 10

1. Unique Constraint                                   2.Primary Key Constraint
3. Default Constraint                                  4.Check Constraint

                              SQL Commands
DDL Commands:
   Create Statement:
        Used to create a table.
  Syntax:
             Create table <tablename>
        (<column name1> datatype[ <size>], <column name2> datatype[
  <size>],…..);
  e.g.
    1. create table employee(empcode number(5) ,empname varchar2(20),dob date);
    2. create table emp1
          (empno number constraint pk_emp1 primary key,
           ename varchar2(10) constraint nn_ename1 not null, job varchar2(9));

     Alter Statement:
      Used to modify the structure of a table .
      Syntax:
             Alter table <tablename>add/modify/drop(<column name1> datatype[
   <size>]);
      E.X- 1. alter table employee add (address varchar2(30) not null);
           2. alter table employee modify (address varchar2(40));
           3. alter table employee drop column address;

      Drop Statement:
        Used to drop the structure of a table .
        Syntax:
              Drop table <tablename>;
        E.X- 1. Drop table employee;
      Truncate Statement:
       Used to delete all the records from a table .
       Syntax:
              Truncate table <tablename>;
        E.X- 1. Truncate table empl;

    Rename Statement:
     Used to give another name to a table .
     Syntax:
            Rename table <tablename>;
      E.X- 1. Rename table empl;
DML Commands :
   Insert Statement:


                           Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 9 of 10

      Used to insert records into a table .
      Syntax: insert into <tablename>[<column list>] values (<value>,<value>….);

       E.X- 1. insert into employee values(555,'RAM SHANKAR','25-JUN-1982');

     Update Statement:
      Used to modify or edit records into a table .
      Syntax:
             Update <tablename> set values ;

       E.X- 1. update employee set empname='Ram Gopal' where empcode=7934;

      Delete Statement:
       Used to delete records from a table .
       Syntax:
             delete from <tablename>[where <predicate>];

       E.X- 1. Delete from employee where empno=’e001’;
             2. Delete from empl;

SELECT Commands :

      Select Statement:
       Used to retrive rows or columns from a table .
       Syntax:
             Select <Column list>From <Table name>Where <Conditions>

       E.X- 1. select empno,ename,job,mgr from emp where empno=7934;
  Distinct keyword
       E.X-Select distinct sal from emp;
  IN keyword
       E.X- select * from emp where job in ('CLERK','ANALYST');
     Equivalent to
             select * from emp where job ='CLERK’ or job='ANALYST';
  Between keyword
       E.X- select * from emp where sal between 3000 and 5000;
  Group By Clause
       E.X- select * from emp group by deptno;
  Having Clause
       E.X- select empno,ename,job,mgr from emp having MAX(sal)>2000;
  Order By Clause
       E.X- select empno,ename,job,mgr from emp order by hiredate desc;


  SQL Functions:
   Sum: to find sum total value of a given attribute.



                         Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 10 of 10

           select SUM(sal) "total" from emp;
    Max : Returns the maximum value of the expression
           select MAX(sal) "Maximum" from emp;
 Min: Returns the Minimum value of the expression
          select MIN(hiredate) "Minimum" from emp;
 Avg : returns average value of a group of records for one attribute
         select avg(sal) "Average" from emp;
 Count : Returns the number of rows in the query
        select count(*) "Count" from emp;
JOINS :
Join is a mechanism which will fetch data from multiple tables.There are 4 types of
join.
1.Equi Join :-Which will fetch data from multiple tables when a common attribute is
available among the tables.
E.X 1:-Select ename, deptno, loc from EMP, DEPT where EMP.deptno=
DEPT.deptno;
2.Non-Equi Join:- Which will fetch data from multiple tables when no common
attribute is available among the tables.
Select ename, deptno, sal,grade from EMP, SALGRADE where EMP.sal between
SALGRADE .losal and SALGRADE .hisal;
VIEW :-
View is a virtual image of original table.
E.X-Create view myview as select * from DEPT ;




                      Prepared By Sumit Kumar Gupta, PGT Computer Science

More Related Content

Viewers also liked (15)

Pointers
PointersPointers
Pointers
 
Functions
FunctionsFunctions
Functions
 
2-D array
2-D array2-D array
2-D array
 
File handling
File handlingFile handling
File handling
 
1-D array
1-D array1-D array
1-D array
 
01 computer communication and networks v
01 computer communication and networks v01 computer communication and networks v
01 computer communication and networks v
 
Stack
StackStack
Stack
 
c++ program for Canteen management
c++ program for Canteen managementc++ program for Canteen management
c++ program for Canteen management
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Queue
QueueQueue
Queue
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 

Similar to Unit 3

4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
LPhct2
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
Jafar Nesargi
 

Similar to Unit 3 (20)

Lec02
Lec02Lec02
Lec02
 
4 the sql_standard
4 the  sql_standard4 the  sql_standard
4 the sql_standard
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part i
 
3 relational model
3 relational model3 relational model
3 relational model
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
On_Mapping_Relational_Databases_to_RDF_and_SHACL.pdf
On_Mapping_Relational_Databases_to_RDF_and_SHACL.pdfOn_Mapping_Relational_Databases_to_RDF_and_SHACL.pdf
On_Mapping_Relational_Databases_to_RDF_and_SHACL.pdf
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]
 
Bab 6 pendukung
Bab 6 pendukungBab 6 pendukung
Bab 6 pendukung
 
Ch2
Ch2Ch2
Ch2
 
Cs501 rel algebra
Cs501 rel algebraCs501 rel algebra
Cs501 rel algebra
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
DBMS _Relational model
DBMS _Relational modelDBMS _Relational model
DBMS _Relational model
 
Ch3
Ch3Ch3
Ch3
 
Ch3
Ch3Ch3
Ch3
 
uniT 4 (1).pptx
uniT 4 (1).pptxuniT 4 (1).pptx
uniT 4 (1).pptx
 
RDBMS
RDBMSRDBMS
RDBMS
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
 

More from Swarup Kumar Boro (11)

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2
 
computer science sample papers 3
computer science sample papers 3computer science sample papers 3
computer science sample papers 3
 
computer science sample papers 1
computer science sample papers 1computer science sample papers 1
computer science sample papers 1
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Boolean algebra laws
Boolean algebra lawsBoolean algebra laws
Boolean algebra laws
 
Class
ClassClass
Class
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Physics
PhysicsPhysics
Physics
 
Physics activity
Physics activityPhysics activity
Physics activity
 

Unit 3

  • 1. Page 1 of 10 Content Mapping of Database & SQL DATABASE AND SQL Database Concepts Structured Query Language Relational Data Model Relational Algebra DDL DML DCL Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 10 Database Concepts What is Database ? A database is the collection of data in an organized manner. Advantages of Database over file system • Data Independence can be achieved i.e. data and programs that manipulate the data are two different entities. • The amount of data redundancy(repetition of data) in stored data can be reduced. • Stored Data can be shared by single or multiple users. • Data Integrity (i.e. when database contains only accurate data) can be maintained • Data Security can be easily implemented. Relational Data Model: Relational data model is introduced by C.F.Codd in 1970. The relational data model describes the world as “a collection of inter-related relations (or tables) “. In other way ,in relational data model the data is organized into tables (i.e rows and columns) these tables are called relations. The relational model has provided basis for:  Research on theory of data/relationship/constraint  Numerous database design methodologies  The standard database access language SQL  Almost all modern commercial database management systems Fundamental concepts in Relational Data Model 1. Domain: A domain D is the original sets of atomic values used to model data. By atomic, we mean that each value in the domain is indivisible as far as the relational model is concerned. For example:  The domain of day shift is the set of all possible days : {Mon, Tue, Wed…} 2. Tuple : The row of tables (relations ) are generally referred as tuples. 3. Attribute : The columns of the table (relation) are generally referred as attributes 4. Relation (Relation state): Relation can be viewed as a “table”. In that table, each row represents a tuple of data values and each column represents an attribute. A relation is also a subset of the Cartesian product of a list of domains characterised by a name.. Given n domains denoted by D1, D2, …, Dn , r is a relation defined on these domains if r⊆D1×D2×...×Dn Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 10 5. The degree of a relation is the number of attributes n of its relational schema. 6. The cardinality of a relation is the number of tuples present in a relation. Figure : EMPLOYEE Relational Data Model Key: Key is able to specify how one row is distinguished from another row in a relation. Primary Key : It is one or more columns in a table used to uniquely identify each row in the table.A single column primary key is called as Simple Key.A multicolumn primary key is called Composite Key. Ex: student rollno in Student table(rollno,name,marks) Candidate Key: It is, a minimal set of columns that can be used to identify a single row. In Employee table(employeeID, Name, job, departmentID) would be {employeeID, Name}, {employeeID, Name, job}, and {employeeID, Name, job, departmentID}. So, employeeID is a candidate key.. employeeID Name job departmentID 12460 Pawan kumar Mgr 10 23909 Aarti Sharma Clerk 20 65809 Navneet Thakre Asst. Mgr. 30 Candidate Key Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 10 Alternate Key: In the candidate key the key which is not primary key is the alternate key. In above table (name,job) can be taken as an alternate key. Relational Algebra Concepts: Relational Algebra: It is a Relational Database language which provides notations for deriving information from the permanent relations in the database. Relational Algebra uses some specialized operators to extract data from database. The operators are :   (sigma)   (pi)   (rho)  X(Cartesian product)  U (union)   (intersection)  - (difference)  The SELECT Operation ( (sigma)) SELECT operation is an unary operation and is used to select a subset of the tuples from a relation that satisfy the selection condition. Syntax : <selection condition> (R)  -> used to denote the SELECT operator <selection condition> -> is a boolean expression. e.g. Retrieve from the STUDENT relation the tuples for those students having level >= 2 Result : LEVEL>=2 (STUDENT)  The PROJECT Operation ( (pi)) It is a unary operator which projects out a vertical subset of a given relation Syntax : <attribute list> (R) Query : Retrieve the room numbers of lecturers Result : ROOM# (LECTURER)  The RENAME Operation ( (rho)) When sequence of operations are exercised on the relations, then in many cases, the relation names and attribute names are renamed Query : Rename the STUDENT(S#,SNAME,LEVEL) to LEARNER (ROLL,NAME,DEGREE) Result : LEARNER(ROLL,NAME,DEGREE) (STUDENT) Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 5. Page 5 of 10 • UNION (U): The set of tuples that are in relation R or in relation S, or in both is denoted by R U S or (R union S) Roll Name Marks Roll Name Marks 11 Mounika 65 12 Pallavi 94 14 Mousumi 89 14 Mousumi 89 Relation R Relation S Roll Name Marks 11 Mounika 65 12 Pallavi 94 14 Mousumi 89 R US • INTERSECTION (): The set of tuples that are in R and also in S and is denoted by RS or (R intersect S) Roll Name Marks 14 Mousumi 89 RS • SET DIFFERENCE (-) : The set of tuples that are in R but not in S is denoted by (R minus S) or R-S Roll Name Marks 11 Mounika 65  CARTESIAN PRODUCT (X) Let R and S be relations of degree n1 and n2 respectively. Then the Cartesian Product of R and S, is of degree n1+n2. It is denoted by R X S Suppose relation R is R(A1,A2,…,An) and relation S is S(B1,B2,….,Bm) then Q=R X S is Q(A1,A2,…An,B1,B2,….Bm) If R has k1 tuples and S has k2 tuples then R X S will have k1*k2 tuples. Roll Name Marks Age Class 12 Pallavi 94 17 XII 14 Mousumi 89 14 IX Relation R Relation S Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 6. Page 6 of 10 Roll Name Marks Age Class 12 Pallavi 94 17 XII 12 Pallavi 94 14 IX 14 Mousumi 89 17 XII 14 Mousumi 89 14 IX Relation R X S STRUCTURED QUERY LANGUAGE SQL is a standardized query language that provides interface to relational database system..The SQL has provided a set of commands to access data from the Relational Database. Basic structure of SQL • SELECT Clause : It corresponds the Projection operation of the relational algebra. It is used to list the attributes required in the result of a query. • FROM Clause : It corresponds to the Cartesian Product operation of the relational algebra. It lists the relations to be used for getting the result. • WHERE Clause : It corresponds to the selection predicate of the relational algebra. It consists of a predicate involving attributes of the relations that appear in the from clause. The syntax is Select <Column list> From <Table name> Where <Conditions> Advantages of SQL:  It is a Non-procedural Language  It reduces the amount of time required for creating and manipulating tables.  It is a language for all users  SQL provides easy to learn commands  It is a common Language for all relational databases. Components of SQL:  DDL ( Data Definition Language)  DML ( Data Manipulation Language)  DCL ( Data Control Language) DDL : It is a set of SQL commands used to create,modify and delete database structures. Ex: Create, Alter, Drop, Truncate, Rename Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 7. Page 7 of 10 DML: The Data Manipulation Language (DML) is used to retrieve, insert and modify database information. Ex: Insert, Update, Delete, Select DCL : It is the component of SQL statement that control access to the data. Ex: Commit ,Rollback , Savepoint ,Grant ,Revoke DATA TYPES IN SQL: Data types come in several forms and sizes. i.e. Char ,Varchar,date,number etc. • NUMBER(p,s) : To store zero,positive and negative fixed and floating point numbers with magnitude between 1.0X10-130 and 9.9999…X 10125 • DATE Datatype : used to store date and time information. For each DATE value the following information are stored • century • Year • Month • Day • Hour • Minute • Second  CHAR Datatype : fixed length character string. • VARCHAR2 : variable length character string • VARCHAR : synonymous with VARCHAR2 datatype DATA TYPES Sl.No. Datatype Description 1 VARCHAR2(size) Variable length character with maximum size 2000. Size is in bytes 2 NUMBER(p,s) Number having precision p and scale s. p from 1 to 38 and s from –84 to 127 3 LONG Character data type of variable length upto 2 GB or 231 - 1 4 DATE Valid range from January 1, 4721 BC to December 31, 4712 AD 5 RAW(size) Raw binary data length size bytes. Maximum 255 bytes 6 ROWID Hexadecimal string datatype returned by ROWID pseudocolumn. 7 CHAR(size) Fixed length character data of maximum 255 Constraints: A constraint is a given restriction on a field or a set of fields of a table. Types of Constraint : Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 8. Page 8 of 10 1. Unique Constraint 2.Primary Key Constraint 3. Default Constraint 4.Check Constraint SQL Commands DDL Commands:  Create Statement: Used to create a table. Syntax: Create table <tablename> (<column name1> datatype[ <size>], <column name2> datatype[ <size>],…..); e.g. 1. create table employee(empcode number(5) ,empname varchar2(20),dob date); 2. create table emp1 (empno number constraint pk_emp1 primary key, ename varchar2(10) constraint nn_ename1 not null, job varchar2(9));  Alter Statement: Used to modify the structure of a table . Syntax: Alter table <tablename>add/modify/drop(<column name1> datatype[ <size>]); E.X- 1. alter table employee add (address varchar2(30) not null); 2. alter table employee modify (address varchar2(40)); 3. alter table employee drop column address;  Drop Statement: Used to drop the structure of a table . Syntax: Drop table <tablename>; E.X- 1. Drop table employee;  Truncate Statement: Used to delete all the records from a table . Syntax: Truncate table <tablename>; E.X- 1. Truncate table empl;  Rename Statement: Used to give another name to a table . Syntax: Rename table <tablename>; E.X- 1. Rename table empl; DML Commands :  Insert Statement: Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 9. Page 9 of 10 Used to insert records into a table . Syntax: insert into <tablename>[<column list>] values (<value>,<value>….); E.X- 1. insert into employee values(555,'RAM SHANKAR','25-JUN-1982');  Update Statement: Used to modify or edit records into a table . Syntax: Update <tablename> set values ; E.X- 1. update employee set empname='Ram Gopal' where empcode=7934;  Delete Statement: Used to delete records from a table . Syntax: delete from <tablename>[where <predicate>]; E.X- 1. Delete from employee where empno=’e001’; 2. Delete from empl; SELECT Commands :  Select Statement: Used to retrive rows or columns from a table . Syntax: Select <Column list>From <Table name>Where <Conditions> E.X- 1. select empno,ename,job,mgr from emp where empno=7934; Distinct keyword E.X-Select distinct sal from emp; IN keyword E.X- select * from emp where job in ('CLERK','ANALYST'); Equivalent to select * from emp where job ='CLERK’ or job='ANALYST'; Between keyword E.X- select * from emp where sal between 3000 and 5000; Group By Clause E.X- select * from emp group by deptno; Having Clause E.X- select empno,ename,job,mgr from emp having MAX(sal)>2000; Order By Clause E.X- select empno,ename,job,mgr from emp order by hiredate desc; SQL Functions:  Sum: to find sum total value of a given attribute. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 10. Page 10 of 10 select SUM(sal) "total" from emp;  Max : Returns the maximum value of the expression select MAX(sal) "Maximum" from emp;  Min: Returns the Minimum value of the expression select MIN(hiredate) "Minimum" from emp;  Avg : returns average value of a group of records for one attribute select avg(sal) "Average" from emp;  Count : Returns the number of rows in the query select count(*) "Count" from emp; JOINS : Join is a mechanism which will fetch data from multiple tables.There are 4 types of join. 1.Equi Join :-Which will fetch data from multiple tables when a common attribute is available among the tables. E.X 1:-Select ename, deptno, loc from EMP, DEPT where EMP.deptno= DEPT.deptno; 2.Non-Equi Join:- Which will fetch data from multiple tables when no common attribute is available among the tables. Select ename, deptno, sal,grade from EMP, SALGRADE where EMP.sal between SALGRADE .losal and SALGRADE .hisal; VIEW :- View is a virtual image of original table. E.X-Create view myview as select * from DEPT ; Prepared By Sumit Kumar Gupta, PGT Computer Science