Data Definition and Manipulation
Data Definition and Manipulation
IMPORTANT: for this lesson you need your database, where you have permissions to use the data definition commands.
By data definition, we mean the creation of the storage structures: creating databases, creating tables, indexes, procedures, etc. Also deleting and modifying them.
By data manipulation, we mean everything that has to do with the content of these structures: querying, inserting rows, deleting rows, modifying data...
create a table defining its columns and data types, and with the necessary restrictions (primary key, foreign, non-null, etc.).
add a row to the table with corresponding values in each row component
variant of the previous one that gets the values from a query to the same or another database
modify specific values in one or several rows, depending on the conditions we require.
Create table
The definition of tables is the first step in the building of a database. The set of table descriptions makes up the schema of the database and it is representing a specific
information system
Supposing we want to implement a relational database schema for teachers, subjects (just a PROFESORES ASIGNATURAS
listing of teachers and subjects, without relationship between them). Firstly, we must decide the DNI: varchar(10), codigo: char(5),
attributes of each table and the data type of these attributes: nombre: varchar(40), descripcion: varchar(35),
categoria: char(4), creditos: decimal(3,1),
ingreso: date creditosp: decimal(3,1)
Besides, in order to fulfill the restrictions of the relational model, we must choose the appropriate primary keys: DNI for profesores and código for asignaturas. Obviously,
the way we build these tables is our decision as database designers. Maybe, in other situation, the definition of the tables could be different.
The statement CREATE TABLE allows us to create each table we need for our database:
The list of columns, in its simplest form, is a set of expressions (as many as columns we want, and separated with commans) like:
Restrictions
Restrictions are rules, usually established in the moment of the creation of a table, to guarantee the integrity of data.
Basically, restrictions oblide to follow certain rules when a row is inserted, deleted or updated, and the operation will be performed only if the restrictions of the table are
fulfilled.
PRIMARY KEY: identifies each row uniquely using one or more columns, the columns that are part of a primary key can not have null value.
FOREIGN KEY: establishes a relation between one/more column(s) of a table and another column(s) of the referenced table, being this/these last column(s) the
PRIMARY KEY.
From these restrictions, only the one referring the primary key, containing as many columns as necessary, will be used:
The following sentences create PROFESORES and ASIGNATURAS create table profesores (
tables DNI varchar(10),
nombre varchar(40),
categoria char(4),
ingreso date,
primary key (DNI));
Engines of MariaDB/MySQL
What was shown in previous section is the standard of SQL and its syntax is admitted by most of the commercial DBMS
However, MySQL is a peculiar product that offers various storing and management options in order to offer alternatives to
improve performance and data integrity. In particular, we can choose between tables MyISAM and InnoDB.
If we want to keep referential integrity between our tables is necessary to specify the engine InnoDB. In a standard instalation
of MySQL, if we don´t say anything, the creation of the table is done using MyISAM and it has a very important effect: if we
define foreign keys, the system ignores them and does not review the referential integrity.
Eso nos obligaría a concretar el motor de almacenamiento tanto en las tablas con claves ajenas como en las referenciadas:
Al igual que se va a comentar a continuación, tenemos varias posibilidades de gestión del almacenamiento, algunas de ellas
directamente relacionadas con la integridad referencial.
No obstante, y adelantándonos, si no especificamos nada MariaDB utiliza el motor innodb, y no hace falta añadir la cláusula
engine=innodb a la creación de tablas, como sí exige MySQL. A partir de ahora, asumimos que trabajaremos con MariaBD y
no incluiremos esa especificación.
Drop table
If we want to delete a table, we must order it to the DBMS with the statement DROP TABLE: Delete the table asignaturas
Catalog information
Remember that executing DESC nombreTabla or DESCRIBE nombreTabla, all the information related to the columns of the table must be shown, the internal order of them
in the table, and their data types.
In the moment of creating a restriction, apart from specifying the rules that must be fulfilled, we can give it a name. To establish the names of the restrictions, the following
agreement is used: being descriptive names, starting with, for example,
We are not going to study in depth the name of the restrictions nor the syntax to create them, but we need to know that if we don't assign them a name, the DBMS will do.
Insert
In order to insert new data in a database we will use the statement INSERT of SQL. With the syntax shown next we will be able to add new data in each table of a database.
At first, we will see the minumum statement syntax, formed by INTO and VALUES clauses.
dni 55555555
categoría TU
dni 66
categoría ASO
The result of the statement INSERT, will always be the number of added rows, in case of a properly execution. For the cases in the statement breaking any restriction of the
database, and therefore, the execution is not correct, the result will indicate which restriction has been broken. The DBMS, each time we add a new data in a table, is in
charge of checking the active restrictions, in our case, the primary keys, that as we already know, do not admit nulls or duplicates.
dni 66
categoría XXX
Sometimes we do not want or we can't have values for all the columns, or we want Add a new teacher with DNI 88888888 and nombre ARMANDO SUÁREZ
to use a different order that the one defined in the table. Specifying a list of insert into profesores (dni, nombre)
columns before the VALUES allows to indicate the system which columns will have values ('88888888', 'ARMANDO SUAREZ');
a value and which is this value.
The system will try to assign to the non indicate columns a default value, or in case it was defined, a null value.
It is advisable to indicate always the columns with values, being all of them or not part of the table. The reason are:
There is no need to take into account if we are giving values to all of the columns or only some of them in the INSERT statement.
If the structure of the table is modified by any reason, for example new columns are added, and we usually do not indicate the name of the columns, the statement
won't work anymore
There exists the possibility of using NULL value, when the column admits it. insert into profesores (dni,nombre,categoría,ingreso)
Although it is usually simplified as "absence of value", remember that NULL means values ('99999999','MARIA MIRO', NULL, NULL);
unkown data (we don`t know if it has a value and in case it has some we don't
know it). In any case, if the column admits them it can be specified in the INSERT
sentence.
Equally, a change in the order of the columns must correspond with the exact
insert into profesores (categoria, dni, nombre)
position of the value to be assigned:
values (NULL, '77','ANA FERNANDEZ');
Suppose that in the BD Ejemplo there exists a table called OPTATIVAS with the code and credits of the optative subjects.
We will create this table, choosing the code of the subject as primary key and adding the create table optativas (
restriction that all the rows must have a value in the credits column.
asignatura char (5),
creditos decimal(3,1) not null,
primary key (asignatura));
There exists the possibility to insert the result of a column instead of indicating a list of values to be inserted. This allows us to insert more than one row in a table in a
unique operation, specifically, as many rows as tuples are returned in the SELECT statement.
The other option is inserting the result of the SELECT in a unique insert into optativas (asignatura, creditos)
operation in the table OPTATIVAS. select codigo, creditos
from asignaturas
where creditos < 9;
The result fo the SELECT statement must be coherent with the number of columns and the data types. The following statement will provoke compiling errors:
insert into optativas (asignatura) select codigo, creditos from asignaturas where creditos < 9;
insert into optativas (asignatura,creditos) select codigo from asignaturas where creditos < 9;
This case is different, because we need to assure that the rest of the columns allows this
insert into optativas (asignatura)
insertion. With this type of command, the database engine will assign null values to the remaining
select codigo from asignaturas
columns. A typical situation is that one of the omitted columns does not allow nulls and makes where creditos < 9
insertion impossible.
Check the definition of the OPTATIVAS table, and you will find the NOT NULL constraint on the
credits column. The result will be an error message, the constraint prevents the insertion of rows
to ensure the integrity of the data and, precisely, preventing null values to be put in that column.
Delete
Before starting, we need to clean the drop table if exists optativas;
database: drop table if exists imparte;
drop table if exists profesores;
drop table if exists asignaturas;
insert into asignaturas (codigo, descripcion, creditos, creditosp) values ('HI', 'HISTORIA DE LA
INFORMATICA', 4.5, NULL), ('FBD', 'FUNDAMENTOS DE LAS BASES DE DATOS', 6.0, 1.5), ('DGBD', 'DISEÑO Y
GESTION DE BASES DE DATOS', 6.0, 3.0), ('PC', 'PROGRAMACION CONCURRENTE', 6.0, 1.5), ('FP', 'FUNDAMENTOS
DE LA PROGRAMACION', 9.0, 4.5);
insert into profesores (dni, nombre, categoria, ingreso) values ('21111222','EVA GOMEZ','TEU','1993-10-
01'), ('21222333','MANUEL PALOMAR','TEU','1989-06-16'), ('21333444','RAFAEL ROMERO','ASO6','1992-06-16');
In order to delete rows of various tables we need to execute as many DELETE statements as rows we want to delete.
(Note: in MySQL is possible to delete over various tables but it can be assure that other DBMS allows that also)
Update
insert into asignaturas (codigo, descripcion, creditos, creditosp) values ('HI', 'HISTORIA DE LA
INFORMATICA', 4.5, NULL), ('FBD', 'FUNDAMENTOS DE LAS BASES DE DATOS', 6.0, 1.5), ('DGBD', 'DISEÑO Y
GESTION DE BASES DE DATOS', 6.0, 3.0), ('PC', 'PROGRAMACION CONCURRENTE', 6.0, 1.5), ('FP', 'FUNDAMENTOS
DE LA PROGRAMACION', 9.0, 4.5);
insert into profesores (dni, nombre, categoria, ingreso) values ('21111222','EVA GOMEZ','TEU','1993-10-
01'), ('21222333','MANUEL PALOMAR','TEU','1989-06-16'), ('21333444','RAFAEL ROMERO','ASO6','1992-06-16');
The statement UPDATE will allow us to modify the information stored in a table.
When we want to modify more than one column, the list of columns with the new value will be
indicated separated with commas:
In case a condition is indicated, only the rows that fulfill the condition/s will be Modify the date of "ingreso" to 1 de enero de 2003 only for those teachers with TEU
updated: category.
update profesores
set ingreso='01/01/2003'
where categoria = 'TEU';
There exists the possibility of modifying the information stored in a table assigning as new value/s the result of a query.
The result of the query can be assign to one single column or a list of columns. In the first case, the SELECT statement will only return one value that must match with the
data type of the column being assigned.
It is important to assure that the subselect returns only one value and this value
update imparte
matches with the expected data type.
set asignatura='BDA',
dni = (select dni from profesores)
where asignatura like '%BD%';
ERROR:
the subquery returns more than one row
On the other hand, the following statement will work in others DBMS but not in MySQL, which is not allowing
updating a table from a subquery of the same table, we need to use a temporal table to perform this type of
operation. If we access to the database using a programming language, PHP for example, it would be more
common to store the value in a variable and modifying the table with this.
Modify the date of enrollment of the teachers with TEU category in order to have the same as the teacher with For this statement we must obtain:
DNI 21333444
However, MariaDB/MySQL returns an error with the message, more or less, "can't update the same table being queried". This also happens with delete. The solution is to
"trick" MariaDB/MySQL with a temporary query:
update profesores
set ingreso = (select ingreso
from (select ingreso
from profesores
where dni='21333444') ttemp
)
where categoria = 'TEU';
Actually, it is not that we are "cheating" MariaDB/MySQL; as you can see in the "Temporary tables" topic, what we are doing is generating a temporary table (ttemp) where
the TEACHERS data we are interested in is copied.
MariaDB, from version 10.3.2 onwards, does not have this limitation.