Sie sind auf Seite 1von 8

To describe the DML (data manipulation language) and to examine the usage of the SELECT command

describe the main functions of the DML describe the main functions of the DML SELECT command describe the five basic types of precidate offered by ANSI standard SQL explain arithmetic expressions and logical connectives

The data manipulation sublanguage of SQL, known as the DML, is used to select, insert, update, and delete data in a relational database management system.

It is the most used and functional of the three SQL sublanguages and it is used by all front-end tools in their day-to-day dealing with the database.

DML requests, or other SQL commands, can be issued from any workstation or terminal that has a valid connection to the host computer running the RDBMS software.

This activity is known as interactive SQL and applies in terminal-to-host and client/server environments.

SQL commands can be issued to the database in three ways: directly to the RDBMS from the command prompt: SQL>

embedded in front-end tools, such as report writers or forms

embedded within an application programming language, such as COBOL or C

The four basic DML commands are SELECT

INSERT UPDATE DELETE

These DML commands combined with conditions and language constructs provide the user with a powerful tool for data manipulation.

The data manipulation sublanguage of SQL, known as the DML, is used to select, insert, update, and delete data in a relational database management system.

It is the most used and functional of the three SQL sublanguages and it is used by all front-end tools in their day-to-day dealing with the database.

DML requests, or other SQL commands, can be issued from any workstation or terminal that has a valid connection to the host computer running the RDBMS software.

This activity is known as interactive SQL and applies in terminal-to-host and client/server environments.

SQL commands can be issued to the database in three ways: directly to the RDBMS from the command prompt: SQL>

embedded in front-end tools, such as report writers or forms

embedded within an application programming language, such as COBOL or C

The four basic DML commands are SELECT

INSERT UPDATE DELETE

These DML commands combined with conditions and language constructs provide the user with a powerful tool for data manipulation. The SELECT command is used to retrieve information from database tables.

The SELECT command supports a number of conditions and operators which define the data to be returned.

The purpose of these conditions and operators is to allow the user to retrieve only relevant rows and columns of data.

The syntax of the SELECT statement is as shown.

SELECT is the keyword which indicates to the DBMS what kind of operation is to be performed.

The * symbol will return all columns of data from the table(s) selected.

DISTINCT is the keyword used to select distinct records without duplicates.

FROM is the keyword which indicates that the following table names are the sources of data.

table_name(s) are the actual constant values of the table names from which the data will be drawn.

The WHERE keyword indicates that the following clauses are the search conditions and only those rows meeting the search criteria are to be returned.

The optional AND and OR keywords allow you to specify additional search conditions.

The GROUP BY clause groups rows of similar data together, producing one summary row of query results for each group.

The HAVING clause is effectively the WHERE or search clause for the GROUP BY clause.

The ORDER BY clause sorts the query results in either ascending (ASC) or descending (DESC) order.

There are a number of rules concerning the format of input when you are using SQL.

A query, including keywords such as SELECT, and column names like prod_codes, can be written in either upper or lower case.

However, values in the database are case-sensitive and must be written exactly as they have been entered into the tables. For example, 'Denver' will be recognized, whereas 'denver' or 'DENVER' will not.

Data values that have been defined as a character string ('Denver' is a good example), must be enclosed in single quotes.

If table and column names include an underscore, like warehouse_id, you must write the name with an underscore. A dash, as in warehouse-id, will not be recognized.

When selecting data there are two methods of specifying which columns are required.

All columns are retrieved when the * symbol is used.

Particular columns can be retrieved by specifying their column names.

The syntax for retrieving all columns and rows from a table is as follows: SELECT * FROM table_name(s);

In most SQL implementations statements end with a semicolon.

Suppose you wanted to see all rows and columns from the products table.

This is how the SELECT statement would look on screen: SQL> SELECT * 2

FROM products;

Notice the SQL prompt before the statement. Line numbers are also displayed. SQL statements can be spread across command lines and indented to improve readability.

The full products table is as shown.

If you want information from particular columns in a table then the column names must be specified.

The syntax for specifying column names is as follows: SELECT column_name(s) FROM table_name(s);

Columns will be displayed in the order the column names are listed in the SELECT statement. They do not have to appear in the same order as in the source table.

The order of the columns can therefore be determined by the user.

Suppose you wanted to see just the description and price of all items in the products table, with the price listed first.

The table shown would be returned by this command: SELECT price,description FROM products;

Suppose a table was required which would display a list of all the prod_codes in the products table. The following SELECT statement would be entered after the SQL> prompt. SELECT prod_code FROM products;

And the returned table would display the data shown.

There are a number of duplicate product codes in the derived table.

A more efficient procedure would be to use the SQL keyword DISTINCT.

DISTINCT is used to prevent the display of duplicate values.

By using the DISTINCT command, you can ensure that only necessary information is displayed.

If the query includes the primary key column of a table in its SELECT list then every row of query results will be unique anyway. This is because the primary key of a table has a different value in each row. In the products table of the DIY database the prod_id column is the primary key.

The syntax for including DISTINCT in a SELECT statement is as follows: SELECT DISTINCT column_name FROM table_name(s);

The returned table would be as shown when this command is entered: SELECT DISTINCT prod_code FROM products;

As you can see, each prod_code is displayed only once.

Only one DISTINCT may be used in a query.

When no conditions are given in the SELECT command, all rows are returned.

However, like columns, particular rows can be specified in a SELECT statement.

Smaller data retrievals require less processing.

This is achieved by using the WHERE clause, and by specifying a condition to be filled.

The syntax of the WHERE clause is as follows: SELECT column_name(s) FROM table_name(s) WHERE condition;

Suppose you wanted to know the price and description of products which sell at less than $10.00.

The complete SELECT statement with a WHERE clause would be as follows: SELECT price,description FROM products WHERE price < 10.00;

The table returned would be as shown.

The condition (in this case a price less than $10.00) takes the form of column name(s), predicate, and parameter.

This corresponds to price, <, and 10.00.

The column names have already appeared in the SELECT clause.

Predicate refers to a set of logical operators.

The parameter can be any of the following: a numeric value an alphanumeric value a null a set

If the value is alphanumeric then it must be enclosed in single quotes.

Suppose you required a list of all warehouse_ids located in the 'W' region.

The SELECT statement to return the relevant information, as shown, would be SELECT region_id, warehouse_id FROM warehouses WHERE region_id = 'W';

Das könnte Ihnen auch gefallen