The data type alone is not enough. The Keeper of the Catalog imposes rules on shelves - so that the data stays consistent and trustworthy. We call these rules constraints, and we write them after the type of a column.
It marks the column that uniquely identifies each row. The value must be unique and cannot be empty:
1id INT PRIMARY KEYIt is like a scroll's catalog number - two rows never share the same one.
Added to the primary key, it makes MySQL assign by itself the next increasing number to every new row (1, 2, 3...):
1id INT AUTO_INCREMENT PRIMARY KEYYou do not have to remember the last number - the Library counts for you. This is the most common way to create an
id column.It forces the column to never be empty:
1nazwa VARCHAR(100) NOT NULLA category without a name makes no sense - so we require it. A column without NOT NULL may stay empty (NULL).
In a column definition we always write: the name, the type, and then the constraints - one after another, separated by a space:
1id INT AUTO_INCREMENT PRIMARY KEY,
2nazwa VARCHAR(100) NOT NULLThis is a pattern you will repeat in almost every table.