Jump to content

Data definition language: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
m Reverted edits by 49.185.161.75 (talk) to last version by WalkingRadiance
 
(522 intermediate revisions by more than 100 users not shown)
Line 1: Line 1:
{{short description|Syntax for defining data structures}}
A '''Data Definition Language''' is a [[computer language]] for defining data. [[XML Schema]] is an example of a pure DDL (although only relevant in the context of [[XML]]). A subset of [[SQL]]'s instructions form another DDL....bellz
{{Distinguish|Data manipulation language}}


{{Multiple issues|
For example in Oracle the DDL statements refer to CREATE,DROP,ALTER etc..
{{Refimprove|date=December 2012}}
{{Cleanup|reason=the article focuses almost entirely on SQL.|date=June 2020}}
}}
[[File:UY3OsG1vuT-saving-a-ddl-file-in-Oracle-Developer.png|alt=Saving a ddl file in Oracle SQL Developer|thumb|Saving a ddl file in Oracle SQL Developer]]
In the context of [[SQL]], '''data definition''' or '''data description language''' ('''DDL''') is a syntax for creating and modifying database objects such as tables, indices, and users. DDL statements are similar to a computer [[programming language]] for defining [[data structure]]s, especially [[database schema]]s. Common examples of DDL statements include <code>CREATE</code>, <code>ALTER</code>, and <code>DROP</code>. If you see a .ddl file, that means the file contains a statement to create a table. Oracle SQL Developer contains the ability to export from an ERD generated with Data Modeler to either a .sql file or a .ddl file.


==History==
These SQL statements define the structure of a database, including rows, columns, tables, indexes, and database specifics such as file locations. DDL SQL statements are more part of the DBMS and have large differences between the SQL variations. DDL SQL commands include the following:
The concept of the data definition language and its name was first introduced in relation to the [[Codasyl]] database model, where the schema of the [[database]] was written in a [[Syntax (programming languages)|language syntax]] describing the [[Record (computer science)|records]], [[Field (computer science)|fields]], and [[Set (abstract data type)|sets]] of the user [[data model]].<ref>{{cite book|last=Olle|first=T. William|title=The Codasyl Approach to Data Base Management|year=1978|publisher=Wiley|isbn=0-471-99579-7|url-access=registration|url=https://archive.org/details/codasylapproacht00olle}}</ref> Later it was used to refer to a subset of [[Structured Query Language]] (SQL) for declaring [[Table (database)|tables]], columns, data types and [[Integrity constraints|constraints]]. [[SQL-92]] introduced a schema manipulation language and schema information tables to query schemas.<ref name="SQL92">{{cite web |title=Information Technology - Database Language SQL |url=http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt |website=SQL92 |publisher=Carnegie Mellon |access-date=12 November 2018}}</ref> These information tables were specified as [[SQL/Schemata]] in [[SQL:2003]]. The term DDL is also used in a generic sense to refer to any [[formal language]] for describing data or information structures.


==Structured Query Language (SQL)==
Create - To make a new database, table, index, or stored query.
Many data description languages use a declarative syntax to define columns and data types. Structured Query Language (SQL), however, 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 elements. These statements can be freely mixed with other SQL statements, making the DDL not a separate language.
Drop - To destroy an existing database, table, index, or view.
DBCC (Database Console Commands) - Statements check the physical and logical consistency of a database.


===CREATE statement===
{{compu-lang-stub}}
The ''create'' command is used to establish a new database, table, index, or [[stored procedure]].


The ''CREATE'' statement in [[SQL]] creates a component in a [[relational database management system]] (RDBMS). In the SQL 1992 specification, the types of components that can be created are schemas, [[table (database)|tables]], [[View (database)|views]], domains, [[character set]]s, [[collation]]s, translations, and assertions.<ref name="SQL92" /> Many implementations extend the syntax to allow creation of additional elements, such as [[Database index|indexes]] and user profiles. Some systems, such as [[PostgreSQL]] and [[Microsoft SQL Server|SQL Server]], allow ''CREATE'', and other DDL commands, inside a [[database transaction]] and thus they may be [[rollback (data management)|rolled back]].<ref>{{cite web |last1=Laudenschlager |first1=Douglas |last2=Milener |first2=Gene |last3=Guyer |first3=Craig |last4=Byham |first4=Rick |title=Transactions (Transact-SQL) |url=https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transactions-transact-sql?view=sql-server-2017 |website=Microsoft Docs |publisher=Microsoft |access-date=12 November 2018}}</ref><ref>{{cite web |title=PostgreSQL Transactions |url=https://www.postgresql.org/docs/8.3/tutorial-transactions.html |website=PostgreSQL 8.3 Documentation |date=7 February 2013 |publisher=PostgreSQL |access-date=12 November 2018}}</ref>
[[Category:Data modeling]]
[[Category:SQL]]


====CREATE TABLE statement====
[[fr:Langage de définition de donnée]]
A commonly used ''CREATE'' command is the ''CREATE TABLE'' command. The typical usage is:
[[it:DDL]]

[[de:DDL]]
CREATE TABLE ''[table name]'' ( ''[column definitions]'' ) ''[table parameters]''

The column definitions are:
*A comma-separated list consisting of any of the following
*Column definition: ''[column name]'' ''[data type]'' ''{NULL | NOT NULL}'' ''{column options}''
*[[Primary key]] definition: ''PRIMARY KEY'' ( ''[comma separated column list]'' )
*Constraints: ''{CONSTRAINT}'' ''[constraint definition]''
*[[relational database system|RDBMS]] specific functionality

An example statement to create a table named ''employees'' with a few columns is:
<syntaxhighlight lang="sql">
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
first_name VARCHAR(50) not null,
last_name VARCHAR(75) not null,
mid_name VARCHAR(50) not null,
dateofbirth DATE not null
);
</syntaxhighlight>

Some forms of ''CREATE TABLE DDL'' may incorporate DML ([[data manipulation language]])-like constructs, such as the ''CREATE TABLE AS SELECT'' (CTaS) syntax of SQL.<ref>
{{cite book
| last = Allen
| first = Grant
| others = Mike Owens
| title = The Definitive Guide to SQLite
| url = https://books.google.com/books?id=WLinoJaOUCwC
| access-date = 2012-10-02
| edition = 2
| series = Apresspod
| year = 2010
| publisher = Apress
| isbn = 9781430232254
| pages = 90–91
| quote = The ''create table'' statement has a special syntax for creating tables from ''select'' statements. [...]: [...] ''create table foods2 as select * from foods;'' [...] Many other databases refer to this approach as ''CTaS'', which stands for Create Table as Select, and that phrase is not uncommon among SQLite users.
}}
</ref>

===DROP statement===
The ''DROP'' statement destroys an existing database, table, index, or view.

A ''DROP'' statement in [[SQL]] removes a component 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 [[table (database)|tables]], [[user (database)|users]], and [[database]]s. Some systems (such as [[PostgreSQL]]) allow DROP and other DDL commands to occur inside of a [[database transaction|transaction]] and thus be [[rollback (data management)|rolled back]]. The typical usage is simply:

DROP ''objecttype'' ''objectname''.

For example, the command to drop a table named '''employees''' is:

<syntaxhighlight lang="sql">
DROP TABLE employees;
</syntaxhighlight>

The ''DROP'' statement is distinct from the ''[[Delete (SQL)|DELETE]]'' and ''[[Truncate (SQL)|TRUNCATE]]'' statements, in that ''DELETE'' and ''TRUNCATE'' 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 removes the entire table from the database.

===ALTER statement===
The ''ALTER'' statement modifies 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''' is:

<syntaxhighlight lang="sql">
ALTER TABLE sink ADD bubbles INTEGER;
ALTER TABLE sink DROP COLUMN bubbles;
</syntaxhighlight>

===TRUNCATE statement===
The ''TRUNCATE'' statement is used to delete all data from a table. It's much faster than ''DELETE''.

<syntaxhighlight lang="sql">
TRUNCATE TABLE table_name;
</syntaxhighlight>

===Referential integrity statements===
Another type of DDL sentence in SQL is used 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 in a ''CREATE TABLE'' or an ''ALTER TABLE'' sentence;

==Other languages==
* [[XML Schema (W3C)|XML Schema]] is an example of a DDL for [[XML]].
* [[JSON#Metadata_and_schema|JSON Schema]] is an example of a DDL for [[JSON]].
* [[Data_Format_Description_Language|DFDL schema]] is an example of a DDL that can describe many text and binary formats.

==See also==
*[[Data control language]]
*[[Data manipulation language]]
*[[Data query language]]
*[[Select (SQL)]]
*[[Insert (SQL)]]
*[[Update (SQL)]]
*[[Delete (SQL)]]
*[[Truncate (SQL)]]

==References==
{{reflist}}

==External links==
* [https://oracletutorial.net/alter-table-modify-column-oracle.html Oracle ALTER TABLE MODIFY column] {{Webarchive|url=https://web.archive.org/web/20210421200756/https://oracletutorial.net/alter-table-modify-column-oracle.html |date=2021-04-21 }}
* [https://oracletutorial.net/dml-ddl-commands-in-oracle.html#ddl-commands-in-oracle DDL Commands In Oracle] {{Webarchive|url=https://web.archive.org/web/20210421203338/https://oracletutorial.net/dml-ddl-commands-in-oracle.html#ddl-commands-in-oracle |date=2021-04-21 }}

{{Database}}

[[Category:Articles with example SQL code]]
[[Category:Data modeling languages]]
[[Category:SQL]]

Latest revision as of 19:09, 27 November 2024

Saving a ddl file in Oracle SQL Developer
Saving a ddl file in Oracle SQL Developer

In the context of SQL, data definition or data description language (DDL) is a syntax for creating and modifying database objects such as tables, indices, and users. DDL statements are similar to a computer programming language for defining data structures, especially database schemas. Common examples of DDL statements include CREATE, ALTER, and DROP. If you see a .ddl file, that means the file contains a statement to create a table. Oracle SQL Developer contains the ability to export from an ERD generated with Data Modeler to either a .sql file or a .ddl file.

History

[edit]

The concept of the data definition language and its name was first introduced in relation to the Codasyl database model, where the schema of the database was written in a language syntax describing the records, fields, and sets of the user data model.[1] Later it was used to refer to a subset of Structured Query Language (SQL) for declaring tables, columns, data types and constraints. SQL-92 introduced a schema manipulation language and schema information tables to query schemas.[2] These information tables were specified as SQL/Schemata in SQL:2003. The term DDL is also used in a generic sense to refer to any formal language for describing data or information structures.

Structured Query Language (SQL)

[edit]

Many data description languages use a declarative syntax to define columns and data types. Structured Query Language (SQL), however, 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 elements. These statements can be freely mixed with other SQL statements, making the DDL not a separate language.

CREATE statement

[edit]

The create command is used to establish a new database, table, index, or stored procedure.

The CREATE statement in SQL creates a component in a relational database management system (RDBMS). In the SQL 1992 specification, the types of components that can be created are schemas, tables, views, domains, character sets, collations, translations, and assertions.[2] Many implementations extend the syntax to allow creation of additional elements, such as indexes and user profiles. Some systems, such as PostgreSQL and SQL Server, allow CREATE, and other DDL commands, inside a database transaction and thus they may be rolled back.[3][4]

CREATE TABLE statement

[edit]

A commonly used CREATE command is the CREATE TABLE command. The typical usage is:

CREATE TABLE [table name] ( [column definitions] ) [table parameters]

The column definitions are:

  • A comma-separated list consisting of any of the following
  • Column definition: [column name] [data type] {NULL | NOT NULL} {column options}
  • Primary key definition: PRIMARY KEY ( [comma separated column list] )
  • Constraints: {CONSTRAINT} [constraint definition]
  • RDBMS specific functionality

An example statement to create a table named employees with a few columns is:

CREATE TABLE employees (
    id            INTEGER       PRIMARY KEY,
    first_name    VARCHAR(50)   not null,
    last_name     VARCHAR(75)   not null,
    mid_name      VARCHAR(50)   not null,
    dateofbirth   DATE          not null
);

Some forms of CREATE TABLE DDL may incorporate DML (data manipulation language)-like constructs, such as the CREATE TABLE AS SELECT (CTaS) syntax of SQL.[5]

DROP statement

[edit]

The DROP statement destroys an existing database, table, index, or view.

A DROP statement in SQL removes a component 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 is:

DROP TABLE employees;

The DROP statement is distinct from the DELETE and TRUNCATE statements, in that DELETE and TRUNCATE 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 removes the entire table from the database.

ALTER statement

[edit]

The ALTER statement modifies 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 is:

ALTER TABLE sink ADD bubbles INTEGER;
ALTER TABLE sink DROP COLUMN bubbles;

TRUNCATE statement

[edit]

The TRUNCATE statement is used to delete all data from a table. It's much faster than DELETE.

TRUNCATE TABLE table_name;

Referential integrity statements

[edit]

Another type of DDL sentence in SQL is used 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 in a CREATE TABLE or an ALTER TABLE sentence;

Other languages

[edit]

See also

[edit]

References

[edit]
  1. ^ Olle, T. William (1978). The Codasyl Approach to Data Base Management. Wiley. ISBN 0-471-99579-7.
  2. ^ a b "Information Technology - Database Language SQL". SQL92. Carnegie Mellon. Retrieved 12 November 2018.
  3. ^ Laudenschlager, Douglas; Milener, Gene; Guyer, Craig; Byham, Rick. "Transactions (Transact-SQL)". Microsoft Docs. Microsoft. Retrieved 12 November 2018.
  4. ^ "PostgreSQL Transactions". PostgreSQL 8.3 Documentation. PostgreSQL. 7 February 2013. Retrieved 12 November 2018.
  5. ^ Allen, Grant (2010). The Definitive Guide to SQLite. Apresspod. Mike Owens (2 ed.). Apress. pp. 90–91. ISBN 9781430232254. Retrieved 2012-10-02. The create table statement has a special syntax for creating tables from select statements. [...]: [...] create table foods2 as select * from foods; [...] Many other databases refer to this approach as CTaS, which stands for Create Table as Select, and that phrase is not uncommon among SQLite users.
[edit]