We use cookies to enhance your experience on the site
CodeWorlds

The architect's workshop - CREATE TABLE and INSERT

A master does not only read scrolls - they build new shelves. Let us return to the art of creating tables and filling them with data.

CREATE TABLE - a new shelf

We create a table with the

CREATE TABLE
statement. Each column has a name and a data type:

1CREATE TABLE wydawnictwa (
2  id INT PRIMARY KEY AUTO_INCREMENT,
3  nazwa VARCHAR(100),
4  miasto VARCHAR(100)
5);

The most common data types:

  • INT
    - a whole number (e.g. a year, a count).
  • VARCHAR(n)
    - short variable-length text up to
    n
    characters (e.g. a title).
  • TEXT
    - long text (e.g. a review).
  • DECIMAL(8,2)
    - a number with decimal places (e.g. a price).
  • DATE
    - a date.

PRIMARY KEY and AUTO_INCREMENT

PRIMARY KEY
is the unique signature of every row.
AUTO_INCREMENT
makes the database assign the next number to each new entry by itself - we do not have to remember it.

INSERT - filling the shelf

We add data with the

INSERT
statement. We can add one row or many at once:

1INSERT INTO czytelnicy (imie, nazwisko, miasto)
2VALUES ('Anna', 'Nowak', 'Kraków'),
3       ('Piotr', 'Lis', 'Gdańsk');

Separate consecutive rows with a comma. This is how the Library grows - shelf by shelf, scroll by scroll!

Go to CodeWorlds