The most important command of the scriptorium is CREATE TABLE. It creates a new, empty shelf ready to receive scrolls.
1CREATE TABLE table_name (
2 column1 TYPE,
3 column2 TYPE,
4 column3 TYPE
5);We read it like this: "Build a shelf named table_name, and in it the fields: column1, column2, column3". Each field has its name and its type.
(.) and end the whole thing with a semicolon ";".In a column definition we first write the name, and right after it the data type:
1tytul VARCHAR(255)This means: "the tytul field accepts text up to 255 characters". The order is always this: NAME, then TYPE.
1CREATE TABLE kategorie (
2 id INT,
3 nazwa VARCHAR(100)
4);A shelf
kategorie was created with two fields: id for numbers and nazwa for short text. It is still empty - but the plan is ready!Very much! A table name is the address of the shelf in the Library. You will later use it to retrieve data (
SELECT * FROM kategorie), add columns or drop the whole shelf. Good names are short, plural (ksiazki, autorzy, czytelnicy) and describe the kind of scrolls stored. We avoid spaces and special characters - instead we use underscores, e.g. rok_wydania.( ) enclose the list of columns - without them MySQL reports an error.When these three elements are in place, the shelf is built in the blink of an eye!