SQL COMMANDS
Introduction
2
SQL is a database computer language designed
for the retrieval and management of data in a
relational database. SQL stands for Structured
Query Language.
Introduction to SQL
3
SQL functions fit into two broad categories:
Data definition language
SQL includes commands to:
Create database objects, such as tables, indexes, and views
Define access rights to those database objects
Data manipulation language
Includescommands to insert, update, delete, and retrieve
data within database tables
Data Types
4
Data type selection is usually dictated by nature of
data and by intended use
Pay close attention to expected use of attributes for
sorting and data retrieval purposes
Data Types (continued)
5
SQL Indexes
6
When primary key is declared, DBMS
automatically creates unique index
Often need additional indexes
Using CREATE INDEX command, SQL indexes can
be created on basis of any selected attribute
Composite index
Index based on two or more attributes
Often used to prevent data duplication
Data Manipulation Commands
7
Adding table rows
Listing table rows
Updating table rows
Deleting table rows
Adding Table Rows
8
INSERT
Used to enter data into table
Syntax:
INSERTINTO columnname
VALUES (value1, value2, … , valuen);
Adding Table Rows (continued)
9
When entering values, notice that:
Row contents are entered between parentheses
Character and date values are entered between
apostrophes
Numerical entries are not enclosed in apostrophes
Attribute entries are separated by commas
A value is required for each column
Use NULL for unknown values
Listing Table Rows
10
SELECT
Used to list contents of table
Syntax:
SELECTcolumnlist
FROM tablename;
Columnlist represents one or more attributes,
separated by commas
Asterisk can be used as wildcard character to list
all attributes
Updating Table Rows
11
UPDATE
Modify data in a table
Syntax:
UPDATE tablename
SET columnname = expression [, columname = expression]
[WHERE conditionlist];
If more than one attribute is to be updated in row,
separate corrections with commas
Deleting Table Rows
12
DELETE
Deletes a table row
Syntax:
DELETE
FROM tablename
[WHERE conditionlist ];
WHERE condition is optional
If WHERE condition is not specified, all rows from
specified table will be deleted
Selecting Rows with
13
Conditional Restrictions
Select partial table contents by placing restrictions
on rows to be included in output
Addconditional restrictions to SELECT statement, using
WHERE clause
Syntax:
SELECTcolumnlist
FROM tablelist
[ WHERE conditionlist ] ;
Selecting Rows with
Conditional Restrictions (continued)
14
Arithmetic Operators:
15
The Rule of Precedence
Perform operations within parentheses
Perform power operations
Perform multiplications and divisions
Perform additions and subtractions
Arithmetic Operators:
The Rule of Precedence (continued)
16
Special Operators
17
BETWEEN
Used to check whether attribute value is within a range
IS NULL
Used to check whether attribute value is null
LIKE
Used to check whether attribute value matches given
string pattern
Special Operators (continued)
18
IN
Used to check whether attribute value matches any
value within a value list
EXISTS
Used to check if subquery returns any rows
Advanced Data Definition Commands
19
All changes in table structure are made by using
ALTER command
Followed by keyword that produces specific change
Following options are available:
ADD
MODIFY
DROP
Changing a Column’s Data Type
20
ALTER can be used to change data type
Some RDBMSs (such as Oracle) do not permit
changes to data types unless column to be changed
is empty
Changing a Column’s Data
21
Characteristics
Use ALTER to change data characteristics
If column to be changed already contains data,
changes in column’s characteristics are permitted if
those changes do not alter the data type
Adding a Column
22
Use ALTER to add column
Do not include the NOT NULL clause for new column
Dropping a Column
23
Use ALTER to drop column
Some RDBMSs impose restrictions on the deletion of an
attribute
Adding Primary and Foreign Key
24
Designations
When table is copied, integrity rules do not copy, so
primary and foreign keys need to be manually
defined on new table
User ALTER TABLE command
Syntax:
ALTER TABLE tablename ADD
PRIMARY KEY(fieldname);
For foreign key, use FOREIGN KEY in place of PRIMARY KEY
Deleting a Table from the Database
25
DROP
Deletes table from database
Syntax:
DROP TABLE tablename;
Advanced Select Queries
26
SQL provides useful functions that can:
Count
Findminimum and maximum values
Calculate averages
SQL allows user to limit queries to only those entries
having no duplicates or entries whose duplicates
may be grouped
Aggregate Functions
27