SQL
Database Management System
1
SQL Overview
• Structured Query Language
• The standard for relational database management
systems (RDBMS)
• RDBMS: A database management system that
manages data as a collection of tables in which all
relationships are represented by common values in
related tables
2
Database Languages
3
DDL
4
Naming Convention
5
Datatype
6
CREATE
7
EXAMPLE TABLE CREATION
8
Overall table definitions
9
ALTER
10
EXAMPLE ALTER
11
EXAMPLE ALTER
12
The Database Model
13
Tasks to be Completed Before Using
a New RDBMS
• Create a database structure
– RDBMS creates physical files that will hold a database
– Differs from one RDBMS to another
• Authentication: Process DBMS uses to verify that only
registered users access the data
– Required for the creation of tables
– User should log on to RDBMS using user ID and
password created by a database administrator
14
Primary Key and Foreign
Key
• Primary key attributes contain both a NOT NULL and a UNIQUE
specification example roll no.
• RDBMS will automatically enforce referential integrity for foreign
keys
• Command sequence ends with a semicolon
15
Data Manipulation
Commands
INSERT: Command to insert data into table
• Syntax - INSERT INTO tablename VALUES();
• Used to add table rows with NULL and NOT NULL
attributes
COMMIT: Command to save changes
• Syntax - COMMIT [WORK];
• Ensures database update integrity
16
Data Manipulation
Commands
SELECT: Command to list the contents
• Syntax - SELECT columnlist FROM tablename;
• Wildcard character(*): Substitute for other
characters/command
UPDATE: Command to modify data
• Syntax - UPDATE tablename SET columnname =
expression [, columnname = expression] [WHERE
conditionlist];
17
SELECT Statement
□ Used for queries on single or multiple tables
□ Clauses of the SELECT statement:
□ SELECT
□ List the columns (and expressions) to 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 categorization of results. It is grouped by specific column
□ HAVING
□ Indicate the conditions under which a category (group) will be
included
□ ORDER BY
□ Sorts the result according to specified criteria
18
SQL statement
processing
order
37 37
SELECT Example
• Find products with standard price less than $275
Table :Comparison Operators in SQL
20
SELECT Example Using Alias
□ Alias is an alternative column or table name
□ Here, CUST is a table alias and Name is a
column alias
21
SELECT Example Using a
Function
□ Using the COUNT aggregate function to find totals
SELECT COUNT(*) FROM ORDERLINE_T
WHERE ORDERID = 1004;
Note: with aggregate functions, you can’t have single-valued
columns included in the SELECT clause, unless they are included
in the GROUP BY clause.
22
SELECT Example–Boolean Operators
□ AND, OR, and NOT Operators for customizing
conditions in WHERE clause
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.
23
Boolean query A without use of parenthes
By default,
processing order
of Boolean
operators is NOT,
then AND, then
OR
24
SELECT Example–Boolean Operators
With parentheses, you can override normal precedence rules. In
this case parentheses make the OR take place before the AND.
25
Boolean query Bwith use of
parentheses
26
Sorting Results with ORDER
BY Clause
• Sort the results first by STATE, and within a state by the CUSTOMER
NAME
Note: the IN operator in this example allows you to include
rows whose CustomerState value is either FL, TX, CA, or HI. It
is more efficient than separate OR conditions.
27
Categorizing Results Using GROUP BY
Clause
• For use with aggregate functions
– Scalar aggregate: single value returned from SQL query with
aggregate function
– Vector aggregate: multiple values returned from SQL query with
aggregate function (via GROUP BY)
You can use single-value fields with aggregate functions if they are
included in the GROUP BY clause. 28
Qualifying Results by
Categories Using the
HAVING Clause
□ For use with GROUP BY
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.
29
Data Manipulation
Commands
WHERE condition
• Specifies the rows to be selected
ROLLBACK: Command to restore the database
• Syntax - ROLLBACK;
• Undoes the changes since the last COMMIT
command
DELETE: Command to delete
• Syntax - DELETE FROM table name
• [WHERE conditionlist]; 30
Special Operators
BETWEEN
• Checks whether attribute value is within a range
IS NULL
• Checks whether attribute value is null
LIKE
• Checks whether attribute value matches given string pattern
IN
• Checks whether attribute value matches any value within a value list
EXISTS
• Checks if subquery returns any rows
31
SQL Data Definition
Command
38
SQL Data Manipulation
Commands
39
THANK YOU