SQL: char data type

This entry is part 13 of 20 in the series SQL Tutorial

char(3) is a fixed-length alphanumeric string of 3 characters.

If we create a table as below:

create table artiste
	(
 id int auto_increment primary key, 
 artiste varchar(30),
 category char(3)
 );

insert into artiste
(artiste, category)
values
('Rod Stewart', 'Rock'),
('New Order', 'Pop');

We could see that Rock has 4 characters which is 1 character bigger than char(3) that is defined for column category.

An error is generated as shown in the screenshot below.

char_3

A correct insert statement would be:

create table artiste
 (
 id int auto_increment primary key, 
 artiste varchar(30),
 category char(3)
 );
 
insert into artiste
(artiste, category)
values
('Rod Stewart', 'Roc'),
('New Order', 'Pop');

Series Navigation<< SQL: Data Types
SQL: Update >>