You are on page 1of 14

2/14/13

Topic Transcript

Use SQL for Queries and Manipulation


Learning Objective

After completing this topic, you should be able to


recognize what the different SQL statements do
1. Introduction to SQL statements
In his design of the relational database management system or RDBMS, Dr. Edgar F. Codd only specified how
data should be stored, which depended on the RDBMS implementation. RDBMS implementations also required a
way to retrieve stored data. This lead to the development of a number of languages.
One such language was the Structured English Query Language that was shortened to SEQUEL. This language
was developed in the early 1970s by Donald D. Chamberlin and Raymond F. Boyce to act as an interface to IBMs
System R relational prototype.
After an enhanced version of this language, SEQUEL/2 was developed, SEQUEL was discovered to be a
trademark of the Hawker Siddeley Aircraft Company. As a result, the name of the language was changed to SQL,
spelled SQL.
With the advent of SQL, both IBM and Relational Software Inc. began working on commercially viable RDBMS
based on SQL. Relational Software Systems, Inc. released its version of SQL-based RDBMS in 1979. And in
1981, IBM released SQL/DS, followed by DB2 in 1983.

Note
Relational Software Inc. was later renamed to Oracle Corporation.

Because of these developments, the American National Standards Institute, commonly referred to as ANSI, began
to standardize SQL.
In the course of standardization, several versions of SQL were released:

SQL-86
SQL-86 was the first standardized version of SQL. Completed in 1986, this SQL version was adopted by
the International Standards Organization or ISO in 1987.
SQL-89
SQL-89 was adopted by ANSI and the International Standards Organization or ISO. This version defined
two levels of SQL level 1 or the basic level and level 2 or the enhanced level. Level 2 also contained an
addendum on declarative entity integrity and referential integrity enhancements.
SQL-92
Approved by both ANSI and the International Standards Organization, commonly referred to as ISO,
SQL-92 contained over 600 pages of standards. Therefore, this version was divided into entry,
intermediate, and full parts.
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

1/14

2/14/13

Topic Transcript

SQL:1999
Approved on 8th December, 1999, SQL:1999 included object-oriented features and was converted into an
object-relational language. This version was very large at 1151 pages, and so was divided into 5 sections,
Framework, Foundation, Call Level Interface, Persistent Stored Modules, and Host Language Bindings
parts.
SQL:2003, and
Released in 2003, SQL:2003 was enhanced and support for XML was added to it. This version contains
nine sections with the Foundation section containing over 1300 pages alone. In 2006, the XML section
was updated to include new features. However, other sections remained the same.
SQL:2008
SQL:2008 is the sixth version of SQL This version contains revisions to the XML section introduced in the
SQL:2003 version along with enhancements to the Foundation section.
The process for standardizing SQL took some time and is still not complete. So SQL wasn't able to address the
requirements of all RDBMS implementations. As a result, the vendors of different RDBMS implementations
included certain additional features in their versions of SQL. This gave rise to several SQL dialects.
Some popular dialects include Procedural Language/SQL or PL/SQL for Oracle, Transact-SQL or TSQL for
Microsoft and Sybase Adaptive Server, SQL PL for IBM DB2, Procedural Language/postgreSQL or PL/pgSQL for
PostgreSQL, and the SQL implemented by MySQL.
Despite all these dialects, SQL is among of the most commonly used languages to manage an RDBMS. SQL
helps you add, retrieve, edit, and even delete data from a relational database.
SQL helps you perform the following functions in an RDBMS:

data definition
You can use SQL to define how data is stored in an RDBMS. Using this function, you can define the
data that needs to be stored, define how it is structured, and specify whether data items are related to
each other.
data retrieval
Using SQL, you can retrieve data stored in an RDBMS. This can be done either by a user or through a
predefined application program.
data manipulation
Allowing data to be edited is another function of SQL. Using this function, you can add data to existing
tables and even modify or delete existing data.
access control
SQL allows you to define the level of access each user has to the database. This function allows you to
prevent unauthorized users from accessing, modifying, or deleting data.
data sharing, and
Data sharing is an important function provided by SQL. This function allows you to ensure data
consistency when more than one user is trying to modify the same data simultaneously.
data integrity
An important function of SQL is that it promotes data integrity. Using this function, you can prevent data
from being corrupted if any changes aren't completed successfully.
To perform any of these functions, you use a SQL query. This query is the name given to SQL statements that are
run against the database. These statements can be categorized according to the function they perform. These
categories depend on whether you want to state what the action is that needs to be performed or how the action
should be performed.
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

2/14

2/14/13

Topic Transcript

The SQL categories or sublanguages that include statements for stating what action to perform are

data manipulation language, referred to as DML


You can use DML statements to manage table data. These statements allow you to add, modify, delete,
and display table data.
data definition language, also known as DDL, and
DDL allows you to define the structure of the objects in a relational database such as creating,
modifying, and deleting tables, views, and indexes.
data control language or DCL
DCL statements enable you to define the access level of different users. These statements allow you to
define various roles and assign them different levels of access. These commands can also be used to
allow or revoke access to database objects such as tables and views.

Question
Which SQL sublangauge allows you to modify the data present in an RDBMS?
Options:
1. DDL
2. DML
3. DCL

Answer
Option 1: This option is incorrect. DDL allows you to create, delete, and modify datab ase ob jects. It
doesn't allow you to modify actual data.
Option 2: This option is correct. You can use DML statements to manage tab le data. DML
statements allow you to add, modify, delete, and display tab le data.
Option 3: This option is incorrect. DCL statements define various roles and assign them different
levels of access. These commands can also b e used to allow or revoke access to datab ase ob jects
such as tab les and views. They don't allow you to modify tab le data.
Correct answer(s):
2. DML

The commonly used DML statements include SELECT, INSERT, UPDATE, and DELETE. The SELECTstatement
enables you to retrieve specific data. This data can be in the form of specific columns or records that meet a
specific condition. As a result, you can use this statement for relational selection, relational projection, and
relational joins.
These are the main clauses for the SELECTstatement.

Syntax
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

3/14

2/14/13

Topic Transcript

SELECT DISTINCT ColumnList


FROM Tab leList
JOIN Tab leName ON Condition
[WHERE Condition]
[ORDER BY Condition]
[GROUP BY ColumnName]
[HAVING Condition]

The SELECTclause requires the names of columns whose data you want to display in a practical
implementation of the relational projection operation. You can either specify a single column or list
multiple columns separated by a comma. However, if you want to display all columns of a table, you can
use the asterisk special character available in a few RDBMSs instead of specifying individual column
names or use the ALLk eyword. You can also specify the optional DISTINCTclause with the SELECT
clause to ensure that only unique values are displayed in the result.
In the FROMclause of the SELECTstatement, you specify the name of the table from which you want to
retrieve data. This is a mandatory clause and can be used to retrieve data from multiple tables, which
need to be separated by a comma. If multiple tables are used, the SELECTstatement implements the
relational join operation. You can specify the optional JOINclause to specify the type of join to use
between the tables.
An optional, but commonly used, clause of the SELECTstatement is the WHEREclause. This clause
allows you to specify a condition. When specified, this clause retrieves only those records that meet the
condition in a practical implementation of the relational selection operation.
The SELECTstatement also has the ORDERBYclause. This is an optional clause that enables you to
sequence the results of your query.
The GROUPBYclause is an optional clause of the SELECTstatement. This clause enables you to group
the results of your query. The GROUPBYclause can sometimes be accompanied by the HAVING
clause, which is used to add a condition to filter your grouped query results.

Supplement
Selecting the link title opens the resource in a new browser window.

Job Aid
Access the job aid SELECT Statement Syntax for the complete syntax of the SELECT statement.

Suppose that you want to retrieve the details of all employees from the Employees table who joined the company
after February 12, 2010.

Code
SELECT EmployeeID, EmployeeName
FROM Employees
WHERE JoiningDate >= 02/12/2010

https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

4/14

2/14/13

Topic Transcript

In the FROMclause, you specify the name of the table as Employees. And, specify the column that contains the
date the employee was hired, in this case JoiningDate, in the WHEREclause condition to find the employees
whose JoiningDateis greater than or equal to February 12, 2010 and then returns only those rows in the
results table.

Code
SELECT EmployeeID, EmployeeName
FROM Employees
WHERE JoiningDate >= 02/12/2010

Another DML statement is the INSERTstatement. This statement allows you to add records to a table. To do
this, you use the INSERTINTOclause followed by the name of the table to which the record is to be added.

Syntax
INSERT INTO Tab leName [(ColumnOne, ColumnTwo, ColumnThree,...)]
VALUES (ValueOne, ValueTwo, ValueThree,...)

In the INSERTstatement, you need to specify the name of the table. The name of the table can be followed by
the names of columns to which you want to add a value. However, if you want to add values to all columns, you do
not need to specify any of the columns. Then, you specify the values separated by commas in the VALUES
clause. These values need to be in the same order as the names of columns listed and should satisfy all
constraints of the table.

Syntax
INSERT INTO Tab leName [(ColumnOne, ColumnTwo, ColumnThree,...)]
VALUES (ValueOne, ValueTwo, ValueThree,...)

For example, you can use the INSERTstatement to add Bob Jones to the Employees table along with his
employee number E251. In this case, the Employees table only contains the three columns, therefore we can use
the second statement to insert the row as well, instead of listing all columns.
If the table contained more than three columns, then the column names would need to be specified, or NULL
would have to be used in the VALUESclause so each column had a value inserted or an error would be raised.

Code
INSERT INTO Employees (EmpID, FirstName, LastName)
VALUES (E251, Bob, Jones)
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

5/14

2/14/13

Topic Transcript

INSERT INTO Employees


VALUES (E251, Bob, Jones)

Another DML statement is UPDATE. This statement allows you to edit one or more values in one or more rows in
a table. To use this statement, you specify the name of the table by using the UPDATEclause and then specify
the change by using the SETclause. In this clause, you assign the required value to the appropriate column.

Syntax
UPDATE Tab leName
SET ColumnName = 'value'
[WHERE Condition]

To specify the record in which the value of the specified column should be changed, you use the optional WHERE
clause. In this clause, you specify the condition that should be met for the update, which is a practical
implementation of the relational selection operation. If the WHEREclause is not specified, all rows are updated with
the new value.

Syntax
UPDATE Tab leName
SET ColumnName = 'value'
[WHERE Condition]

Consider that the name of the Education and Training department was changed to Training and Development. To
reflect this change, you can specify that you want to change the department name field to Training and
Development. You use the WHEREclause to ensure only the rows that contain Education and Training in the
DeptNamecolumn are changed.

Code
UPDATE Employees
SET DeptName = 'Training and Development'
WHERE DeptName = 'Education and Training'

The DELETEstatement is another commonly used DML statement that is used to delete specific records from a
table. This is done by using the DELETEFROMclause. This clause is followed by the name of the table from
which records need to be deleted.

https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

6/14

2/14/13

Topic Transcript

Code
DELETE FROM TableName
[WHERE condition]

To restrict the record or records that should be deleted, the DELETEFROMclause is followed by the optional
WHEREclause. In this clause, you define the condition that defines the records that should be deleted. This
condition retrieves selective records and is a practical implementation of the relational selection operation. If this
clause is omitted, all the records are deleted from the table.

Code
DELETE FROM TableName
[WHERE condition]

For example, consider that an employee whose employee ID is 251 resigns and you want to delete this
employee's record. To do this, you specify the Employeestable in the FROMclause and check for employee ID
E251in the WHEREclause.
In the second example, the WHEREclause is not used, and therefore all records in the table are deleted.

Code
DELETE FROM Employees
WHERE EmpID = 'E251'
DELETE FROM Employees

There are three commonly used DDL statements that are used to manage database objects, which are
CREATE
ALTER, and
DROP
The CREATEstatement allows you to design and create a table.

Syntax
CREATE TABLE Tab leName(
column DataType,
column DataType,
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

7/14

2/14/13

Topic Transcript

column DataType NOT NULL,


PRIMARY KEY (column)
CONSTRAINT ConstraintName
FOREIGN KEY (column)
REFERENCES Tab leName (column));

To use the CREATETABLEstatement, you first specify the name of the table you want to create in the
CREATETABLEclause.
After specifying the name of the table you want to create, you name each column in the table. This
name is followed by the data type for the column. Some commonly used data types are INT, CHAR,
VARCHAR, and DATE. Of these data types, INTallows only numbers, CHARallows only characters. The
VARCHARdata type allows you to enter both numbers and characters and can be a variable length, and
the DATEdata type accepts only dates and you can specify the format in which they must be entered.
You can also define the length of fields by specifying a number in parenthesis after the data type, some
data types, such as NUMBER, require this. Additionally, you can specify that a field cannot contain null
values by adding the NOTNULLclause at the end of the field declaration.
When you have specified fields for the table, you can define a primary k ey for the table. This k ey can
either be a single field or a combination of fields that is used to uniquely identify the rows in the table.
To do this, you use the PRIMARYKEYclause followed by the appropriate field names in parentheses.
If the table you are creating requires a foreign k ey constraint, you can add it using the FOREIGNKEY
clause. In this clause, you specify the column in the table that will have the constraint on it along with
the k eyword REFERENCES. This k eyword is followed by the name of the table and the column it
references. This is usually the primary k ey of the other table.

Supplement
Selecting the link title opens the resource in a new browser window.

Job Aid
Access the job aid CREATE TABLE Statement Syntax for the complete syntax of the CREATE
TABLE statement.

For example, suppose that you want to create a table named Sales. The table should contain a sales ID, the date
on which the sale was made, the ID of the product that was sold, and the quantity of the product sold. You also
want the combination of sales ID and product ID to be the primary key of the table because they will uniquely
identify the rows, and you want to ensure that no field is left blank. A foreign key constraint will also need to be
added to the table to reference the SalesEmployeestable for the SalesIDcolumn.

Code
CREATE TABLE Sales(
SalesID
INT
NOT NULL,
SalesDate DATE
NOT NULL,
ProductID VARCHAR(6) NOT NULL,
Quantity
NUMBER(5) NOT NULL,
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

8/14

2/14/13

Topic Transcript

PRIMARY KEY (SalesID, ProductID),


CONSTRAINT SalesIDFK
FOREIGN KEY (SalesID)
REFERENCES SalesEmployees (SalesID),
);

To create this table, you first specify Salesas the table name by using the CREATETABLEclause. Then you
declare the four columns of the table. SalesIDis an integer, INT, to identify the sales person and ProductID
has the VARCHARfield type because it will contain a combination of characters and numbers. You must also
specify the maximum length of the value that can be specified, in this case six. SalesDateis a DATEfield while
Quantity is a NUMBERthat can contain up to 5 digits. You can also add the NOTNULLclause at the end of the
declaration of each column to prevent them from being left blank.

Code
CREATE TABLE Sales(
SalesID
INT
NOT NULL,
SalesDate DATE
NOT NULL,
ProductID VARCHAR(6) NOT NULL,
Quantity
NUMBER(5) NOT NULL,

After declaring the columns, you can specify the SalesIDand ProductIDcolumns as the primary keys of the
table. You then specify SalesIDas the foreign key of the Salestable and that it references the SalesID
column in the SalesEmployeestable.

Code
CREATE TABLE Sales(
SalesID
INT
NOT NULL,
SalesDate DATE
NOT NULL,
ProductID VARCHAR(6) NOT NULL,
Quantity
NUMBER(5) NOT NULL,
PRIMARY KEY (SalesID, ProductID),
CONSTRAINT SalesIDFK
FOREIGN KEY (SalesID)
REFERENCES SalesEmployees (SalesID),
);

DDL statements also allow you to modify the structure of existing objects using the ALTERstatement. You can
use this statement to add and delete columns from a table. You can also use it to modify the data type of an
existing column. To do this, you first use the ALTERTABLEstatement followed by the name of the table you want
to modify.

Syntax
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

9/14

2/14/13

Topic Transcript

ALTER Tab leName


ADD columnName DataType(Length) [NOT NULL];
DROP columnName [RESTRICT] [CASCADE] ;
ALTER columnName DataType(Length) [NOT NULL];
ADD constraint
DROP constraint

The ALTERTABLEstatement accepts these clauses, which you can use according to the changes you want to
make to the table:

Syntax
ALTER Tab leName
ADD columnName DataType(Length) [NOT NULL];
DROP columnName [RESTRICT] [CASCADE] ;
ALTER columnName DataType(Length) [NOT NULL];
ADD constraint
DROP constraint

ADD
The ADDclause accepts the name of the column and the data type of the column you want to add. You
can also use this clause to specify the length of the field and whether it can accept NULLvalues.
The ADDclause can also be used to add constraints to a table, by specifying the constraint definition.
DROP, and
You can specify the name of the column you want to delete by using the DROPclause. The specified
column is deleted from the table specified by using the ALTERstatement. To ensure data integrity, you
can specify two additional columns, RESTRICTand CASCADE. RESTRICTcan be used to ensure that
no other objects depend on the column being dropped. If there is a dependency, the DROPclause results
in an error. CASCADEcan be used to drop any objects that depend on the column being dropped. You
can also use the DROPclause to remove a constraint from a table by specifying the constraint name.
ALTER
Using the ALTERclause, you can change the column definition in the table, such as changing the data
type or by adding a constraint, such as NULLor NOT NULL. To do this, you specify the name of an
existing column along with the data type you want to change it to. You also specify the maximum length
each value.

https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

10/14

2/14/13

Topic Transcript

Supplement
Selecting the link title opens the resource in a new browser window.

Job Aid
Access the job aid ALTER TABLE Statement Syntax for the complete syntax of the ALTER TABLE
statement.

For example, say that you want to add a customer ID column to the Salestable. To do this, you can use the
ADDclause to specify the details of the new field. You can also append the declaration with the NOTNULL
constraint to ensure that the field isn't left blank.

Code
ALTER Sales
ADD CustomerID VARCHAR(10) NOT NULL;

The DDL statement DROPenables you to delete a table. This statement accepts the name of the table you want
to delete after the DROPTABLEclause.

Syntax
DROP TABLE Tab leName

For example, if you want to delete the table that contains the details of an old project, you can specify the name
of the table after the DROPTABLEclause.

Code
DROP TABLE OldTable

DCL also has certain commonly used statements. These statements are the GRANTand REVOKEstatements.
The GRANTstatement specifies the DML statements that a user is allowed to perform. For example, you can
specify that a user can retrieve and modify records and is therefore, allowed to perform both the SELECTand
UPDATEstatements. You can also indicate whether the user can grant the same privileges to other users by
using the WITHGRANTOPTION.

https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

11/14

2/14/13

Topic Transcript

Syntax
GRANT [ALL | privileges]
ON ob ject
TO [user | PUBLIC]
WITH GRANT OPTION

The GRANTstatement accepts two clauses ONand TO. The ONclause is used to specify the table on which the
rights are to be granted. The TOclause defines the user who is being granted the right. You can also specify the
PUBLICkeyword, which will grant all privileges to all the users.

Syntax
GRANT [ALL | privileges]
ON ob ject
TO [user | PUBLIC]
WITH GRANT OPTION

Note
Some RDBMSs, such as DB2, allow you to specify multiple users in the TOclause b y separating
them with commas.

For example, consider that you want to only allow the administrator to select, update, and delete data from the
Sales table, and have the user, UserOne, to only select and update the data. To do this, you need to use two
GRANTstatements for each user. The first statement grants the administrator their permissions and the second
statement grants the applicable privileges to the UserOne user.

Code
GRANT SELECT, UPDATE, DELETE
ON Sales
TO Administrator
GRANT SELECT, UPDATE
ON Sales
TO UserOne

The format of the REVOKEstatement is similar to the GRANTstatement. This statement also accepts the DML
statements that users are allowed to perform along with the name of the table and the users or PUBLIC. The only
difference is that this statement removes the users' right to perform the specified statements. For example, you
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

12/14

2/14/13

Topic Transcript

can prevent users from deleting data from a table.

Syntax
REVOKE [GRANT OPTION FOR ] privilege
ON Tab leName
FROM [user | PUBLIC]
[CASCADE | RESTRICT]

Now consider that a user named, UserN has left the organization. So you want to revoke the rights of this user
from the Sales table. To do this, you can use the REVOKEstatement.

Code
REVOKE SELECT, UPDATE
ON Sales
FROM UserN

Question
You want to retrieve the details of all employees who have worked on a particular project.
Which statement will you use?
Options:
1. SELECT
2. UPDATE
3. CREATE
4. GRANT

Answer
Option 1: This option is correct. The SELECTstatement is a DML statement that is used to retrieve
data from a tab le. This command accepts the FROMand WHEREclauses that help specify the tab le
and the records that need to b e retrieved.
Option 2: This option is incorrect. The UPDATEstatement is a DML statement that allows you to edit
the values present in a tab le. This statement doesn't help you retrieve data from the tab le.
Option 3: This option is incorrect. The CREATEstatement is a DDL statement that allows you to
design and create a tab le that contains the fields you specify. This statement doesn't help you
retrieve data from the tab le.
Option 4: This option is incorrect. The GRANTstatement is a DCL statement that defines the DML
https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

13/14

2/14/13

Topic Transcript

statements that a user is allowed to perform. This statement doesn't help you retrieve data from the
tab le.
Correct answer(s):
1. SELECT

Summary
SQL is one of the most commonly used methods to manage the data in a relational database. This continuously
evolving language enables you to define, retrieve, manipulate, control, share, and secure relational data.
SQL statements can be divided into subcategories according to their function. So while DML statements enable
you to retrieve the required data, DDL statements enable you to create a structure for the data. SQL also has DCL
statements that allow you to control what each user can do to the data stored in the database.

2013 SkillSoft Ireland Limited

https://xlibrary.skillport.com/courseware/Content/cca/df_dbfn_a02_it_enus//output/t5/misc/transcript.html

14/14