<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>RDBMS | Portfolio</title><link>https://ansangah.github.io/en/tags/rdbms/</link><atom:link href="https://ansangah.github.io/en/tags/rdbms/index.xml" rel="self" type="application/rss+xml"/><description>RDBMS</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en</language><lastBuildDate>Wed, 30 Apr 2025 00:00:00 +0000</lastBuildDate><image><url>https://ansangah.github.io/media/sharing.png</url><title>RDBMS</title><link>https://ansangah.github.io/en/tags/rdbms/</link></image><item><title>Databases</title><link>https://ansangah.github.io/en/blog/subjects/db/</link><pubDate>Wed, 30 Apr 2025 00:00:00 +0000</pubDate><guid>https://ansangah.github.io/en/blog/subjects/db/</guid><description>&lt;h2 id="1-database-normalization"&gt;1. Database Normalization&lt;/h2&gt;
&lt;h3 id="11-definition"&gt;1.1 Definition&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Normalization&lt;/strong&gt; decomposes relations so that only related attributes remain together, eliminating anomalies and redundancy.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="12-anomalies"&gt;1.2 Anomalies&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Insertion anomaly:&lt;/strong&gt; need to insert unrelated data to add a new fact.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Update anomaly:&lt;/strong&gt; modifying one duplicate tuple but not the others causes inconsistencies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deletion anomaly:&lt;/strong&gt; removing a tuple unintentionally deletes valuable information.&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote class="border-l-4 border-neutral-300 dark:border-neutral-600 pl-4 italic text-neutral-600 dark:text-neutral-400 my-6"&gt;
&lt;p&gt;Normalization uses &lt;strong&gt;functional dependencies&lt;/strong&gt; to prevent these anomalies.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="2-functional-dependencies"&gt;2. Functional Dependencies&lt;/h2&gt;
&lt;h3 id="21-definition"&gt;2.1 Definition&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In relation R, if attribute set $X$ determines $Y$, we denote $X \rightarrow Y$—tuples with the same $X$ must share the same $Y$.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="22-guidelines"&gt;2.2 Guidelines&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Derive dependencies from attribute semantics and business rules, not from sample data alone.&lt;/li&gt;
&lt;li&gt;Primary and candidate keys typically determine all other attributes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="23-full-functional-dependency"&gt;2.3 Full Functional Dependency&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;$X \rightarrow Y$ but no proper subset of $X$ determines $Y$.&lt;/li&gt;
&lt;li&gt;Indicates $Y$ depends on the entire composite key.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="24-partial-functional-dependency"&gt;2.4 Partial Functional Dependency&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;When $Y$ depends on only part of a composite key.&lt;/li&gt;
&lt;li&gt;Break such relations into 2NF to remove partial dependencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="3-normal-forms"&gt;3. Normal Forms&lt;/h2&gt;
&lt;h3 id="31-first-normal-form-1nf"&gt;3.1 First Normal Form (1NF)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;All attribute values are atomic (no repeating groups or arrays).&lt;/li&gt;
&lt;li&gt;Remove multi-valued attributes. If non-key attributes do not fully depend on the key, move to 2NF.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="32-second-normal-form-2nf"&gt;3.2 Second Normal Form (2NF)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In 1NF and every non-key attribute is &lt;strong&gt;fully functionally dependent&lt;/strong&gt; on the primary key.&lt;/li&gt;
&lt;li&gt;Eliminates partial dependencies; composite-key relations may require decomposition.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="33-third-normal-form-3nf"&gt;3.3 Third Normal Form (3NF)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In 2NF and no non-key attribute is &lt;strong&gt;transitively dependent&lt;/strong&gt; on the primary key (no $A \rightarrow B$, $B \rightarrow C$ implying $A \rightarrow C$ among non-keys).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="34-boycecodd-normal-form-bcnf"&gt;3.4 Boyce–Codd Normal Form (BCNF)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Stronger than 3NF: every determinant in a functional dependency is a candidate key.&lt;/li&gt;
&lt;li&gt;Needed when multiple candidate keys exist and 3NF still allows anomalies.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote class="border-l-4 border-neutral-300 dark:border-neutral-600 pl-4 italic text-neutral-600 dark:text-neutral-400 my-6"&gt;
&lt;p&gt;Higher normal forms reduce redundancy but may increase join cost. Aim for a balance; 5NF is rarely necessary in practice.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id="35-lossless-decomposition--dependency-preservation"&gt;3.5 Lossless Decomposition &amp;amp; Dependency Preservation&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Decompose relations without losing information and retain essential dependencies.&lt;/li&gt;
&lt;li&gt;Joining the decomposed tables must reconstruct the original data.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="4-views"&gt;4. Views&lt;/h2&gt;
&lt;h3 id="41-definition"&gt;4.1 Definition&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;view&lt;/strong&gt; is a virtual table defined by a SELECT query over base tables. It stores no data, providing abstraction, reuse, and security.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="42-syntax"&gt;4.2 Syntax&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;CREATE&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;VIEW&lt;/span&gt; view_name [(column_list)]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;AS&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt; ...
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[&lt;span style="color:#66d9ef"&gt;WITH&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;CHECK&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;OPTION&lt;/span&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;WITH CHECK OPTION&lt;/code&gt; enforces view predicates during DML operations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ORDER BY&lt;/code&gt; is not allowed inside view definitions; apply ordering when querying the view.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="43-dml-restrictions"&gt;4.3 DML Restrictions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;View updates propagate to base tables only when:
&lt;ol&gt;
&lt;li&gt;key and NOT NULL columns are present,&lt;/li&gt;
&lt;li&gt;no aggregates, DISTINCT, GROUP BY, or UNION,&lt;/li&gt;
&lt;li&gt;simple single-table views.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Dropping a view leaves base tables intact, but drop dependent constraints first.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="44-benefits"&gt;4.4 Benefits&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Simplify complex queries.&lt;/li&gt;
&lt;li&gt;Provide column-level access control.&lt;/li&gt;
&lt;li&gt;Maintain logical independence when base schema changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="5-indexing"&gt;5. Indexing&lt;/h2&gt;
&lt;h3 id="51-what--why"&gt;5.1 What &amp;amp; Why&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;An index is a data structure that enables &lt;strong&gt;fast record retrieval&lt;/strong&gt;—most RDBMSs use B+ Trees (sometimes hashes).&lt;/li&gt;
&lt;li&gt;Conceptually stores key-value pairs (key → record address).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="52-how-it-works"&gt;5.2 How It Works&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Look up the search key in the index.&lt;/li&gt;
&lt;li&gt;Follow the pointer to fetch the record, avoiding full table scans and reducing complexity to O(log n).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="53-design-considerations"&gt;5.3 Design Considerations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Best for selective predicates, frequent sorting/grouping, join keys, read-heavy workloads.&lt;/li&gt;
&lt;li&gt;Caveats: maintenance cost on INSERT/UPDATE/DELETE, functions/casts may prevent usage, low selectivity columns yield little benefit.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="54-clustered-vs-nonclustered"&gt;5.4 Clustered vs. Nonclustered&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Clustered index:&lt;/strong&gt; table rows are stored in index order (e.g., InnoDB primary key).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nonclustered index:&lt;/strong&gt; separate structure points to the actual data pages.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="6-triggers"&gt;6. Triggers&lt;/h2&gt;
&lt;h3 id="61-concept"&gt;6.1 Concept&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Stored routines that fire automatically in response to events (DML, DDL, login).&lt;/li&gt;
&lt;li&gt;Enforce integrity rules, record audit logs, maintain derived values.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="62-types"&gt;6.2 Types&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;BEFORE&lt;/strong&gt; triggers: execute prior to the event—validate or adjust values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AFTER&lt;/strong&gt; triggers: run post-event—log or cascade changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;INSTEAD OF&lt;/strong&gt; triggers: mainly for views; intercept operations and apply custom logic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="63-cautions"&gt;6.3 Cautions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Complex trigger chains can hurt performance or cause recursion.&lt;/li&gt;
&lt;li&gt;Failures roll back the entire transaction—add proper exception handling.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="7-stored-procedures"&gt;7. Stored Procedures&lt;/h2&gt;
&lt;h3 id="71-definition"&gt;7.1 Definition&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Precompiled SQL code stored in the database; accepts parameters to automate recurring tasks.&lt;/li&gt;
&lt;li&gt;Executes on the server, reducing network trips and centralising business logic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="72-example"&gt;7.2 Example&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;CREATE&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;PROCEDURE&lt;/span&gt; proc_name(&lt;span style="color:#66d9ef"&gt;IN&lt;/span&gt; p INT, &lt;span style="color:#66d9ef"&gt;OUT&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;result&lt;/span&gt; VARCHAR(&lt;span style="color:#ae81ff"&gt;50&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;BEGIN&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;-- SQL statements
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;END&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;Invoke with &lt;code&gt;CALL proc_name(value, @out);&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="73-pros--cons"&gt;7.3 Pros &amp;amp; Cons&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Pros: reusable, secure (permission-controlled), potential performance gains.&lt;/li&gt;
&lt;li&gt;Cons: higher complexity, tricky version control and testing, DBMS vendor lock-in.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="74-procedure-vs-function"&gt;7.4 Procedure vs. Function&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Functions must return a value and can appear inside SELECT queries.&lt;/li&gt;
&lt;li&gt;Procedures may omit a return value, use OUT parameters, and manage transactions.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="8-transactions"&gt;8. Transactions&lt;/h2&gt;
&lt;h3 id="81-definition"&gt;8.1 Definition&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;transaction&lt;/strong&gt; is a logical unit of work that groups multiple SQL statements. Commit all if successful, otherwise roll back to maintain consistency.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="82-acid-properties"&gt;8.2 ACID Properties&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Atomicity:&lt;/strong&gt; all-or-nothing execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Consistency:&lt;/strong&gt; preserves integrity constraints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolation:&lt;/strong&gt; concurrent transactions do not interfere.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Durability:&lt;/strong&gt; committed changes survive failures.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="83-isolation-levels"&gt;8.3 Isolation Levels&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Read Uncommitted:&lt;/strong&gt; allows dirty reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read Committed:&lt;/strong&gt; prevents dirty reads (default in many systems).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Repeatable Read:&lt;/strong&gt; guarantees repeatable reads; prevents non-repeatable reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Serializable:&lt;/strong&gt; equivalent to serial execution; safest but slowest.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="84-concurrency-control"&gt;8.4 Concurrency Control&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Locks (shared/exclusive, two-phase locking), optimistic control, timestamp ordering, MVCC.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="85-design-tips"&gt;8.5 Design Tips&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Keep transactions short to avoid deadlocks and lock contention.&lt;/li&gt;
&lt;li&gt;Common pattern: &lt;code&gt;BEGIN; ...; COMMIT;&lt;/code&gt; with &lt;code&gt;ROLLBACK;&lt;/code&gt; on errors.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="9-production-checklist"&gt;9. Production Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Balance normalization and denormalization according to read/write patterns.&lt;/li&gt;
&lt;li&gt;Maintain only necessary indexes and review usage statistics regularly.&lt;/li&gt;
&lt;li&gt;Include logging and error handling in triggers/procedures for auditability.&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>