Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda CREATE e Restrições | DDL e DML em SQL
SQL Intermediário
course content

Conteúdo do Curso

SQL Intermediário

SQL Intermediário

1. Agrupamento
2. Subconsultas Aninhadas
3. Juntando Tabelas
4. DDL e DML em SQL

book
CREATE e Restrições

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:

1234
CREATE TABLE example ( id INT PRIMARY KEY, some_info VARCHAR(50) );
copy

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

Ao executar esses exemplos, você não obterá nenhum resultado porque esses exemplos apenas criam uma nova tabela. Se executar o código novamente, você receberá um erro informando que a tabela já existe. Esses trechos de código são exemplos, e mais adiante na tarefa, dados serão inseridos nestas tabelas recém-criadas e exibidos na tela para que você possa verificar que tudo está funcionando.

1234567
CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), birthdate DATE, salary DECIMAL(10, 2), is_active BOOLEAN );
copy

With this query, we create an empty table that should contain information about users, including:

  1. An ID with an integer data type;
  2. Information about the name, with a VARCHAR(50) data type;
  3. Information about the birth date, with a DATE data type;
  4. Information about the salary, with a floating-point number data type;
  5. Whether the user is active, with a data type that only accepts true or false values.

Com esta consulta, criamos uma tabela vazia que deve conter informações sobre os usuários, incluindo:

  1. Um ID com um tipo de dados inteiro;
  2. Informações sobre o nome, com um tipo de dados VARCHAR(50);
  3. Informações sobre a data de nascimento, com um tipo de dados DATE.
  4. Informações sobre o salário, com um tipo de dados de número de ponto flutuante;
  5. Se o usuário está ativo, com um tipo de dados que só aceita valores true ou false.
1234567
CREATE TABLE users_2 ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, birthdate DATE, salary DECIMAL(10, 2) DEFAULT 50000, is_active BOOLEAN );
copy

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.

Tarefa

Swipe to start coding

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, and pages.
  • 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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1
toggle bottom row

book
CREATE e Restrições

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:

1234
CREATE TABLE example ( id INT PRIMARY KEY, some_info VARCHAR(50) );
copy

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

Ao executar esses exemplos, você não obterá nenhum resultado porque esses exemplos apenas criam uma nova tabela. Se executar o código novamente, você receberá um erro informando que a tabela já existe. Esses trechos de código são exemplos, e mais adiante na tarefa, dados serão inseridos nestas tabelas recém-criadas e exibidos na tela para que você possa verificar que tudo está funcionando.

1234567
CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), birthdate DATE, salary DECIMAL(10, 2), is_active BOOLEAN );
copy

With this query, we create an empty table that should contain information about users, including:

  1. An ID with an integer data type;
  2. Information about the name, with a VARCHAR(50) data type;
  3. Information about the birth date, with a DATE data type;
  4. Information about the salary, with a floating-point number data type;
  5. Whether the user is active, with a data type that only accepts true or false values.

Com esta consulta, criamos uma tabela vazia que deve conter informações sobre os usuários, incluindo:

  1. Um ID com um tipo de dados inteiro;
  2. Informações sobre o nome, com um tipo de dados VARCHAR(50);
  3. Informações sobre a data de nascimento, com um tipo de dados DATE.
  4. Informações sobre o salário, com um tipo de dados de número de ponto flutuante;
  5. Se o usuário está ativo, com um tipo de dados que só aceita valores true ou false.
1234567
CREATE TABLE users_2 ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, birthdate DATE, salary DECIMAL(10, 2) DEFAULT 50000, is_active BOOLEAN );
copy

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.

Tarefa

Swipe to start coding

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, and pages.
  • 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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt