Revision
2. Text data types - revision

Instruction

Okay, let's start with text data types. So far, we've learnt 3 text types:

  1. VARCHAR, which allows us to enter a value of up to the given amount of characters,
  2. CHAR which is used for data of constant length and
  3. TEXT or CLOB data type for longer passages.

The choice of the proper type is not always obvious. For instance, if we want to store user passwords and they are supposed to be up to 20 characters long, you may think that VARCHAR(20) will be the right choice. However, it is good practice to store passwords in your database encrypted.

For instance, we can use a special hash function like SHA-512. The good thing is, it is not easy to decrypt such passwords, so if the data from our database falls into the wrong hands, it won't be easy to get to know the stolen passwords.

The password hash generated with the SHA-512 function is 512 bytes long, which is more or less 88 characters, depending on the encoding.

Exercise

Let's create a simple table for an internet forum.

Let's name the table forum_user. We want the following columns: first_name, nickname, password, description.

The first two columns are up to 25 characters long. The third column is up to 88 characters long. The last column description is a longer text.

Stuck? Here's a hint!

Type

CREATE TABLE forum_user (
   first_name varchar(25),
   nickname varchar(25),
   password varchar(88),
   description text
);

Console

Code editor

Result

TableConsole