SQL: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(39 intermediate revisions by the same user not shown)
Line 10: Line 10:
This article documents standard SQL. For database-specific extensions, we'll link to the databases-specific DML operations page.
This article documents standard SQL. For database-specific extensions, we'll link to the databases-specific DML operations page.


SQL is a non-procedural language <font color=darkkhaki>TODO: [https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch01.html#sql_colon_a_nonprocedural_language this]</font>
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]].


=Terms=
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.
==Table==
In a relational model, data is stored in a set of tables, also known as relations. Within a relation, data is organized in [[Relational_Databases#Row|rows]] (records) and [[Relational_Databases#Column|columns]].


Also see: {{Internal|Relational_Databases#Table|Relational Databases &#124; Table}}
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.
==Primary Key==
{{Internal|Relational_Databases#Primary_Key|Relational Databases &#124; Primary Keys}}


==Entity==
=<span id='Table'></span>Relational Database Terminology=
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}}


==Result Set==
==Result Set==
The result of an [[SQL_SELECT#Overview|SQL query]], known as a '''result set''', is also a [[Relational_Databases#Table_(Relation)|table]] maintained in memory.
==View==
==View==


=SQL Schema Statements (DDL)=
=SQL Schema Statements (DDL)=
<font color=darkkhaki>
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: {{Internal|SQL_Database_Metadata#Overview|Database Metadata}}
* Refactor [[PostgreSQL_DDL_Operations#Internal|PostgreSQL DDL Operations]] and surface in "Standard SQL" everything that can be handled with standard SQL.
==<span id='CADT'></span><tt>CREATE | ALTER | DROP TABLE</tt>==
* Refactor [[MySQL_DDL_Operations|MySQL DDL Operations]] and surface in "Standard SQL" everything that can be handled with standard SQL.
{{Internal|SQL_CREATE_ALTER_DROP_TABLE#Overview|<tt>CREATE</tt> &#124; <tt>ALTER</tt> &#124; <tt>DROP</tt> <tt>TABLE</tt>}}
</font>


==<tt>CREATE</tt>==
=SQL Data Statements (DML)=
===<tt>CREATE TABLE</tt>===
===<tt>ALTER TABLE</tt>===


=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).
<font color=darkkhaki>
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>==
==<span id='SELECT'></span>Queries with <tt>SELECT</tt>==
{{Internal|SQL_SELECT#Overview|Queries with <tt>SELECT</tt>}}
{{Internal|SQL_SELECT#Overview|Queries with <tt>SELECT</tt>}}


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


<font color=darkkhaki>Transactions</font>.
{{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>=
Line 63: Line 57:
</font>
</font>
=Indexes=
=Indexes=
{{Internal|SQL_Indexes#Overview|SQL Indexes}}
=Constraints=
=Constraints=
{{Internal|SQL_Constraints#Overview|SQL Constraints}}
=Set Operations=
=Set Operations=
=SQL Conditional Logic=
=SQL Conditional Logic=

Revision as of 23:50, 22 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

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

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

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).

INSERT

INSERT

UPDATE

UPDATE

DELETE

DELETE

Queries with SELECT

Queries with SELECT

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

NULL in conditions.

Indexes

SQL Indexes

Constraints

SQL Constraints

Set Operations

SQL Conditional Logic