Вы находитесь на странице: 1из 11

Set operations (SQL)

From Wikipedia, the free encyclopedia Contents


[hide]

o o o

1 UNION operator 1.1 Examples 2 INTERSECT operator 2.1 Example 3 EXCEPT operator 3.1 Example 4 See also 5 References 6 External links

[edit]UNION

operator

In SQL the UNION clause combines the results of two SQL queries into a single table of all matching rows. The two queries must result in the same number of columns and compatibledata types in order to unite. Any duplicate records are automatically removed unless UNION ALL is used. can be useful in data warehouse applications where tables aren't perfectly normalized.[1] A simple example would be a database having tables sales2005 and sales2006 that have identical structures but are separated because of performance considerations. A UNION query could combine results from both tables.
UNION

Note that UNION does not guarantee the order of rows. Rows from the second operand may appear before, after, or mixed with rows from the first operand. In situations where a specific order is desired, ORDER BY must be used. Note that UNION
ALL

may be much faster than plain UNION.

[edit]Examples

Given these two tables:

sales2005

person amount

Joe

1000

Alex

2000

Bob

5000

sales2006

person amount

Joe

2000

Alex

2000

Zach

35000

Executing this statement: SELECT * FROM sales2005 UNION SELECT * FROM sales2006; yields this result set, though the order of the rows can vary because no ORDER BY clause was supplied:
person amount

Joe

1000

Alex

2000

Bob

5000

Joe

2000

Zach

35000

Note that there are two rows for Joe because those rows are distinct across their columns. There is only one row for Alex because those rows are not distinct for both columns. gives different results, because it will not eliminate duplicates. Executing this statement:
UNION ALL

SELECT * FROM sales2005 UNION ALL SELECT * FROM sales2006; would give these results, again allowing variance for the lack of an ORDER BY statement:
person amount

Joe

1000

Joe

2000

Alex

2000

Alex

2000

Bob

5000

Zach

35000

The discussion of full outer joins also has an example that uses UNION.
[edit]INTERSECT

operator

The SQL INTERSECT operator takes the results of two queries and returns only rows that appear in both result sets. For purposes of duplicate removal the INTERSECT operator does not distinguish between NULLs. The INTERSECT operator removes duplicate rows from the final result set. The INTERSECT ALL operator does not remove duplicate rows from the final result set.
[edit]Example

The following example INTERSECT query returns all rows from the Orders table where Quantity is between 50 and 100. SELECT * FROM Orders WHERE Quantity BETWEEN 1 AND 100 INTERSECT SELECT * FROM Orders WHERE Quantity BETWEEN 50 AND 200;
[edit]EXCEPT

operator

The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do not appear in a second result set. The EXCEPT ALL operator (not supported in MSSQL) does not remove duplicates. For purposes of row elimination and duplicate removal, the EXCEPT operator does not distinguish between NULLs. Notably, the Oracle platform provides a MINUS operator which is functionally equivalent to the SQL standard EXCEPT DISTINCT operator [1].
[edit]Example

The following example EXCEPT query returns all rows from the Orders table where Quantity is between 1 and 49, and those with a Quantity between 76 and 100. Worded another way; the query returns all rows where the Quantity is between 1 and 100, apart from rows where the quantity is between 50 and 75. SELECT * FROM Orders WHERE Quantity BETWEEN 1 AND 100 EXCEPT SELECT * FROM Orders WHERE Quantity BETWEEN 50 AND 75;
[edit]

Data Definition Language


From Wikipedia, the free encyclopedia

A Data Definition Language or Data Description Language (DDL) is a computer language for defining data structures. The term DDL was first introduced in relation to the Codasyldatabase model, where the schema of the database was written in a Data Description Language describing the records, fields, and "sets" making up the user Data Model. Later it was used to refer to a subset of SQL, but is now used in a generic sense to refer to any formal language for describing data or information structures, like XML schemas.
Contents
[hide]

1 SQL 1.1 CREATE statements o o 1.1.1 CREATE TABLE statement 1.2 DROP statements 1.3 ALTER statements

o o

1.4 Referential integrity statements 2 XML Schema 3 DDL Tools and Related Applications 3.1 Apache DdlUtils 4 See also 5 External links

[edit]SQL Unlike many data description languages, SQL uses a collection of imperative verbs whose effect is to modify the schema of the database by adding, changing, or deleting definitions of tables or other objects. These statements can be freely mixed with other SQL statements, so the DDL is not truly a separate language. The most commonly encountered statement is CREATE TABLE. [edit]CREATE

statements

Create - To make a new database, table, index, or stored query. A CREATE statement in SQL creates an object inside of a relational database management system (RDBMS). The types of objects that can be created depends on which RDBMS is being used, but most support the creation of tables, indexes, users, synonyms and databases. Some systems (such as PostgreSQL) allow CREATE, and other DDL commands, inside of a transaction and thus they may be rolled back. [edit]CREATE TABLE statement Perhaps the most common CREATE command is the CREATE TABLE command. The typical usage is:
CREATE [TEMPORARY] TABLE [table name] ( [column definitions] ) [table parameters].

Column Definitions: A comma-separated list consisting of any of the following

Column definition: [column name] [data type] {NULL | NOT NULL} {column Primary key definition: PRIMARY KEY ( [comma separated column list] ) CONSTRAINTS: {CONSTRAINT} [constraint definition] RDBMS specific functionality

options}

For example, the command to create a table named employees with a few sample columns would be: CREATE TABLE employees ( id INTEGER first_name CHAR(50) last_name CHAR(75) dateofbirth DATE ); [edit]DROP PRIMARY KEY, NULL, NOT NULL, NULL

statements

Drop - To destroy an existing database, table, index, or view. A DROP statement in SQL removes an object from a relational database management system (RDBMS). The types of objects that can be dropped depends on which RDBMS is being used, but most support the dropping of tables, users, and databases. Some systems (such as PostgreSQL) allow DROP and other DDL commands to occur inside of a transaction and thus be rolled back. The typical usage is simply:
DROP objecttype objectname.

For example, the command to drop a table named employees would be: DROP TABLE employees; The DROP statement is distinct from the DELETE and TRUNCATE statements, in that they do not remove the table itself. For example, a DELETE statement might delete some (or all) data from a table while leaving the table itself in the database, whereas a DROP statement would remove the entire table from the database. [edit]ALTER

statements

Alter - To modify an existing database object. An ALTER statement in SQL changes the properties of an object inside of a relational database management system (RDBMS). The types of objects that can be altered depends on which RDBMS is being used. The typical usage is:
ALTER objecttype objectname parameters.

For example, the command to add (then remove) a column named bubbles for an existing table named sink would be: ALTER TABLE sink ADD bubbles INTEGER; ALTER TABLE sink DROP COLUMN bubbles; [edit]Referential

integrity statements

Finally, other kind of DDL sentence in SQL are the statements to define referential integrity relationships, usually implemented as primary key and foreign key tags in some columns of the tables. These two statements can be included inside a CREATE TABLE or an ALTER TABLE sentence.

Data Manipulation Language


From Wikipedia, the free encyclopedia

This article needs additional citations for verification.


Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (June 2009)

Data Manipulation Language (DML) is a family of computer languages used by computer programs and/or database users to insert, delete and update data in a database. Read-only querying, i.e. SELECT, of this data may be considered to be either part of DML or outside it, depending on the context. Currently the most popular data manipulation language is that of SQL, which is used to retrieve and manipulate data in a Relational database.[1] Other forms of DML are those used byIMS/DLI, CODASYL databases (such as IDMS), and others. Data Manipulation Language comprises the 'SQL-data change' statements,[2] which modify stored data but not the schema or database objects. Manipulation of persistent databaseobjects (e.g. tables or stored procedures) via the 'SQL-schema' statements,
[2]

rather than the data stored within them, is considered to be part of a separate Data

Definition Language. In SQL these two categories are similar in their detailed syntax, data types, expressions etc., but distinct in their overall function.[2] Data Manipulation Languages have their functional capability organized by the initial word in a statement, which is almost always a verb. In the case of SQL, these verbs are:
SELECT ... FROM ... WHERE ... INSERT INTO ... VALUES ... UPDATE ... SET ... WHERE ... DELETE FROM ... WHERE ...

The purely read-only SELECT query statement is classed with the 'SQL-data' statements[2] and so is considered by the standard to be outside of DML. The SELECT ... INTO form is considered to be DML because it manipulates (i.e. modifies) data. In common practice though, this distinction is not made and SELECT is widely considered to be part of DML.[3] Most SQL database implementations extend their SQL capabilities by providing imperative, i.e., procedural, languages. Examples of these are Oracle's PL/SQL and DB2's SQL PL. Data manipulation languages tend to have many different flavors and capabilities between database vendors. There have been a number of standards established for SQL by ANSI,[1] but vendors still provide their own extensions to the standard while not implementing the entire standard.

Query by Example
From Wikipedia, the free encyclopedia

Query by Example (QBE) is a database query language for relational databases. It was devised by Mosh M. Zloof at IBM Research during the mid 1970s, in parallel to the development of SQL. It is the first graphical query language, using visual tables where the user would enter commands, example elements and conditions. Many graphical front-ends for databases use the ideas from QBE today. Originally limited only for the purpose of retrieving data, QBE was later extended to allow other operations, such as inserts, deletes and updates, as well as creation of temporary tables. The motivation behind QBE is that a parser can convert the user's actions into statements expressed in a database manipulation language, such as SQL. Behind the scenes; it is this statement that is actually executed. A suitably comprehensive front-end can minimize the burden on the user to remember the finer details of SQL, and it is easier and more productive for end-users (and even programmers) to select tables and columns by selecting them rather than typing in their names,

In the context of information retrieval, QBE has a somewhat different meaning. The user can submit a document, or several documents, and ask for "similar" documents to be retrieved from a document database. Similarlity search is based comparing document vectors (see Vector Space Model). QBE is a seminal work in End-user development, frequently cited in research papers as an early example of this topic. Currently, QBE is supported in several relational database front ends, notably Microsoft Access, which implements "Visual Query by Example", as well as Microsoft SQL Server Enterprise Manager. It is also implement in several object-oriented databases (e.g. in db4o[1]).
Contents
[hide]

1 Example 2 As a general technique 3 See also 4 References 5 Sources 6 External links

[edit]Example

A simple example using the Suppliers and Parts database is given here to illustrate how QBE works.

[edit]As

a general technique

The term also refers to a general technique influenced by Zloof's work whereby only items with search values are used to "filter" the results. It provides a way for a software user to perform queries without having to know a query language (such as SQL). The software can automatically

generate the queries for the user (usually behind the scenes). Here are some examples: Example Form B:
.....Name: Bob ..Address: .....City: ....State: TX ..Zipcode:

Resulting SQL:
SELECT * FROM Contacts WHERE Name='Bob' AND State='TX'

Note how blank items do not generate SQL terms. Since "Address" is blank, there is no clause generated for it. Example Form C:
.....Name: ..Address: .....City: Sampleton ....State: ..Zipcode: 12345

Resulting SQL:
SELECT * FROM Contacts WHERE City='Sampleton' AND Zipcode=12345

More advanced versions of QBE have other comparison operator options, often via a pull-down menu, such as "Contains", "Starts With", "GreaterThan", and so forth.
[edit]

Вам также может понравиться