We use cookies to enhance your experience on the site
CodeWorlds

Data types - choosing the right drawer

Every field of a shelf must know what kind of data it accepts. It is like drawers for scrolls: one for numbers, another for dates, another for long stories. In MySQL we call these drawers data types.

Numbers

  • INT - whole numbers, e.g. a year of publication, an identifier. Ideal for
    rok_wydania
    or
    id
    .
  • DECIMAL(p,s) - decimal numbers with exact precision.
    p
    is the total number of digits,
    s
    is the digits after the decimal point.
    DECIMAL(8,2)
    will store amounts like
    29.99
    - perfect for
    cena
    .

Text

  • VARCHAR(n) - text of variable length, up to
    n
    characters. For
    tytul
    we will use e.g.
    VARCHAR(255)
    , for
    imie
    -
    VARCHAR(50)
    .
  • TEXT - long texts without a strict limit, e.g. a book
    opis
    (description) or the content of a review.

Dates

  • DATE - a date alone, e.g.
    2024-03-15
    . Fits
    data_wypozyczenia
    ,
    data_zwrotu
    .
  • DATETIME - a date together with a time, e.g.
    2024-03-15 14:30:00
    .

The golden rule

Match the type to the data! Numbers to INT, amounts to DECIMAL, short strings to VARCHAR, long descriptions to TEXT, dates to DATE. The wrong drawer means chaos in the Library - we do not keep numbers in a drawer meant for text, nor do we cram text into a drawer meant for numbers.

Go to CodeWorlds