Contenido del Curso
SQL Intermedio
SQL Intermedio
CREATE y Restricciones
Previously, we worked for different companies and executed SELECT
queries for their needs. However, we need to learn how to create and modify tables.
Tables are created using the CREATE
statement, which has a similar structure to the SELECT
statement, except instead of selecting data, it creates data:
CREATE TABLE example ( id INT PRIMARY KEY, some_info VARCHAR(50) );
Note
When you run these examples, you won't get any output because these examples only create a new table. If you run the code again, you will get an error saying that the table already exists. These code snippets are examples, and later in the task, data will be inserted into these newly created tables and displayed on the screen so you can see that everything is working.
Nota
Cuando ejecutes estos ejemplos, no obtendrás ninguna salida porque estos ejemplos solo crean una nueva tabla. Si ejecutas el código nuevamente, recibirás un error indicando que la tabla ya existe. Estos fragmentos de código son ejemplos, y más adelante en la tarea, se insertarán datos en estas tablas recién creadas y se mostrarán en pantalla para que puedas ver que todo está funcionando.
CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), birthdate DATE, salary DECIMAL(10, 2), is_active BOOLEAN );
With this query, we create an empty table that should contain information about users, including:
- An
ID
with an integer data type; - Information about the
name
, with aVARCHAR(50)
data type; - Information about the birth date, with a
DATE
data type; - Information about the salary, with a floating-point number data type;
- Whether the user is active, with a data type that only accepts
true
orfalse
values.
Con esta consulta, creamos una tabla vacía que debe contener información sobre los usuarios, incluyendo:
- Un
ID
con un tipo de dato entero; - Información sobre el
nombre
, con un tipo de datoVARCHAR(50)
; - Información sobre la fecha de nacimiento, con un tipo de dato
DATE
. - Información sobre el salario, con un tipo de dato de número de punto flotante;
- Si el usuario está activo, con un tipo de dato que solo acepta valores
true
ofalse
.
CREATE TABLE users_2 ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, birthdate DATE, salary DECIMAL(10, 2) DEFAULT 50000, is_active BOOLEAN );
Now, the name
column must always have a value, as it cannot be empty or null. Also, if no salary is specified, it will default to 50000
.
Using constraints like these helps ensure that the data in your table is accurate and follows the rules you set.
Swipe to show code editor
Your task is to create a table named library
.
This table should have 4 columns:
id
- integer primary key;title
- varchar, not null;author
- varchar;pages
- int.
At the end of the query, be sure to put a semicolon (;
).
Please use these column names exactly as specified.
Note
On the right, you will see a large amount of code; do not modify it. It is written to ensure that your solution is correctly checked. We will learn everything written there later in this section.
Brief Instructions
- Use a CREATE query to create a new table named
library
. - The table should have four columns:
id
,title
,author
, andpages
. - For the first column, specify
INT PRIMARY KEY
. - For the second column, specify
VARCHAR(50) NOT NULL
. - For the third column, specify
VARCHAR(50)
. - For the fourth column, specify
INT
.
Solución
¡Gracias por tus comentarios!
CREATE y Restricciones
Previously, we worked for different companies and executed SELECT
queries for their needs. However, we need to learn how to create and modify tables.
Tables are created using the CREATE
statement, which has a similar structure to the SELECT
statement, except instead of selecting data, it creates data:
CREATE TABLE example ( id INT PRIMARY KEY, some_info VARCHAR(50) );
Note
When you run these examples, you won't get any output because these examples only create a new table. If you run the code again, you will get an error saying that the table already exists. These code snippets are examples, and later in the task, data will be inserted into these newly created tables and displayed on the screen so you can see that everything is working.
Nota
Cuando ejecutes estos ejemplos, no obtendrás ninguna salida porque estos ejemplos solo crean una nueva tabla. Si ejecutas el código nuevamente, recibirás un error indicando que la tabla ya existe. Estos fragmentos de código son ejemplos, y más adelante en la tarea, se insertarán datos en estas tablas recién creadas y se mostrarán en pantalla para que puedas ver que todo está funcionando.
CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), birthdate DATE, salary DECIMAL(10, 2), is_active BOOLEAN );
With this query, we create an empty table that should contain information about users, including:
- An
ID
with an integer data type; - Information about the
name
, with aVARCHAR(50)
data type; - Information about the birth date, with a
DATE
data type; - Information about the salary, with a floating-point number data type;
- Whether the user is active, with a data type that only accepts
true
orfalse
values.
Con esta consulta, creamos una tabla vacía que debe contener información sobre los usuarios, incluyendo:
- Un
ID
con un tipo de dato entero; - Información sobre el
nombre
, con un tipo de datoVARCHAR(50)
; - Información sobre la fecha de nacimiento, con un tipo de dato
DATE
. - Información sobre el salario, con un tipo de dato de número de punto flotante;
- Si el usuario está activo, con un tipo de dato que solo acepta valores
true
ofalse
.
CREATE TABLE users_2 ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, birthdate DATE, salary DECIMAL(10, 2) DEFAULT 50000, is_active BOOLEAN );
Now, the name
column must always have a value, as it cannot be empty or null. Also, if no salary is specified, it will default to 50000
.
Using constraints like these helps ensure that the data in your table is accurate and follows the rules you set.
Swipe to show code editor
Your task is to create a table named library
.
This table should have 4 columns:
id
- integer primary key;title
- varchar, not null;author
- varchar;pages
- int.
At the end of the query, be sure to put a semicolon (;
).
Please use these column names exactly as specified.
Note
On the right, you will see a large amount of code; do not modify it. It is written to ensure that your solution is correctly checked. We will learn everything written there later in this section.
Brief Instructions
- Use a CREATE query to create a new table named
library
. - The table should have four columns:
id
,title
,author
, andpages
. - For the first column, specify
INT PRIMARY KEY
. - For the second column, specify
VARCHAR(50) NOT NULL
. - For the third column, specify
VARCHAR(50)
. - For the fourth column, specify
INT
.
Solución
¡Gracias por tus comentarios!