SQL: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* [https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604 Learning SQL, 3rd Edition] by Alan Beaulieu
* [https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604 Learning SQL, 3rd Edition] by Alan Beaulieu
* http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt


=Internal=
=Internal=
Line 12: Line 13:
SQL is a standard non-procedural language designed to work with relational databases, allowing the users to interact with the database instance by issuing [[#SQL_Schema_Statements_(DDL)|schema statements]], [[#SQL_Data_Statements_(DML)|data statements]] and [[#SQL_Transaction_Statements|transaction statements]].
SQL is a standard non-procedural language designed to work with relational databases, allowing the users to interact with the database instance by issuing [[#SQL_Schema_Statements_(DDL)|schema statements]], [[#SQL_Data_Statements_(DML)|data statements]] and [[#SQL_Transaction_Statements|transaction statements]].


SQL statements define the necessary inputs and outputs for the database interaction, but it does not define how the statements should be executed. The details of the execution are left to the database engine component known as the '''optimizer'''.
SQL statements define the necessary inputs and outputs for the database interaction, but it does not define how the statements should be executed. The details of the execution are left to the database engine component known as the optimizer. The optimizer parses the SQL statements and depending on how the tables are configured and what [[SQL_Indexes#Overview|indexes]] exist, it decides the most efficient execution path. Most database engines will allow you to influence the optimizer's decisions by accepting optimizer hints.


The first standard for the SQL language was published by the American National Standards Institute (ANSI) in 1986. Subsequent refinements led to new releases of the SQL standard in 1989, 1992, 1999, 2003, 2006, 2008, 2011, and 2016.
The first standard for the SQL language was published by the American National Standards Institute (ANSI) in 1986. Subsequent refinements led to new releases of the SQL standard in 1989, 1992, 1999, 2003, 2006, 2008, 2011, and 2016.


=<span id='Table'></span>Relational Database Terminology=
=<span id='Table'></span>Relational Database Terminology=
==Table==
In a relational model, data is stored in a set of [[Relational_Databases#Table_(Relation)|tables]], also known as [[Relational_Databases#Table_(Relation)|relations]]. Within a relation, data is organized in [[Relational_Databases#Row|rows]] ([[Relational_Databases#Record_(Row)|records]]) and [[Relational_Databases#Column|columns]]. Each row describes an [[Relational_Databases#Entity|entity]]. Each is uniquely identified by a [[Relational_Databases#Primary_Key|primary key]]. The rows may contain columns that are copies of other tables' primary keys. These are knowns as [[Relational_Databases#Foreign_Key|foreign keys]] and are used in [[SQL_Querying_Multiple_Tables#Join|joining]] related data together.
In a relational model, data is stored in a set of [[Relational_Databases#Table_(Relation)|tables]], also known as [[Relational_Databases#Table_(Relation)|relations]]. Within a relation, data is organized in [[Relational_Databases#Row|rows]] ([[Relational_Databases#Record_(Row)|records]]) and [[Relational_Databases#Column|columns]]. Each row describes an [[Relational_Databases#Entity|entity]]. Each is uniquely identified by a [[Relational_Databases#Primary_Key|primary key]]. The rows may contain columns that are copies of other tables' primary keys. These are knowns as [[Relational_Databases#Foreign_Key|foreign keys]] and are used in [[SQL_Querying_Multiple_Tables#Join|joining]] related data together.


Also see: {{Internal|Relational_Databases#Table|Relational Databases}}
Also see: {{Internal|Relational_Databases#Table|Relational Databases}}
From an SQL perspective, there are four types of tables. All of them can be used in the <code>[[SQL_SELECT#FROM|FROM]]</code> clause of a [[SQL_SELECT#Overview|SELECT query]]:
===Permanent Table===
A table created with a <code>[[SQL_CREATE_ALTER_DROP_TABLE#CREATE|CREATE TABLE]]</code> statement.
===Temporary Table===
A temporary (or volatile) table can be created with <code>[[SQL_CREATE_ALTER_DROP_TABLE#CREATE|CREATE TEMPORARY TABLE]]</code>. It looks just like a permanent table, but any data inserted into a temporary table will disappear at some point, generally at the end of a transaction or when the database session is closed.
===Derived Table===
Derived tables are dynamically built in memory when a [[SQL_SELECT#Containing_Query|containing query]] executes a [[SQL_SELECT#Subqueries_(Querying_a_Derived_Table)|subquery]]. The derived table data is held in memory for the duration of the query execution and then discarded. See: {{Internal|SQL_SELECT#Querying_a_Derived_Table|Querying a Derived Table}}
===<span id='Virtual'></span><span id='View'></span>Virtual Table (View)===
A '''view''', or a '''virtual table''', is a query that is stored in the database's [[#Data_Dictionary|data dictionary]]. It looks and acts like a table, but there is no data associated with the view. A view is created with a <code>[[SQL_CREATE_ALTER_DROP_VIEW#CREATE|CREATE VIEW]]</code> statement.


==Result Set==
==Result Set==
Line 27: Line 41:


=SQL Schema Statements (DDL)=
=SQL Schema Statements (DDL)=
SQL schema statements are used to define the data structures stored in the database. This SQL subset is also known as Data Definition Language (DDL).
SQL schema statements are used to define the data structures stored in the database. This SQL subset is also known as Data Definition Language (DDL). All database elements (tables, constraints, etc.) create with the SQL schema statements are stored in a special set of table called the <span id='Data_Dictionary'></span>'''data dictionary''', or '''metadata'''. Also see: {{Internal|SQL_Database_Metadata#Overview|Database Metadata}}
 
==<span id='CADT'></span><tt>CREATE | ALTER | DROP TABLE</tt>==
<font color=darkkhaki>
* Refactor [[PostgreSQL_DDL_Operations#Internal|PostgreSQL DDL Operations]] and surface in "Standard SQL" everything that can be handled with standard SQL.
* Refactor [[MySQL_DDL_Operations|MySQL DDL Operations]] and surface in "Standard SQL" everything that can be handled with standard SQL.
</font>
 
==<tt>CREATE | ALTER | DROP TABLE</tt>==
{{Internal|SQL_CREATE_ALTER_DROP_TABLE#Overview|<tt>CREATE</tt> &#124; <tt>ALTER</tt> &#124; <tt>DROP</tt> <tt>TABLE</tt>}}
{{Internal|SQL_CREATE_ALTER_DROP_TABLE#Overview|<tt>CREATE</tt> &#124; <tt>ALTER</tt> &#124; <tt>DROP</tt> <tt>TABLE</tt>}}
==<span id='CADV'></span><tt>CREATE | ALTER | DROP VIEW</tt>==
{{Internal|SQL_CREATE_ALTER_DROP_VIEW#Overview|<tt>CREATE</tt> &#124; <tt>ALTER</tt> &#124; <tt>DROP</tt> <tt>VIEW</tt>}}
==Constraints==
{{Internal|SQL_Constraints#Overview|SQL Constraints}}


=SQL Data Statements (DML)=
=SQL Data Statements (DML)=
SQL data statements are used to manipulate the data structures perviously defined using the [[#SQL_Schema_Statements_(DDL)|SQL schema statements]]. This SQL subset is also known as Data Manipulation Language (DML).
SQL data statements are used to manipulate the data structures perviously defined using the [[#SQL_Schema_Statements_(DDL)|SQL schema statements]]. This SQL subset is also known as Data Manipulation Language (DML).
 
==<span id='SELECT'></span>Queries with <tt>SELECT</tt>==
<font color=darkkhaki>
{{Internal|SQL_SELECT#Overview|Queries with <tt>SELECT</tt>}}
Refactor [[PostgreSQL_DML_Operations#Internal|PostgreSQL DML Operations]] and surface in "Standard SQL" everything that can be handled with standard SQL.</font>
==<tt>INSERT</tt>==
==<tt>INSERT</tt>==
{{Internal|SQL_INSERT#Overview|<tt>INSERT</tt>}}
==<tt>UPDATE</tt>==
==<tt>UPDATE</tt>==
<font color=darkkhaki>UPDATE WHERE</font>
{{Internal|SQL_UPDATE#Overview|<tt>UPDATE</tt>}}
==<tt>DELETE</tt>==
==<tt>DELETE</tt>==
<font color=darkkhaki>DELETE WHERE</font>
{{Internal|SQL_DELETE#Overview|<tt>DELETE</tt>}}
==<span id='SELECT'></span>Queries with <tt>SELECT</tt>==
{{Internal|SQL_SELECT#Overview|Queries with <tt>SELECT</tt>}}


=SQL Transaction Statements=
=SQL Transaction Statements=
Line 56: Line 65:
{{Internal|SQL_Transaction_Statements#Overview|Transaction Statements}}
{{Internal|SQL_Transaction_Statements#Overview|Transaction Statements}}


=Data Types=
=Standard SQL Data Types=
<font color=darkkhaki>
{{Internal|SQL_Data_Types#Overview|Standard SQL Data Types}}
* SQL data types
* Database-specific data types
* Working with different data types
* Conversion functions
</font>


=SQL <tt>NULL</tt>=
=SQL <tt>NULL</tt>=
<font color=darkkhaki>
There are cases when values for certain columns cannot be determined. In these cases, the column is said to be <code>NULL</code> (it is not said that it equals to <code>NULL</code>). <code>NULL</code> indicates an absence of a value. <code>NULL</code> is used in the following cases:
<code>NULL</code> in conditions.
* not applicable
</font>
* unknown
* empty set
When [[SQL_CREATE_ALTER_DROP_TABLE#Overview|creating a table]], the columns are allowed to be <code>NULL</code> by default. If specific columns must not accept <code>NULL</code> values, they must be declared with a [[SQL_Constraints#NOT_NULL_Constraint|NOT NULL constraint]].  
 
Also see: {{Internal|SQL_WHERE#NULL_in_Conditions|<tt>NULL</tt> in <tt>WHERE</tt> Conditions}}
 
=Indexes=
=Indexes=
=Constraints=
{{Internal|SQL_Indexes#Overview|SQL Indexes}}
 
=Set Operations=
=Set Operations=
=SQL Conditional Logic=
{{Internal|SQL_Set_Operations#Overview|SQL Set Operations}}
=Analytic Functions=
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch16.html</font>
=Parameterized SQL Statements=
:{| class="wikitable" style="text-align: left;"
|-
| [[PostgreSQL_Concepts#Parameterized_SQL_Statements|Parameterized SQL Statements in PostgreSQL]] || Parameterized SQL Statements in MySQL
|-
|}

Latest revision as of 19:30, 31 May 2024

External

Internal

Overview

This article documents standard SQL. For database-specific extensions, we'll link to the databases-specific DML operations page.

SQL is a standard non-procedural language designed to work with relational databases, allowing the users to interact with the database instance by issuing schema statements, data statements and transaction statements.

SQL statements define the necessary inputs and outputs for the database interaction, but it does not define how the statements should be executed. The details of the execution are left to the database engine component known as the optimizer. The optimizer parses the SQL statements and depending on how the tables are configured and what indexes exist, it decides the most efficient execution path. Most database engines will allow you to influence the optimizer's decisions by accepting optimizer hints.

The first standard for the SQL language was published by the American National Standards Institute (ANSI) in 1986. Subsequent refinements led to new releases of the SQL standard in 1989, 1992, 1999, 2003, 2006, 2008, 2011, and 2016.

Relational Database Terminology

Table

In a relational model, data is stored in a set of tables, also known as relations. Within a relation, data is organized in rows (records) and columns. Each row describes an entity. Each is uniquely identified by a primary key. The rows may contain columns that are copies of other tables' primary keys. These are knowns as foreign keys and are used in joining related data together.

Also see:

Relational Databases

From an SQL perspective, there are four types of tables. All of them can be used in the FROM clause of a SELECT query:

Permanent Table

A table created with a CREATE TABLE statement.

Temporary Table

A temporary (or volatile) table can be created with CREATE TEMPORARY TABLE. It looks just like a permanent table, but any data inserted into a temporary table will disappear at some point, generally at the end of a transaction or when the database session is closed.

Derived Table

Derived tables are dynamically built in memory when a containing query executes a subquery. The derived table data is held in memory for the duration of the query execution and then discarded. See:

Querying a Derived Table

Virtual Table (View)

A view, or a virtual table, is a query that is stored in the database's data dictionary. It looks and acts like a table, but there is no data associated with the view. A view is created with a CREATE VIEW statement.

Result Set

The result of an SQL query, known as a result set, is also a table maintained in memory.

View

SQL Schema Statements (DDL)

SQL schema statements are used to define the data structures stored in the database. This SQL subset is also known as Data Definition Language (DDL). All database elements (tables, constraints, etc.) create with the SQL schema statements are stored in a special set of table called the data dictionary, or metadata. Also see:

Database Metadata

CREATE | ALTER | DROP TABLE

CREATE | ALTER | DROP TABLE

CREATE | ALTER | DROP VIEW

CREATE | ALTER | DROP VIEW

Constraints

SQL Constraints

SQL Data Statements (DML)

SQL data statements are used to manipulate the data structures perviously defined using the SQL schema statements. This SQL subset is also known as Data Manipulation Language (DML).

Queries with SELECT

Queries with SELECT

INSERT

INSERT

UPDATE

UPDATE

DELETE

DELETE

SQL Transaction Statements

The SQL transaction statements are used to begin, end, and rollback transactions.

Transaction Statements

Standard SQL Data Types

Standard SQL Data Types

SQL NULL

There are cases when values for certain columns cannot be determined. In these cases, the column is said to be NULL (it is not said that it equals to NULL). NULL indicates an absence of a value. NULL is used in the following cases:

  • not applicable
  • unknown
  • empty set

When creating a table, the columns are allowed to be NULL by default. If specific columns must not accept NULL values, they must be declared with a NOT NULL constraint.

Also see:

NULL in WHERE Conditions

Indexes

SQL Indexes

Set Operations

SQL Set Operations

Analytic Functions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch16.html

Parameterized SQL Statements

Parameterized SQL Statements in PostgreSQL Parameterized SQL Statements in MySQL