Vous êtes sur la page 1sur 47

SQL Definition: The structured query language is an industry-standard language used

for manipulation of data in a relational database. The major SQL commands of


interest to database users are SELECT, INSERT, JOIN and UPDATE.

Transact-SQL Definition: Transact-SQL is Microsoft's proprietary extension to the


Structured Query Language (SQL). It includes additional functionality designed to
support Microsoft SQL Server.

Microsoft SQL Server provides the stored procedure mechanism to simplify the database
development process by grouping Transact-SQL statements into manageable blocks.

Stored ProcedureDefinition: Stored procedures are precompiled database queries


that improve the security, efficiency and usability of database client/server
applications. Developers specify a stored procedure in terms of input and output
variables. They then compile the code on the database platform and make it
available to aplication developers for use in other environments, such as web
applications. All of the major database platforms, including Oracle, SQL Server and
MySQL support stored procedures. The major benefits of this technology are the
substantial performance gains from precompiled execution, the reduction of
client/server traffic, development efficiency gains from code reuse and abstraction
and the security controls inherent in granting users permissions on specific stored
procedures instead of the underlying database tables.

Benefits of Stored Procedures

Why should you use stored procedures? Let's take a look at the key benefits of this
technology:

• Precompiled execution. SQL Server compiles each stored procedure once


and then reutilizes the execution plan. This results in tremendous performance
boosts when stored procedures are called repeatedly.
• Reduced client/server traffic. If network bandwidth is a concern in your
environment, you'll be happy to learn that stored procedures can reduce long SQL
queries to a single line that is transmitted over the wire.
• Efficient reuse of code and programming abstraction. Stored procedures
can be used by multiple users and client programs. If you utilize them in a
planned manner, you'll find the development cycle takes less time.
• Enhanced security controls. You can grant users permission to execute a
stored procedure independently of underlying table permissions.

Structure

Stored procedures are extremely similar to the constructs seen in other programming
languages. They accept data in the form of input parameters that are specified at execution
time. These input parameters (if implemented) are utilized in the execution of a series of
statements that produce some result. This result is returned to the calling environment
through the use of a recordset, output parameters and a return code. That may sound like a
mouthful, but you'll find that stored procedures are actually quite simple.
Example

Let's take a look at a practical example. Assume we have the table shown at the bottom of
this page, named Inventory. This information is updated in real-time and warehouse managers
are constantly checking the levels of products stored at their warehouse and available for
shipment. In the past, each manager would run queries similar to the following:
SELECT Product, Quantity
FROM Inventory
WHERE Warehouse = 'FL'
This resulted in very inefficient performance at the SQL Server. Each time a warehouse
manager executed the query, the database server was forced to recompile the query and
execute it from scratch. It also required the warehouse manager to have knowledge of SQL
and appropriate permissions to access the table information.

We can simplify this process through the use of a stored procedure. Let's create a procedure
called sp_GetInventory that retrieves the inventory levels for a given warehouse. Here's the
SQL code:
CREATE PROCEDURE sp_GetInventory
@location varchar(10)
AS
SELECT Product, Quantity
FROM Inventory
WHERE Warehouse = @location
Our Florida warehouse manager can then access inventory levels by issuing the command
EXECUTE sp_GetInventory 'FL'
The New York warehouse manager can use the same stored procedure to access that area's
inventory.
EXECUTE sp_GetInventory 'NY'
Granted, this is a simple example, but the benefits of abstraction can be seen here. The
warehouse manager does not need to understand SQL or the inner workings of the procedure.
From a performance perspective, the stored procedure will work wonders. The SQL Sever
creates an execution plan once and then reutilizes it by plugging in the appropriate
parameters at execution time.

Now that you've learned the benefits of stored procedures, get out there and use them! Try a
few examples and measure the performance enhancements achieved -- you'll be amazed!

Inventory Table

ID Product Warehouse Quantity


142 Green beans NY 100
214 Peas FL 200
825 Corn NY 140
512 Lima beans NY 180
491 Tomatoes FL 80
379 Watermelon FL 85
SQL Statements for Creating a Stored Procedure
A stored procedure is a group of SQL statements that form a logical unit and perform a
particular task, and they are used to encapsulate a set of operations or queries to execute
on a database server. For example, operations on an employee database (hire, fire,
promote, lookup) could be coded as stored procedures executed by application code.
Stored procedures can be compiled and executed with different parameters and results,
and they may have any combination of input, output, and input/output parameters.

Note: Stored procedures are supported by most DBMSs, but there is a fair amount of
variation in their syntax and capabilities. For this reason, this simple example of what a
stored procedure looks like and how it is invoked from JDBC is not intended to be run.

This simple stored procedure has no parameters. Even though most stored procedures do
something more complex than this example, it serves to illustrate some basic points about
them. As previously stated, the syntax for defining a stored procedure is different for each
DBMS. For example, some use begin . . . end , or other keywords to indicate the
beginning and ending of the procedure definition. In some DBMSs, the following SQL
statement creates a stored procedure:

create procedure SHOW_SUPPLIERS


as
select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME
from SUPPLIERS, COFFEES
where SUPPLIERS.SUP_ID = COFFEES.SUP_ID
order by SUP_NAME

The following code puts the SQL statement into a string and assigns it to the variable
createProcedure, which we will use later:

String createProcedure = "create procedure SHOW_SUPPLIERS " +


"as " +
"select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " +
"from SUPPLIERS, COFFEES " +
"where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " +
"order by SUP_NAME";

The following code fragment uses the Connection object con to create a Statement
object, which is used to send the SQL statement creating the stored procedure to the
database:

Statement stmt = con.createStatement();


stmt.executeUpdate(createProcedure);

The procedure SHOW_SUPPLIERS is compiled and stored in the database as a database


object that can be called, similar to the way you would call a method.
Calling a Stored Procedure from JDBC

JDBC allows you to call a database stored procedure from an application written in the
Java programming language. The first step is to create a CallableStatement object. As
with Statement and PreparedStatement objects, this is done with an open Connection
object. A callableStatement object contains a call to a stored procedure; it does not
contain the stored procedure itself. The first line of code below creates a call to the stored
procedure SHOW_SUPPLIERS using the connection con. The part that is enclosed in curly
braces is the escape syntax for stored procedures. When the driver encounters "{call
SHOW_SUPPLIERS}", it will translate this escape syntax into the native SQL used by the
database to call the stored procedure named SHOW_SUPPLIERS.

CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");


ResultSet rs = cs.executeQuery();

The ResultSet rs will be similar to the following:

SUP_NAME COF_NAME
---------------- -----------------------
Acme, Inc. Colombian
Acme, Inc. Colombian_Decaf
Superior Coffee French_Roast
Superior Coffee French_Roast_Decaf
The High Ground Espresso

Note that the method used to execute cs is executeQuery because cs calls a stored
procedure that contains one query and thus produces one result set. If the procedure had
contained one update or one DDL statement, the method executeUpdate would have
been the one to use. It is sometimes the case, however, that a stored procedure contains
more than one SQL statement, in which case it will produce more than one result set,
more than one update count, or some combination of result sets and update counts. In this
case, where there are multiple results, the method execute should be used to execute the
CallableStatement .

The class CallableStatement is a subclass of PreparedStatement, so a


CallableStatement object can take input parameters just as a PreparedStatement
object can. In addition, a CallableStatement object can take output parameters, or
parameters that are for both input and output. INOUT parameters and the method
execute are used rarely.

Creating Complete JDBC Applications


Up to this point, you have seen only code fragments. Now you will see some samples.

The first sample code creates the table COFFEES; the second one inserts values into the
table and prints the results of a query. The third application creates the table SUPPLIERS,
and the fourth populates it with values. After you have run this code, you can try a query
that is a join between the tables COFFEES and SUPPLIERS, as in the fifth code example.
The sixth code sample is an application that demonstrates a transaction and also shows
how to set placeholder parameters in a PreparedStatement object using a for loop.

Because they are complete applications, they include some elements of the Java
programming language we have not shown before in the code fragments. We will explain
these elements briefly here.

Putting Code in a Class Definition

In the Java™ programming language, any code you want to execute must be inside a
class definition. You type the class definition in a file and give the file the name of the
class with .java appended to it. So if you have a class named MySQLStatement, its
definition should be in a file named MySQLStatement.java.

Importing Classes to Make Them Visible

The first thing to do is to import the packages or classes you will be using in the new
class. The classes in our examples all use the java.sql package (the JDBC™ API),
which becomes available when the following line of code precedes the class definition:

import java.sql.*;

The star ( * ) indicates that all of the classes in the package java.sql are to be imported.
Importing a class makes it visible and means that you do not have to write out the fully
qualified name when you use a method or field from that class. If you do not include "
import java.sql.*; " in your code, you will have to write " java.sql. " plus the
class name in front of all the JDBC fields or methods you use every time you use them.
Note that you can import individual classes selectively rather than a whole package. Java
does not require that you import classes or packages, but doing so makes writing code a
lot more convenient.

Any lines importing classes appear at the top of all the code samples, as they must if they
are going to make the imported classes visible to the class being defined. The actual class
definition follows any lines that import classes.

Using the main Method

If a class is to be executed, it must contain a static public main method. This method
comes right after the line declaring the class and invokes the other methods in the class.
The keyword static indicates that this method operates on a class level rather than on
individual instances of a class. The keyword public means that members of any class
can access this method. Since we are not just defining classes to be used by other classes
but instead want to run them, the example applications in this chapter all include a main
method.
Using try and catch Blocks

Something else all the sample applications include is try and catch blocks. These are
the Java programming language's mechanism for handling exceptions. Java requires that
when a method throws an exception, there be some mechanism to handle it. Generally a
catch block will catch the exception and specify what happens (which you may choose
to be nothing). In the sample code, we use two try blocks and two catch blocks. The
first try block contains the method Class.forName, from the java.lang package. This
method throws a ClassNotFoundException, so the catch block immediately following
it deals with that exception. The second try block contains JDBC methods, which all
throw SQLExceptions, so one catch block at the end of the application can handle all of
the rest of the exceptions that might be thrown because they will all be SQLException
objects.

Retrieving Exceptions

JDBC lets you see the warnings and exceptions generated by your DBMS and by the Java
compiler. To see exceptions, you can have a catch block print them out. For example,
the following two catch blocks from the sample code print out a message explaining the
exception:

try {
// Code that could generate an exception goes here.
// If an exception is generated, the catch block below
// will print out information about it.
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}

try {
Class.forName("myDriverClassName");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

If you were to run CreateCOFFEES.java twice, you would get an error message similar
to this:

SQLException: There is already an object named 'COFFEES'


in the database.
Severity 16, State 1, Line 1

This example illustrates printing out the message component of an SQLException object,
which is sufficient for most situations.

There are actually three components, however, and to be complete, you can print them all
out. The following code fragment shows a catch block that is complete in two ways.
First, it prints out all three parts of an SQLException object: the message (a string that
describes the error), the SQL state (a string identifying the error according to the X/Open
SQLState conventions), and the vendor error code (a number that is the driver vendor's
error code number). The SQLException object ex is caught, and its three components are
accessed with the methods getMessage , getSQLState , and getErrorCode .

The second way the following catch block is complete is that it gets all of the exceptions
that might have been thrown. If there is a second exception, it will be chained to ex, so
ex.getNextException is called to see if there is another exception. If there is, the while
loop continues and prints out the next exception's message, SQLState, and vendor error
code. This continues until there are no more exceptions.

try {
// Code that could generate an exception goes here.
// If an exception is generated, the catch block below
// will print out information about it.
} catch(SQLException ex) {
System.out.println("\n--- SQLException caught ---\n");
while (ex != null) {
System.out.println("Message: "
+ ex.getMessage ());
System.out.println("SQLState: "
+ ex.getSQLState ());
System.out.println("ErrorCode: "
+ ex.getErrorCode ());
ex = ex.getNextException();
System.out.println("");
}
}

If you were to substitute the catch block above into the code and run it after the table
COFFEES had already been created, you would get the following printout:

--- SQLException caught ---


Message: There is already an object named 'COFFEES' in the database.
Severity 16, State 1, Line 1
SQLState: 42501
ErrorCode: 2714

The vendor error code is specific to each driver, so you need to check your driver
documentation for a list of error codes and what they mean.

Retrieving Warnings

SQLWarning objects are a subclass of SQLException that deal with database access
warnings. Warnings do not stop the execution of an application, as exceptions do; they
simply alert the user that something did not happen as planned. For example, a warning
might let you know that a privilege you attempted to revoke was not revoked. Or a
warning might tell you that an error occurred during a requested disconnection.
A warning can be reported on a Connection object, a Statement object (including
PreparedStatement and CallableStatement objects), or a ResultSet object. Each of
these classes has a getWarnings method, which you must invoke in order to see the first
warning reported on the calling object. If getWarnings returns a warning, you can call
the SQLWarning method getNextWarning on it to get any additional warnings. Executing
a statement automatically clears the warnings from a previous statement, so they do not
build up. This means, however, that if you want to retrieve warnings reported on a
statement, you must do so before you execute another statement.

The following code fragment illustrates how to get complete information about any
warnings reported on the Statement object stmt and also on the ResultSet object rs :

Statement stmt = con.createStatement();


ResultSet rs = stmt.executeQuery("select COF_NAME from COFFEES");
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
System.out.println("Coffees available at the Coffee Break: ");
System.out.println(" " + coffeeName);
SQLWarning warning = stmt.getWarnings();
if (warning != null) {
System.out.println("\n---Warning---\n");
while (warning != null) {
System.out.println("Message: "
+ warning.getMessage());
System.out.println("SQLState: "
+ warning.getSQLState());
System.out.print("Vendor error code: ");
System.out.println(warning.getErrorCode());
System.out.println("");
warning = warning.getNextWarning();
}
}
SQLWarning warn = rs.getWarnings();
if (warn != null) {
System.out.println("\n---Warning---\n");
while (warn != null) {
System.out.println("Message: "
+ warn.getMessage());
System.out.println("SQLState: "
+ warn.getSQLState());
System.out.print("Vendor error code: ");
System.out.println(warn.getErrorCode());
System.out.println("");
warn = warn.getNextWarning();
}
}
}

Warnings are actually rather uncommon. Of those that are reported, by far the most
common warning is a DataTruncation warning, a subclass of SQLWarning. All
DataTruncation objects have an SQLState of 01004, indicating that there was a problem
with reading or writing data. DataTruncation methods let you find out in which column
or parameter data was truncated, whether the truncation was on a read or write operation,
how many bytes should have been transferred, and how many bytes were actually
transferred

Introduction to Microsoft SQL Server 2005


After a long gap in the release of SQL Server databases, Microsoft recently released SQL
Server 2005 (formerly code-named Yukon). In this substantial upgrade, they've packed the
new database engine full of features. Probably the most significant one that will catch your
attention is the price tag – it’s up to 25% higher than SQL Server 2000. A single processor
license for SQL Server 2005 Enterprise Edition will set you back approximately $25,000. That’s
not cheap, but Microsoft has made some great advances in functionality that make up the
difference.

In this first part of our series on this new product, let’s take a look at the four different
editions of SQL Server 2005 that Microsoft plans to release:

• SQL Server 2005 Express replaces the Microsoft Data Engine (MSDE) as the
free version of SQL Server for application development and lightweight use. It
remains free and retains the limitations of MSDE with respect to client
connections and performance. It’s a great tool for developing and testing
applications and extremely small implementations, but that’s about as far as you
can run with it.
• SQL Server 2005 Workgroup is the new entrant in the product line. It’s
billed as a “small business SQL Server” and it offers an impressive array of
functionality for a $3,899 price tag per processor. (It’s also available under a 5-
user license for $739). Workgroup edition maxes out at 2 CPUs with 3GB of RAM
and allows for most of the functionality you’d expect from a server-based
relational database. It offers limited replication capabilities as well.

• The workhorse SQL Server 2005 Standard Edition remains the staple of the
product line for serious database applications. It can handle up to 4 CPUs with an
unlimited amount of RAM. Standard Edition 2005 introduces database mirroring
and integration services. It’s priced at $5,999 for a processor or $2,799 for 5
users.
• The big kid on the block is SQL Server 2005 Enterprise Edition. With the
release of 2005, Enterprise Edition allows unlimited scalability and partitioning.
It’s truly an enterprise-class database and it’s hefty price tag ($24,999 per
processor or $13,499 for 5 users) reflects its value.

Those are the basics of SQL Server 2005! In future weeks, we’ll explore some of the
new functionality offered by this powerful database.

Creating Tables in Microsoft SQL Server


SQL Server databases rely upon tables to store data. In this tutorial, we'll explore the process
of designing and implementing a database table in Microsoft SQL Server.
The first step of implementing a SQL Server table is decidedly non-technical. Sit down with a
pencil and paper and sketch out the design of your database. You'll want to ensure that you
include appropriate fields for your business needs and select the correct data types to hold
your data.

You may wish to consult our Database Normalization Basics article for some tips on proper
database design
Start SQL Server Management Studio

Open Microsoft SQL Server Management Studio (SSMS) and connect to the server where you'd
like to add a new table.

Expand the Tables Folder for the Appropriate Database

Once you've connected to the right SQL Server, expand the Databases folder and select the
database where you'd like to add a new table. Expand that database's folder and then expand
the Tables subfolder.
Start Table Designer
Right-click on the Tables subfolder and select the New Table option. This will start SQL
Server's graphical Table Designer, as shown in the image above.

Add Columns to Your Table

Now it's time to add the columns you designed in step 1. Begin by clicking in the first empty
cell under the Column Name heading in Table Designer.
Once you've entered an appropriate name, select the data type from the drop-down box in the
next column. If you're using a data type that allows different lengths, you may specify the
exact length by changing the value that appears in parentheses following the data type name.

If you'd like to allow NULL values in this column, click "Allow Nulls".

Repeat this process until you've added all necessary columns to your SQL Server database
table.

Select a Primary Key


Next, highlight the column(s) that you've selected for your table's primary key. Then click the
key icon in the taskbar to set the primary key. If you have a multivalued primary key, use the
CTRL key to highlight multiple rows before clicking the key icon.
Once you've done this, the primary key column(s) will have a key symbol, as shown in the
image above.

If you need assistance, you might want to read the article Selecting a Primary Key.

Save Your New Table


Don't forget to save your table! When you click the save icon for the first time, you'll be asked
to provide a unique name for your table.
http://databases.about.com/od/sql/Structure
d_Query_Language_SQL.htm

Learning SQL
The Structured Query Language (SQL) forms the backbone of all relational databases. This language offers a flexibl
shapes and sizes and is used as the basis for all user and administrator interactions with the database.

1. Introducing SQL
2. Retrieving Data
3. Manipulating Databases

Introducing SQL
Learning SQL is an important step in developing your database skills. In this section, you'll
learn the basics of the Structured Query Language and discover resources helpful to advance
your skills

• SQL Fundamentals
• Introducing to SQL
• Top 6 SQL Books
• Structured Query Language (SQL)

Retrieving Data
Once you have your data in a SQL database, it's important to know how to retrieve it properly.
In this section, you'll learn how to retrieve data with basic SELECT statements. You'll also
discover how you can leverage the power of SQL to perform advanced table joins and other
complex queries.

• Retrieving Data with SQL Queries: Introducing the SELECT Statement


• Retrieving Data from Multiple Tables with SQL Joins
• Using Self-Joins in SQL

• NULLs and JOINs


• Aggregate Functions in SQL
• Summarizing Data with CUBE and ROLLUP

Manipulating Databases
The Data Manipulation Language (DML) is a subset of SQL allowing you to manipulate the
underlying structure of your database. In this section, you'll learn how to create databases
and tables, delete data from your database and understand the complexities of NULL values.

• Creating Databases and Tables in SQL


• Deleting Data from an SQL Table
• All About NULL Values
SQL Fundamentals
The Structured Query Language (SQL) comprises one of the fundamental building blocks of
modern database architecture. SQL defines the methods used to create and manipulate
relational databases on all major platforms. At first glance, the language may seem
intimidating and complex but it's really not all that bad. In a series of articles over the next
few weeks we'll explore the inner workings of SQL together. By the time we're through, you'll
have the fundamental knowledge you need to go out there and start working with databases!

This week, our first article in the SQL series provides an introduction to the basic concepts
behind SQL and we'll take a brief look at some of the main commands used to create and
modify databases. Throughout this article, please keep our goal in mind: we're trying to getthe
"big picture" of SQL -- not a mastery of the individual commands. We'll provide a few
examples for illustrative purposes and explain the theory behind them, but don't feel
frustrated if you can't write your own SQL commands after reading this article. We'll cover
each of the major commands in detail in future weekly installments. If you'd like a reminder in
your e-mail inbox each week when the next article is posted, please take a moment and
subscribe to our newsletter.

By the way, the correct pronunciation of SQL is a contentious issue within the database
community. In their SQL standard, the American National Standards Institute declared that
the official pronunciation is "es queue el." However, many database professionals have taken
to the slang pronunciation "sequel." The choice is yours.

SQL comes in many flavors. Oracle databases utilize their proprietary PL/SQL. Microsoft SQL
Server makes use of Transact-SQL. However, all of these variations are based upon the
industry standard ANSI SQL. In our tutorial series, we'll stick to ANSI-compliant
SQLcommands that will work on any modern relational database system.

SQL commands can be divided into two main sublanguages. The Data Definition Language
(DDL) contains the commands used to create and destroy databases and database objects.
After the database structure is defined with DDL, database administrators and users can utilize
the Data Manipulation Language to insert, retrieve and modify the data contained within it. In
the next two sections of this article, we'll explore DDL and DML in further detail. In future
articles we'll take an in-depth look at specific SQL commands.

Now, let's take a look at the Data Definition Language. Read on!

Data Definition Language


The Data Definition Language (DDL) is used to create and destroy databases and database
objects. These commands will primarily be used by database administrators during the setup
and removal phases of a database project. Let's take a look at the structure and usage of four
basic DDL commands:

The Data Definition Language (DDL) is used to create and destroy databases and database
objects. These commands will primarily be used by database administrators during the setup
and removal phases of a database project. Let's take a look at the structure and usage of four
basic DDL commands:
CREATE

Installing a database management system (DBMS) on a computer allows you to create and
manage many independent databases. For example, you may want to maintain a database of
customer contacts for your sales department and a personnel database for your HR
department. The CREATE command can be used to establish each of these databases on your
platform. For example, the command:

CREATE DATABASE employees


creates an empty database named "employees" on your DBMS. After creating the database,
your next step is to create tables that will contain data. (If this doesn't make sense, you might
want to read the article Microsoft Access Fundamentals for an overview of tables and
databases.) Another variant of the CREATE command can be used for this purpose. The
command:
CREATE TABLE personal_info (first_name char(20) not null, last_name char(20) not null,
employee_id int not null)

establishes a table titled "personal_info" in the current database. In our example, the table
contains three attributes: first_name, last_name and employee_id. Don't worry about the
other information included in the command -- we'll cover that in a future article.

USE

The USE command allows you to specify the database you wish to work with within your
DBMS. For example, if we're currently working in the sales database and want to issue some
commands that will affect the employees database, we would preface them with the following
SQL command:

USE employees

It's important to always be conscious of the database you are working in before issuing SQL
commands that manipulate data.

ALTER

Once you've created a table within a database, you may wish to modify the definition of it. The
ALTER command allows you to make changes to the structure of a table without deleting and
recreating it. Take a look at the following command:

ALTER TABLE personal_info


ADD salary money null

This example adds a new attribute to the personal_info table -- an employee's salary. The
"money" argument specifies that an employee's salary will be stored using a dollars and cents
format. Finally, the "null" keyword tells the database that it's OK for this field to contain no
value for any given employee.

DROP

The final command of the Data Definition Language, DROP, allows us to remove entire
database objects from our DBMS. For example, if we want to permanently remove the
personal_info table that we created, we'd use the following command:

DROP TABLE personal_info

Similarly, the command below would be used to remove the entire employees database:

DROP DATABASE employees

Use this command with care! Remember that the DROP command removes entire data
structures from your database. If you want to remove individual records, use the DELETE
command of the Data Manipulation Language.

That's the Data Definition Language in a nutshell. In the next section of this article, we'll take
a look at how the Data Manipulation Language is used to manipulate the information
Data Manipulation Language
The Data Manipulation Language (DML) is used to retrieve, insert and modify database
information. These commands will be used by all database users during the routine operation
of the database. Let's take a brief look at the basic DML commands:

The Data Manipulation Language (DML) is used to retrieve, insert and modify database
information. These commands will be used by all database users during the routine operation
of the database. Let's take a brief look at the basic DML commands:

INSERT
The INSERT command in SQL is used to add records to an existing table. Returning to the
personal_info example from the previous section, let's imagine that our HR department needs
to add a new employee to their database. They could use a command similar to the one shown
below:

INSERT INTO personal_info


values('bart','simpson',12345,$45000)

Note that there are four values specified for the record. These correspond to the table
attributes in the order they were defined: first_name, last_name, employee_id, and salary.

SELECT

The SELECT command is the most commonly used command in SQL. It allows database users
to retrieve the specific information they desire from an operational database. Let's take a look
at a few examples, again using the personal_info table from our employees database.

The command shown below retrieves all of the information contained within the personal_info
table. Note that the asterisk is used as a wildcard in SQL. This literally means "Select
everything from the personal_info table."

SELECT *
FROM personal_info

Alternatively, users may want to limit the attributes that are retrieved from the database. For
example, the Human Resources department may require a list of the last names of all
employees in the company. The following SQL command would retrieve only that information:

SELECT last_name
FROM personal_info

Finally, the WHERE clause can be used to limit the records that are retrieved to those that
meet specified criteria. The CEO might be interested in reviewing the personnel records of all
highly paid employees. The following command retrieves all of the data contained within
personal_info for records that have a salary value greater than $50,000:

SELECT *
FROM personal_info
WHERE salary > $50000

UPDATE

The UPDATE command can be used to modify information contained within a table, either in
bulk or individually. Each year, our company gives all employees a 3% cost-of-living increase
in their salary. The following SQL command could be used to quickly apply this to all of the
employees stored in the database:

UPDATE personal_info
SET salary = salary * 1.03
On the other hand, our new employee Bart Simpson has demonstrated performance above
and beyond the call of duty. Management wishes to recognize his stellar accomplishments with
a $5,000 raise. The WHERE clause could be used to single out Bart for this raise:

UPDATE personal_info
SET salary = salary + $5000
WHERE employee_id = 12345

DELETE

Finally, let's take a look at the DELETE command. You'll find that the syntax of this command
is similar to that of the other DML commands. Unfortunately, our latest corporate earnings
report didn't quite meet expectations and poor Bart has been laid off. The DELETE command
with a WHERE clause can be used to remove his record from the personal_info table:

DELETE FROM personal_info


WHERE employee_id = 12345
JOIN Statements
Now that you’ve learned the basics of SQL, it’s time to move on to one of the most powerful
concepts the language has to offer – the JOIN statement. Quite simply, these statements
allow you to combine data in multiple tables to quickly and efficiently process large quantities
of data. These statements are where the true power of a database resides.

We’ll first explore the use of a basic JOIN operation to combine data from two tables. In future
installments, we’ll explore the use of outer and inner joins to achieve added power.
We’ll continue with our example using the PERSONAL_INFO table, but first we’ll need to add
an additional table to the mix. Let’s assume we have a table called DISCIPLINARY_ACTION
that was created with the following statement:

CREATE TABLE disciplinary_action (action_id int not null, employee_id int not null, comments
char(500))

This table contains the results of disciplinary actions on company employees. You’ll notice that
it doesn’t contain any information about the employee other than the employee number. It’s
then easy to imagine many scenarios where we might want to combine information from the
DISCIPLINARY_ACTION and PERSONAL_INFO tables.

Assume we’ve been tasked with creating a report that lists the disciplinary actions taken
against all employees with a salary greater than $40,000. The use of a JOIN operation in this
case is quite straightforward. We can retrieve this information using the following command:
SELECT personal_info.first_name, personal_info.last_name, disciplinary_action.comments
FROM personal_info, disciplinary_action
WHERE personal_info.employee_id = disciplinary_action.employee_id
AND personal_info.salary > 40000

As you can see, we simply specified the two tables that we wished to join in the FROM clause
and then included a statement in the WHERE clause to limit the results to records that had
matching employee IDs and met our criteria of a salary greater than $40,000.

Congratulations! You've learned the basic concepts behind the Structured Query Language.
Stay tuned for future articles that explore these commands in-depth.
Structured Query Language (SQL)
The Structured Query Language (SQL) forms the backbone of most modern database systems.
These links provide the best resources on the Net for neophytes and expert database
administrators alike!
Joining Multiple Tables with SQL Inner Join Statements
You can use SQL JOIN statements to combine data from three or more tables. In an earlier
article, we took a look at using inner joins and outer joins to combine data from two different
tables. In many cases, youÂ’ll want to take this a step further and combine data from three or
more tables. Let's take a look at the SQL statements that allow you to accomplish this goal for
an inner join.

You can use SQL JOIN statements to combine data from three or more tables. In an earlier
article, we took a look at using inner joins and outer joins to combine data from two different
tables. In many cases, you’ll want to take this a step further and combine data from three or
more tables. Let's take a look at the SQL statements that allow you to accomplish this goal for
an inner join.

You may recall from our basic inner join example that the SQL statement below combines data
from the Drivers and Vehicles tables in cases where the driver and vehicle are located in the
same city:
SELECT lastname, firstname, tag
FROM drivers, vehicles
WHERE drivers.location = vehicles.location
This query produced the following results:
lastname firstname tag
-------- --------- ---
Baker Roland H122JM
Smythe Michael D824HA
Smythe Michael P091YF
Jacobs Abraham J291QR
Jacobs Abraham L990MT
Now, let’s extend this example to include a third table. Imagine that you wanted to include
only drivers and vehicles present at locations that are open on the weekend. You could bring a
third table into your query by extending the JOIN statement as follows:
SELECT lastname, firstname, tag, open_weekends
FROM drivers, vehicles, locations
WHERE drivers.location = vehicles.location
AND vehicles.location = locations.location
AND locations.open_weekends = 'Yes'
lastname firstname tag open_weekends
-------- --------- --- -------------
Baker Roland H122JM yes
Jacobs Abraham J291QR yes
Jacobs Abraham L990MT yes
This powerful extension to the basic SQL JOIN statement allows you to combine data in a
complex manner. In addition to combining tables with an inner join, you can also use this
technique to combine multiple tables using an outer join. As you may recall, outer joins
include results that exist in one table but do not have a corresponding match in the joined
table.

Self-Joins in SQL
You can use a self-join to simplify nested SQL queries where the inner and outer queries
reference the same table. These joins allow you to retrieve related records from the same
table.
You can use a self-join to simplify nested SQL queries where the inner and outer queries
reference the same table. These joins allow you to retrieve related records from the same
table. The most common case where you'd use a self-join is when you have a table that
references itself, such as the employees table shown below:

id first_name last_name manager


----------- --------------- --------------- -----------
1 Pat Crystal NULL
2 Dennis Miller 1
3 Jacob Smith 1
4 Allen Hunter 2
5 Mary Underwood 3
6 Joy Needham 3
In this table, the manager attribute simply references the employee ID of another employee in
the same table. For example, Dennis Miller reports to Pat Crystal. Pat is apparently the
president of this company, as she reports to no one.

Suppose you're tasked with writing a SQL query to retrieve a list of employees and their
managers. You can't write a basic SQL SELECT statement to retrieve this information, as you
need to cross reference information contained in other records within the same table.
Fortunately, you can use a self-join to solve this dilemma by joining the table to itself.

Here's the SQL statement that will retrieve the desired results:
SELECT e.first_name AS 'Employee FN', e.last_name AS 'Employee LN', m.first_name AS
'Manager FN', m.last_name AS 'Manager LN'
FROM employees AS e LEFT OUTER JOIN employees AS m
ON e.manager =m.id
And the corresponding output:
Employee FN Employee LN Manager FN Manager LN
--------------- --------------- --------------- ---------------
Pat Crystal NULL NULL
Dennis Miller Pat Crystal
Jacob Smith Pat Crystal
Allen Hunter Dennis Miller
Mary Underwood Jacob Smith
Joy Needham Jacob Smith
(6 row(s) affected)
Notice that it's extremely important to select the correct join type when writing a self-join. In
this case, we used a LEFT OUTER JOIN to ensure we had output records corresponding to each
employee. If we used an INNER JOIN instead, we would have omitted Pat Crystal, the
company president, from our list, as she does not have a manager.

Structured Query Language (SQL)


Most large-scale databases use the Structured Query Language (SQL) to define all user and
administrator interactions. This language offers a flexible interface for databases of all shapes
and sizes.

Most large-scale databases use the Structured Query Language (SQL) to facilitate user and
administrator interactions. This language offers a flexible interface for databases of all shapes
and sizes.

You might not see it, but SQL’s always there!

The first important point to make is that [em]all[/em] database transactions are made in SQL,
whether you realize it or not. Nowadays, there are a large number of graphical user interfaces
(GUIs) that simplify database administration tasks. If you're a SQL Server user, you may be
familiar with tools like Enterprise Manager.
MySQL users may use any of a number of front ends. There are also quite a few third-party
applications that interact with different databases (in fact, many of these can work with
multiple database platforms simultaneously.) Did you ever wonder how these applications
work? That’s right! They use SQL! The front-end translates your mouse clicks and text entries
into SQL and then “speaks” to the database in the universal language of SQL.

Flavors of SQL

SQL comes in many flavors. Oracle databases utilize their proprietary PL/SQL. Microsoft SQL
Server makes use of Transact-SQL. However, all of these variations are based upon the
industry standard ANSI SQL. In our tutorial series, we'll stick to ANSI-compliant SQL
commands that will work on any modern relational database system.

DDL and DML

SQL commands can be divided into two main sublanguages. The Data Definition Language
(DDL) contains the commands used to create and destroy databases and database objects.
After the database structure is defined with DDL, database administrators and users can use
the Data Manipulation Language to insert, retrieve and modify the data contained within it.

Is SQL Knowledge Required?

Do you need to know SQL? Not necessarily. If you’re comfortable using the graphical front end
tools, they might be more than sufficient to meet your needs. However, most serious database
administrators and developers rely upon custom-written SQL code to ensure that their
transactions meet user requirements in the most efficient manner possible. In any event, you
should at least have a passing familiarity with this important language that forms the bedrock
of relational databases.

Learning More

If you're ready to dive in and learn more about SQL, keep browsing this site. I suggest
starting with the SQL Fundamentals article.

Book Review: SQL Cookbook


O'Reilly's SQL Cookbook is the best reference I've seen for skilled SQL developers. Modeled
after a true cookbook, it offers many short snippets of SQL code designed to solve specific
problems. You can pick and choose the ingredients you need to help complete your SQL
project.

Retrieving Data with the SELECT Statement


The SELECT command is the most commonly used command in SQL. It allows database users
to retrieve the specific information they desire from an operational database.

The SELECT command is the most commonly used command in SQL. It allows database users
to retrieve the specific information they desire from an operational database. In a moment,
we’ll take a look at a few examples. First, we need to take a brief look at an example database
table.

Imagine that we’re building a database of employees for our company. We might want this
database to contain contact information, payroll details, job history, supervisor relationships,
performance reviews and tons of other information. For this example, we’ll focus on a single
table, called personal_info, which contains contact information for all of our employees.

The table might be set up with the following attributes:

• employee_id
• first_name
• last_name
• street_address
• city
• state
• zip
• phone

One of the first tasks we might face as the administrator of this database would be to simply
retrieve all of the information it contains. This can be done with the command shown below:

SELECT *
FROM personal_info;

Note that the asterisk is used as a wildcard in SQL. This literally means "Select everything
from the personal_info table."
Alternatively, users may want to limit the attributes that are retrieved from the database. For
example, the Human Resources department may require a list of the last names of all
employees in the company. The following SQL command would retrieve only that information:

SELECT last_name
FROM personal_info;

Finally, the WHERE clause can be used to limit the records that are retrieved to those that
meet specified criteria. Suppose the CEO is planning to visit California and wants to make sure
he is familiar with all of the employees living there. We might use the following command to
get the information he needs:

SELECT *
FROM personal_info
WHERE state = ‘CA’;

We can even get a little more complicated by combining several of these concepts. Let’s
imagine that the CEO is only visiting the Los Angeles office and isn’t interested in a great deal
of detail. He just wants to know the names of all employees in the Los Angeles, CA office. We
could use this SQL:

SELECT first_name, last_name


FROM personal_info
WHERE state=’CA’ and city=’Los Angeles’;

Those are the basics of the SELECT statement! You should now be able to retrieve information
from a database with simple queries!

Introduction to SQL
The Structured Query Language (SQL) is the language of databases. All modern relational
databases, including Access, FileMaker Pro, Microsoft SQL Server and Oracle use SQL as their
basic building block. In fact, it’s often the only way that you can truly interact with the
database itself.
Using Self-Joins in SQL
Did you know that you can use a self-join to simplify nested SQL queries where the inner and
outer queries reference the same table? Let's take a look at an example.

Did you know that you can use a self-join to simplify nested SQL queries where the inner and
outer queries reference the same table? Let's take a look at an example.

In our database we have the employees table shown below and we want to obtain a list of all
employees who live in the same town as your About.com Guide to Databases.

Table: Employees

• VARCHAR first_name
• VARCHAR last_name
• VARCHAR city

• VARCHAR state
• VARCHAR zip PRIMARY KEY

• We could use this SQL query:

SELECT last_name, first_name


FROM employees
WHERE zip in
( SELECT zip
FROM employees
WHERE last_name="Chapple"
AND first_name="Mike")

Or we could simplify the query using a nested join, as shown below:

SELECT e1.last_name, e1.first_name


FROM employees e1, employees e2
WHERE e1.zip = e2.zip
AND e2.last_name="Chapple"
AND e2.first_name="Mike"

You’ll undoubtedly find that using self-joins can simplify many SQL queries that make
multiple references to the same table. Relational databases that perform query
optimization are also capable of providing great performance enhancement for
queries written in this way.

SQL Visual Quickstart


This software-independent book takes a hands-on approach to learning SQL. After some brief
introductory material, it dives right into developing SQL queries designed to meet specific
goals. It's a great reference for those who occasionally use SQL and forget the details.
Creating Databases and Tables In SQL
Learn to create databases and tables using the Structured Query Language (SQL) in this step-
by-step tutorial from your About.com Guide to Databases.
SQL Fundamentals
Would you like to learn SQL? This tutorial will introduce you to the basics.
Access Controls in SQL
Our databases contain information that is vital to the continued operations of our
organizations. In this article, your About.com Guide introduces the SQL commands used to
control access to your database.

Security is paramount to database administrators seeking to protect their gigabytes of vital


business data from the prying eyes of unauthorized outsiders and insiders attempting to
exceed their authority. All relational database management systems provide some sort of
intrinsic security mechanisms designed to minimize these threats. They range from the simple
password protection offered by Microsoft Access to the complex user/role structure supported
by advanced relational databases like Oracle and Microsoft SQL Server. This article focuses on
the security mechanisms common to all databases that implement the Structured Query
Language (or SQL). Together, we'll walk through the process of strengthening data access
controls and ensuring the safety of your data.

Server-based databases all support a user concept similar to that used in computer operating
systems. If you're familiar with the user/group hierarchy found in Microsoft Windows NT and
Windows 2000, you'll find that the user/role groupings supported by SQL Server and Oracle
are very similar.

It is highly recommended that you create individual database user accounts for each person
who will be accessing your database. It's technically possible to share accounts between users
or simply use one user account for each type of user that needs to access your database, but I
strongly discourage this practice for two reasons. First, it will eliminate individual
accountability -- if a user makes a change to your database (let's say by giving himself a
$5,000 raise), you won't be able to trace it back to a specific person through the use of audit
logs. Furthermore, if a specific user leaves your organization and you wish to remove his or
her access from the database, you'll be forced to change the password that all users rely
upon.

The methods for creating user accounts vary from platform to platform and you'll have to
consult your DBMS-specific documentation for the exact procedure. Microsoft SQL Server
users should investigate the use of the sp_adduser stored procedure. Oracle database
administrators will find the CREATE USER command useful. You also might want to investigate
alternative authentication schemes. For example, Microsoft SQL Server supports the use of
Windows NT Integrated Security. Under this scheme, users are identified to the database by
their Windows NT user accounts and are not required to enter an additional user ID and
password to access the database. This approach is extremely popular among database
administrators because it shifts the burden of account management to the network
administration staff and it provides the ease of a single sign-on to the end user.

If you're in an environment with a small number of users, you'll probably find that creating
user accounts and assigning permissions directly to them is sufficient for your needs.
However, if you have a large number of users, you'll most likely be overwhelmed by the
burden of maintaining accounts and proper permissions. To ease this burden, relational
databases support the notion of roles. Database roles function similarly to Windows NT
groups. User accounts are assigned to role(s) and permissions are then assigned to the role as
a whole rather than the individual user accounts. For example, we could create a DBA role and
then add the user accounts of our administrative staff to this role. Once we've done this, we
can assign a specific permission to all present (and future) administrators by simply assigning
the permission to the role. Once again, the procedures for creating roles varies from platform
to platform. MS SQL Server administrators should investigate the sp_addrole stored procedure
while Oracle DBAs should use the CREATE ROLE syntax.

Once you've populated your database with users and roles, it's time to begin assigning
permissions.
Granting Permissions
Now that we've added users to our database, it's time to begin strengthening security by
adding permissions. Our first step will be to grant appropriate database permissions to our
users. We'll accomplish this through the use of the SQL GRANT statement.
Here's the syntax of the statement:

GRANT <permissions>
[ON <table>]
TO <user/role>
[WITH GRANT OPTION]
Now, let's take a look at this statement line-by-line. The first line, GRANT <permissions>,
allows us to specify the specific table permissions we are granting. These can be either table-
level permissions (such as SELECT, INSERT, UPDATE and DELETE) or database permissions
(such as CREATE TABLE, ALTER DATABASE and GRANT). More than one permission can be
granted in a single GRANT statement, but table-level permissions and database-level
permissions may not be combined in a single statement.

The second line, ON <table>, is used to specify the affected table for table-level permissions.
This line is omitted if we are granting database-level permissions. The third line specifies the
user or role that is being granted permissions.

Finally, the fourth line, WITH GRANT OPTION, is optional. If this line is included in the
statement, the user affected is also permitted to grant these same permissions to other users.
Note that the WITH GRANT OPTION can not be specified when the permissions are assigned to
a role.

Let's look at a few examples. In our first scenario, we have recently hired a group of 42 data
entry operators who will be adding and maintaining customer records. They need to be able to
access information in the Customers table, modify this information and add new records to the
table. They should not be able to entirely delete a record from the database. First, we should
create user accounts for each operator and then add them all to a new role, DataEntry. Next,
we should use the following SQL statement to grant them the appropriate permissions:

GRANT SELECT, INSERT, UPDATE


ON Customers
TO DataEntry

And that's all there is to it! Now let's examine a case where we're assigning database-level
permissions. We want to allow members of the DBA role to add new tables to our database.
Furthermore, we want them to be able to grant other users permission to do the same. Here's
the SQL statement:

GRANT CREATE TABLE


TO DBA
WITH GRANT OPTION

Notice that we've included the WITH GRANT OPTION line to ensure that our DBAs can assign
this permission to other users.

At this point, we've learned how to add users and roles to a database and assign them
permissions as necessary. In the next section of this article, we'll look at the methods for
removing permissions from users. Read on!
Removing Permissions
Once we've granted permissions, it often proves necessary to revoke them at a later date.
Fortunately, SQL provides us with the REVOKE command to remove previously granted
permissions. Here's the syntax:

REVOKE [GRANT OPTION FOR] <permissions>


ON <table>
FROM <user/role>
You'll notice that the syntax of this command is similar to that of the GRANT command. The
only difference is that WITH GRANT OPTION is specified on the REVOKE command line rather
than at the end of the command. As an example, let's imagine we want to revoke Mary's
previously granted permission to remove records from the Customers database. We'd use the
following command:

REVOKE DELETE
ON Customers
FROM Mary

And that's all there is to it! There's one additional mechanism supported by Microsoft SQL
Server that is worth mentioning -- the DENY command. This command can be used to
explicitly deny a permission to a user that they might otherwise have through a current or
future role membership. Here's the syntax:

DENY <permissions>
ON <table>
TO <user/role>
Returning to our previous example, let's imagine that Mary was also a member of the
Managers role that also had access to the Customers table. The previous REVOKE statement
would not be sufficient to deny her access to the table. It would remove the permission
granted to her through a GRANT statement targeting her user account, but would not affect
the permissions gained through her membership in the Managers role. However, if we use a
DENY statement it will block her inheritance of the permission. Here's the command:

DENY DELETE
ON Customers
TO Mary

The DENY command essentially creates a "negative permission" in the database access
controls. If we later decide to give Mary permission to remove rows from the Customers table,
we can't simply use the GRANT command. That command would be immediately overridden by
the existing DENY. Instead, we would first use the REVOKE command to remove the negative
permission entry as follows:

REVOKE DELETE
ON Customers
FROM Mary

You'll notice that this command is exactly the same as the one used to remove a positive
permission. Remember that the DENY and GRANT commands both work in a similar fashion --
they both create permissions (positive or negative) in the database access control mechanism.
The REVOKE command removes all positive and negative permissions for the specified user.
Once this command has been issued, Mary will be able to delete rows from the table if she is a
member of a role that possesses that permission. Alternatively, a GRANT command could be
issued to provide the DELETE permission directly to her account.

Throughout the course of this article, you've learned a good deal about the access control
mechanisms supported by the Standard Query Language. This introduction should provide you
with a good starting point, but I encourage you to reference your DBMS documentation to
learn the enhanced security measures supported by your system. You'll find that many
databases support more advanced access control mechanisms, such as granting permissions
on specific columns.

Be sure to check back next week for another informative article on databases. If you're not
already receiving our newsletter, be sure to subscribe and you'll receive a weekly reminder in
your inbox!
Aggregate Functions in SQL
SQL provides several aggregate functions to assist with data summarization. In this article we
explore the usage of SUM, AVG, COUNT, MIN and MAX.

By their very nature, our databases contain a lot of data. In previous features,
we've explored methods of extracting the specific data we're looking for using the
Structured Query Language (SQL). Those methods worked great when we were
seeking the proverbial needle in the haystack. We were able to answer obscure
questions like "What are the last names of all customers who have purchased
Siberian wool during the slow months of July and August?"

Oftentimes, we're also interested in summarizing our data to determine trends or


produce top-level reports. For example, the purchasing manager may not be
interested in a listing of all widget sales, but may simply want to know the number of
widgets sold this month. Fortunately, SQL provides aggregate functions to assist
with the summarization of large volumes of data. In this three-segment article, we'll
look at functions that allow us to add and average data, count records meeting
specific criteria and find the largest and smallest values in a table.

All of our queries will use the WidgetOrder table described below. Please note that
this table is not normalized and I've combined several data entities into one table for
the purpose of simplifying this scenario. A good relational design would likely have
Products, Orders, and Customers tables at a minimum.

OrderID FirstName LastName Quantity UnitPrice Continent

122 John Jacob 21 4.52 North America

923 Ralph Wiggum 192 3.99 North America

238 Ryan Johnson 87 4.49 Africa

829 Mary Smith 842 2.99 North America

824 Elizabeth Marks 48 3.48 Africa


753 James Linea 9 7.85 North America

942 Alan Jonas 638 3.29 Europe

Let's begin by taking a look at the SUM function. It is used within a SELECT statement and,
predictably, returns the summation of a series of values. If the widget project manager
wanted to know the total number of widgets sold to date, we could use the following query:

SELECT SUM(Quantity) AS Total


FROM WidgetOrders

Our results would appear as:

Total
-----------
1837

The AVG (average) function works in a similar manner to provide the mathematical average of
a series of values. Let's try a slightly more complicated task this time. We'd like to find out
the average dollar amount of all orders placed on the North American continent. Note that
we'll have to multiply the Quantity column by the UnitPrice column to compute the dollar
amount of each order. Here's what our query will look like:

SELECT AVG(UnitPrice * Quantity) As AveragePrice


FROM WidgetOrders
WHERE Continent = “North America”

And the results:

AveragePrice
---------------------
862.3075

In the next section of this article, we'll explore methods used for counting the number of
records that meet given criteria.

SQL provides the COUNT function to retrieve the number of records in a table that meet given
criteria. We can use the COUNT(*) syntax alone to retrieve the number of rows in a table.
Alternatively, a WHERE clause can be included to restrict the counting to specific records.

For example, suppose our Widgets product manager would like to know how many orders our
company processed that requested over 100 widgets.
Here's the SQL query:

SELECT COUNT(*) AS 'Number of Large Orders'


FROM WidgetOrders
WHERE Quantity > 100

And the results:

Number of Large Orders


----------------------
3

The COUNT function also allows for the use of the DISTINCT keyword and an expression to
count the number of times a unique value for the expression appears in the target data.
Similarly, the ALL keyword returns the total number of times the expression is satisfied,
without worrying about unique values. For example, our product manager would like a simple
query that returned the number of unique continents in our orders database.

First, let's take a look at the use of the ALL keyword:

SELECT COUNT(ALL Continent) As 'Number of Continents'


FROM WidgetOrders

And the result set:

Number of Continents
--------------------
7

Obviously, this is not the desired results. If you recall the contents of the WidgetOrders table
from the previous page, all of our orders came from North America, Africa and Europe. Let's
try the DISTINCT keyword instead:

SELECT COUNT(DISTINCT Continent) As 'Number of Continents'


FROM WidgetOrders

And the output:

Number of Continents
--------------------
3

That's more like it!

In the next section of this article, we'll look at the functions used to find the maximum and
minimum values of an expression.
In this final segment of our aggregate functions feature article, we'll look at the functionality
SQL provides to locate the records containing the smallest and largest values for a given
expression.

The MAX() function returns the largest value in a given data series. We can provide the
function with a field name to return the largest value for a given field in a table. MAX() can
also be used with expressions and GROUP BY clauses for enhanced functionality.

Once again, we'll use the WidgetOrders example table for this query (see the first page of this
article for the specification and contents). Suppose our product manager wanted to find the
order in our database that produced the most revenue for the company. We could use the
following query to find the order with the largest total dollar value:

SELECT MAX(Quantity * UnitPrice)As 'Largest Order'


FROM WidgetOrders

Our results would look like this:

Largest Order
---------------------
2517.58

The MIN() function functions in the same manner, but returns the minimum value for the
expression. Let's try a slightly more complicated example utilizing the MIN() function. Our
sales department is currently analyzing data on small widget orders. They'd like us to retrieve
information on the smallest widget order placed on each continent. This requires the use of
the MIN() function on a computed value and a GROUP BY clause to summarize data by
continent.

Here's the SQL:

SELECT Continent, MIN(Quantity * UnitPrice) AS 'Smallest Order'


FROM WidgetOrders
GROUP BY Continent

And our result set:

Continent Smallest Order


------------- ---------------------
Africa 167.04
Europe 2099.02
North America 70.65

That's it for this week. If you're not clear on the use of aggregate functions, be sure to stop
by our forum for some assistance. Check back each week for coverage of a new databases
topic. If you'd like a weekly reminder by e-mail, be sure to subscribe to our newsletter.
All About NULL Values
Users new to the world of databases are often confused by a special value particular to our
field – the NULL value. Read about the proper uses of NULL in this article.

Users new to the world of databases are often confused by a special value particular to our
field – the NULL value. This value can be found in a field containing any type of data and has a
very special meaning within the context of a relational database. It’s probably best to begin
our discussion of NULL with a few words about what NULL is not:

• NULL is not the number zero.


• NULL is not the empty string (“”) value.

Rather, NULL is the value used to represent an unknown piece of data. Let’s take a look at a
simple example: a table containing the inventory for a fruit stand. Suppose that our inventory
contains 10 apples, 3 oranges. We also stock plums, but our inventory information is
incomplete and we don’t know how many (if any) plums are in stock. Using the NULL value,
we would have the inventory table shown at the bottom of this page.

It would clearly be incorrect to include a quantity of 0 for the plums record, because that
would imply that we had no plums in inventory. On the contrary, we might have some plums,
we’re just not sure.

Databases treat NULL values in a special way, depending upon the type of operation that it is
used in. When a NULL value appears as an operand to an AND operation, the operation’s value
is FALSE if the other operand is FALSE (there is no way the expression could be TRUE with one
FALSE operand). On the other hand, the result is NULL (unknown) if the other operand is
either TRUE or NULL (because we can’t tell what the result would be.)

The OR operand treats NULL values in a similar fashion. If the other operand is TRUE, the
result of the OR is TRUE (because the real value of the NULL operand doesn’t matter.) On the
other hand, if the other operand is either FALSE or NULL, the result of the OR operation is
NULL.

There are two special operands used to test for the presence of the NULL value. ISNULL
returns TRUE only when the supplied operand has a NULL value. Conversely, ISNOTNULL
returns TRUE when the supplied operand does not have a NULL value. These are quite
important functions. Avoid one of the most common database mistakes: testing an operand
for a NULL value by comparing it to the empty string or zero is not correct!

That’s NULL in a nutshell! Take a few moments to review these concepts and get them straight
in your head. You’ll be glad you did down the road!

Fruit Stand Inventory

Item Quantity
Apples 10
Oranges 3
Plums NULL

Creating Databases and Tables in SQL


Just getting started with the Structured Query Language? This tutorial walks you through the
process of creating tables and databases in SQL databases.
Deleting Data from SQL Tables
The SQL DELETE command allows the removal of some or all of the data stored in a relational
database table.
Oftentimes, it becomes necessary to remove obsolete information from a relational
database. Fortunately, Structured Query Language provides a flexible DELETE
command that can be used to remove some or all of the information stored within a
table.

Let's take a brief look at the command's syntax:

DELETE FROM
{table name | view name}
[WHERE search_conditions]

Notice that the command itself is quite simple. There are only two variables -- the
table or view to delete from and the search conditions.

Let's first discuss the target of the deletion. According to the ANSI SQL standard, it
is possible to delete from either a table or a view. However, I'd strongly encourage
you to avoid using the DELETE command (or any data manipulation command, for
that matter) on a view. Some versions of SQL simply don't support this syntax and
the results of modifying a view can be somewhat unpredictable.

The search_conditions field offers no surprises to students of SQL -- it uses the same
format as the search_conditions utilized with the SELECT statement. You can include
any comparison operators to limit the data that is removed from the table. Notice
that the search_conditions field is actually an optional arguement (hence the square
brackets surrounding it). Omission of this argument will result in the deletion of the
entire table.

Now let's turn to some examples. Before we get started, we need to create a table
and load it with some sample data. We'll create a students table for our small town
high school. Execute the following SQL code against your DBMS of choice:

CREATE TABLE students


(
first_name varchar(50),
last_name varchar(50),
id integer PRIMARY KEY
)

INSERT INTO students VALUES ('John', 'Doe', 284)


INSERT INTO students VALUES ('Mike', 'Ryan', 302)
INSERT INTO students VALUES ('Jane', 'Smith', 245)
INSERT INTO students VALUES ('MaryAnn', 'Pringle', 142)
INSERT INTO students VALUES ('Charlotte', 'Bronte', 199)
INSERT INTO students VALUES ('Bill', 'Arlington', 410)

Bill Arlington had stellar academic achievement and was allowed to graduate early.
Therefore, we must remove him from the database. As SQL experts, we know that
when we want to select a single record, it's prudent to use the primary key in the
search condition to prevent accidental removal of similar records. Here's our syntax:

DELETE FROM students


WHERE id = 410

Here are the contents of the modified table:

first_name last_name id

Mike Ryan 302

MaryAnn Pringle 142

Charlotte Bronte 199

Jane Smith 245

John Doe 284

Now let's try something a bit more complicated -- deleting all of the students with ID
numbers between 240 and 290. Here's the SQL:

DELETE FROM students


WHERE id BETWEEN 240 AND 290

and the newly modified table:

first_name last_name id

MaryAnn Pringle 142


Charlotte Bronte 199

Mike Ryan 302

And finally, we're sorry to report that our school closed it's doors due to dwindling
enrollment. Out of respect for student privacy, we need to remove all of the data
from the table. This SQL command will do the trick:

DELETE FROM students

And that's DELETE in a nutshell. Be sure to check back often for new articles on
database topics!

Retrieving Data from Multiple Tables with SQL Joins


SQL join statements allow you to combine data from two or more tables in your query results.
Learn how to leverage this powerful technology to supercharge your database queries.

In several recent articles we explored the fundamental concepts of SQL, the process of
creating databases and database tables and the art of retrieving data from a database using
simple queries. This article expands on these topics and looks at using join techniques to
retrieve data from multiple tables.

By way of example, let's return to our fictitious XYZ Corporation. XYZ utilizes an
Oracle database to track the movements of their vehicle fleet and drivers between their
facilities. Some employees are assigned to drive trucks while others are assigned to drive
cars. Take a moment to examine the following two tables from their vehicle management
database:

drivers

licensenum lastname firstname location class


13232 Baker Roland New York Car

18431 Smythe Michael Miami Truck

41948 Jacobs Abraham Seattle Car

81231 Ryan Jack Annapolis Car

vehicles

tag location class

D824HA Miami Truck

H122JM New York Car

J291QR Seattle Car

L990MT Seattle Truck

P091YF Miami Car

In the previous article, we looked at methods used to retrieve data from single tables. For
example, we could use simple SELECT statements to answer questions such as:

• Which drivers are located in New York?


• How many cars are in each city?

• Which drivers assigned to drive trucks are located in Miami?

Practical applications often require the combination of data from multiple tables. Our
vehicle managers might make requests like the following:

• List all of the vehicle/driver pairings possible without relocating a vehicle


or driver

• List all of the drivers authorized to drive vehicles located in Miami

Granted, it would be possible to create complex SELECT statements using subqueries to


fulfill these requests. However, there's a much simpler method -- the use of inner and
outer joins. We'll explore each of these concepts in the next two sections of this article.

Inner joins (also known as equijoins) are used to contain information from a combination
of two or more tables. The join condition determines which records are paired together
and is specified in the WHERE clause. For example, let's create a list of driver/vehicle
match-ups where both the vehicle and driver are located in the same city. The following
SQL query will accomplish this task:

SELECT lastname, firstname, tag


FROM drivers, vehicles
WHERE drivers.location = vehicles.location

And let's take a look at the results:

lastname firstname tag


-------- --------- ---
Baker Roland H122JM
Smythe Michael D824HA
Smythe Michael P091YF
Jacobs Abraham J291QR
Jacobs Abraham L990MT

Notice that the results are exactly what we sought. It is possible to further refine the
query by specifying additional criteria in the WHERE clause. Our vehicle managers took
a look at the results of our last query and noticed that the previous query matches drivers
to vehicles that they are not authorized to drive (e.g. truck drivers to cars and vice-versa).
We can use the following query to resolve this problem:

SELECT lastname, firstname, tag, vehicles.class


FROM drivers, vehicles
WHERE drivers.location = vehicles.location
AND drivers.class = vehicles.class

Notice that in this example we needed to specify the source table for the class attribute in
the SELECT clause. This is due to the fact that class is unambiguous – it appears in both
tables and we need to specify which table’s column should be included in the query
results. In this case it does not make a difference as the columns are identical and they
are joined using an equijoin. However, if the columns contained different data this
distinction would be critical. Here are the results of this query:

lastname FirstName Tag Class


-------- --------- --- -----
Baker Roland H122JM Car
Smythe Michael D824HA Truck
Jacobs Abraham J291QR Car

Notice that the rows pairing Michael Smythe to a car and Abraham Jacobs to a truck have
been removed.

You can also use inner joins to combine data from three or more tables.

Outer joins allow database users to include additional information in the query results.
We'll explore them in the next section of this article

Take a moment and review the database tables located on the first page of this article.
Notice that we have a driver -- Jack Ryan -- who is located in a city where there are no
vehicles. Our vehicle managers would like this information to be included in their query
results to ensure that drivers do not sit idly by waiting for a vehicle to arrive. We can use
outer joins to include records from one table that have no corresponding record in the
joined table. Let's create a list of driver/vehicle pairings that includes records for drivers
with no vehicles in their city. We can use the following query:

SELECT lastname, firstname, driver.city, tag


FROM drivers, vehicles
WHERE drivers.location = vehicles.location (+)

Notice that the outer join operator "(+)" is included in this query. This operator is placed
in the join condition next to the table that is allowed to have NULL values. This query
would produce the following results:

lastname firstname citytag


-------- --------- -------
Baker Roland NewYorkH122JM
Smythe Michael MiamiD824HA
Smythe Michael MiamiP091YF
Jacobs Abraham SeattleJ291QR
Jacobs Abraham Seattle L990MT
Ryan Patrick Annapolis

This time our results include the stranded Patrick Ryan and our vehicle management
department can now dispatch a vehicle to pick him up.

Note that there are other possible ways to accomplish the results seen in this article and
syntax may vary slightly from DBMS to DBMS. These examples were designed to work
with Oracle databases, so your mileage may vary. Furthermore, as you advance in your
knowledge of SQL you’ll discover that there is often more than one way to accomplish a
desired result and oftentimes one way is just as good as another. Case in point, it is also
possible to specify a join condition in the FROM clause rather than the WHERE clause.
For example, we used the following SELECT statement earlier in this article:

SELECT lastname, firstname, tag


FROM drivers, vehicles
WHERE drivers.location = vehicles.location
AND drivers.class = vehicles.class

The same query could be rewritten as:

SELECT lastname, firstname, tag


FROM drivers INNER JOIN vehicles ON drivers.location =
vehicles.location
WHERE drivers.class = vehicles.class

That's it for this week! Be sure to check back next week for a new exciting article on
databases. If you'd like a reminder in your Inbox, subscribe to the About Databases
newsletter.

Retrieving Data with SQL Queries


Learn to retrieve information from your database with the SQL SELECT statement. This tutorial
provides an introduction to the basics of database queries.

The Structured Query Language offers database users a powerful and flexible data
retrieval mechanism -- the SELECT statement. In this article, we'll take a look at the
general form of the SELECT statement and compose a few sample database queries
together. If this is your first foray into the world of the Structured Query Language, you
may wish to review the article SQL Fundamentals before continuing. If you're looking to
design a new database from scratch, the article Creating Databases and Tables in SQL
should prove a good jumping-off point.
Now that you've brushed up on the basics, let's begin our exploration of the SELECT
statement. As with previous SQL lessons, we'll continue to use statements that are
compliant with the ANSI SQL standard. You may wish to consult the documentation for
your DBMS to determine whether it supports advanced options that may enhance the
efficiency and/or efficacy of your SQL code.

The general form of the SELECT statement appears below:

SELECT select_list
FROM source
WHERE condition(s)
GROUP BY expression
HAVING condition
ORDER BY expression

The first line of the statement tells the SQL processor that this command is a SELECT
statement and that we wish to retrieve information from a database. The select_list
allows us to specify the type of information we wish to retrieve. The FROM clause in the
second line specifies the specific database table(s) involved and the WHERE clause gives
us the capability to limit the results to those records that meet the specified condition(s).
The final three clauses represent advanced features outside the scope of this article --
we'll explore them in future SQL lessons.

The easiest way to learn SQL is by example. With that in mind, let's begin looking at
some database queries. Throughout this article, we'll use the employees table from the
fictional XYZ Corporation human resources database to illustrate all of our queries.
Here's the entire table:

EmployeeID LastName FirstName Salary ReportsTo

1 Smith John 32000 2

2 Scampi Sue 45000 NULL

3 Kendall Tom 29500 2

4 Jones Abraham 35000 2


5 Allen Bill 17250 4
6 Reynolds Allison 19500 4
7 Johnson Katie 21000 3

Retreiving an Entire Table

XYZ Corporation's Director of Human Resources receives a monthly report providing


salary and reporting information for each company employee. The generation of this
report is an example of the SELECT statement's simplest form. It simply retrieves all of
the information contained within a database table -- every column and every row. Here's
the query that will accomplish this result:

SELECT * FROM employees

Pretty straightforward, right? The asterisk (*) appearing in the select_list is a wildcard
used to inform the database that we would like to retrieve information from all of the
columns in the employees table identified in the FROM clause. We wanted to retrieve all
of the information in the database, so it wasn't necessary to use a WHERE clause to
restrict the rows selected from the table. Here's what our query results look like:

EmployeeID LastName FirstName Salary ReportsTo


---------- -------- --------- ------ ---------
1 Smith John 32000 2
2 Scampi Sue 45000 NULL
3 Kendall Tom 29500 2
4 Jones Abraham 35000 2
5 Allen Bill 17250 4
6 Reynolds Allison 19500 4
7 Johnson Katie 21000 3

In the next section of this lesson, we'll look at some more powerful queries that allow you
to restrict the information retrieved from the database.

In the first part of this feature, we looked at the general form of the SELECT statement
and a simple query that retrieved all of the information contained within a table. Let's go
a step further and look at some queries that restrict the information retrieved.

Retrieving Selected Columns from a Table

Our last example produced a report for the Director of Human Resources that contained
all of the salary and reporting information for every employee of XYZ Corporation.
There are several mid-level managers within the department that also require access to
reporting information as part of their duties. These managers do not need access to salary
information, so we'd like to provide them with a report containing limited information
from the database -- each employee's name, ID number and the ID number of their
manager.
Here's a SQL SELECT statement that accomplishes the desired result:

SELECT EmployeeID, LastName, FirstName, ReportsTo


FROM employees

This query looks somewhat different from the previous one. Notice that the asterisk
wildcard has been replaced with a list of the column names we would like to include in
our query results. The Salary column is omitted to satisfy privacy concerns by limiting
the information provided to mid-level managers. Here's the output of this query:

EmployeeID LastName FirstName ReportsTo


---------- -------- --------- ---------
1 Smith John 2
2 Scampi Sue NULL
3 Kendall Tom 2
4 Jones Abraham 2
5 Allen Bill 4
6 Reynolds Allison 4
7 Johnson Katie 3

Retrieving Selected Rows from a Table

XYZ's President, Sue Scampi, would like a report providing detailed information on all of the
employees that report directly to her. To produce this report, we need to restrict the rows
that appear in the query results through the use of a WHERE clause. Let's look at the SQL
code used to accomplish this result.

SELECT *
FROM employees
WHERE ReportsTo = 2

Notice that the wildcard has reappeared in the select_list in order to provide Ms. Scampi with
the detailed report she requested. We've added on a WHERE clause that limits the results to
those rows where the ReportsTo field contains a value of 2 (Sue's Employee ID). Here are the
results of executing the above query:

Employee LastName FirstName Salary ReportsTo


-------- -------- --------- ------ ---------
1 Smith John 32000 2
3 Kendall Tom 29500 2
4 Jones Abraham 35000 2

After reviewing this report, Sue decides that she would like to further limit the results to those
employees that earn a salary in excess of $30,000. We can use a compound condition in the
WHERE clause to achieve these results. Here's the revised SQL query:
SELECT *
FROM employees
WHERE ReportsTo = 2 AND Salary > 30000

And the results of this query:

Employee LastName FirstName Salary ReportsTo


-------- -------- --------- ------ ---------
1 Smith John 32000 2
4 Jones Abraham 35000 2

Notice that Tom Kendall's record dropped out of the results because his salary did not meet
the minimum requirement of $30,000.

In the final section of this article we'll look at two techniques used to enhance query results.

In the first two sections of this lesson, we examined basic database queries and more
powerful statements that restrict the query results. Now let's look at two techniques used
to enhance the display of query output.

Renaming Columns in Query Results

All too often, database tables contain cryptic column headings that don't make sense to
users outside of a company's IT department. Fortunately, SQL provides a mechanism that
allows us to change the headings displayed in query output for enhanced readability.

For example, let's look at a database query that displays the name of every employee of
XYZ Corporation along with their annual salary. Our database simply labels the
compensation column "Salary" but doesn't clarify the time period involved. Here's a
query that straightens things out:

SELECT LastName, FirstName, Salary AS 'Annual Salary'


FROM employees

Notice that the third field in the SELECT clause is slightly different from previous
examples. The AS statement modifies the column heading used in the query output. It's
necessary to enclose this heading in single quotes to incorporate the space character.
Here's the output of this query:

EmployeeID LastName FirstName Annual Salary ReportsTo


---------- -------- --------- ------ ---------
1 Smith John 32000 2
2 Scampi Sue 45000 NULL
3 Kendall Tom 29500 2
4 Jones Abraham 35000 2
5 Allen Bill 17250 4
6 Reynolds Allison 19500 4
7 Johnson Katie 21000 3

Removing Duplicate Values from Query Results

For our final challenge, the Human Resources Director would like us to produce a report
containing the employee ID numbers of each employee that has subordinates. We can obtain
this information using the following query:

SELECT ReportsTo
FROM employees

That produces the following results:

ReportsTo
---------
2
NULL
2
2
4
4
3

Unfortunately, life's not that simple and there's a catch. The HR Manager took one look at the
query results and asked why employee IDs appeared multiple times. We're sent back to the
drawing board with a request to remove the duplicate values. This is accomplished by
inserting the DISTINCT keyword in the select_list as follows:

SELECT DISTINCT ReportsTo


FROM employees

That produces the following results:

ReportsTo
---------
2
NULL
4
3

The duplicate ID numbers have been eliminated and we're left with the desired report. Notice
that the NULL value appears in the query output. NULL is considered a unique value and will
appear once (and only once) in query output when the DISTINCT keyword is used.

SQL Fundamentals
The Structured Query Language provides the foundation for all relational database systems.
Join us as we explore the fundamental concepts behind this powerful language.
Summarizing Data with CUBE and ROLLUP
SQL's CUBE and ROLLUP commands allow for the efficient summarization of data.

Looking for a quick, efficient way to summarize the data stored in your database? The
SQL ROLLUP and CUBE commands offer a valuable tool for gaining some quick and
dirty insight into your data. ROLLUP and CUBE are SQL extensions and they're
available in SQL Server 6.5 (and above) and Oracle 8i (and above).

The CUBE command is added to an SQL

To provide an example, let's imagine a table that contains the number and type of pets
available for sale at our chain of pet stores:

Pets

Type Store Number

Dog Miami 12

Cat Miami 18

Turtle Tampa 4

Dog Tampa 14

Cat Naples 9

Dog Naples 5

Turtle Naples 1
As the proud owners of this Florida pet superstore, we'd like to take a quick look at
various aspects of our inventory. We could hire an SQL programmer to sit down and
write a number of queries to retrieve the exact data that we're looking for. However, our
dataset isn't very large and we enjoy looking at the raw numbers. Our hunger for data
can be appeased using the CUBE command. Here's the sample SQL:

SELECT Type, Store, SUM(Number) as Number


FROM Pets
GROUP BY type,store
WITH CUBE

And the results of the query:

Type Store Number

Cat Miami 18

Cat Naples 9

Cat NULL 27

Dog Miami 12

Dog Naples 5

Dog Tampa 14

Dog NULL 31

Turtle Naples 1
Turtle Tampa 4

Turtle NULL 5

NULL NULL 63

NULL Miami 30

NULL Naples 15

NULL Tampa 18

Wow! That's a lot of data! Notice that we are presented with a number of additional
groupings that contain NULL fields that wouldn't appear in the results of a normal
GROUP BY command. These are the summarization rows added by the CUBE
statement. Analyzing the data, you'll notice that our chain has 27 cats, 31 dogs and 5
turtles spread among our three stores. Our Miami store has the largest number of pets in
stock with a whopping inventory of 30 pets.

We're not particularly interested in the total number of pets at each store -- we'd just like
to know our statewide inventory of each species along with the standard GROUP BY
data. Utilizing the ROLLUP operator instead of the CUBE operator will eliminate the
results that contain a NULL in the first column.

Here's the SQL:

SELECT Type, Store, SUM(Number) as Number


FROM Pets
GROUP BY type,store
WITH ROLLUP

And the results:


Type Store Number

Cat Miami 18

Cat Naples 9

Cat NULL 27

Dog Miami 12

Dog Naples 5

Dog Tampa 14

Dog NULL 31

Turtle Naples 1

Turtle Tampa 4

Turtle NULL 5

NULL NULL 63
And that's CUBE and ROLLUP in a nutshell! Be sure to check back next week for
another exciting journey into the world of databases!

Fuzzy Logic and SQL


How can fuzzy logic be used to enhance SQL? Fuzzy Systems Solutions markets a tool that
does just that! This series explains the basics.
Getting Started with SQL
The Web Developer's Virtual Library provides a very readable introduction to the Structured
Query Language.
SQL for Web Nerds
A tremendous resource for all database administrators! Philip Greenspun provides the
complete text of an SQL reference book online.
SQL Optimization
Are your SQL queries as efficient as possible? BASIS Corporation provides a feature article
describing SQL optimization techniques.
Teach Yourself SQL in 21 Days
This online book from Que offers a three-week tutorial on the Structured Query Language. At
the conclusion of the series of lessons, readers will have gained a solid understanding of SQL
concepts.

Vous aimerez peut-être aussi