Need help in developing DB logic

Don’t use “smart keys”.

This is a bad idea called “smart keys” or “encoding information in keys”.

See this answer which contains this excerpt:

Despite it now being easy to implement a Smart Key, it is hard to recommend that you create one of your own that isn’t a natural key, because they tend to eventually run into trouble, whatever their advantages, because it makes the databases harder to refactor, imposes an order which is difficult to change and may not be optimal for your queries, requires a string comparison if the Smart Key includes non-numeric characters, and is less effective than a composite key in helping range-based aggregations. It also violates the basic relational guideline that every column should store atomic values

Smart Keys also tend to outgrow their original coding constraints

(Notice that seat locations are typically identified by smart keys in that they are a row number and a count across a row. But they are also typically visibly physically permanently bolted into that formation. And imagine if they were labelled and rearranged.)

Educate yourself about database design.

Just describe your business in the most straightforward terms. That is how relational model databases & DBMSs work.

Find enough fill-in-the-[named-]blanks sentence templates to describe your business situations:

"customer [cid] has name [firstname] [lastname]
    AND customer [cid] has a phone number [phonenumber] of type [type] ..."
"customer [cid] can use credit card #[card_no]"
"seat [seatid] is at row [row] and column [column]"
"seat [seatid] is booked"
"seat [seatid] is temporarily committed to an unfinished booking"
...

For each such parameterized sentence template (aka predicate) have a base table where the names of the blanks/parameters are column names. Each row in a table states the statement (proposition) got from filling in the blanks per its column values; each row not in a table states NOT the statement from filling in the blanks per its column values.

Then for each table find every functional dependency (FD) that holds. (When a predicate can be expressed in the form “… AND column = F(column1,…)” then we say that column set {column1,…} functionally determines column column and that FD set → column holds.) Then identify every candidate key (CK). (A superkey is a column set that functionally determines every column. Ie that is unique, ie where each subrow of values for those columns appears only in one row of a table. A CK is a superkey that doesn’t contain a smaller superkey.) Then find every join dependency (JD). (Some predicates say “… AND …” for some number of ANDs & “…”s. There is a JD when the table for each predicate “…” would look like what you get from taking only its columns from the original table.) Note that every FD comes with an associated (binary) JD.

Then normalize your tables to fifth normal form (5NF). This means decomposing (ie replacing a table in which JD “… AND …” holds by tables whose predicates are the “…”s) until each JD that holds is implied by the CKs (ie must hold when the JDs from the FDs from the CKs hold.) (For performance reasons one can also then denormalize by combining to base tables that aren’t in 5NF.)

See this answer and this one.

Then we query by describing the rows we want. We do this by connecting base table predicates with logical operators (ie AND, OR, NOT, FOR SOME, FOR ALL etc) and function calls to give the predicates for the tables we want and/or by connecting base table names by relation operators (ie JOIN, UNION, MINUS/EXCEPT, PROJECT/SELECT, RENAME/AS) to give the values of the tables we want and/or both (eg RESTRICT/WHERE).

The JOIN of two tables holds the rows that make a true statement from, ie has as predicate, the AND of their predicates; and the UNION the OR, the MINUS/EXCEPT the AND NOT; and that PROJECT/SELECT columns of a table puts FOR SOME all-other-columns before its predicate; and RESTRICT/WHERE puts AND condition after its predicate; and the RENAME/AS of column renames that parameter in its predicate. So a table expression corresponds to a predicate: A table (base table or query result) value contains the rows that make a true statement from its (base table’s or query expression’s) predicate.

See this answer.

The same goes for constraints, which are true statements that collectively describe the application situations and database states than can arise given the situations that can arise and the base table predicates.

See this answer.

Leave a Comment