Database Design: Selecting and Limiting Favorites

Currently, the database has a Members table and a Products table with a combined Favorites table , which consists of primary foreign keys from the Participants and Products table . I have a requirement to set a limit on the number of products that a participant can place in his favorites by 5.

Where does this restriction come from? Is this something done in the database (MySQL) and therefore will be part of my existing schema? Or is it a programming function that can be done using PHP?

+3
source share
2 answers

The question has been answered, however, as you seek understanding ...

The idea with databases is that all such restrictions and restrictions on data are placed in the database itself (as an autonomous unit). COnstraints data should be in the database not only in the application. ISO / IEC / ANSI SQL provides several types of constraints for different purposes:

  • FOREIGN KEY constraints for referential integrity (as well as performance, Open Architecture compliance, etc.)

  • CHECK Constraints to check data values ​​of other columns and prohibit violations

  • RULE Restrictions to prohibit data that is out of range or specify exact data formats

Your classic simple RULE or CHECK. And the correct answer for Database and Database Design is a RULE or CHECK, not code.

, . . , , . Db , , , . , , , .

-SQL- Standard-SQL. . " " : , , ..

MyNonSQL/PHP - . , .

+3

PHP.

SELECT COUNT(*) FROM members_products WHERE member_id = 3 .

+1

Source: https://habr.com/ru/post/1785095/


All Articles