WORABE POLYTECHNIC COLLEGE
INFORMATION COMMUNICATION
                TECHNOLOGY DEPARTMENT
   DATABASE ADMINISTRATION LEVEL IV
             ADVANCED STRUCTURED QUERY
                     LANGUAGE
                                                 Compiled by Aliyan Aman
Sunday, June 9, 2019   Compiled by Aliyan Aman
                                                  Sunday, May 26, © 2019   1
                                   Contents
Write ADVANCED SQL                 WriteADVANCED SQL                 Write ADVANCED SQL
statement to retrieve and sort     statements that use               statements that use
data                               functions                         aggregation and filtering
 SQL             SQL In SQL         Over                      ORDER Identifying Special SQL
 Wildcar         SQL    outer       viewing of                BY    functions
 ds              Betwee Join        SQL                             Identifying Mathematical
                 n                  Functions                       functions
                                                                    Identifying String functions
 SQL Top SQL              SQL       Sorting                   GROUP Identifying Date functions
 SQL Like Inner           Union     commands                  BY    Identifying Aggregate
          Join            SQL                                       functions
          SQL             Select   SQL Joins                        proc
          Alias           Into
   Sunday, June 9, 2019             Compiled by Aliyan Aman                               2
       Structural Query Language
• SQL is Structured Query Language, which is a computer language for storing,
   manipulating and retrieving/querying data stored in relational database
   management system.
• SQL, is a standardized computer language that was originally developed by IBM
   for querying, altering and defining relational databases, using declarative
   statements.
• All relational database management systems like MySQL, MS Access, Oracle,
   Sybase, Informix, postgres and SQL Server use SQL as standard database
   language.
• SQL is pronounced /ˌɛs kjuː ˈɛl/ (letter by letter) or /ˈsiːkw l/ (as a word).
Many standards out there:
• ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), ….
• Vendors support various subsets
Also, they are using different dialects, such as:
• MS SQL Server using T-SQL,
• Oracle using PL/SQL,
  Sunday, June 9, 2019               Compiled by Aliyan Aman                     3
• MS Access version of SQL is called JET SQL (native format) etc.
                             Why SQL?
• Allows users to access/retrieve data in relational database
  management systems.
• Allows users to describe the data.
• Allows users to define the data in database and manipulate that data.
• Allows to embed within other languages using SQL modules, libraries
  & pre-compilers.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views
• SQL can execute queries against 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 views in a database
   Sunday, June 9, 2019            Compiled by Aliyan Aman              4
                   SQL(Structural Query Language)
SQL Languages
1. Data Definition Language (DDL):
  Allows DBA or user to describe and name entitles, attributes and
   relationships required for the application.
  Language used to define each data element required by the
   organization.
  Commands for setting up schema or the intension of database
  These commands are used to setup a database, create, delete and
   alter table with the facility of handling constraints
  are used to define the database structure or schema.
  Sunday, June 9, 2019        Compiled by Aliyan Aman            5 Aliyan A.
                             Examples
• CREATE - Creates a new table, a view of a table, or other
  object in database
• ALTER - modifies the structure an existing object in various
  ways—for example, adding a column to an existing table.
• DROP - Deletes an entire table, a view of a table or other object
  in the database.
• TRUNCATE - remove all records from a table, including all
  spaces allocated for the records are removed
• COMMENT - add comments to the data dictionary
• RENAME
    Sunday, June 9, 2019
                        - rename an object
                                        Compiled by Aliyan Aman
                                                            6
              2. Data Manipulation Language (DML)
• Is a core command used by end-users and programmers to store, retrieve, and access the data in the
  database e.g. SQL
• The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete
  data.
• Since the required data or Query by the user will be extracted using this type of language, it is also
  called "Query Language"
• The acronym CRUD refers to all of the major functions that need to be implemented in a relational
  database application to consider it complete.
• Each letter in the acronym can be mapped to a standard SQL statement:
Examples
•   SELECT - retrieve data from the a database
•   INSERT - insert data into a table
•   UPDATE - updates existing data within a table
•   DELETE - deletes all records from a table, the space for the records remain
•   LOCK   TABLE
       Sunday,          - control concurrency
               June 9, 2019                     Compiled by Aliyan Aman                 7
                          3.Data control languages
DCL commands are used to control access to data.
1:- GRANT - gives user's access privileges to database
• Allows a DBA to define access control and privileges for
  users.
• It is a mechanism for implementing security at a
  database object level.
2:- REVOKE - withdraw access privileges given with the
GRANT command
   Sunday, June 9, 2019            Compiled by Aliyan Aman   8
                                                 9
Sunday, June 9, 2019   Compiled by Aliyan Aman
                       SQL Data Types
In MS SQL there are three main data types :
1. Text data type
2. Number data type
3. Date/Time data type.
Sunday, June 9, 2019       Compiled by Aliyan Aman   10
                 Text data type:
Data type                Description
char(size)               Holds a fixed length string (can contain letters,and special
                         characters). The fixed size is specified in parenthesis. Can
                         store up to 255 characters
Varchar(size)            Holds a variable length string (can contain letters, numbers,
                         and special characters). The fixed size is specified in
                         parenthesis. Can store up to 8000 characters
Tintext                  Holds a string with a maximum length of 255 characters
Text                     Holds a string with a maximum length of 65,535 characters
Mediumtext               Holds a string with a maximum length of 16,777,215
                         characters
Longtext
  Sunday, June 9, 2019   Holds a string with    abymaximum
                                         Compiled  Aliyan Aman length of 4,294,967,295 11
                         characters
                                     Number types:
Data type                 Description
TINYINT(size)             -128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits
                          may be specified in parenthesis
SMALLINT(size)            -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number
                          of digits may be specified in parenthesis
MEDIUMINT(size)           -8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum
                          number of digits may be specified in parenthesis
INT(size)                 -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The
                          maximum number of digits may be specified in parenthesis
BIGINT(size)
FLOAT(size,d)             A small number with a floating decimal point. The maximum number of
                          digits may be specified in the size parameter. The maximum number of digits
                          to the right of the decimal point is specified in the d parameter
   Sunday, June 9, 2019                      Compiled by Aliyan Aman                                    12
DOUBLE(size,d)
                       3. Date types:
   Data type  Description
   DATE()     A date. Format: YYYY-MM-DD
   DATETIME() A date and time combination. Format: YYYY-MM-
              DD HH:MM:SS
   TIME()
   YEAR()
Sunday, June 9, 2019         Compiled by Aliyan Aman          13
What is RDBMS?
• RDBMS stands for Relational Database Management System. RDBMS is the
  basis for SQL and for all modern database systems like MS SQL Server, IBM
  DB2, Oracle, MySQL, and Microsoft Access.
A Relational database management system (RDBMS) is a database management
system (DBMS) that is based on the relational model as introduced by E. F. Codd.
                           Creating a database
• We can create databases either by using commands or by wizard in ms sql.
• To create databases use the following syntax:
Create database database_name;
 Eg. Create database CIT;
• Now the database is created by the name CIT
    Sunday, June 9, 2019            Compiled by Aliyan Aman                 14
Sunday, June 9, 2019   Compiled by Aliyan Aman   15
Sunday, June 9, 2019   Compiled by Aliyan Aman   16
   Working on tables
•Tables are the basic structure where data is stored
in the database.
•The table is a collection of related data entries and
it consists of columns and rows.
•The data in RDBMS is stored in database objects
called tables. Many database tools allow you to
create tables without writing SQL, but given that
tables are the container of all the data, it is
important to include the CREATE
TABLE syntax.
•Remember, a table is the most common and
simplest form of data storage in a relational
database.
      Sunday, June 9, 2019           Compiled by Aliyan Aman   17
                 Table name                                                Attribute names
                                        Tables in SQL
            Product
                             PName         Price                Category       Manufacturer
                             Gizmo        $19.99                Gadgets        GizmoWorks
                          Powergizmo      $29.99                Gadgets        GizmoWorks
                          SingleTouch     $149.99            Photography           Canon
                          MultiTouch      $203.99             Household            Hitachi
                       Tuples or rows
Sunday, June 9, 2019                      Compiled by Aliyan Aman                             18
           What is field?                               What is record or row?
• Every table is broken up into smaller entities
  called fields.                                        • A record, also called a row of
• A field is a column in a table that is designed to
  maintain specific information about every record        data, is each individual entry
  in the table.                                           that exists in a table. For
• A field is another name for column of the table.
• The fields in the CUSTOMERS table consist of            example, there are 7 records
  ID, NAME, AGE, ADDRESS and SALARY.
• For example, a column in the CUSTOMERS table
                                                          in the above CUSTOMERS
  is ADDRESS, which represents location                   table. Following is a single
  description and would consist of the following:
          ADDRESS
                                                          row of data or record in the
          Ahmedabad                                       CUSTOMERS table:
          Delhi
                                                              1 Ramesh   32   Ahmedabad   2000.0
          Kota
          Mumbai
          Bhopal
          MP
     Sunday, June 9, 2019                Compiled by Aliyan Aman                              19
                          Tables Explained
              • The schema of a table is the table name and
                its attributes:
              Product(PName, Price, Category, Manfacturer)
              • A key is an attribute whose values are unique;
                we underline a key
              Product(PName, Price, Category, Manfacturer)
Sunday, June 9, 2019            Compiled by Aliyan Aman          20
                       What is NULL value?
• A NULL value in a table is a value in a field that appears to
  be blank, which means a field with a NULL value is a field
  with no value.
• It is very important to understand that a NULL value is
  different than a zero value or a field that contains spaces. A
  field with a NULL value is one that has been left blank
  during record creation.
Sunday, June 9, 2019        Compiled by Aliyan Aman            21
                                SQL Constraints
• Constraints are the rules enforced on data columns on table. These are used to limit the
  type of data that can go into a table. This ensures the accuracy and reliability of the data
  in the database.
• Constraints could be column level or table level. Column level constraints are applied
  only to one column, whereas table level constraints are applied to the whole table.
 NOT NULL Constraint: Ensures that a column cannot have NULL value.
 DEFAULT Constraint: Provides a default value for a column when none is specified.
 UNIQUE Constraint: Ensures that all values in a column are different.
 PRIMARY Key: Uniquely identified each rows/records in a database table.
 FOREIGN Key: Uniquely identified a rows/records in any another database table.
 CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
  certain conditions.
 INDEX: Use to create and retrieve data from the database very quickly.
     Sunday, June 9, 2019               Compiled by Aliyan Aman                           22
                         NOT NULL Constraint:
• By default, a column can hold NULL values. If you do not
  want a column to have a NULL value, then you need to
  define such constraint on this column specifying that NULL
  is now not allowed for that column.
• A NULL is not the same as no data, rather, it represents
  unknown data.
• Example:
• For example, the following SQL creates a new table called
  CUSTOMERS and adds five columns, three of which, ID and
  NAME and AGE, specify not to accept NULLs:
  Sunday, June 9, 2019         Compiled by Aliyan Aman         23
     CUSTOMERS table example
•   CREATE TABLE CUSTOMERS(
•   ID INT NOT NULL,
•   NAME VARCHAR (20) NOT NULL,
•   AGE INT NOT NULL,
•   ADDRESS CHAR (25) ,
•   SALARY DECIMAL (18, 2),
•   PRIMARY KEY (ID)
•   );      If CUSTOMERS table has already been created, then to add a NOT
                            NULL constraint to SALARY column in Oracle and MySQL, you
                            would write a statement similar to the following:
                            ALTER TABLE CUSTOMERS
                            ALTER SALARY FLOAT NOT NULL;
     Sunday, June 9, 2019                      Compiled by Aliyan Aman                  24
          DEFAULT Constraint:
• The DEFAULT constraint provides a default value to a
  column when the INSERT INTO statement does not
  provide a specific value.
• Example:
• For example, the following SQL creates a new table
  called CUSTOMERS and adds five columns. Here,
  SALARY column is set to 5000.00 by default, so in
  case INSERT INTO statement does not provide a
  value for this column. then by default this column
  would be set to 5000.00.
Sunday, June 9, 2019       Compiled by Aliyan Aman       25
Default constraint -example
•   CREATE TABLE CUSTOMERS(
•   ID INT NOT NULL,              If CUSTOMERS table has already been
•   NAME VARCHAR (20) NOT NULL, created, then to add a DFAULT constraint to
                                  SALARY column, you would write a statement
•   AGE INT NOT NULL,             similar to the following using alter statement:
•   ADDRESS CHAR (25) ,           ALTER TABLE CUSTOMERS
                                  MODIFY SALARY float DEFAULT
•   SALARY float DEFAULT 5000.00, 5000.00;
•   PRIMARY KEY (ID)
•   );
                Drop Default Constraint:
                To drop a DEFAULT constraint, use the following SQL:
                ALTER TABLE CUSTOMERS
                ALTER COLUMN SALARY DROP DEFAULT;
     Sunday, June 9, 2019                   Compiled by Aliyan Aman           26
UNIQUE Constraint:
 • The UNIQUE Constraint prevents two records from having
   identical values in a particular column. In the
   CUSTOMERS table, for example, you might want to
   prevent two or more people from having identical age.
 • For example, the following SQL creates a new table called
   CUSTOMERS and adds five columns. Here, AGE column
   is set to UNIQUE, so that you can not have two records
   with same age:
 Sunday, June 9, 2019    Compiled by Aliyan Aman           27
Unique constraint
                           If CUSTOMERS table has already been created,
    example                then to add a UNIQUE constraint to AGE
                           column, you would write a statement similar to
CREATE TABLE CUSTOMERS(    the following:
                           ALTER TABLE CUSTOMERS
ID INT NOT NULL,           ALTER AGE INT NOT NULL UNIQUE;
NAME VARCHAR(20) NOT NULL, You can also use following syntax, which
                           supports naming the constraint in multiple
AGE INT NOT NULL UNIQUE, columns as well:
ADDRESS CHAR (25),         ALTER TABLE CUSTOMERS
                           ADD CONSTRAINT myUniqueConstraint
SALARY float,              UNIQUE(AGE, SALARY);
PRIMARY KEY (ID)                       DROP a UNIQUE Constraint:
);                                     To drop a UNIQUE constraint, use the following
                                       SQL:
                                       ALTER TABLE CUSTOMERS
                                       DROP CONSTRAINT myUniqueConstraint;
   Sunday, June 9, 2019        Compiled by Aliyan Aman                             28
                          PRIMARY Key:
• A primary key is a field in a table which uniquely identifies each
  row/record in a database table.
• Primary keys must contain unique values.
• A primary key column cannot have NULL values.
• A table can have only one primary key, which may consist of
  single or multiple fields. When multiple fields are used as a
  primary key, they are called a composite key.
• If a table has a primary key defined on any field(s), then you can
  not have two records having the same value of that field(s).
• Note: You would use these concepts while creating database
  tables.
   Sunday, June 9, 2019       Compiled by Aliyan Aman             29
  Create Primary Key:                            To create a PRIMARY KEY constraint on the "ID" column when
                                                 CUSTOMERS table already exists, use the following SQL syntax:
                                                 ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID);
Here is the syntax to define ID                  NOTE: If you use the ALTER TABLE statement to add a primary
attribute as a primary key in a                  key, the primary key column(s) must already have been declared
                                                 to not contain NULL values (when the table was first created).
CUSTOMERS table.                                     For defining a PRIMARY KEY constraint on multiple
CREATE TABLE CUSTOMERS(                              columns, use the following SQL syntax:
                                                     CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
                                                     ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,                          NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,                                    AGE INT NOT NULL,
ADDRESS CHAR (25) ,                                  ADDRESS CHAR (25) ,
SALARY float,                                        SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)                                     PRIMARY KEY (ID, NAME)
                                                     );
);
                                                     To create a PRIMARY KEY constraint on the "ID" and
Delete Primary Key:                                  "NAMES" columns when CUSTOMERS table already
You can clear the primary key constraints from the   exists, use the following SQL syntax:
table, Use Syntax:                                   ALTER TABLE CUSTOMERS
ALTER TABLE CUSTOMERS DROP PRIMARY KEY;              ADD CONSTRAINT PK_CUSTID PRIMARY KEY (ID,
                                                     NAME);
       Sunday, June 9, 2019                   Compiled by Aliyan Aman                                       30
             FOREIGN Key:
• A foreign key is a key used to link two tables together. This is
  sometimes called a referencing key.
• Foreign Key is a column or a combination of columns whose
  values match a Primary Key in a different table.
• The relationship between 2 tables matches the Primary
  Key in one of the tables with a Foreign Key in the second
  table.
• If a table has a primary key defined on any field(s), then you
  can not have two records having the same value of that
  field(s).
                                Compiled by Aliyan Aman              31
         Sunday, June 9, 2019
    Example:                                 ORDERS table:
Consider the structure of the                CREATE TABLE ORDERS (
two tables as follows:                       ID INT NOT NULL,
                                             DATE DATETIME,
CUSTOMERS table:                             CUSTOMER_ID INT foreign key(ID)
CREATE TABLE CUSTOMERS(                      references CUSTOMERS(ID),
                                             AMOUNT double,
ID INT NOT NULL,                             PRIMARY KEY (ID));
NAME VARCHAR (20) NOT
NULL,                  If ORDERS table has already been created, and the foreign key has not yet
AGE INT NOT NULL, been      set, use the syntax for specifying a foreign key by altering a table.
                       ALTER TABLE ORDERS
ADDRESS CHAR(25), ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);
SALARY float,          DROP a FOREIGN KEY Constraint:
PRIMARY KEY (ID) To drop a FOREIGN KEY constraint, use the following
                       SQL:
);                     ALTER TABLE ORDERS
                                DROP FOREIGN KEY;
     Sunday, June 9, 2019               Compiled by Aliyan Aman                           32
  CHECK Constraint:                                   If CUSTOMERS table has already
The CHECK Constraint enables a condition to           been created, then to add a CHECK
check the value being entered into a record. If the constraint to AGE column, you would
condition evaluates to false, the record violates the write a statement similar to the
constraint and isn’t entered into the table.
                                                      following:
For example, the following SQL creates a new        ALTER TABLE CUSTOMERS
table called CUSTOMERS and adds five columns.       ALTER AGE INT NOT NULL CHECK (AGE
Here, we add a CHECK with AGE column, so that       >= 18 );
you can not have any CUSTOMER below 18              You can also use following syntax, which
years:                                              supports naming the constraint in multiple
CREATE TABLE CUSTOMERS(                             columns as well:
ID INT NOT NULL,                                    ALTER TABLE CUSTOMERS
NAME VARCHAR (20) NOT NULL,                         ADD CONSTRAINT myCheckConstraint
AGE INT NOT NULL CHECK (AGE >= 18),                 CHECK(AGE >= 18);
ADDRESS CHAR (25) ,                                 DROP a CHECK Constraint:
SALARY DECIMAL (18, 2),                             To drop a CHECK constraint, use the following
PRIMARY KEY (ID)                                    SQL. This syntax does not work with MySQL:
);                                                  ALTER TABLE CUSTOMERS
                                                    DROP CONSTRAINT myCheckConstraint;
     Sunday, June 9, 2019             Compiled by Aliyan Aman                               33
                            Best practice:
• When creating tables you should consider following guidelines:
• Tables: Use upper case and singular form in table names – not plural, e.g.,
“STUDENT” (not students)
• Columns: Use Pascal notation, e.g., “StudentId”
Primary Key:
• If the table name is “COURSE”, name the Primary Key column “CourseId”,
   etc.
• “Always” use Integer and Identity(1,1) for Primary Keys. Use UNIQUE
• constraint for other columns that needs to be unique, e.g. RoomNumber
• Specify Required Columns (NOT NULL) – i.e., which columns that need to
   have data or not
• Standardize on few/these Data Types: int, float, varchar(x), datetime, bit
• Use English for table and column names
• Avoid abbreviations! (Use RoomNumber – not RoomNo, RoomNr, ...)
    Sunday, June 9, 2019        Compiled by Aliyan Aman                 34
                       Data Integrity
The following categories of the data integrity exist with each
RDBMS:
• Entity Integrity : There are no duplicate rows in a table.
• Domain Integrity : Enforces valid entries for a given column
  by restricting the type, the format, or the range of values.
• Referential Integrity : Rows cannot be deleted which are
  used by other records.
• User-Defined Integrity : Enforces some specific business
  rules that do not fall into entity, domain, or referential
  integrity.
Sunday, June 9, 2019     Compiled by Aliyan Aman            35
Advanced SQL
• SQL TOP Clause
• The TOP clause is used to specify
  the number of records to return.         Syntax
                                           SELECT TOP number|percent
• The TOP clause can be very               column_name(s)
  useful on large tables with               FROM table_name
  thousands of records. Returning a
                                                SELECT *
  large number of records can
  impact on performance.                          FROM Persons
• Note: Not all database systems
  support the TOP clause/                         LIMIT 5
    Sunday, June 9, 2019    Compiled by Aliyan Aman                    36
    SQL TOP Example
                The "Persons" table:                                    Now we want to select only 50% of the
P_Id     LastName         FirstName     Address         City            records in the table above.
1        Hansen           Ola           Timoteivn 10    Sandnes         We use the following SELECT
2        Svendson         Tove          Borgvn 23       Sandnes         statement:
3        Pettersen        Kari          Storgt 20       Stavanger       SELECT TOP 50 PERCENT * FROM
4        Nilsen           Tom           Vingvn 23       Stavanger       Persons
    Now we want to select only the two first                            The result-set will look like this:
records in the table above.
                                                                    P_Id LastName FirstName Address         City
    We use the following SELECT statement:
     SELECT TOP 2 * FROM Persons                                    1      Hansen     Ola    Timoteivn 10   Sandnes
    The result-set will look like this:                             2      Svendson   Tove   Borgvn 23      Sandnes
P_Id   LastName       FirstName       Address          City
1      Hansen         Ola             Timoteivn 10 Sandnes
2      Svendson Tove                  Borgvn 23        Sandnes
       Sunday, June 9, 2019                              Compiled by Aliyan Aman                                   37
                           SQL LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a specified pattern in a
  column.
• The SQL LIKE clause is used to compare a value to similar values using
  wildcard operators.
• The LIKE operator is used to search for a specified pattern in a column.
• There are two wildcards used in conjunction with the LIKE operator:
   – The percent sign (%)
   – The underscore (_)
• The percent sign represents zero, one, or multiple characters. The
  underscore represents a single number or character. The symbols can be
  used in combinations.
    Sunday, June 9, 2019             Compiled by Aliyan Aman                           38
                                   Syntax:
• The basic syntax of % and _ is as follows:
SELECT column_name(s FROM table_name             General Syntax
WHERE column_name LIKE 'XXXX%'                   SELECT column_name(s)
• or
SELECT column_name(s FROM table_name             FROM table_name
WHERE column_name LIKE '%XXXX%'                  WHERE column_name LIKE
• or
SELECT column_name(s FROM table_name
                                                 pattern
WHERE column_name LIKE 'XXXX_'
• or
SELECT column_name(s FROM table_name                 You can combine N number of conditions
WHERE column_name LIKE '_XXXX'                       using AND or OR operators. Here,
• or                                                 XXXX could be any numeric or string
SELECT column_name(s FROM table_name                 value. SQL LIKE Syntax
WHERE column_name LIKE '_XXXX_'
  Sunday, June 9, 2019             Compiled by Aliyan Aman                               39
                                   Example:
Here are number of examples showing WHERE part having different LIKE clause with '%'
and '_‘
operators: Statement Description
WHERE SALARY LIKE '200%'         Finds any values that start with 200
WHERE SALARY LIKE '%200%'        Finds any values that have 200 in
                                 any position
WHERE SALARY LIKE '_00%'         Finds any values that have 00 in the
                                 second and third positions
WHERE SALARY LIKE '2_%_%'        Finds any values that start with 2 and
                                 are at least 3 characters in length
WHERE SALARY LIKE '%2'           Finds any values that end with 2
WHERE SALARY LIKE '_2%3'         Finds any values that have a 2 in the
                                 second position and end with a 3
WHERE SALARY LIKE '2___3'        Finds any values in a five-digit
                                 number that start with 2 and end with 3
    Sunday, June 9, 2019             Compiled by Aliyan Aman                       40
SQL Like Example                                                         To select the persons living in a city that does NOT contain the pattern "tav"
                                                                         from the "Persons" table, by using the NOT keyword.
                                                                          SELECT * FROM Persons
        The "Persons" table:                                              WHERE City NOT LIKE '%tav%'
                                                                         The result-set will look like this:
    ጰ   LastName      FirstName    Address        City
                                                                                    P_Id    LastName FirstName Address               City
    1   Hansen        Ola          Timoteivn 10   Sandnes                           1       Hansen   Ola       Timoteivn 10          Sandnes
    2   Svendson      Tove         Borgvn 23      Sandnes                           2       Svendson Tove      Borgvn 23             Sandnes
    3   Pettersen     Kari         Storgt 20      Stavanger                   To select the persons living in a city that ends with an
                                                                              "s" from the "Persons" table.
To select the persons living in a city that                                   SELECT * FROM Persons
starts with "s" from the table above.                                         WHERE City LIKE '%s'
                                                                              The result-set will look like this:
SELECT * FROM Persons                                                           P_Id       LastName FirstName               Address             City
 WHERE City LIKE 's%'                                                           1          Hansen        Ola                Timoteivn 10 Sandnes
The result-set will look like this:
                                                                                2          Svendson      Tove               Borgvn 23           Sandnes
P_Id    LastName     FirstName    Address           City              To select the persons living in a city that contains the pattern
                                                                      "tav" from the "Persons" table.
1       Hansen       Ola          Timoteivn 10 Sandnes                SELECT * FROM Persons
2       Svendson     Tove         Borgvn 23         Sandnes            WHERE City LIKE '%tav%'
                                                                      The result-set will look like this:
3       Pettersen    Kari         Storgt 20         Stavanger                   P_Id       LastName        FirstName          Address        City
                                                                                3      Pettersen           Kari               Storgt 20      Stavanger
        Sunday, June 9, 2019                                    Compiled by Aliyan Aman                                                              41
Sunday, June 9, 2019   Compiled by Aliyan Aman   42
Sunday, June 9, 2019   Compiled by Aliyan Aman   43
Sunday, June 9, 2019   Compiled by Aliyan Aman   44
Sunday, June 9, 2019   Compiled by Aliyan Aman   45
Sunday, June 9, 2019   Compiled by Aliyan Aman   46
SQL Sub Queries
• A Subquery or Inner query or Nested query is a query within another
  SQL query and embedded within the WHERE clause.
• A subquery is used to return data that will be used in the main query
  as a condition to further restrict the data to be retrieved.
• Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE
  statements along with the operators like =, <, >, >=, <=, IN, BETWEEN
  etc.
Sunday, June 9, 2019          Compiled by Aliyan Aman                 47
There are a few rules that subqueries must
follows:
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause, unless multiple
  columns are in the main query for the subquery to compare its selected columns.
• An ORDER BY cannot be used in a subquery, although the main query can use an
  ORDER BY. The GROUP BY can be used to perform the same function as the
  ORDER BY in a subquery.
• Subqueries that return more than one row can only be used with multiple value
  operators, such as the IN operator.
• The SELECT list cannot include any references to values that evaluate to a BLOB,
  ARRAY, CLOB, or NCLOB.
• A subquery cannot be immediately enclosed in a set function.
• The BETWEEN operator cannot be used with a subquery; however, the BETWEEN
  operator can be used within the subquery.
Sunday, June 9, 2019              Compiled by Aliyan Aman                        48
Subqueries with the SELECT Statement:
• Subqueries are most frequently used with the SELECT statement. The
  basic syntax is as follows:
        SELECT column_name [, column_name ]
        FROM table1 [, table2 ]
       WHERE column_name OPERATOR
       (SELECT column_name [, column_name ]
       FROM table1 [, table2 ]
       [WHERE])
Sunday, June 9, 2019               Compiled by Aliyan Aman         49
Example:
Consider the CUSTOMERS table having the following records:
ID NAME               AGE ADDRESS         SALARY              SQL> SELECT * FROM CUSTOMERS WHERE ID IN (SELECT
                                                              ID FROM CUSTOMERS WHERE SALARY > 4500) ;
1   Ramesh            35    Ahmedabad     2000.00
2   Khilan            25    Delshi        1500.00 output
3   Kaushik           23    Kota          2000.00  ID  NAME                     AGE    ADDRESS          SALARY
                                                   4   Chaitai                 25      Mumbai           6500
4   Chaitai           25    Mumbai        6500.00  5   Hardik                  27      Bhopal           8500
5   Hardik            27    Bhopal        8500.00  7   Muffy                   24      Indore           10000
6   Komal             22    MP            4500.00
7   Muffy             24    Indore        10000.00
     Sunday, June 9, 2019                          Compiled by Aliyan Aman                                 50
Subqueries with the INSERT Statement:
• Subqueries also can be used with INSERT statements. The INSERT
  statement uses the data returned from the subquery to insert into
  another table. The selected data in the subquery can be modified
  with any of the character, date or number functions.
• The basic syntax is as follows:
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Sunday, June 9, 2019         Compiled by Aliyan Aman                  51
Example:
• Consider a table CUSTOMERS_BKP with similar structure as
  CUSTOMERS table. Now to copy complete CUSTOMERS table
  into CUSTOMERS_BKP, following is the syntax:
• SQL> INSERT INTO CUSTOMERS_BKP
SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS) ;
Sunday, June 9, 2019    Compiled by Aliyan Aman          52
Subqueries with the UPDATE Statement:
• The subquery can be used in conjunction with the UPDATE
  statement. Either single or multiple columns in a table can be
  updated when using a subquery with the UPDATE statement.
• The basic syntax is as follows:
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Sunday, June 9, 2019        Compiled by Aliyan Aman                53
Example:
• Following example updates SALARY by                      This would impact two rows and finally
  0.25 times in CUSTOMERS table for all                    CUSTOMERS table would have the following
                                                           records:
  the customers whose AGE is greater
  than or equal to 27:
                                                           OUTPUT
SQL> UPDATE CUSTOMERS
                                                   ID       NAME       AGE   ADDRESS        SALARY
SET SALARY = SALARY * 0.25                         1        Ramesh    35     Ahmedabad      1.953125
                                                   2        Khilan    25     Delshi         1500
WHERE AGE IN (SELECT AGE                           3        Kaushik   23     Kota           2000
FROM CUSTOMERS                                     4        Chaitai   25     Mumbai         6500
                                                   5        Hardik    27     Bhopal         132.8125
WHERE AGE >= 27 );                                 6        Komal     22     MP             4500
                                                   7        Muffy     24     Indore         10000
    Sunday, June 9, 2019         Compiled by Aliyan Aman                                       54
Subqueries with the DELETE Statement:
• The subquery can be used in conjunction with the DELETE
  statement like with any other statements mentioned above.
• The basic syntax is as follows:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Sunday, June 9, 2019       Compiled by Aliyan Aman            55
  Example:
• Assuming, we have CUSTOMERS_BKP
  table available which is backup of
  CUSTOMERS table.
• Following example deletes records from
  CUSTOMERS table for all the customers
  whose AGE is greater than or equal to 27:            ID NAME AGE    ADDRESS SALARY
                                                       2 Khilan 25    Delshi 1500
• SQL> DELETE FROM CUSTOMERS                           3 Kaushik 23   Kota   2000
WHERE AGE IN (SELECT AGE FROM                          4 Chaitai 25   Mumbai 6500
                                                       6 Komal 22     MP     4500
CUSTOMERS_BKP                                          7 Muffy 24     Indore 10000
WHERE AGE > 27 );
• This would impact two rows and finally
  CUSTOMERS table would have the
  following records:
  Sunday, June 9, 2019       Compiled by Aliyan Aman                               56
                 SQL JOIN
• Selects data from two or more tables tied
  together by matching table columns.
•Used whenever we have to select data from 2
 or more tables.
•SQL JOIN syntax:
• SELECT [COLUMN 1], [COLUMN 2]
        FROM [TABLE 1] , [TABLE 2]
 WHERE [TABLE 1].[COLUMN NAME 1] JOIN
  OPERATOR [TABLE 2].[COLUMN NAME 2]
Sunday, June 9, 2019        Compiled by Aliyan Aman   57
                  Contd…
• Example: to display who has ordered a product and what did they ordered
  from Employees and Orders tables, using emp_id as a joining coulumn.
                SELECT Employees.Name, Orders.product
                  FROM Employees INNER JOIN Orders
                  ON Employees.emp_id=Orders.emp_id
• 2 SQL CROSS JOIN
• will return all records where each row from the first table is combined with
  each row from the second table.
• Which also mean CROSS JOIN returns the Cartesian product of the sets
  of rows from the joined tables.
• SQL CROSS JOIN syntax:
        SELECT * FROM [TABLE 1] CROSS JOIN [TABLE 2]
 Sunday, June 9, 2019             Compiled by Aliyan Aman                        58
Sunday, June 9, 2019   Compiled by Aliyan Aman   59
SQL INNER JOIN
• For two tables that want to join together need
  to have some data in common, like unique id
• SQL INNER JOIN syntax:
      SELECT *FROM [TABLE 1] INNER JOIN
                    [TABLE 2]
     ON [TABLE 1].[COLUMN NAME 1] = [TABLE
              2].[COLUMN NAME 2]
Sunday, June 9, 2019   Compiled by Aliyan Aman     60
Sunday, June 9, 2019   Compiled by Aliyan Aman   61
                 Contd…
               4 SQL LEFT JOIN
               •will return all records those
                satisfy the join condition, while it
                keeps attribute of the first (left
                hand side) table.
               •SQL LEFT JOIN syntax:
                    SELECT field1, field2, field3,..
                 FROM [TABLE 1] LEFT JOIN [TABLE
                                 2]
                 ON TABLE1.keyfield=TABLE2.keyfield
Sunday, June 9, 2019          Compiled by Aliyan Aman   62
Sunday, June 9, 2019   Compiled by Aliyan Aman   63
                 Contd…
               • 3 SQL RIGHT JOIN
               • will return all records those satisfy the
                 join condition, while it keeps attribute
                 of the second (right hand side) table.
               • SQL RIGHT JOIN syntax:
                           SELECT field1, field2, field3,..
                       FROM [TABLE 1] RIGHT JOIN [TABLE 2]
                        ON TABLE1.keyfield=TABLE2.keyfield
Sunday, June 9, 2019                Compiled by Aliyan Aman   64
Sunday, June 9, 2019   Compiled by Aliyan Aman   65
   Cross Join
SELECT     Name,Gender,Salary,DepartmentName
FROM       tblEmployee
CROSS JOIN tblDepartment
   Sunday, June 9, 2019               Compiled by Aliyan Aman   66
Sunday, June 9, 2019   Compiled by Aliyan Aman   67
This type of join is based on one table. It joins the table with itself.
Sunday, June 9, 2019          Compiled by Aliyan Aman                  68
                       Summary of SQL JOINS
Sunday, June 9, 2019                     Compiled by Aliyan Aman   69
                        Summary of SQL JOINS
INNER JOIN OR JOIN                                     LEFT OUTER JOIN OR LEFT JOIN
Marching rows-non matching rows eliminated             Marching rows + non matching rows from left table
 Sunday, June 9, 2019                  Compiled by Aliyan Aman                                   70
Advanced or intelligent joins
• Advanced or intelligent joins in QL Server
• Retrieve only the non matching rows from the left
  table
• Retrieve only the non matching rows from the
  right table
• Retrieve only the non matching rows from both
  the left and right table
Sunday, June 9, 2019   Compiled by Aliyan Aman   71
Sunday, June 9, 2019   Compiled by Aliyan Aman   72
                                         SELECT       [COLUMN_LIST]
                                          FROM       tblEmployee E
                                         LEFT JOIN   tblDepartment D
                                         ON          E.DeptID=D.Id
                                         WHERE       D.Id IS NULL
Sunday, June 9, 2019   Compiled by Aliyan Aman                         73
                                                 SELECT        [COLUMN_LIST]
                                                 FROM          tblEmployee E
                                                 RIGHT JOIN    tblDepartment D
                                                 ON           E.DeptID=D.Id
                                                 WHERE        E.Id IS NULL
Sunday, June 9, 2019   Compiled by Aliyan Aman                                   74
                                                 SELECT      [COLUMN_LIST]
                                                 FROM         tblEmployee E
                                                 RIGHT JOIN tblDepartment D
                                                 ON          E.DeptID=D.Id
                                                 WHERE      E.Id IS NULL
Sunday, June 9, 2019   Compiled by Aliyan Aman   OR         D.Id IS NULL      75
Stored Procedures
Sunday, June 9, 2019   Compiled by Aliyan Aman   76
   Stored procedures
• Stored procedure is a group of T-SQL(Transaction SQL)
  statements. If you have a situation, where you write the
  same query over and over again, you an save that specific
  query as a stored procedure and call it just by its name.
   Sunday, June 9, 2019    Compiled by Aliyan Aman        77
Sunday, June 9, 2019   Compiled by Aliyan Aman   78
Stored Procedures
Sunday, June 9, 2019   Compiled by Aliyan Aman   79
Sunday, June 9, 2019   Compiled by Aliyan Aman   80
Stored Procedures with parameter cont…
Sunday, June 9, 2019   Compiled by Aliyan Aman   81
Sunday, June 9, 2019   Compiled by Aliyan Aman   82
Stored Procedure with parameter example
                                                     To create the stored procedure
                                                      write from the second line of
                                                       SQL statement and execute
                                                     execute it. Then that will create
                                                          the stored procedure .
                                                      To execute the created stored
                                                      procedure select and execute
                                                     the first line of SQL statement.
Sunday, June 9, 2019       Compiled by Aliyan Aman                            83
To Encrypt the text of the SP
Sunday, June 9, 2019   Compiled by Aliyan Aman   84
A transaction is the propagation of one or more changes to the
database. For example, if you are creating a record or updating a
record or deleting a record from the table, then you are performing
transaction on the table. It is important to control transactions to
ensure        data integrity and to handle
    Sunday, June 9, 2019
                                                     database errors.
                                     Compiled by Aliyan Aman          85
Transactions cont…
Sunday, June 9, 2019   Compiled by Aliyan Aman   86
Sunday, June 9, 2019   Compiled by Aliyan Aman   87
                   Transaction continued..
Sunday, June 9, 2019               Compiled by Aliyan Aman   88
Sunday, June 9, 2019   Compiled by Aliyan Aman   89
Transaction Control
There are following commands used to control transactions:
• COMMIT: to save the changes.
• ROLLBACK: to rollback the changes.
• SAVEPOINT: creates points within groups of transactions in which to
  ROLLBACK
• SET TRANSACTION: Places a name on a transaction.
Transactional control commands are only used with the DML
commands INSERT, UPDATE and DELETE only.
They can not be used while creating tables or dropping them because
these operations are automatically committed in the database.
 Sunday, June 9, 2019           Compiled by Aliyan Aman                 90
The COMMIT Command:
• The COMMIT command is the transactional command used to save
  changes invoked by a transaction to the database.
• The COMMIT command saves all transactions to the database since
  the last COMMIT or ROLLBACK command.
• The syntax for COMMIT command is as follows:
                              COMMIT;
Sunday, June 9, 2019        Compiled by Aliyan Aman                 91
   Commit example
  ID NAME             AGE ADDRESS     SALARY            As a result, two rows from the table would be deleted
  1   Ramesh        35    Ahmedabad   2000.0.           and SELECT statement would produce the following
                                                        result:
  2   Khilan        25    Delshi         1500
  3   Kaushik       23    Kota           2000                       ID    NAME      AGE ADDRESS    SALARY
  4   Chaitai       25    Mumbai         6500
  5   Hardik        27    Bhopal         8500                       1 Ramesh 35 Ahmedabad 2000
  6   Komal         22    MP             4500
  7   Muffy         24    Indore      3 10000                             Kaushik   23   Kota      2000
Following is the example, which would 5                                   Hardik    27   Bhopal    8500
delete records from the table having  6                                   Komal     22   MP        4500
age = 25 and then COMMIT the          7                                   Muffy     24   Indore    10000
changes in the database.
SQL> DELETE FROM CUSTOMERS
WHERE AGE = 25;
SQL> COMMIT;
   Sunday, June 9, 2019                         Compiled by Aliyan Aman                                 92
The ROLLBACK Command
• The ROLLBACK command is the transactional command used to undo
  transactions that have not already been saved to the database.
• The ROLLBACK command can only be used to undo transactions since the
  last COMMIT or ROLLBACK command was issued.
• The syntax for ROLLBACK command is as follows:
                                  ROLLBACK;
• Following is the example, which would delete records from the table
  having age = 25 and then ROLLBACK the changes in the database.
• SQL> DELETE FROM CUSTOMERS           ID NAME    AGE ADDRESS   SALARY
• WHERE AGE = 25;                      1 Ramesh 35    Ahmedabad 2000.0.
                                       2 Khilan  25   Delshi       1500
• SQL> ROLLBACK;                       3 Kaushik 23   Kota         2000
                                         4   Chaitai     25   Mumbai    6500
                                         5   Hardik      27   Bhopal    8500
                                         6   Komal       22   MP        4500
                                         7   Muffy       24   Indore   10000
Sunday, June 9, 2019           Compiled by Aliyan Aman                         93
The SAVEPOINT Command
• A SAVEPOINT is a point in a transaction when you can roll the
  transaction back to a certain point without rolling back the entire
  transaction.
• The syntax for SAVEPOINT command is as follows:
                      SAVEPOINT SAVEPOINT_NAME;
• This command serves only in the creation of a SAVEPOINT among
  transactional statements. The ROLLBACK command is used to undo a
  group of transactions.
• The syntax for rolling back to a SAVEPOINT is as follows:
                    ROLLBACK TO SAVEPOINT_NAME;
Sunday, June 9, 2019         Compiled by Aliyan Aman                    94
The SAVEPOINT Command
• A SAVEPOINT is a point in a transaction when you can roll the transaction back
  to a certain point without rolling back the entire transaction.
• The syntax for SAVEPOINT command is as follows:
                          SAVEPOINT SAVEPOINT_NAME;
• This command serves only in the creation of a SAVEPOINT among
  transactional statements. The ROLLBACK command is used to undo a group of
  transactions.
• The syntax for rolling back to a SAVEPOINT is as follows:
                        ROLLBACK TO SAVEPOINT_NAME;
• Following is an example where you plan to delete the three different records
  from the CUSTOMERS table. You want to create a SAVEPOINT before each
  delete, so that you can ROLLBACK to any SAVEPOINT at any time to return the
  appropriate data to its original state:
Sunday, June 9, 2019              Compiled by Aliyan Aman                      95
                                                     Now that the three deletions have taken
       Cont…                                         place, say you have changed your mind
•   here is the series of operations:                and decided to ROLLBACK to the
•   SQL> SAVEPOINT SP1;                              SAVEPOINT that you identified as SP2.
•   Savepoint created.
•   SQL> DELETE FROM CUSTOMERS                       Because SP2 was created after the first
    WHERE ID=1;                                      deletion, the last two deletions are undone:
•   1 row deleted.                                   SQL> ROLLBACK TO SP2;
•   SQL> SAVEPOINT SP2;                              Rollback complete.
•   Savepoint created.                               ID NAME         AGE ADDRESS SALARY
•   SQL> DELETE FROM CUSTOMERS                       2 Khilan       25     Delshi         1500
    WHERE ID=2;
•   1 row deleted.                                   3 Kaushik 23          Kota           2000
•   SQL> SAVEPOINT SP3;                              4 Chaitai 25          Mumbai         6500
•   Savepoint created.                               5 Hardik 27           Bhopal         8500
•   SQL> DELETE FROM CUSTOMERS
    WHERE ID=3;                                      6 Komal        22     MP             4500
•   1 row deleted.                                   7 Muffy        24     Indore        10000
       Sunday, June 9, 2019             Compiled by Aliyan Aman                          96
    The RELEASE SAVEPOINT Command
• The RELEASE SAVEPOINT command is used to remove a SAVEPOINT that you have
  created.
• The syntax for RELEASE SAVEPOINT is as follows:
                       RELEASE SAVEPOINT SAVEPOINT_NAME;
• Once a SAVEPOINT has been released, you can no longer use the ROLLBACK
  command to undo transactions performed since the SAVEPOINT.
• The SET TRANSACTION Command:
• The SET TRANSACTION command can be used to initiate a database transaction.
  This command is used to specify characteristics for the transaction that follows.
• For example, you can specify a transaction to be read only or read write.
• The syntax for SET TRANSACTION is as follows:
                   SET TRANSACTION [ READ WRITE | READ ONLY ];
    Sunday, June 9, 2019             Compiled by Aliyan Aman                    97
Sunday, June 9, 2019   Compiled by Aliyan Aman   98
                       SQL SERVER DEADLOCK
Sunday, June 9, 2019           Compiled by Aliyan Aman   99
Sunday, June 9, 2019   Compiled by Aliyan Aman   100
Sunday, June 9, 2019   Compiled by Aliyan Aman   101
Sunday, June 9, 2019   Compiled by Aliyan Aman   102
Syntax for creating a Trigger
• CREATE TRIGGER <TriggerName> on <TableName>
• FOR INSERT, UPDATE, DELETE
• AS
• … Create your Code here
• GO
• The Trigger will automatically be executed when data is inserted,
  updated or deleted in the table as specified in the Trigger header.
Sunday, June 9, 2019           Compiled by Aliyan Aman                  103
INSERTED and DELETED
• Inside triggers we can use two special tables: the DELETED table and the INSERTED
  tables.
• SQL Server automatically creates and manages these tables. You can use these
  temporary, memory-resident tables to test the effects of certain data modifications.
• You cannot modify the data in these tables.
• The DELETED table stores copies of the affected rows during DELETE and UPDATE
  statements.
• During the execution of a DELETE or UPDATE statement, rows are deleted from the
  trigger table and transferred to the DELETED table.
• The INSERTED table stores copies of the affected rows during INSERT and UPDATE
  statements.
• During an insert or update transaction, new rows are added to both the INSERTED table
  and the trigger table.
• The rows in the INSERTED table are copies of the new rows in the trigger table.
Sunday, June 9, 2019                 Compiled by Aliyan Aman                          104
Example
• We will create a TRIGGER that will check if the Phone number is valid
  when we insert or update data in the CUSTOMER table.
• The validation check will be very simple, i.e., we will check if the
  Phone number is less than 8 digits (which is normal length in
  Norway).
• If the Phone number is less than 8 digits, the following message
  “Phone Number is not valid” be written in place of the wrong number
  in the Phone column.
Sunday, June 9, 2019          Compiled by Aliyan Aman                105
                Create trigger example
•IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CheckPhoneNumber‘ AND type = 'TR')
•DROP TRIGGER CheckPhoneNumber
•GO
•CREATE TRIGGER CheckPhoneNumber ON CUSTOMER
•FOR UPDATE, INSERT
•AS
•DECLARE
•@CustomerId int,
•@Phone varchar(50),
•@Message varchar(50)
•set nocount on
•select @CustomerId = CustomerId from INSERTED
•select @Phone = Phone from INSERTED
•set @Message = 'Phone Number ' + @Phone + ' is not valid‘
•if len(@Phone) < 8 --Check if Phone Number have less than 8 digits
• update CUSTOMER set Phone = @Message where CustomerId = @CustomerId
• set nocount off
• GO
    Sunday, June 9, 2019       Compiled by Aliyan Aman                                     106
Sunday, June 9, 2019   Compiled by Aliyan Aman   107
Sunday, June 9, 2019   Compiled by Aliyan Aman   108
Sunday, June 9, 2019   Compiled by Aliyan Aman   109
• Read more about trigger
Sunday, June 9, 2019        Compiled by Aliyan Aman   110
Sunday, June 9, 2019   Compiled by Aliyan Aman   111
How SQL Server Communicat with other
Applications
• A Database is a structured way to store lots of information. The
  information is stored in different tables. “Everything” today is stored
  in databases.
Examples:
• Bank/Account systems
• Information in Web pages such as Facebook, Wikipedia, YouTubelots
  of other systems.
• This means we need to be able to communicate with the database
  from other applications and programming languages in order to
  insert, update or retrieve data from the database.
Sunday, June 9, 2019           Compiled by Aliyan Aman                      112
                        ODBC
• ODBC (Open Database Connectivity) is
  a standardized interface (API) for
  accessing the database from a client.
• You can use this standard to
  communicate with databases from
  different vendors, such as Oracle, SQL
  Server, etc.
• The designers of ODBC aimed to make
  it independent of programming
  languages, database systems, and
  operating systems.
• We will use the ODBC Data Source
  Administrator:
 Sunday, June 9, 2019            Compiled by Aliyan Aman   113
Sunday, June 9, 2019   Compiled by Aliyan Aman   114
 Connect SQL Server with Visual Studio
• We can connect Microsoft Visual Studio with SQL Server for
  database use.
• Visual studio uses SQL Server to store the database that is used
  by window forms.
 Sunday, June 9, 2019           Compiled by Aliyan Aman              115
                                    ❹                ❺
                       ❻                                 ❼
Sunday, June 9, 2019       Compiled by Aliyan Aman           116
   Microsoft Excel
• Microsoft Excel has the ability to retrieve data from different data sources, including
  different database systems. It is very simple to retrieve data from SQL Server into Excel
  since Excel and SQL Server has the same vendor (Microsoft).
   Sunday, June 9, 2019                Compiled by Aliyan Aman                         117
Sunday, June 9, 2019   Compiled by Aliyan Aman   118
•END!!!!
•THANK
 YOUUUUUUUUUUUU
Sunday, June 9, 2019   Compiled by Aliyan Aman   119