SQL
SQL Overview
Purpose of SQL Standard
Benefits of a Standardized Relational Language
SQL Environment
SQL Environment
Figure 6-1
A simplified schematic of a typical SQL environment, as described by the SQL: 2008 standard
SQL Major Aspects
SQL Major Aspects
SQL Major Aspects
SQL Major Aspects
And others…
Database lifecycle revisited
36
Requirements
Logical Design
Physical design
Implementation
Logical Design
Structured Query Language (SQL)
SQL
37
Modules in EasyShop
DBMS
Customer processing
Supplier
processing
Item
processing
SQL
Demo: EasyShop application
SQL statements
DDL
(Data Definition Language)
CREATE
ALTER
DROP
TRUNCATE
DML
(Data Manipulation Language)
INSERT
UPDATE
DELETE
SELECT
DCL
(Data Control Language)
GRANT
REVOKE
TCL
(Transaction Control Language)
COMMIT
ROLLBACK
38
DDL Commands�
CREATE :�This command is used to create a new table in SQL. The user has to give information like table name, column names, and their datatypes.
Syntax:
CREATE TABLE table_name
( column_1 datatype,
column_2 datatype,
column_3 datatype, .... );
Example
�We need to create a table for storing Student information of a particular College. Create syntax would be as below.
CREATE TABLE Student_info
( College_Id number(2),
College_name varchar(30),
Branch varchar(10) );
ALTER Command
Syntax to add a column to an existing table.
Example:
�In our Student_info table, we want to add a new column for CGPA.
The syntax:
TRUNCATE:
This command is used to remove all rows from the table, but the structure of the table still exists.
Syntax:
Syntax to remove an existing table.
Example :
The College Authority wants to remove the details of all students for new batches but wants to keep the table structure.
Syntax:� Syntax to drop an existing table.
DROP TABLE table_name;
Example:�If the College Authority wants to change their Database by deleting the Student_info Table.
Data Manipulation Language (DML)
DML Commands
�SELECT�
The basic syntax for writing a SELECT query in SQL is as follows :
SELECT column_name1, column_name2, …� FROM table_name� WHERE condition_ expression;
The parameters used in the above syntax are as follows :
Example :
SELECT customer_id,� sale_date,� order_id,� store_state� FROM customers;
INSERT�
The basic syntax for writing INSERT statements in SQL is as follows :
UPDATE
The syntax for writing an UPDATE statement is as follows :
UPDATE table_name� SET column_name_1 = value1, column_name_2 = value2, ...� WHERE condition;
Example :
UPDATE customers� SET store_state = 'DL'� WHERE store_state = 'NY';
DELETE�
DELETE FROM table_name WHERE condition;
DCL (Data Control Language)
Examples of DCL commands:
This command is use to give user access privileges to a database.
Syntax:
GRANT SELECT ON Users TO'Tom'@'localhost;
Revoke:
Syntax:
REVOKE privilege_name ON object_name FROM {user_name |PUBLIC |role_name}
For example:
REVOKE SELECT, UPDATE ON student FROM BCA, MCA;
Some of The Most Important SQL Commands�
Data types and operators in SQL
39
Data types
NUMBER
CHAR
VARCHAR2
DATE
Relational operators
=, !=,
<, >,
<=, >=
Logical operators
AND
OR
NOT
Logical operators:�
Operator | Description |
Logical AND compares between two Booleans as expressions and returns true when both expressions are true. | |
Logical OR compares between two Booleans as expressions and returns true when one of the expressions is true. | |
Not takes a single Boolean as an argument and changes its value from false to true or from true to false. |
The Logical operators are those that are true or false. They return true or false values to combine one or more true or false values.�
Logical operators:
AND Operator:
Syntax:
condition1,2,..N : first condition, second condition and so on
OR Operator �
�Syntax:
WHERE condition1 OR condition2 OR... conditionN; table_name:
name of the table condition1,2,..N : first condition, second condition
and so on
OR Operator �
AND Operator �
Query:
SELECT * FROM Student WHERE Age = 18 AND ADDRESS = 'Delhi';
ROLL_NO | NAME | ADDRESS | PHONE | Age |
1 | Ram | Delhi | XXXXXXXXXX | 18 |
4 | SURESH | Delhi | XXXXXXXXXX | 18 |
Output: �
OR Operator
ROLL_NO | NAME | ADDRESS | PHONE | Age |
1 | Ram | Delhi | XXXXXXXXXX | 18 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
Combining AND and OR:�
Syntax:
SELECT * FROM table_name WHERE condition1 AND (condition2 OR condition3);
Query:
SELECT * FROM Student WHERE Age = 18 AND (NAME = 'Ram' OR NAME = 'RAMESH');
NOT Operator�
Customer ID | Customer Name | City | PostalCode | Country |
1 | John Wick | New York | 1248 | USA |
2 | Around the Horn | London | WA1 1DP | UK |
3 | Rohan | New Delhi | 100084 | India |
NOT Operator�
Customer ID | Customer Name | City | PostalCode | Country |
1 | John Wick | New York | 1248 | USA |
3 | Rohan | New Delhi | 100084 | India |
Combining AND and NOT�
Customer ID | Customer Name | City | PostalCode | Country |
3 | Rohan | New Delhi | 100084 | India |
You can also combine the AND, OR and NOT operators.�Example:�SELECT * FROM Customers WHERE NOT Country=’USA’ AND NOT Country=’UK’;
DDL and DML
40
Constraints - Types:
Constraints - Levels:
SQL Constraints
SQL Constraints
SQL NOT NULL Constraint�
Example
SQL UNIQUE Constraint�
CREATE TABLE Persons (� ID int NOT NULL UNIQUE,� LastName varchar(255) NOT NULL,� FirstName varchar(255),� Age int�);
SQL PRIMARY KEY Constraint�
SQL PRIMARY KEY on CREATE TABLE
Example:
CREATE TABLE Persons (� ID int NOT NULL,� LastName varchar(255) NOT NULL,� FirstName varchar(255),� Age int,� PRIMARY KEY (ID)�);
SQL FOREIGN KEY Constraint
SQL FOREIGN KEY Constraint
SQL CHECK Constraint�
ALTER TABLE
46
SELECT
47
Operators:
Select Statement with operators
IN | TRUE if the operand is equal to one of a list of expressions |
SELECT * FROM Customers
WHERE City IN ('Paris','London');
Like
LIKE | TRUE if the operand matches a pattern |
SELECT * FROM Customers
WHERE City LIKE 's%';
BETWEEN�
BETWEEN | TRUE if the operand is within the range of comparisons |
SELECT * FROM Products
WHERE Price BETWEEN 50 AND 60;
DISTINCT, ORDER BY
Confidential
Copyright © 2017, Infosys Limited
48
SQL SELECT DISTINCT Statement�
SQL ORDER BY Keyword�
The ORDER BY command is used to sort the result set in ascending or descending order.
SQL CASE Statement�
SQL CASE Statement�
SQL CASE Statement�
SQL CASE Statement
SQL functions
Single row functions
51
Multi row functions (Aggregate functions)