How do you create a table with a column of type CHAR?
Posted by EveClark
Last Updated: July 04, 2024
To create a table with a column of type CHAR, you would use the SQL CREATE TABLE statement. The CHAR data type is used to store fixed-length strings. When you define a CHAR(n) column, it will always occupy n bytes, padding with spaces if the string is shorter than n. Here's a basic example of how to create a table with a CHAR column:
CREATE TABLE sample_table (
    id INT PRIMARY KEY,
    name CHAR(10)
);
In this example: - sample_table is the name of the table. - id is an integer column that serves as the primary key. - name is a column of type CHAR with a fixed length of 10 characters. When inserting data into the name column, if a value is shorter than 10 characters, it will be padded with spaces. For example, inserting John would store John (with 6 trailing spaces).