Hello. Faced MySQL. The course is introductory, but still I would like to know at least the basics. Found one example:
CREATE TABLE usr ( usr_id INT AUTO_INCREMENT NOT NULL, FIRST VARCHAR(25) NOT NULL, surname VARCHAR(50) NOT NULL, PRIMARY KEY(usr_id) ) ENGINE=InnoDB CHARACTER SET=UTF8; CREATE TABLE product ( prod_id INT AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, descr VARCHAR(255) NOT NULL, PRIMARY KEY(prod_id) ) ENGINE=InnoDB CHARACTER SET=UTF8; CREATE TABLE invoice ( inv_id INT AUTO_INCREMENT NOT NULL, usr_id INT NOT NULL, prod_id INT NOT NULL, quantity INT NOT NULL, PRIMARY KEY(inv_id), FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE RESTRICT, FOREIGN KEY (prod_id) REFERENCES product(prod_id) ON UPDATE CASCADE ON DELETE RESTRICT ) ENGINE=InnoDB CHARACTER SET=UTF8; The essence is as follows: a table of goods, customers and a table of orders. Questions:
- The primary key and the external one, as I understand it, are the same for different tables: for example, prod_id for product tables is PK, and for orders, external?
- The line
usr_id INT AUTO_INCREMENT NOT NULL,means the creation of the primary key table buyers, ID increases automatically and can not be zero? - What makes
ENGINE=InnoDB CHARACTER SET=UTF8;(something with encoding) and is it necessary? - In this case, the parents will be buyers and products, the order table is a subsidiary?
- The
FOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE RESTRICT,entryFOREIGN KEY (usr_id) REFERENCES usr(usr_id) ON UPDATE CASCADE ON DELETE RESTRICT,shows that the change in record is the product displayed in orders, and the change in the order is not displayed on the product (it was written on the website)? Did I understand correctly, if you remove the product, then the order associated with it will be removed, and if, for example, the buyer canceled the order, then the product remains in the table of goods? And these features (changes) are indicated always and only after the foreign keys of the parent tables for this child when it is created? - The last question is in the connections. I read that there are different connections in the database, for example, one to many, many to many, and one to one. As in the case of the examples mentioned above, connections are created? After all, the buyer can order not one product, but two or more? This is not specifically indicated or something I do not see? From the very beginning I thought that in essence one record corresponds to another. Or, it is precisely thanks to these notorious keys that a connection is created: one buyer can have several goods - one to many. Right lmi, I understand if I express myself correctly, if so.
PS: perhaps many questions will seem profane, but for this, after all, a forum has been created. Thanks in advance to those who have enough patience to answer me.