SlideShare uma empresa Scribd logo
1 de 29
SQL Query
Getting to the data …….
Objectives
 DDL vs. DML
 DML Statements
SQL Language
 DDL - DDL is abbreviation of Data Definition Language. It is used to create and modify the structure
of database objects in database.
 Examples: CREATE, ALTER, DROP statements
 DML - DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify,
delete, insert and update data in database.
 Examples: SELECT, UPDATE, INSERT statements
 DCL - DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and
referential integrity as well it is used to control access to database by securing it.
 Examples: GRANT, REVOKE statements
 TCL - TCL is abbreviation of Transactional Control Language. It is used to manage different
transactions occurring within a database.
 Examples: COMMIT, ROLLBACK statements
Data Manipulation Language (DML)
 The verbs for the basic SQL commands are:
 SELECT - to retrieve (query) data
 UPDATE - to modify existing data
 DELETE - to modify existing data
 INSERT - to add new data
 We further break down SQL commands into the following categories:
 Data Creation
 Data Manipulation
 Data Retrieval
 Advanced SQL with JOINS, Subqueries and UNIONS
Systems Development
Life Cycle
Project Identification
and Selection
Project Initiation
and Planning
Analysis
Physical Design
Implementation
Maintenance
Logical Design
Enterprise modeling
Conceptual data modeling
Logical database design
Physical database design
and definition
Database implementation
Database maintenance
Database
Development Process
SQL Database Definition
 Data Definition Language (DDL)
 Major CREATE statements:
 CREATE SCHEMA – defines a portion of the database owned by a particular user
 CREATE TABLE – defines a table and its columns
 CREATE VIEW – defines a logical table from one or more views
 Other CREATE statements: CHARACTER SET, COLLATION, TRANSLATION, ASSERTION, DOMAIN
SQL - Insert
 The SQL INSERT INTO clause is used to insert data into a SQL table. The SQL INSERT INTO is
frequently used and has the following generic syntax:
 The SQL INSERT INTO clause has actually two parts - the first specifying the table we are inserting
into and giving the list of columns we are inserting values for, and the second specifying the values
inserted in the column list from the first part.
INSERT INTO Table1 (Column1, Column2, Column3)
VALUES (ColumnValue1, ColumnValue2, ColumnValue3)
SQL - Insert
 Inserting a record with all fields
 INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’, 1355 S. Himes Blvd.’,
‘Gainesville’, ‘FL’, 32601);
 Inserting a record with specified fields
 INSERT INTO PRODUCT_T (PRODUCT_ID, PRODUCT_DESCRIPTION, PRODUCT_FINISH,
STANDARD_PRICE, PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);
 Inserting records from another table
 INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE = ‘CA’;
SQL – Delete
 The SQL DELETE Query is used to delete the existing records from a table.
 You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records
would be deleted.
 Removes rows from a table
 Delete certain rows
 DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’;
 Delete all rows
 DELETE FROM CUSTOMER_T;
SQL - Drop
 The SQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers,
constraints, and permission specifications for that table.
 NOTE: You have to be careful while using this command because once a table is deleted then all the
information available in the table would also be lost forever.
 The SQL TRUNCATE TABLE command is used to delete complete data from an existing table.
 You can also use DROP TABLE command to delete complete table but it would remove complete
table structure form the database and you would need to re-create this table once again if you wish
you store some data.
SQL - Update
 The SQL UPDATE command is used to modify data stored in database tables.
 Modifies data in existing rows
UPDATE PRODUCT_T
SET UNIT_PRICE = 775
WHERE PRODUCT_ID = 7;
SQL - Select
 SQL SELECT is without a doubt the most frequently used SQL command that's why we are starting
our tutorial with it. The SQL SELECT command is used to retrieve data from one or more database
tables.
 Clauses of the SELECT statement:
 SELECT - List the columns (and expressions) that should be returned from the query
 FROM - Indicate the table(s) or view(s) from which data will be obtained
 WHERE - Indicate the conditions under which a row will be included in the result
 GROUP BY - Indicate columns to group the results
 HAVING - Indicate the conditions under which a group will be included
 ORDER BY - Sorts the result according to specified columns
SQL Statement processing order
SQL Comparison Operators
Operator Meaning
= Equal To
> Greater Than
>= Greater Than or Equal To
< Less Than
<= Less Than or Equal To
<> Not Equal To
!= Not Equal To
SQL Aliases
 SQL aliases are used to give a database table, or a column in a table, a temporary name.
 Basically aliases are created to make column names more readable.
SELECT CUST.CUSTOMER AS NAME, CUST.CUSTOMER_ADDRESS
FROM CUSTOMER_V CUST
WHERE NAME = ‘Home Furnishings’;
SQL Aggregate Functions
 SQL aggregate functions return a single value, calculated from values in a column.
 Useful aggregate functions:
 AVG() - Returns the average value
 COUNT() - Returns the number of rows
 FIRST() - Returns the first value
 LAST() - Returns the last value
 MAX() - Returns the largest value
 MIN() - Returns the smallest value
 SUM() - Returns the sum
SQL Aggregate Functions
 Using the COUNT aggregate function to find totals
 The COUNT(column_name) function returns the number of values (NULL values will not be counted)
of the specified column:
SELECT COUNT(*) FROM ORDER_LINE_V
WHERE ORDER_ID = 1004;
SQL Boolean Operators
 AND, OR, and NOT Operators for customizing conditions in WHERE clause
 The AND operator displays a record if both the first condition AND the second condition are true.
 The OR operator displays a record if either the first condition OR the second condition is true.
SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE
FROM PRODUCT_V
WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’
OR PRODUCT_DESCRIPTION LIKE ‘%Table’)
AND UNIT_PRICE > 300;
SQL Between Operator
 The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL Wildcards
 In SQL, wildcard characters are used with the SQL LIKE operator.
 SQL wildcards are used to search for data within a table.
 With SQL, the wildcards are:
Wildcard Description
% A substitute for zero or more characters
_ A substitute for a single character
[charlist] Sets and ranges of characters to match
[^charlist]
or
[!charlist]
Matches only a character NOT specified within the brackets
SQL Order Clause
 The ORDER BY keyword is used to sort the result-set by one or more columns.
 The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a
descending order, you can use the DESC keyword.
SELECT CUSTOMER_NAME, CITY, STATE
FROM CUSTOMER_V
WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY STATE, CUSTOMER_NAME;
SQL Group Clause
 Categorizing results
SELECT STATE, COUNT(STATE)
FROM CUSTOMER_V
GROUP BY STATE;
SQL Having Clause
 For use with GROUP BY
SELECT STATE, COUNT(STATE)
FROM CUSTOMER_V
GROUP BY STATE
HAVING COUNT(STATE) > 1;
 Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only
those groups with total numbers greater than 1 will be included in final result
SQL Unions
 The SQL UNION clause/operator is used to combine the results of two or more SELECT statements
without returning any duplicate rows.
 To use UNION, each SELECT must have the same number of columns selected, the same number of
column expressions, the same data type, and have them in the same order, but they do not have to
be the same length.
SELECT column1 [, column2 ]
FROM table1 [, table2 ] [WHERE condition]
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ] [WHERE condition]
SQL Distinct
 The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the
duplicate records and fetching only unique records.
 There may be a situation when you have multiple duplicate records in a table. While fetching such
records, it makes more sense to fetch only unique records instead of fetching duplicate records.
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
Summary
 The Structured Query Language (SQL) defines a standard for interacting with relational
databases
 Most platforms support ANSI-SQL 92
 Most platforms provide many non-ANSI-SQL additions
 Most important data modification SQL statements:
 SELECT: Returning rows
 UPDATE: Modifying existing rows
 INSERT: Creating new rows
 DELETE: Removing existing rows

Mais conteúdo relacionado

Semelhante a SQL Query

Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql newSANTOSH RATH
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETEAbrar ali
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxEliasPetros
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL CommandsShrija Madhu
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESVENNILAV6
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statementsMohd Tousif
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDev Chauhan
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxEliasPetros
 

Semelhante a SQL Query (20)

Sql slid
Sql slidSql slid
Sql slid
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
Query
QueryQuery
Query
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
Hira
HiraHira
Hira
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
SQL | DML
SQL | DMLSQL | DML
SQL | DML
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
 
Adbms
AdbmsAdbms
Adbms
 
Lab
LabLab
Lab
 

Último

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Último (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

SQL Query

  • 1. SQL Query Getting to the data …….
  • 2. Objectives  DDL vs. DML  DML Statements
  • 3. SQL Language  DDL - DDL is abbreviation of Data Definition Language. It is used to create and modify the structure of database objects in database.  Examples: CREATE, ALTER, DROP statements  DML - DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert and update data in database.  Examples: SELECT, UPDATE, INSERT statements  DCL - DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it.  Examples: GRANT, REVOKE statements  TCL - TCL is abbreviation of Transactional Control Language. It is used to manage different transactions occurring within a database.  Examples: COMMIT, ROLLBACK statements
  • 4. Data Manipulation Language (DML)  The verbs for the basic SQL commands are:  SELECT - to retrieve (query) data  UPDATE - to modify existing data  DELETE - to modify existing data  INSERT - to add new data  We further break down SQL commands into the following categories:  Data Creation  Data Manipulation  Data Retrieval  Advanced SQL with JOINS, Subqueries and UNIONS
  • 5. Systems Development Life Cycle Project Identification and Selection Project Initiation and Planning Analysis Physical Design Implementation Maintenance Logical Design Enterprise modeling Conceptual data modeling Logical database design Physical database design and definition Database implementation Database maintenance Database Development Process
  • 6.
  • 7.
  • 8.
  • 9. SQL Database Definition  Data Definition Language (DDL)  Major CREATE statements:  CREATE SCHEMA – defines a portion of the database owned by a particular user  CREATE TABLE – defines a table and its columns  CREATE VIEW – defines a logical table from one or more views  Other CREATE statements: CHARACTER SET, COLLATION, TRANSLATION, ASSERTION, DOMAIN
  • 10. SQL - Insert  The SQL INSERT INTO clause is used to insert data into a SQL table. The SQL INSERT INTO is frequently used and has the following generic syntax:  The SQL INSERT INTO clause has actually two parts - the first specifying the table we are inserting into and giving the list of columns we are inserting values for, and the second specifying the values inserted in the column list from the first part. INSERT INTO Table1 (Column1, Column2, Column3) VALUES (ColumnValue1, ColumnValue2, ColumnValue3)
  • 11. SQL - Insert  Inserting a record with all fields  INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’, 1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);  Inserting a record with specified fields  INSERT INTO PRODUCT_T (PRODUCT_ID, PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);  Inserting records from another table  INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE = ‘CA’;
  • 12. SQL – Delete  The SQL DELETE Query is used to delete the existing records from a table.  You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.  Removes rows from a table  Delete certain rows  DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’;  Delete all rows  DELETE FROM CUSTOMER_T;
  • 13. SQL - Drop  The SQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers, constraints, and permission specifications for that table.  NOTE: You have to be careful while using this command because once a table is deleted then all the information available in the table would also be lost forever.  The SQL TRUNCATE TABLE command is used to delete complete data from an existing table.  You can also use DROP TABLE command to delete complete table but it would remove complete table structure form the database and you would need to re-create this table once again if you wish you store some data.
  • 14. SQL - Update  The SQL UPDATE command is used to modify data stored in database tables.  Modifies data in existing rows UPDATE PRODUCT_T SET UNIT_PRICE = 775 WHERE PRODUCT_ID = 7;
  • 15. SQL - Select  SQL SELECT is without a doubt the most frequently used SQL command that's why we are starting our tutorial with it. The SQL SELECT command is used to retrieve data from one or more database tables.  Clauses of the SELECT statement:  SELECT - List the columns (and expressions) that should be returned from the query  FROM - Indicate the table(s) or view(s) from which data will be obtained  WHERE - Indicate the conditions under which a row will be included in the result  GROUP BY - Indicate columns to group the results  HAVING - Indicate the conditions under which a group will be included  ORDER BY - Sorts the result according to specified columns
  • 17. SQL Comparison Operators Operator Meaning = Equal To > Greater Than >= Greater Than or Equal To < Less Than <= Less Than or Equal To <> Not Equal To != Not Equal To
  • 18. SQL Aliases  SQL aliases are used to give a database table, or a column in a table, a temporary name.  Basically aliases are created to make column names more readable. SELECT CUST.CUSTOMER AS NAME, CUST.CUSTOMER_ADDRESS FROM CUSTOMER_V CUST WHERE NAME = ‘Home Furnishings’;
  • 19. SQL Aggregate Functions  SQL aggregate functions return a single value, calculated from values in a column.  Useful aggregate functions:  AVG() - Returns the average value  COUNT() - Returns the number of rows  FIRST() - Returns the first value  LAST() - Returns the last value  MAX() - Returns the largest value  MIN() - Returns the smallest value  SUM() - Returns the sum
  • 20. SQL Aggregate Functions  Using the COUNT aggregate function to find totals  The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(*) FROM ORDER_LINE_V WHERE ORDER_ID = 1004;
  • 21. SQL Boolean Operators  AND, OR, and NOT Operators for customizing conditions in WHERE clause  The AND operator displays a record if both the first condition AND the second condition are true.  The OR operator displays a record if either the first condition OR the second condition is true. SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE FROM PRODUCT_V WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’ OR PRODUCT_DESCRIPTION LIKE ‘%Table’) AND UNIT_PRICE > 300;
  • 22. SQL Between Operator  The BETWEEN operator selects values within a range. The values can be numbers, text, or dates. SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
  • 23. SQL Wildcards  In SQL, wildcard characters are used with the SQL LIKE operator.  SQL wildcards are used to search for data within a table.  With SQL, the wildcards are: Wildcard Description % A substitute for zero or more characters _ A substitute for a single character [charlist] Sets and ranges of characters to match [^charlist] or [!charlist] Matches only a character NOT specified within the brackets
  • 24. SQL Order Clause  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword. SELECT CUSTOMER_NAME, CITY, STATE FROM CUSTOMER_V WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’) ORDER BY STATE, CUSTOMER_NAME;
  • 25. SQL Group Clause  Categorizing results SELECT STATE, COUNT(STATE) FROM CUSTOMER_V GROUP BY STATE;
  • 26. SQL Having Clause  For use with GROUP BY SELECT STATE, COUNT(STATE) FROM CUSTOMER_V GROUP BY STATE HAVING COUNT(STATE) > 1;  Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result
  • 27. SQL Unions  The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning any duplicate rows.  To use UNION, each SELECT must have the same number of columns selected, the same number of column expressions, the same data type, and have them in the same order, but they do not have to be the same length. SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition] UNION SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
  • 28. SQL Distinct  The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetching only unique records.  There may be a situation when you have multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only unique records instead of fetching duplicate records. SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE [condition]
  • 29. Summary  The Structured Query Language (SQL) defines a standard for interacting with relational databases  Most platforms support ANSI-SQL 92  Most platforms provide many non-ANSI-SQL additions  Most important data modification SQL statements:  SELECT: Returning rows  UPDATE: Modifying existing rows  INSERT: Creating new rows  DELETE: Removing existing rows

Notas do Editor

  1. Relational Model
  2. Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed
  3. Note: the IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions
  4. Note: you can use single-value fields with aggregate functions if they are included in the GROUP BY clause