<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wikitechia.top/index.php?action=history&amp;feed=atom&amp;title=SQL_-_Data_Definition</id>
	<title>SQL - Data Definition - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wikitechia.top/index.php?action=history&amp;feed=atom&amp;title=SQL_-_Data_Definition"/>
	<link rel="alternate" type="text/html" href="http://wikitechia.top/index.php?title=SQL_-_Data_Definition&amp;action=history"/>
	<updated>2026-10-26T04:39:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>http://wikitechia.top/index.php?title=SQL_-_Data_Definition&amp;diff=1928&amp;oldid=prev</id>
		<title>Root: Created page with &quot;= SQL: Data Definition Language (DDL) =  In this section, we will focus on the use of SQL to create database and table structures, using SQL as a Data Definition Language (DDL).  == Database creation and deletion ==  The main SQL DDL statements are:  * CREATE/DROP DATABASE * CREATE/DROP/ALTER TABLE  === Database creation - CREATE ===  The SQL statement CREATE is used to create the database and the structure of the tables.  Syntax of the CREATE DATABASE statement:  &lt;synta...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wikitechia.top/index.php?title=SQL_-_Data_Definition&amp;diff=1928&amp;oldid=prev"/>
		<updated>2026-01-14T13:34:23Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= SQL: Data Definition Language (DDL) =  In this section, we will focus on the use of SQL to create database and table structures, using SQL as a Data Definition Language (DDL).  == Database creation and deletion ==  The main SQL DDL statements are:  * CREATE/DROP DATABASE * CREATE/DROP/ALTER TABLE  === Database creation - CREATE ===  The SQL statement CREATE is used to create the database and the structure of the tables.  Syntax of the CREATE DATABASE statement:  &amp;lt;synta...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= SQL: Data Definition Language (DDL) =&lt;br /&gt;
&lt;br /&gt;
In this section, we will focus on the use of SQL to create database and table structures, using SQL as a Data Definition Language (DDL).&lt;br /&gt;
&lt;br /&gt;
== Database creation and deletion ==&lt;br /&gt;
&lt;br /&gt;
The main SQL DDL statements are:&lt;br /&gt;
&lt;br /&gt;
* CREATE/DROP DATABASE&lt;br /&gt;
* CREATE/DROP/ALTER TABLE&lt;br /&gt;
&lt;br /&gt;
=== Database creation - CREATE ===&lt;br /&gt;
&lt;br /&gt;
The SQL statement CREATE is used to create the database and the structure of the tables.&lt;br /&gt;
&lt;br /&gt;
Syntax of the CREATE DATABASE statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE DATABASE databasename;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example: Example of creating a new DB named Traders:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE DATABASE Traders;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the database has been created, the next step is to create its tables.&lt;br /&gt;
&lt;br /&gt;
=== Database deletion - DROP ===&lt;br /&gt;
&lt;br /&gt;
Syntax of the DROP DATABASE statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP DATABASE [IF EXISTS] databasename;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example: Example of deleting an existing database:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP DATABASE IF EXISTS Traders;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: It is common to use the deletion and creation instructions of a database together, using the conditional (IF EXISTS) to avoid errors.&lt;br /&gt;
&lt;br /&gt;
=== Database modification - ALTER DATABASE ===&lt;br /&gt;
&lt;br /&gt;
Syntax of the ALTER DATABASE statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
ALTER {DATABASE | SCHEMA} db_Name&lt;br /&gt;
    alter specification...&lt;br /&gt;
&lt;br /&gt;
alter specification:&lt;br /&gt;
    [DEFAULT] CHARACTER SET [=] charset_name&lt;br /&gt;
    [DEFAULT] COLLATE [=] collation_name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where:&lt;br /&gt;
&lt;br /&gt;
* ALTER DATABASE allows changing the general characteristics of a database. These characteristics are stored in the db.opt file in the database directory. To use the ALTER DATABASE command, you need the ALTER privilege on the database. ALTER SCHEMA is a synonym for ALTER DATABASE.&lt;br /&gt;
* The CHARACTER SET clause changes the default character set of the database.&lt;br /&gt;
* The COLLATE clause changes the default collation set of the database.&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
* You can see which character sets and collations are available using, respectively, the SHOW CHARACTER SET and SHOW COLLATION instructions.&lt;br /&gt;
* Changing the default character set or collation of a database does not affect the character set or collation of any stored procedure or stored function created previously that depended on the default values. These must be deleted and recreated to apply the changes in the character set or collation.&lt;br /&gt;
&lt;br /&gt;
Example: Modification of the character set and collation of the database.&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
ALTER DATABASE laMevaBD&lt;br /&gt;
    CHARACTER SET utf8mb4&lt;br /&gt;
    COLLATE utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Table creation - CREATE TABLE ==&lt;br /&gt;
&lt;br /&gt;
Syntax of the CREATE TABLE statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE nom_taula (&lt;br /&gt;
 nom_columna tipus_de_dada restriccions_de_columna,&lt;br /&gt;
 nom_columna tipus_de_dada restriccions_de_columna,&lt;br /&gt;
 ...&lt;br /&gt;
 restriccions_de_taula&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where:&lt;br /&gt;
* nom_taula is the table name of the database, such as, for example, Employees.&lt;br /&gt;
* Each field of CREATE TABLE has three parts:&lt;br /&gt;
** Column name&lt;br /&gt;
** Data type&lt;br /&gt;
** Optional column constraints&lt;br /&gt;
&lt;br /&gt;
=== Column names ===&lt;br /&gt;
&lt;br /&gt;
 the name of a column must be unique within the table. Some examples of column names are FirstName and LastName.&lt;br /&gt;
&lt;br /&gt;
=== Data types ===&lt;br /&gt;
&lt;br /&gt;
The data type must be one of the types defined by MySQL/MariaDB. It must be taken into account that many data types have a length, such as: CHAR(35) or DECIMAL(8,2).&lt;br /&gt;
&lt;br /&gt;
The main data types we can use are the following:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Class !! Type !! Description !! Range&lt;br /&gt;
|-&lt;br /&gt;
| &amp;#039;&amp;#039;&amp;#039;Numeric&amp;#039;&amp;#039;&amp;#039; || TINYINT || Very small integers (1 Byte) || -128 to 127 or 0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| || SMALLINT || Small integers (2 Bytes) || -32718 to 32767 or 0 to 65535&lt;br /&gt;
|-&lt;br /&gt;
| || MEDIUMINT || Medium integers (3 Bytes) || -8388608 to 8388607 or 0 to 16777215&lt;br /&gt;
|-&lt;br /&gt;
| || INT or INTEGER || Integers (4 Bytes) || -2147483648 to 2147483647 or 0 to 4294967295&lt;br /&gt;
|-&lt;br /&gt;
| || BIGINT || Very large integers (8 Bytes) || -2^63 to 2^63-1 or 0 to 2^64-1&lt;br /&gt;
|-&lt;br /&gt;
| || FLOAT || Floating point numbers and simple precision ||&lt;br /&gt;
|-&lt;br /&gt;
| || DOUBLE or REAL || Floating point numbers and double precision ||&lt;br /&gt;
|-&lt;br /&gt;
| || DECIMAL or NUMERIC (M,D) || Floating point numbers stored as a string, where M indicates the display width and D the number of decimals ||&lt;br /&gt;
|-&lt;br /&gt;
| &amp;#039;&amp;#039;&amp;#039;String&amp;#039;&amp;#039;&amp;#039; || CHAR(D) || Fixed length string, where D indicates the length || 1 to 255&lt;br /&gt;
|-&lt;br /&gt;
| || VARCHAR(D) || Variable length string, where D indicates the length || 0 to 65535&lt;br /&gt;
|-&lt;br /&gt;
| || TEXT || Text || 0 to 65535&lt;br /&gt;
|-&lt;br /&gt;
| || ENUM || Enumerates types (limits to a list) || Example: ENUM(&amp;#039;a&amp;#039;,&amp;#039;b&amp;#039;,&amp;#039;c&amp;#039;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;#039;&amp;#039;&amp;#039;Date and time&amp;#039;&amp;#039;&amp;#039; || DATE || Date in YYYY-MM-DD format || 1000-01-01 to 9999-12-31&lt;br /&gt;
|-&lt;br /&gt;
| || TIME || Time in hh:mm:ss format ||&lt;br /&gt;
|-&lt;br /&gt;
| || DATETIME || Date and time in YYYY-MM-DD hh:mm:ss format || 1000-01-01 00:00:00 to 9999-12-31 23:59:59&lt;br /&gt;
|-&lt;br /&gt;
| || TIMESTAMP || Date and time in YYYYMMDDhhmmss format ||&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Numeric data types ====&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Integers&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
** From small to large: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT. You can see how much they occupy and their range of values at: Integer types.&lt;br /&gt;
** Optionally, integer types can be UNSIGNED. In this case, only positive numbers can be saved.&lt;br /&gt;
&lt;br /&gt;
Note: Furthermore, integer types can carry a length, for example, INT(5). This length does not modify the range and size of the data type; it is only an indication of how applications should display the data. If we put a value that exceeds the data range, it will give us an error.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Reals&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
The DECIMAL (or NUMERIC) type allows saving numbers with a certain precision and scale. DECIMAL guarantees that the numbers will be saved exactly with the specified precision.&lt;br /&gt;
&lt;br /&gt;
The declaration, for example, DECIMAL(5,2), indicates that up to 5 figures will be saved, of which two are decimals. Thus, the range of values that can be saved in a DECIMAL(5,2) is from -999.99 to 999.99. DECIMAL(5) is a synonym for DECIMAL(5,0), and DECIMAL is a synonym for DECIMAL(10).&lt;br /&gt;
&lt;br /&gt;
The FLOAT and DOUBLE types allow saving approximate decimal numbers and are equivalent to the float and double data types of some programming languages, such as Java.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Bit sequences&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
The BIT type allows saving bit sequences. For example, BIT(8) saves 8-bit sequences. To indicate a literal bit value, it is done with the prefix &amp;quot;b&amp;quot;, for example: b&amp;#039;100101&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Booleans&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
To save a true or false value, we can use the BOOL (or BOOLEAN) type. Actually, these words are synonyms for TINYINT(1). Values different from 0 are considered true and 0 is considered false.&lt;br /&gt;
You can also use a BIT(1) to save a boolean value.&lt;br /&gt;
&lt;br /&gt;
==== Text data types ====&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Short text strings&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
The CHAR and VARCHAR types are used for the most common text strings. Both need to be declared indicating the amount of characters to save, for example, CHAR(30) or VARCHAR(30).&lt;br /&gt;
&lt;br /&gt;
The difference lies in the fact that CHAR(30) will always save 30 characters. If the string saved is shorter, spaces are added to complete the indicated amount of characters (these spaces are removed again when the data is retrieved). In contrast, for VARCHAR(30), what is indicated is the maximum characters that can be saved, but shorter strings are saved without occupying extra space.&lt;br /&gt;
&lt;br /&gt;
In general, CHAR should be used for data that we know will always have the same length and VARCHAR for data of variable length. For data that always has the same length, VARCHAR occupies more space internally. Furthermore, if all the fields of a table are of fixed length, searching for data on this table is faster.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Byte strings&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
BINARY and VARBINARY are used to save byte sequences that are not text or that are not to be treated as text.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Long strings&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
TEXT is used for long text strings and BLOB for long binary strings. The types resemble VARCHAR and VARBINARY respectively but have a much higher storage capacity.&lt;br /&gt;
&lt;br /&gt;
TEXT and BLOB each have 4 variants: TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT, and TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB respectively. The only difference between them is the maximum length they accept.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;String ranges (enumerations)&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
The ENUM type allows specifying a fixed set of text strings that are valid for a certain field. For example, a field that saves the name of a day of the week could be declared as:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
ENUM(&amp;#039;dilluns&amp;#039;,&amp;#039;dimarts&amp;#039;,&amp;#039;dimecres&amp;#039;,&amp;#039;dijous&amp;#039;,&amp;#039;divendres&amp;#039;,&amp;#039;dissabte&amp;#039;,&amp;#039;diumenge&amp;#039;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Internally, ENUM data is saved as integers, so an ENUM occupies much less than using, for example, a VARCHAR to save the same information. Using an ENUM is similar to using an integer number with the advantage that with an ENUM queries and responses are clearer.&lt;br /&gt;
&lt;br /&gt;
The SET type also allows defining a set of valid text strings for a field but, unlike ENUM, a record can take none, one, or more values from the values defined in the set.&lt;br /&gt;
&lt;br /&gt;
==== Time and date data types ====&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Dates&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
The DATE type allows saving dates. Dates are saved in year-month-day format, for example, &amp;#039;2016-10-05&amp;#039;.&lt;br /&gt;
The DATETIME type allows storing combinations of a specific date and time, for example, &amp;#039;2016-10-05 15:40:32&amp;#039;. Seconds also accept fractions.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Time&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
The TIME type is used to save a specific time, without a date, for example &amp;#039;15:30:32&amp;#039;.&lt;br /&gt;
Finally, the YEAR type is used to save years, for example: &amp;#039;2016&amp;#039;.&lt;br /&gt;
The TIMESTAMP type is similar to DATETIME, but is always stored in the UTC time zone and has a more reduced range of values. It is often used to mark events related to the database itself; for example, if we want to save the moment a row was last updated, we would use a TIMESTAMP type field.&lt;br /&gt;
&lt;br /&gt;
== Optional column constraints ==&lt;br /&gt;
&lt;br /&gt;
The declaration of a column can be accompanied by a series of modifiers that specify the behavior it must have.&lt;br /&gt;
&lt;br /&gt;
=== NULL and NOT NULL ===&lt;br /&gt;
&lt;br /&gt;
The NULL modifier indicates that the column can have NULL values, while NOT NULL prohibits NULL values from being saved. If neither is indicated, it is assumed that NULL values can be saved.&lt;br /&gt;
&lt;br /&gt;
=== DEFAULT ===&lt;br /&gt;
&lt;br /&gt;
The DEFAULT modifier allows specifying a default value for the column, so that if when inserting a row the value for this column is not indicated, the default value is taken.&lt;br /&gt;
&lt;br /&gt;
=== AUTO_INCREMENT ===&lt;br /&gt;
&lt;br /&gt;
The AUTO_INCREMENT modifier indicates that it is an integer column to which we want a different value to be assigned for each row. It is used mainly to generate synthetic primary keys that receive a valid value automatically.&lt;br /&gt;
&lt;br /&gt;
=== UNIQUE ===&lt;br /&gt;
&lt;br /&gt;
The UNIQUE modifier indicates that there cannot be two equal NOT NULL values in that column for the whole table.&lt;br /&gt;
&lt;br /&gt;
Declaring a column as UNIQUE causes an index to be automatically created for the selected column. This index speeds up searches and sortings that use the column.&lt;br /&gt;
&lt;br /&gt;
=== PRIMARY KEY ===&lt;br /&gt;
&lt;br /&gt;
Indicates that this column is the primary key of the table. If the primary key consists of more than one column, it must be specified after the declaration of all columns.&lt;br /&gt;
&lt;br /&gt;
A primary key is, automatically, NOT NULL.&lt;br /&gt;
&lt;br /&gt;
MySQL/MariaDB automatically creates an index for all primary keys.&lt;br /&gt;
&lt;br /&gt;
Example: To illustrate the creation of a table and the use of the various types and modifiers, we are going to create an example Employees table:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Employees (&lt;br /&gt;
 Id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 DNI             CHAR(9) NOT NULL UNIQUE,&lt;br /&gt;
 DepartmentId    INT UNSIGNED NOT NULL,&lt;br /&gt;
 FirstName       VARCHAR(50) NOT NULL,&lt;br /&gt;
 LastName        VARCHAR(100) NOT NULL DEFAULT &amp;#039;&amp;#039;,&lt;br /&gt;
 Active          TINYINT(1) NOT NULL DEFAULT TRUE,&lt;br /&gt;
 SupervisorId    INT UNSIGNED,&lt;br /&gt;
 BirthDate       DATE NOT NULL&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where:&lt;br /&gt;
* The first field is an identifier of the workers, of type unsigned INT. The identifier of each worker will be created automatically if we do not indicate otherwise, and constitutes the primary key of the table.&lt;br /&gt;
* The second field is the DNI, which always occupies 9 characters. NULL values are not accepted and there cannot be two repeated DNI in the table.&lt;br /&gt;
&lt;br /&gt;
Example: Similarly, we can create a Departments table, a Projects table and an Assignments table using the SQL command CREATE TABLE as shown in the following examples.&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Departments (&lt;br /&gt;
 Id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 DepartmentName VARCHAR(30) NOT NULL UNIQUE,&lt;br /&gt;
 OfficeNumber   CHAR(5) NOT NULL,&lt;br /&gt;
 Phone          CHAR(13) NOT NULL&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE Projects (&lt;br /&gt;
 Id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 ProjectName    VARCHAR(100) NOT NULL,&lt;br /&gt;
 DepartmentId   INT UNSIGNED NOT NULL,&lt;br /&gt;
 MaxHours       DECIMAL(8,2) NOT NULL DEFAULT 100,&lt;br /&gt;
 StartDate      DATETIME,&lt;br /&gt;
 EndDate        DATETIME&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE Assignments (&lt;br /&gt;
 Id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 EmployeeId     INT UNSIGNED NOT NULL,&lt;br /&gt;
 HoursWorked    DECIMAL(6,2) UNSIGNED&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Table constraints - CONSTRAINT PRIMARY KEY ==&lt;br /&gt;
&lt;br /&gt;
Table constraints are identified by the keyword CONSTRAINT and can be used to define various constraints, as described below.&lt;br /&gt;
&lt;br /&gt;
=== Primary key constraints ===&lt;br /&gt;
&lt;br /&gt;
The primary key (PRIMARY KEY) can also be specified as a table constraint. This is especially useful when the key is formed by more than one column.&lt;br /&gt;
&lt;br /&gt;
Example: For example, in a hotel chain, a room is identified by the hotel key and the room number:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Rooms (&lt;br /&gt;
 HotelNo INT UNSIGNED NOT NULL,&lt;br /&gt;
 RoomNo  INT UNSIGNED NOT NULL,&lt;br /&gt;
 Type CHAR(9),&lt;br /&gt;
 Price DECIMAL(6,2),&lt;br /&gt;
 CONSTRAINT PRIMARY KEY (HotelNo, RoomNo)&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Foreign key constraints - CONSTRAINT FOREIGN KEY ===&lt;br /&gt;
&lt;br /&gt;
The foreign key constraint (FOREIGN KEY) defines a column, or a combination of columns, whose value matches the primary key of another table.&lt;br /&gt;
&lt;br /&gt;
The use of foreign keys guarantees referential integrity between the two tables: any insertion or modification of the column or columns that are foreign keys must necessarily match valid primary key values in the main table, or the operation will fail.&lt;br /&gt;
&lt;br /&gt;
The behavior to be adopted in case the referenced elements in the main table are modified or deleted can be chosen at the moment of creating the foreign key constraint.&lt;br /&gt;
&lt;br /&gt;
The data type of the foreign key must match the data type of the primary key it refers to.&lt;br /&gt;
&lt;br /&gt;
Example: For example, we can create the tables from the previous section adding the appropriate foreign key constraints:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Projects (&lt;br /&gt;
 Id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 ProjectName    VARCHAR(100) NOT NULL,&lt;br /&gt;
 DepartmentId   INT UNSIGNED NOT NULL,&lt;br /&gt;
 MaxHours       DECIMAL(8,2) NOT NULL DEFAULT 100,&lt;br /&gt;
 StartDate      DATETIME,&lt;br /&gt;
 EndDate        DATETIME,&lt;br /&gt;
 CONSTRAINT FOREIGN KEY (DepartmentId) REFERENCES Departments(Id)&lt;br /&gt;
   ON DELETE NO ACTION ON UPDATE CASCADE&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the Projects table, we have considered that a department cannot be deleted while it has assigned projects.&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Assignments (&lt;br /&gt;
 Id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 EmployeeId     INT UNSIGNED NOT NULL,&lt;br /&gt;
 HoursWorked    DECIMAL(6,2) UNSIGNED,&lt;br /&gt;
 CONSTRAINT FOREIGN KEY (EmployeeId) REFERENCES Employees(Id)&lt;br /&gt;
   ON DELETE CASCADE ON UPDATE CASCADE&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In contrast, in the Assignments table, we have considered it appropriate to delete all tasks assigned to an employee in case the employee is deleted.&lt;br /&gt;
&lt;br /&gt;
Note: If the foreign key is formed by more than one field, the foreign key constraints must include all the columns of the primary key of the referenced table.&lt;br /&gt;
&lt;br /&gt;
Example: For example, a bookings table on the hotel rooms from the previous section could look like this:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Bookings (&lt;br /&gt;
  BookingNumber   INT UNSIGNED PRIMARY KEY,&lt;br /&gt;
  CheckIn         DATE NOT NULL,&lt;br /&gt;
  CheckOut        DATE NOT NULL,&lt;br /&gt;
  CustomerId      INT UNSIGNED NOT NULL,&lt;br /&gt;
  HotelNo         INT UNSIGNED NOT NULL,&lt;br /&gt;
  RoomNo          INT UNSIGNED NOT NULL,&lt;br /&gt;
  CONSTRAINT FOREIGN KEY (CustomerId) REFERENCES Customers(Id)&lt;br /&gt;
    ON DELETE CASCADE ON UPDATE CASCADE,&lt;br /&gt;
  CONSTRAINT FOREIGN KEY (HotelNo, RoomNo) REFERENCES Rooms(HotelNo, RoomNo)&lt;br /&gt;
    ON DELETE NO ACTION ON UPDATE CASCADE&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Check constraints - CHECK ===&lt;br /&gt;
&lt;br /&gt;
The CHECK constraint limits the values that can be stored in a column or set of columns.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Sales (&lt;br /&gt;
 SalesId INT PRIMARY KEY,&lt;br /&gt;
 SalesPersonId INT CHECK (SalesPersonId BETWEEN 100 AND 199),&lt;br /&gt;
 Name CHAR(15),&lt;br /&gt;
 Age INT UNSIGNED CHECK (Age &amp;gt;= 21),&lt;br /&gt;
 Quota DECIMAL(7,2) CHECK (Quota &amp;gt;= 0.0),&lt;br /&gt;
 HireDate DateTime,&lt;br /&gt;
 CHECK ((HireDate &amp;lt; &amp;#039;2016-01-01&amp;#039;) OR (Quota&amp;lt;=300000))&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UNIQUE constraints ===&lt;br /&gt;
&lt;br /&gt;
If the set formed by several columns cannot be repeated, we can define a UNIQUE constraint at the table level.&lt;br /&gt;
&lt;br /&gt;
Example: In this case, although we admit that several projects are named the same, we do not let the same department have two projects with the same name.&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE Projects (&lt;br /&gt;
 Id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;
 ProjectName    VARCHAR(100) NOT NULL,&lt;br /&gt;
 DepartmentId   INT UNSIGNED NOT NULL,&lt;br /&gt;
 MaxHours       DECIMAL(8,2) NOT NULL DEFAULT 100,&lt;br /&gt;
 StartDate      DATETIME,&lt;br /&gt;
 EndDate        DATETIME,&lt;br /&gt;
 CONSTRAINT FOREIGN KEY (DepartmentId) REFERENCES Departments(Id)&lt;br /&gt;
   ON DELETE NO ACTION ON UPDATE CASCADE,&lt;br /&gt;
 CONSTRAINT UNIQUE (ProjectName, DepartmentId)&lt;br /&gt;
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ENGINE, CHARSET and COLLATE ===&lt;br /&gt;
&lt;br /&gt;
It is important to understand the difference between ENGINE, CHARSET and COLLATE when defining a table in a MySQL or MariaDB database.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;ENGINE&amp;#039;&amp;#039;&amp;#039; (Storage Engine): ENGINE specifies the storage engine that the table will use. Each storage engine has its own characteristics and optimizations. For example, InnoDB is a transactional engine that supports foreign keys and transactions, while MyISAM is simpler and can be faster for some operations.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE exemple (&lt;br /&gt;
  id INT,&lt;br /&gt;
  nom VARCHAR(50)&lt;br /&gt;
) ENGINE=InnoDB;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;CHARSET&amp;#039;&amp;#039;&amp;#039; (Character set): CHARSET refers to the character set that the table will store. Each character has a specific numerical representation in a character set, and this will affect how textual data is saved and retrieved. For example, the utf8 character set allows storing Unicode characters, while latin1 is limited to a smaller set of characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE exemple (&lt;br /&gt;
  id INT,&lt;br /&gt;
  nom VARCHAR(50)&lt;br /&gt;
) ENGINE=InnoDB CHARSET=utf8;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;COLLATE&amp;#039;&amp;#039;&amp;#039; (Sort order): COLLATE determines the sort order and string comparison in the table. Different languages and regions can have different sorting and comparison rules. COLLATE can be used to ensure that string comparisons are done in a specific way.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE exemple (&lt;br /&gt;
  id INT,&lt;br /&gt;
  nom VARCHAR(50)&lt;br /&gt;
) ENGINE=InnoDB CHARSET=utf8 COLLATE=utf8_general_ci;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
* ENGINE: determines the storage engine that manages the table.&lt;br /&gt;
* CHARSET and COLLATE: influence how the data is stored and compared.&lt;br /&gt;
&lt;br /&gt;
== Table modification - ALTER TABLE ==&lt;br /&gt;
&lt;br /&gt;
Statements for table modification:&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE Hotels ADD Name VARCHAR(100);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; desc Hotels;&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| Field   | Type             | Null | Key | Default | Extra |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| HotelNo | int(10) unsigned | NO   | PRI | NULL    |       |&lt;br /&gt;
| Name    | varchar(100)     | YES  |     | NULL    |       |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
2 rows in set (0.00 sec)&lt;br /&gt;
&lt;br /&gt;
&amp;gt; ALTER TABLE Hotels ADD CONSTRAINT UNIQUE (Name);&lt;br /&gt;
Query OK, 0 rows affected (0.38 sec)&lt;br /&gt;
Records: 0  Duplicates: 0  Warnings: 0&lt;br /&gt;
&lt;br /&gt;
&amp;gt; desc Hotels;&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| Field   | Type             | Null | Key | Default | Extra |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| HotelNo | int(10) unsigned | NO   | PRI | NULL    |       |&lt;br /&gt;
| Name    | varchar(100)     | YES  | UNI | NULL    |       |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
2 rows in set (0.00 sec)&lt;br /&gt;
&lt;br /&gt;
&amp;gt; ALTER TABLE Hotels MODIFY Name VARCHAR(100) NOT NULL;&lt;br /&gt;
Query OK, 0 rows affected (1.33 sec)&lt;br /&gt;
Records: 0  Duplicates: 0  Warnings: 0&lt;br /&gt;
&lt;br /&gt;
&amp;gt; desc Hotels;&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| Field   | Type             | Null | Key | Default | Extra |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| HotelNo | int(10) unsigned | NO   | PRI | NULL    |       |&lt;br /&gt;
| Name    | varchar(100)     | NO   | UNI | NULL    |       |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
2 rows in set (0.01 sec)&lt;br /&gt;
&lt;br /&gt;
&amp;gt; ALTER TABLE Hotels DROP COLUMN Name;&lt;br /&gt;
Query OK, 0 rows affected (0.97 sec)&lt;br /&gt;
Records: 0  Duplicates: 0  Warnings: 0&lt;br /&gt;
&lt;br /&gt;
&amp;gt; desc Hotels;&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| Field   | Type             | Null | Key | Default | Extra |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
| HotelNo | int(10) unsigned | NO   | PRI | NULL    |       |&lt;br /&gt;
+---------+------------------+------+-----+---------+-------+&lt;br /&gt;
1 row in set (0.00 sec)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Table deletion - DROP TABLE ==&lt;br /&gt;
&lt;br /&gt;
The DROP TABLE statement allows deleting a table from the database.&lt;br /&gt;
&lt;br /&gt;
Syntax of the statement:&lt;br /&gt;
&lt;br /&gt;
Syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP TABLE [IF EXISTS] nomTaula;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example: This statement would delete the Hotels table from the database:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP TABLE Hotels;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: It is common to use DROP TABLE with CREATE TABLE including IF EXISTS to avoid errors.&lt;br /&gt;
&lt;br /&gt;
== Emptying table content - TRUNCATE ==&lt;br /&gt;
&lt;br /&gt;
In contrast, the TRUNCATE statement allows emptying the content of a table, without deleting the table itself:&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
TRUNCATE Hotels;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Indexes ==&lt;br /&gt;
&lt;br /&gt;
Creating indexes in MySQL or MariaDB database tables is an important practice for improving query performance. An index is like an ordered list that points to the locations of the data within a table, similar to how a word would be found in a book thanks to the alphabetical index.&lt;br /&gt;
&lt;br /&gt;
An index is similar to how a book index works; it allows finding information without needing to read the entire book.&lt;br /&gt;
&lt;br /&gt;
An index is a structure that provides fast access to the rows of a table. In SQL, it is a structure associated with a table that speeds up the retrieval of rows from the table.&lt;br /&gt;
&lt;br /&gt;
Indexes will be created for the columns that are used for queries (those used to search for data). Tables with indexes need more space; therefore, the insertion, update, or deletion of data will require more time.&lt;br /&gt;
&lt;br /&gt;
SQL automatically creates indexes for values with constraints of type PRIMARY KEY and UNIQUE.&lt;br /&gt;
&lt;br /&gt;
Why create indexes?&lt;br /&gt;
&lt;br /&gt;
* Improves performance: queries are executed more quickly as the index provides more efficient access.&lt;br /&gt;
&lt;br /&gt;
=== Creating an index ===&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE INDEX xdepartament ON materia(departament);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deleting an index ===&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
1. Delete the index you created in the previous section (xdepartament).&lt;br /&gt;
2. Attempt to delete the indexes that were created automatically by the program.&lt;br /&gt;
&lt;br /&gt;
The solution is:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP INDEX xdepartament ON materia;&lt;br /&gt;
&lt;br /&gt;
DROP INDEX materia ON materia;&lt;br /&gt;
&lt;br /&gt;
DROP INDEX primary ON materia;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: You cannot delete this index because it is a primary key.&lt;br /&gt;
&lt;br /&gt;
== Creating views - CREATE VIEW ==&lt;br /&gt;
&lt;br /&gt;
A view is similar to a table, but it does not store real data; instead, the data shown is the result of executing a SELECT query on other tables.&lt;br /&gt;
&lt;br /&gt;
On a view, we can execute any SELECT, but we cannot execute most data modification statements because it is complicated to know which real tables the modifications should be applied to.&lt;br /&gt;
&lt;br /&gt;
To create a view, the CREATE VIEW statement is used.&lt;br /&gt;
&lt;br /&gt;
A view does not store new data; instead, what is seen is the result of executing a SELECT query that has been saved as part of the view definition (we can see the SELECT in question by executing SHOW CREATE TABLE film_list).&lt;br /&gt;
&lt;br /&gt;
Example: For example, on the Chinook database, we could create a view that summarizes the information for each album:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE VIEW AlbumInfo AS&lt;br /&gt;
  SELECT al.Title AS Album, a.Name AS Artist, GROUP_CONCAT(t.Name) AS Tracks&lt;br /&gt;
  FROM Album al&lt;br /&gt;
  JOIN Artist a ON al.ArtistId=a.ArtistId&lt;br /&gt;
  JOIN Track t ON al.AlbumId=t.AlbumId&lt;br /&gt;
  GROUP BY al.AlbumId, al.Title, a.Name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where:&lt;br /&gt;
* This view contains the title of the album, the name of the artist, and a list with the name of the included songs.&lt;br /&gt;
* The GROUP_CONCAT() function joins all the values of a column that have been joined due to a GROUP BY into a single text string.&lt;br /&gt;
&lt;br /&gt;
We can perform queries on this view:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM AlbumInfo WHERE Artist LIKE &amp;#039;The Rolling Stones&amp;#039;;&lt;br /&gt;
&lt;br /&gt;
*************************** 1. row ***************************&lt;br /&gt;
 Album: Hot Rocks, 1964-1971 (Disc 1)&lt;br /&gt;
Artist: The Rolling Stones&lt;br /&gt;
Tracks: Under My Thumb,Play With Fire,Get Off Of My Cloud,Paint It Black, Let&amp;#039;s Spend The Night Together,Heart Of Stone,As Tears Go By, 19th Nervous Breakdown,Ruby Tuesday,Time Is On My Side,Satisfaction, Mother&amp;#039;s Little Helper&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This view will not allow modifications because the GROUP BY makes it impossible to know what the original rows to modify were.&lt;br /&gt;
&lt;br /&gt;
Example: In this example, we create a view that shows the information of the tracks and the name of their genre:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE VIEW TrackGenre AS&lt;br /&gt;
  SELECT TrackId, Track.Name AS TrackName, Genre.Name AS GenreName&lt;br /&gt;
  FROM Track&lt;br /&gt;
  JOIN Genre ON Track.GenreId=Genre.GenreId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, we can, for example, execute UPDATE because the DBMS can deduce which is the real data it must modify:&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM TrackGenre WHERE TrackId=10;&lt;br /&gt;
+---------+------------+-----------+&lt;br /&gt;
| TrackId | TrackName  | GenreName |&lt;br /&gt;
+---------+------------+-----------+&lt;br /&gt;
|      10 | Evil Walks | Rock      |&lt;br /&gt;
+---------+------------+-----------+&lt;br /&gt;
1 row in set (0.00 sec)&lt;br /&gt;
&lt;br /&gt;
UPDATE TrackGenre SET GenreName=&amp;#039;Blues&amp;#039; WHERE TrackId=10;&lt;br /&gt;
Query OK, 1 row affected (0.01 sec)&lt;br /&gt;
Rows matched: 1  Changed: 1  Warnings: 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ATTENTION: The change that has been made might not be what we expected: the name of the Rock genre has been modified and renamed to Blues; the GenreId of the track has not been modified.&lt;br /&gt;
&lt;br /&gt;
== Deleting views ==&lt;br /&gt;
&lt;br /&gt;
The syntax for deleting views is the following:&lt;br /&gt;
&lt;br /&gt;
Syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP VIEW [IF EXISTS]&lt;br /&gt;
    view_name [, view_name] ...&lt;br /&gt;
    [RESTRICT | CASCADE]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deleting more than one view at the same time is allowed.&lt;br /&gt;
&lt;br /&gt;
Example of use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DROP VIEW IF EXISTS AlbumInfo, TrackGenre;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:IT Databases]]&lt;/div&gt;</summary>
		<author><name>Root</name></author>
	</entry>
</feed>