---
# System prepended metadata

title: 'Programming - SQL syntax #2'
tags: [Programming, syntax, Database]

---

# Programming - SQL syntax #2

###### tags: `Programming` `Database` `syntax`

---

# 2024/10/14:

## My case:

### original syntax:
```
SELECT * FROM CE_Document LEFT JOIN LN_District 
where (CED_CourtName like '%keyword%') 
  OR (CED_DocumentNumber like '%keyword%') 
  OR (LND_Name like '%keyword%') 
order by CEC_UpdatedAt DESC
```

### fixed syntax:
```
SELECT * FROM CE_Document 
INNER JOIN LN_District ON CED_DistrictId = LND_DistrictId 
where (CED_CourtName like '%天天%') 
  OR (CED_DocumentNumber like '%天天%') 
  OR (LND_Name like '%天天%') 
order by CEC_UpdatedAt DESC
```


## Q1: 
I have a sql query syntax:
```
SELECT * FROM CE_Document LEFT JOIN LN_District where (CED_CourtName like '%keyword%') OR (CED_DocumentNumber like '%keyword%') OR (LND_Name like '%keyword%') order by CEC_UpdatedAt DESC
```

the columns `CED_CourtName` and `CED_DocumentNumber` belong to the table `CE_Document` and 
the column `LND_Name` belongs to the table `LN_District`

I see the query result might have many records in which many columns of ``CE_Document`` look reduntdant in the 

if I need to use other technique to get more precise result? thanks

## A - ChatGPT:
To get more precise results and avoid redundant data in the query result, 
here are some possible optimizations and techniques you can apply:

### 1. Use ``INNER JOIN`` Instead of ``LEFT JOIN`` (If Applicable):
If you don't need records from CE_Document that 
do not have corresponding records in ``LN_District``, 
switching to an ``INNER JOIN`` will __give you more precise results 
by filtering out rows without matches in both tables__.

INNER JOIN 確保兩個 Tables 的 row 都有值!!

```
SELECT * FROM CE_Document 
INNER JOIN LN_District ON CE_Document.district_id = LN_District.id
WHERE (CED_CourtName LIKE '%keyword%') 
  OR (CED_DocumentNumber LIKE '%keyword%') 
  OR (LND_Name LIKE '%keyword%')
ORDER BY CEC_UpdatedAt DESC
```

### 2. Filter Using ``AND`` Instead of ``OR``:
If using ``OR`` results in too many records, 
consider making the filter more strict by using ``AND`` conditions, 
depending on the logic you need. This will narrow down the results:

```
WHERE (CED_CourtName LIKE '%keyword%') 
  AND (CED_DocumentNumber LIKE '%keyword%')
```

### 3. Select Only Relevant Columns:
Instead of selecting all columns with ``SELECT *``, 
explicitly specify the columns you need. This reduces redundancy, 
as you avoid retrieving unnecessary data. For example:

```
SELECT CE_Document.CED_CourtName, CE_Document.CED_DocumentNumber, LN_District.LND_Name, CE_Document.CEC_UpdatedAt 
FROM CE_Document 
LEFT JOIN LN_District ON CE_Document.district_id = LN_District.id
WHERE (CED_CourtName LIKE '%keyword%') 
  OR (CED_DocumentNumber LIKE '%keyword%') 
  OR (LND_Name LIKE '%keyword%')
ORDER BY CEC_UpdatedAt DESC
```

### 4. Limit Results with LIMIT:

If you want to avoid too many results, 
you can limit the number of rows returned with the ``LIMIT`` clause:

```
ORDER BY CEC_UpdatedAt DESC 
LIMIT 50
```

### 5. Apply Full-Text Search (FTS):
If your database supports Full-Text Search 
(e.g., FTS in SQLite or MATCH AGAINST in MySQL), 
you can achieve more precise and optimized text search results. 
FTS is usually faster and more relevant than LIKE for large text fields:

#### SQLite FTS: 
You need to create an FTS virtual table to search the text:

```
CREATE VIRTUAL TABLE CE_Document_FTS USING fts4(CED_CourtName, CED_DocumentNumber);
SELECT * FROM CE_Document_FTS 
WHERE CE_Document_FTS MATCH 'keyword'
ORDER BY rank;
```

### MySQL Full-Text Search:

```
SELECT * FROM CE_Document 
LEFT JOIN LN_District ON CE_Document.district_id = LN_District.id
WHERE MATCH (CED_CourtName, CED_DocumentNumber) AGAINST ('keyword' IN NATURAL LANGUAGE MODE)
  OR MATCH (LND_Name) AGAINST ('keyword' IN NATURAL LANGUAGE MODE)
ORDER BY CEC_UpdatedAt DESC;
```

### 6. Rank Results with Conditional Weights:
You can also prioritize results 
by giving higher weight to matches on specific columns, 
which makes your results more relevant:

```
SELECT *, 
CASE 
  WHEN CED_CourtName LIKE '%keyword%' THEN 3
  WHEN CED_DocumentNumber LIKE '%keyword%' THEN 2
  WHEN LND_Name LIKE '%keyword%' THEN 1
END AS relevance
FROM CE_Document 
LEFT JOIN LN_District ON CE_Document.district_id = LN_District.id
WHERE (CED_CourtName LIKE '%keyword%') 
  OR (CED_DocumentNumber LIKE '%keyword%') 
  OR (LND_Name LIKE '%keyword%')
ORDER BY relevance DESC, CEC_UpdatedAt DESC;
```


### Summary:
* Use ``INNER JOIN`` to avoid redundant records if applicable.
* Replace ``OR`` with ``AND`` if more strict filtering is needed.
* Select only relevant columns to reduce data size and redundancy.
* Use ``LIMIT`` to control the number of records.
* Apply Full-Text Search if supported by your database for more efficient text searches.
* Add conditional weights to prioritize certain matches for more relevant results.


## Q2:
Because I use Room database, if such stragegy is a good idea: 
I simply query on ``CE_Document`` with LIKE.
then filter out the Relation respect to the  ``LN_District`` entity? thanks


```
data class CourtEntrustmentDocRelation(
    @Embedded
    val ceDocument: CeDocumentEntity = CeDocumentEntity(),  // CE_Document table

    @Relation(
        entity = LnDistrictEntity::class,
        parentColumn = "CED_DistrictId",
        entityColumn = "LND_DistrictId",
    )
    val lnDistrict: LnDistrictEntity = LnDistrictEntity(), // LN_District table

    @Relation(
        entity = LnSectionEntity::class,
        parentColumn = "CED_SectionId",
        entityColumn = "LNS_SectionId",
    )
    val lnSection: LnSectionEntity = LnSectionEntity(), // LN_Section table
)
```

## A - ChatGPT:

Yes, querying the ``CE_Document`` table with ``LIKE`` 
and then filtering out the relations for the ``LN_District`` entity 
is a reasonable strategy in Room. Here’s why it can work well:

### Benefits of This Strategy:
1. __Reduced Query Complexity__: 
By querying only on the ``CE_Document`` table using ``LIKE``, 
you __avoid the overhead of complex joins in the initial query__. 
This __simplifies the query and can improve performance__, 
especially when there are many rows to filter.

2. __Efficient Filtering Using Relations__: 
Once you fetch the data from ``CE_Document``, 
Room will automatically handle the relationships 
between ``CE_Document`` and ``LN_District`` (as well as ``LN_Section``) 
using the ``@Relation annotation``. 
This __keeps the code clean and ensures 
that you’re only dealing with relevant data when accessing the relationships__.

3. __Separation of Concerns__: 
By first querying ``CE_Document`` and then dealing with relations separately, 
you separate the logic of searching from the logic of data relationships. 
This makes it __easier to manage and maintain the code in the long run__.

### Example Workflow:

1. Query the ``CE_Document Table``: 
You can use a ``LIKE`` query to filter records based on 
``CED_CourtName`` or ``CED_DocumentNumber`` in the ``CE_Document`` table.

```
@Query("SELECT * FROM CE_Document WHERE CED_CourtName LIKE :keyword OR CED_DocumentNumber LIKE :keyword ORDER BY CEC_UpdatedAt DESC")
fun getDocumentsByKeyword(keyword: String): List<CourtEntrustmentDocRelation>
```

2. Retrieve and Use Relations: 
Room will handle the relations to ``LN_District`` and ``LN_Section`` 
for each ``CourtEntrustmentDocRelation`` when you access the corresponding properties. 
You can filter or process these relations as needed.

3. Filtering Based on ``LN_District`` in Code:
If needed, you can further filter the results based on 
the ``LN_District`` entity after retrieving the ``CourtEntrustmentDocRelation``:

```
val documents = documentDao.getDocumentsByKeyword("%keyword%")

// Filter the results based on LN_District
val filteredDocuments = documents.filter {
    it.lnDistrict.LND_Name.contains("some_district_name", ignoreCase = true)
}
```

### Considerations:
* __Performance__: 
For large datasets, querying CE_Document first and then 
filtering in-memory can be efficient as it reduces the number of rows initially fetched, 
and Room can handle relationships efficiently in the background.
* __Scalability__: 
If the dataset is large and you need to perform complex filtering 
on the ``LN_District`` entity often, you might want to consider 
adding filtering directly to the SQL query.

In summary, this approach should work well in many cases, 
keeping the query straightforward and utilizing Room's relationship mapping effectively.

---

# 2024/05/31 - alter type of a column in a SQL-based table

## Q1
about the MS SQL server, how can I alter a column's type via the sql syntax, thanks 

## ChatGPT
To alter a column's type in Microsoft SQL Server, y
ou use the ``ALTER TABLE statement`` in combination with the ``ALTER COLUMN`` clause. 
The basic syntax is as follows:

```
ALTER TABLE table_name
ALTER COLUMN column_name new_data_type;
```

### Important Considerations
*__Data Type Compatibility__: 
Ensure that the new data type is compatible with the existing data in the column, 
or you __may need to handle data conversion__.
*__Nullable Columns__: 
If the column __allows ``NULLs``, ensure the new data type can also handle ``NULLs`` if needed__.
*__Constraints and Indexes__: 
Changing a column's type might require dropping and recreating constraints or indexes.

### Example Usage

#### Change Column Type from ``VARCHAR`` to ``INT``

Suppose you have a table ``Employees`` and 
you want to change the data type of the ``EmployeeCode`` column from ``VARCHAR(10)`` to ``INT``:

```
-- First, ensure that the column contains only numeric values to avoid conversion errors
UPDATE Employees
SET EmployeeCode = NULL
WHERE TRY_CAST(EmployeeCode AS INT) IS NULL;

-- Then, alter the column type
ALTER TABLE Employees
ALTER COLUMN EmployeeCode INT;
Change Column Type from INT to VARCHAR
```

If you want to change the data type of the ``EmployeeCode`` column from ``INT`` to ``VARCHAR(10)``:

```
ALTER TABLE Employees
ALTER COLUMN EmployeeCode VARCHAR(10);
```

#### Step-by-Step Guide
1. Check Current Data and Constraints
Before changing the column type, check the current data and constraints. 
Ensure the existing data can be converted to the new type without loss.

```
SELECT * FROM Employees;
```

2. Alter the Column
Use the ``ALTER TABLE`` statement to change the column type.

```
ALTER TABLE Employees
ALTER COLUMN EmployeeCode VARCHAR(10);
```

3. Handle Constraints and Indexes
If there are constraints or indexes on the column, 
you __might need to drop them before changing the column type and recreate them afterward__.

```
-- Example: Dropping a constraint
ALTER TABLE Employees
DROP CONSTRAINT constraint_name;

-- Alter the column type
ALTER TABLE Employees
ALTER COLUMN EmployeeCode VARCHAR(10);

-- Recreate the constraint
ALTER TABLE Employees
ADD CONSTRAINT constraint_name CHECK (EmployeeCode LIKE '[0-9]%');
```

### Full Example with Constraints and Data Handling
Here's a more comprehensive example that 
includes handling constraints and ensuring data compatibility:

```
-- Step 1: Check current data and constraints
SELECT * FROM Employees;

-- Step 2: Handle constraints (if any)
ALTER TABLE Employees
DROP CONSTRAINT CK_EmployeeCode_Numeric;  -- Example constraint

-- Step 3: Ensure data compatibility (if changing to a more restrictive type)
UPDATE Employees
SET EmployeeCode = NULL
WHERE TRY_CAST(EmployeeCode AS INT) IS NULL;

-- Step 4: Alter the column type
ALTER TABLE Employees
ALTER COLUMN EmployeeCode INT;

-- Step 5: Recreate constraints (if needed)
ALTER TABLE Employees
ADD CONSTRAINT CK_EmployeeCode_Numeric CHECK (EmployeeCode IS NOT NULL);
```

### Summary
Ensure Data Compatibility: Verify the existing data can be converted to the new type.
Handle Constraints and Indexes: Drop and recreate them as needed.
Alter the Column: Use the ALTER TABLE ... ALTER COLUMN statement.
By following these steps, you can successfully change a column's data type 
in SQL Server while maintaining data integrity and adhering to constraints.

## Q2
your info in comprehensive, thumb up
and if I just want to change from e.g., nvarchar(20) to nvarchar(2000),
does I need to do something like you mentioned before:
```
UPDATE Employees
SET EmployeeCode = NULL
WHERE TRY_CAST(EmployeeCode AS INT) IS NULL;
```

## ChatGPT
If you are simply changing the length of an ``NVARCHAR`` column 
(e.g., from ``NVARCHAR(20)`` to ``NVARCHAR(2000)``), 
you __generally do not need to perform the data compatibility check 
or set values to ``NULL``__ as described in the previous example. 
This is because changing the length of a string data type 
does not affect the data stored within the column, 
assuming all current data fits within the new length limit.

Simple Alteration Example
Here’s how you can alter the column directly:

```
ALTER TABLE Employees
ALTER COLUMN EmployeeCode NVARCHAR(2000);
```

### Important Considerations
* __Constraints and Indexes__: 
If there are constraints or indexes on the column, 
they should not be affected by a change in length 
unless they are explicitly defined with the column length. 
Typically, this isn't the case, but it's good practice to check.
* __Data Validation__: 
Since you're only increasing the size, there is no risk of data truncation. 
However, __if you were decreasing the size__, 
you would __need to ensure no data exceeds the new limit__.
* __Impact on Performance and Storage__: 
Increasing the size of the column can impact performance and storage, especially if the table has a large number of rows. Ensure that the change is necessary and beneficial.

### Full Example
Here’s a complete example of changing an ``NVARCHAR(20)`` column to ``NVARCHAR(2000)`` 
in a table called ``Employees``:

Step 1: Check Current Data (Optional)
It’s always good to inspect the current data to ensure there won’t be any unexpected issues.

```
SELECT DISTINCT LEN(EmployeeCode) AS Length, COUNT(*) AS Count
FROM Employees
GROUP BY LEN(EmployeeCode)
ORDER BY Length;
```

Step 2: Alter the Column
Perform the alteration directly.

```
ALTER TABLE Employees
ALTER COLUMN EmployeeCode NVARCHAR(2000);
```

### Summary
Simple Type Change: For changing ``NVARCHAR(20)`` to ``NVARCHAR(2000)``, 
you don't need to handle data compatibility checks 
as you would when changing to a different type.
Execution: Just use the ``ALTER TABLE`` statement to alter the column directly.
This straightforward approach ensures that 
your ``NVARCHAR`` column is resized with minimal hassle and risk.



---

# 2024/05/20:

## Q1:

if the design of questionnaire consists of multiple "Question Topics";
Each "Question Topic" consists of multiple "Sections".
Each "Secton" of a "Question Topic" consists of different types of questions
- checkboxs(multiple selections), 
- radio button (single selection)
- text input.

I want to save the anwser of a Question Topic to the database.
If using a table to save the answers of a single secon is a good idea?
or you have other better idea? thanks

## A1:

Saving answers from a questionnaire that consists of multiple "Question Topics," 
each with multiple "Sections" containing different types of questions 
(checkboxes, radio buttons, text input), can be structured efficiently 
using a relational database. Here’s a detailed approach to designing your database:

### Database Schema Design

#### Tables for Basic Entities:
* __QuestionTopic__: Stores metadata about each question topic.
* __Section__: Stores metadata about each section within a question topic.
* __Question__: Stores metadata about each question within a section.
* __Answer__: Stores the actual answers provided by users.

### Table Structures
* __``QuestionTopic``__
```
CREATE TABLE QuestionTopic (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    description TEXT
);
```

* __``Section``__
```
CREATE TABLE Section (
    id INT PRIMARY KEY AUTO_INCREMENT,
    question_topic_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    FOREIGN KEY (question_topic_id) REFERENCES QuestionTopic(id)
);
```

* __``Question``__
```
CREATE TABLE Question (
    id INT PRIMARY KEY AUTO_INCREMENT,
    section_id INT NOT NULL,
    question_text TEXT NOT NULL,
    question_type ENUM('checkbox', 'radio', 'text') NOT NULL,
    FOREIGN KEY (section_id) REFERENCES Section(id)
);
```

* __``Answer``__
```
CREATE TABLE Answer (
    id INT PRIMARY KEY AUTO_INCREMENT,
    question_id INT NOT NULL,
    user_id INT NOT NULL,  -- Assuming you have a User table for respondent information
    answer_text TEXT,
    answer_option_ids TEXT,  -- CSV or JSON string for checkbox/radio options
    FOREIGN KEY (question_id) REFERENCES Question(id)
);
```

### Detailed Explanation:

#### 1. ``QuestionTopic`` Table:
* Each row represents a unique question topic.

#### 2. ``Section`` Table:
* Each row represents a section within a specific question topic.
* ``question_topic_id`` links the section to a question topic.

#### 3. ``Question`` Table:

* Each row represents a question within a section.
* The ``question_type`` column determines the type of the question (checkbox, radio, text).
* ``section_id`` links the question to a section.

#### 4. ``Answer`` Table:

* Stores the answers submitted by users.
* ``question_id`` links the answer to a specific question.
* ``user_id`` links the answer to a specific user.
* ``answer_text`` stores the answer for text input questions.
* ``answer_option_ids`` stores selected options for checkbox and radio button questions 
in a CSV or JSON format.

### Benefits of This Approach:
#### 1. Normalization:

The data is normalized, reducing redundancy.
Easy to manage and query.

#### 2. Flexibility:

Adding new questions, sections, or topics requires inserting new rows without altering the table structure.
Supports various question types efficiently.

#### 3. Scalability:

The structure can handle large datasets and complex queries.

### Storing Checkbox and Radio Answers:
For checkboxes and radio buttons, storing selected options 
as a CSV or JSON string in the ``answer_option_ids`` field 
__helps in easily retrieving and processing the data__. 
This approach is particularly useful when a question allows multiple selections (checkbox).

### Example Queries:

* Inserting an ``Answer`` for a Text ``Question``:

```
INSERT INTO Answer (question_id, user_id, answer_text)
VALUES (1, 123, 'This is my answer.');
```

* Inserting an Answer for a Checkbox Question:

```
INSERT INTO Answer (question_id, user_id, answer_option_ids)
VALUES (2, 123, '1,3,4');  -- Assuming options 1, 3, and 4 were selected
```

* Fetching ``Answers`` for a ``User``:

```
SELECT q.question_text, a.answer_text, a.answer_option_ids
FROM Answer a
JOIN Question q ON a.question_id = q.id
WHERE a.user_id = 123;
```

This schema provides a robust, scalable, and flexible way to handle the diverse question types and hierarchical structure of your questionnaire.

---

## Q2: 
From your perspective, basically, a record in the ``Answer`` table can contain Text, Radio button, and Checkboxs.

Are there any limitations for such ``Answer`` table design that I should take into account? thanks 

## A2:
While the proposed Answer table design is flexible and handles different types of questions 
(text, radio buttons, checkboxes), there are a few limitations and considerations to keep in mind:

1. __Redundancy and Unused Fields__
Each record in the Answer table will have fields for ``answer_text`` and ``answer_option_ids``, 
but __not all fields will be used for every question type__. 
For example, a text question will not use ``answer_option_ids``, 
and a radio button question won't use ``answer_text``.

2. __Data Integrity and Validation__
Ensuring that __only the relevant fields are populated for each question type can be challenging__. 
For instance:
* For "text" questions, only ``answer_text`` should be populated.
* For "checkbox" questions, ``answer_option_ids`` should be populated.
* For "radio" questions, ``answer_option_ids`` should contain a single selected option.
To enforce this, __additional constraints or application-level validation logic would be necessary__.

3. __Query Complexity__
__Queries might become complex when dealing with different types of answers__. 
For example, aggregating responses for reporting purposes 
requires different handling for text responses compared to checkbox responses.

4. __Storage Efficiency__
__Storing multiple types of answers in a single table could lead to inefficient use of space__. 
Depending on the database system and the volume of data, this could impact performance.

5. __Scalability__
As the number of questions, sections, and answers grows, 
managing and scaling the database might become more challenging. 
This is especially true if the volume of __unused fields grows significantly__.

### Alternative Design: Separate Tables for Each Question Type
One way to address these limitations is to __use separate tables for each type of answer__:

* ``AnswerText`` Table
```
CREATE TABLE AnswerText (
    id INT PRIMARY KEY AUTO_INCREMENT,
    question_id INT NOT NULL,
    user_id INT NOT NULL,
    answer_text TEXT,
    FOREIGN KEY (question_id) REFERENCES Question(id)
);
```

* `AnswerOption`` Table (for radio and checkbox)
```
CREATE TABLE AnswerOption (
    id INT PRIMARY KEY AUTO_INCREMENT,
    question_id INT NOT NULL,
    user_id INT NOT NULL,
    option_id INT NOT NULL,
    FOREIGN KEY (question_id) REFERENCES Question(id)
);
```

### Benefits of Separate Tables:

1. __Data Integrity__: 
Each table is tailored to store only the relevant type of data, 
reducing the risk of invalid data entries.
2. __Storage Efficiency__: 
More efficient use of storage, as only relevant fields are stored.
3. __Query Simplification__: 
Queries can be simpler and more efficient, as each table only contains one type of data.

### Potential Trade-offs:
1. __Complexity in Schema__: 
The schema becomes more complex with multiple tables.
2. __Join Operations__: 
Queries that need to aggregate data across different question types 
will require more complex join operations.

### Summary

The initial design with a single Answer table is flexible and straightforward 
but has some limitations regarding redundancy, data integrity, and query complexity. 
Using separate tables for different answer types can address these issues but at the cost of increased schema complexity.

Depending on the specific requirements, volume of data, and the complexity of the questionnaire, 
you can choose the approach that best fits your needs. 
For small to moderate-sized applications, the initial design may suffice. 
For larger and more complex systems, considering a more normalized approach with separate tables might be beneficial.


---

# 跨層 Query (2024/03/14)

## Q1:
I have 3 Tables, one is ````ItemApply``, one is 'ItemApplyDetail' and one is 'Item'.

'ItemApplyDetail' use a foreign key related to a 'ItemApply''s primary and yet another foreign key related to a 'Item'.
'ItemApply' has a column 'applyDate' to describe the apply date.
'ItemApplyDetail' has a column 'status' to describe the apply state of item.
the apply state of item likes ``state_apply -> state_take_out -> state_put_back``.

Typically,  the apply state must be 'state_put_back', a new 'ItemApply' can be created.

When I query the 'Item's, I want to know its up-to-date apply state.
My thoughts is to not only query the 'Item' table but also query the 'ItemApplyDetail' (including the related 'ItemApply') and find the newest 'ItemApply'.

Do you have a better option to do so? Thanks

### A1 by ChatGPT:

Your approach seems reasonable for determining the up-to-date apply state of an item. However, you might consider using a SQL query that joins the necessary tables and selects the most recent apply state for each item. This way, you can retrieve the information you need directly from the database without having to perform additional processing in your application code.

Here's an example SQL query that achieves this:

```
SELECT
    i.itemId,
    i.itemName,
    COALESCE(MAX(CASE WHEN iad.status = 'state_apply' THEN ia.applyDate END), '') AS applyDate_state_apply,
    COALESCE(MAX(CASE WHEN iad.status = 'state_take_out' THEN ia.applyDate END), '') AS applyDate_state_take_out,
    COALESCE(MAX(CASE WHEN iad.status = 'state_put_back' THEN ia.applyDate END), '') AS applyDate_state_put_back,
    COALESCE(MAX(CASE WHEN iad.status = 'state_put_back' THEN iad.status END), '') AS current_apply_state
FROM
    Item i
LEFT JOIN
    ItemApplyDetail iad ON i.itemId = iad.itemId
LEFT JOIN
    ItemApply ia ON iad.applyId = ia.applyId
GROUP BY
    i.itemId, i.itemName;
```

This query joins the ``Item`` table with ``ItemApplyDetail`` and ``ItemApply`` tables, 
and then groups the results by ``item``. 
It uses __"conditional aggregation"__ to __find the most recent apply date"
for each apply state (``state_apply``, ``state_take_out``, ``state_put_back``) 
and selects the current apply state based on the most recent ``state_put_back`` entry. 
The ``COALESCE`` function is used to handle ``NULL`` values gracefully.

This query should give you a result set with each item's ID, name, 
and its up-to-date apply state along with the corresponding apply dates for each state. 
You can adjust the query as needed to fit your specific schema and requirements.


## ``COALESCE()`` function

The ``COALESCE()`` function is used to __return the first ``non-null`` expression in a list of expressions__. 
It takes multiple arguments and returns the first non-null value. 
If all arguments are ``null``, it returns ``null``.

Here's the syntax for ``COALESCE()``:

```
COALESCE(expr1, expr2, ...)``:
```
* ``expr1``, ``expr2``, etc.: 
Expressions that are evaluated in the order listed. 
The function returns the value of the first expression that is not ``null``.

Here's an example of how ``COALESCE()`` works:

```
SELECT COALESCE(NULL, 'Second', 'Third'); -- Returns 'Second'
```

In this example, since the first expression is ``null``, 
the function returns the value of the second expression (``'Second'``).

``COALESCE()`` is particularly useful for handling situations 
where __you have multiple potential values and 
want to prioritize them based on their availability or validity__. 
It's commonly used in scenarios like __handling default values or fallback values__ in SQL queries.

## ``CASE`` expression

In SQL, ``CASE`` is a conditional expression used for branching logic within a query. 
It allows you to __evaluate conditions and return different values based on those conditions__. 
The ``CASE`` expression has the following syntax:

```
CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ...
    ELSE result_else
END
```

* ``WHEN condition1 THEN result1``: 
If ``condition1`` is true, the expression returns ``result1``.
* ``WHEN condition2 THEN result2``: 
If ``condition1`` is false but ``condition2`` is true, the expression returns ``result2``.
* ``ELSE result_else``: 
If none of the conditions are true, the expression returns ``result_else``. 
__This part is optional__.

Here's an example to illustrate how ``CASE`` works:

```
SELECT
    CASE 
        WHEN score >= 90 THEN 'A'
        WHEN score >= 80 THEN 'B'
        WHEN score >= 70 THEN 'C'
        ELSE 'D'
    END AS grade
FROM
    students;
```

In this example, depending on the value of the ``score`` column in the ``students`` table, 
the ``CASE`` expression will return a corresponding ``grade`` (``'A'``, ``'B'``, ``'C'``, or ``'D'``).

In the SQL syntax you provided:

```
CASE WHEN iad.status = 'state_apply' THEN ia.applyDate END
```

It's checking if the ``status`` column in the ``ItemApplyDetail`` table is equal to ``state_apply``. 
If it is, it returns the ``applyDate`` from the related ``ItemApply`` (specified by ``ia``). 
If the condition is not met, it returns ``NULL``. 
This is often used within an __aggregate function__ like ``MAX()`` 
to conditionally select certain values based on conditions.

## Q2:
i learn a lot, use ``CASE`` expression, ``COALESCE()`` and ``MAX()`` functions 
to address the complex query problem. thanks a lot.
I have modified the sql syntax you provided to query the item, 
I got three date for states: ``state_apply``, ``state_take_out``, ``state_put_back``
and the ``current_apply_state``.
I noticed that ``current_apply_state`` only has value when the apply state is ``state_put_back``
If I want the other valid states, i simply to add other when case, 
e.g.:
```
COALESCE(MAX(
  CASE 
    WHEN iad.status = 'state_put_back' THEN iad.status 
    WHEN iad.status = 'state_put_back' THEN iad.status 
    WHEN iad.status = 'state_put_back' THEN iad.status 
  END), ''
) AS current_apply_state
```
or simply get replace ``COALESCE(...)``  with ``iad.status``

### A2 by ChatGPT:

It seems like you're trying to handle different cases for the ``current_apply_state`` field 
based on the different states (``state_apply``, ``state_take_out``, ``state_put_back``). 
If you only want to consider certain states as valid for the ``current_apply_state``, 
you can adjust your ``CASE`` expression accordingly.

Here's an example of how you can modify the CASE expression to consider multiple states:

``
COALESCE(
  MAX(
    CASE 
      WHEN iad.status = 'state_apply' THEN iad.status 
      WHEN iad.status = 'state_take_out' THEN iad.status 
      WHEN iad.status = 'state_put_back' THEN iad.status 
    END
  ), ''
) AS current_apply_state
``

In this modified version, the ``CASE`` expression checks for three different states 
(``state_apply``, ``state_take_out``, ``state_put_back``) and 
returns the status if it matches any of these states. 
The ``MAX()`` function is then __used to find the maximum status among these three states__.

However, if you want to simply retrieve the status without considering any specific conditions, you can directly use iad.status without the CASE expression and MAX() function:

```
COALESCE(iad.status, '') AS current_apply_state
```

This will return the value of ``iad.status`` directly, or an empty string if it's ``NULL``. 
__This approach assumes that ``iad.status`` already contains the most up-to-date state for the item__. 
If you have specific conditions for determining the ``current_apply_state``, 
you can use the ``CASE`` expression approach as demonstrated earlier.

## Q3
I found a query record 
which ``applyDate_state_take_out`` is after ``applyDate_state_put_back``
but it ``current_apply_state`` is ``state_put_back``.

the correct ``current_apply_state`` should be ``state_take_out``

if I can do any better? thanks


To address the scenario where the ``applyDate_state_take_out`` is after ``applyDate_state_put_back``, 
but the ``current_apply_state`` is incorrectly showing as ``state_put_back``, 
you can refine your SQL query to ensure that 
the ``current_apply_state`` accurately reflects the latest state based on the ``applyDate``.

One way to achieve this is by __using a subquery to find the maximum applyDate for each state__, 
and then joining this result with your main query 
to determine the ``current_apply_state`` based on the latest ``applyDate``.

Here's an adjusted version of the SQL query that incorporates this approach:

```
SELECT
    i.itemId,
    i.itemName,
    COALESCE(MAX(CASE WHEN iad.status = 'state_apply' THEN ia.applyDate END), '') AS applyDate_state_apply,
    COALESCE(MAX(CASE WHEN iad.status = 'state_take_out' THEN ia.applyDate END), '') AS applyDate_state_take_out,
    COALESCE(MAX(CASE WHEN iad.status = 'state_put_back' THEN ia.applyDate END), '') AS applyDate_state_put_back,
    COALESCE((
        SELECT iad2.status
        FROM ItemApplyDetail iad2
        JOIN ItemApply ia2 ON iad2.applyId = ia2.applyId
        WHERE i.itemId = iad2.itemId
        ORDER BY ia2.applyDate DESC
        LIMIT 1
    ), '') AS current_apply_state
FROM
    Item i
LEFT JOIN
    ItemApplyDetail iad ON i.itemId = iad.itemId
LEFT JOIN
    ItemApply ia ON iad.applyId = ia.applyId
GROUP BY
    i.itemId, i.itemName;
```

In this adjusted query:

We use a subquery to find the latest status (``current_apply_state``) for each item 
by ordering the results based on the ``applyDate`` in descending order 
and selecting only the first row (``LIMIT 1``).
This subquery is incorporated into the main query to determine the ``current_apply_state`` based on the latest ``applyDate``.
With this approach, the ``current_apply_state`` will accurately reflect the latest state 
based on the ``applyDate``, even if the ``applyDate_state_take_out`` is after the ``applyDate_state_put_back``.


## A by Gemini:

```
SELECT 
    g.RFG_GalleryId,
    g.RFG_Code,
    g.RFG_NameCht, 
    gad.RFGAD_Status
FROM RF_Gallery g
LEFT JOIN
    RF_GalleryApplyDetail gad ON g.RFG_GalleryId = gad.RFGAD_GalleryId 
LEFT JOIN
    RF_GalleryApply ga ON gad.RFGAD_GalleryApplyId = ga.RFGA_GalleryApplyId 
WHERE ga.RFGA_ApplyDate = (
  SELECT MAX(RFGA_ApplyDate)
  FROM RF_GalleryApply ga2
  JOIN RF_GalleryApplyDetail gad2 ON ga2.RFGA_GalleryApplyId = gad2.RFGAD_GalleryApplyId
  WHERE g.RFG_GalleryId = gad2.RFGAD_GalleryId
)
AND (gad.RFGAD_Status = '出箱櫃' OR gad.RFGAD_Status = '出箱' OR gad.RFGAD_Status = '出庫' OR 
gad.RFGAD_Status = '入庫' OR gad.RFGAD_Status = '入原箱櫃' OR gad.RFGAD_Status = '入原貯箱' OR 
gad.RFGAD_Status = '入箱' OR gad.RFGAD_Status = '原箱' OR RFGAD_Status IS NULL);
```

結果怪怪的

## My exercise


```
SELECT
    g.RFG_GalleryId,
    g.RFG_Code,
    g.RFG_NameCht,
    COALESCE(MAX(
      CASE 
        WHEN gad.RFGAD_Status = '出箱櫃' THEN ga.RFGA_ApplyDate 
        WHEN gad.RFGAD_Status = '出箱' THEN ga.RFGA_ApplyDate 
      END
    ), '') AS applyDate_state_take_out,
    COALESCE(MAX(
      CASE 
        WHEN gad.RFGAD_Status = '出庫' THEN ga.RFGA_ApplyDate 
      END
    ), '') AS applyDate_state_out_bound,
    COALESCE(MAX(
      CASE 
        WHEN gad.RFGAD_Status = '入庫' THEN ga.RFGA_ApplyDate 
      END
    ), '') AS applyDate_state_in_bound,
    COALESCE((
      CASE 
        WHEN gad.RFGAD_Status = '入原箱櫃' THEN ga.RFGA_ApplyDate 
        WHEN gad.RFGAD_Status = '入原貯箱' THEN ga.RFGA_ApplyDate 
        WHEN gad.RFGAD_Status = '原箱' THEN ga.RFGA_ApplyDate 
        WHEN gad.RFGAD_Status = '入箱' THEN ga.RFGA_ApplyDate 
      END
    ), '') AS applyDate_state_put_back,
    COALESCE((
      SELECT gad2.RFGAD_Status
                 FROM RF_GalleryApplyDetail gad2
                 JOIN RF_GalleryApply ga2 ON gad2.RFGAD_GalleryApplyId = ga2.RFGA_GalleryApplyId
                 WHERE g.RFG_GalleryId = gad2.RFGAD_GalleryId
                 ORDER BY ga2.RFGA_ApplyDate DESC
                 LIMIT 1
    ), '') AS current_apply_state 
FROM 
    RF_Gallery g 
LEFT JOIN
    RF_GalleryApplyDetail gad ON g.RFG_GalleryId = gad.RFGAD_GalleryId 
LEFT JOIN
    RF_GalleryApply ga ON gad.RFGAD_GalleryApplyId = ga.RFGA_GalleryApplyId 
GROUP BY 
    g.RFG_GalleryId, g.RFG_NameCht
```

### 心得

從這個 Query 和目前 DB schema 的關聯, 
我原本以為關聯是有一個主從關係 (方向性)
``RF_GalleryApply`` -->
``RF_GalleryApplyDetail`` -->
``RF_Gallery``

但實際上, Primary key - foreign key 只是建立關聯,
實際上看怎麼查來決定它的主從關係 (方向性)

# 2024/01/11

## SQLite db - 修改 Table 增加 column

![螢幕擷取畫面 2024-01-11 151552](https://hackmd.io/_uploads/Bk76IMaOT.png)

![螢幕擷取畫面 2024-01-11 152120](https://hackmd.io/_uploads/SkyiDGaup.png)

![螢幕擷取畫面 2024-01-11 152142](https://hackmd.io/_uploads/ByMsPM6O6.png)

![螢幕擷取畫面 2024-01-11 152356](https://hackmd.io/_uploads/B1riPGaOT.png)


# 2023/10/04

To query records whose certain column (e.g., foo) has value (NOT NULL), 
you can use the ``IS NOT NULL`` operator in SQL. 
This operator returns true if the column value is not null, 
and false otherwise.

For example, if you have a table called ``products`` 
with columns ``id``, ``name``, ``price``, and ``category``, 
and you want to select only the rows 
where the ``category`` is not null, you can use this SQL query:

```
-- Select all columns from products where category is not null
SELECT * FROM products WHERE category IS NOT NULL;
```

This query will return a result set like this:

| id | name | price | category |
| --- | --- | --- | --- |
| 1 | Laptop | 999.99 | Electronics |
| 2 | Book | 19.99 | Literature |
| 3 | Coffee | 4.99 | Beverages |
| 4 | Shirt | 29.99 | Clothing |

You can also combine the ``IS NOT NULL`` operator with other operators, 
such as ``AND``, ``OR``, and ``NOT``, to create more complex conditions.

For example, if you want to select only the rows 
where the ``name`` starts with 'B' and 
the ``price`` is not null, you can use this SQL query:

```
-- Select all columns from products where name starts with 'B' and price is not null
SELECT * FROM products WHERE name LIKE 'B%' AND price IS NOT NULL;
```

This query will return a result set like this:

| id | name | price | category |
| --- | --- | --- | --- |
| 2 | Book | 19.99 | Literature |
| 5 | Bagel | 2.99 | Food |


# 2023/07/07

## SQL Server Management Studio 編號某 row 欄位

### Reference
* [Edit selected rows manually in SQL Server](https://stackoverflow.com/questions/9552877/edit-selected-rows-manually-in-sql-server)

### Syntax

```
SELECT [SAT_TransactionId]
  ,[SAT_Amount]
  ,[SAT_BuyerBatchNo]
  ,[SAT_BuyerId]
FROM [AMIS_Tainan].[dbo].[SA_Transaction]
where (SAT_CreateDate >= '2023-07-05') AND (SAT_CreateDate < '2023-07-06') 
where SAT_ReferenceNo = '11207061500056'
```

![](https://hackmd.io/_uploads/BkxxkM1HK3.png)

![](https://hackmd.io/_uploads/SJ_1zyBKh.png)

![](https://hackmd.io/_uploads/HyJez1SF2.png)

![](https://hackmd.io/_uploads/H1G4zkSKh.png)

修改完, 關閉畫面就會儲存 (更新) 該筆資料

# 2023/07/06

```
select SATNT_TicketNo, count(*) from SA_TransTemp
GROUP by SATNT_TicketNo
HAVING count(*) > 1
```

![](https://hackmd.io/_uploads/S1hXaoQtn.png)

# 2023/08/11

玉井供應人 的 Code 共6碼數字，其中都是 ``41` 開頭
阿仁想要輸入 1~4 碼數字, 來找

問 ChatGPT 給我 
```
where SAS_Code LIKE '%1264%' AND SAS_Code NOT LIKE '%41264%';
```

我後來微調成
```
if (keyword.length() > 0) {
  String codeKeyword = (keyword.length() <= 4) ? "'41%" + keyword + "%'" : "";
  if (codeKeyword.length() > 0) {
    whereSyntax = whereSyntax + " OR " + AppDataName.SAS_CODE + " LIKE " + codeKeyword;
  }
}
```

SQL syntax 像 ``WHERE SAS_Code LIKE '41%keyword%'``

# 2023/06/27

## Item1

To update the values in a column by adding a specific value to each row, 
you can use an SQL ``UPDATE`` statement with an arithmetic expression. 
Here's an example:

```
UPDATE YourTable
SET YourColumn = YourColumn + 5;
```

In this example, ``YourTable`` is the name of your table, 
and ``YourColumn`` is the name of the column you want to update. 
The ``SET`` clause specifies that the new value of ``YourColumn`` 
will be the current value of ``YourColumn`` plus 5. 
You can replace 5 with any value you want 
to add to the existing values in the column.

If you want to update the column with a different value 
for each row based on some logic or calculation, 
you can use a subquery or join to derive the value for each row. 
Here's an example that adds a different value based on 
a calculation using another column in the same table:

```
UPDATE YourTable
SET YourColumn = YourColumn + AnotherColumn * 2;
```

In this example, ``AnotherColumn`` is another column 
in ``YourTable`` that is used to calculate the new value 
for each row of ``YourColumn``. You can modify 
the calculation according to your specific requirements.

Remember to replace 
``YourTable`` with the actual name of your table and 
``YourColumn`` with the name of the column you want to update.

## Item2

To replace part of a string with another string in a column, 
you can use the SQL ``REPLACE`` function. Here's an example:

```
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, 'old_substring', 'new_substring');
```

In this example, ``YourTable`` is the name of your table, 
and ``YourColumn`` is the name of the column you want to update. 
The ``REPLACE`` function takes three arguments: 
the column name, 
the old substring you want to replace, and 
the new substring you want to replace it with.

For instance, if you have a column named ``Description`` 
and you want to replace the substring "apple" 
with "orange" in that column, the query would look like this:

```
UPDATE YourTable
SET Description = REPLACE(Description, 'apple', 'orange');
```

Make sure to adjust the table and column names to match your specific scenario.

Note that the REPLACE function will replace all occurrences 
of the old substring within each row of the column. 
If you only want to replace specific occurrences or 
perform more complex replacements, 
you may need to use other string manipulation functions 
or regular expressions depending on your database system's capabilities.

### 2023/10/12

```
UPDATE [AMIS_KH].[dbo].[SA_RfidObject] SET [SARO_TagId]='a43c6ee6' WHERE [SARO_RfidObjectId]='SARO_85DJWP0W0';
```

### 2023/10/19

```
UPDATE [AMIS_KH].[dbo].[SA_Vendue] 
SET [SAVD_VendueDate] = '2023/10/20' 
WHERE [SAVD_VendueDate] = '2023/10/18'
```

## Item3

If you want to replace a substring in a column 
based on a start index and end index, 
you can use a combination of string functions to achieve this. 
Here's an example:

```
UPDATE YourTable
SET YourColumn = STUFF(YourColumn, start_index, end_index - start_index + 1, 'new_substring')
WHERE start_index <= LEN(YourColumn) AND end_index >= 1;
```

In this example, ``YourTable`` is the name of your table, 
and ``YourColumn`` is the name of the column you want to update. 
``start_index`` and ``end_index`` represent 
the start and end positions of the substring 
you want to replace (both inclusive). 
Replace 'new_substring' with the desired replacement string.

The ``STUFF`` function is used to 
__replace the specified portion of the string__. 
It takes four arguments: 
the column name, 
the start index of the substring to be replaced, 
the number of characters to be replaced 
(which is calculated by subtracting the start index 
from the end index and adding 1), and 
the new substring.

The ``WHERE`` clause is used to ensure that 
the replacement occurs only when 
the start and end indexes fall within the valid range of the string.

Make sure to adjust the table and column names, 
as well as the start and end indexes, 
according to your specific scenario.

### My exercise

```
UPDATE SA_TicketTemp
SET SATKT_TicketNo = substr(SATKT_TicketNo, 1, 7) || '102' || substr(SATKT_TicketNo, 11)
```

```
UPDATE SA_TransTemp
SET SATNT_TicketNo = substr(SATNT_TicketNo, 1, 7) || '102' || substr(SATNT_TicketNo, 11)
```

```
UPDATE SA_TransTemp
SET SATNT_TransactionNo = substr(SATNT_TransactionNo, 1, 7) || '102' || substr(SATNT_TransactionNo, 11)
```

```
UPDATE SA_TicketTemp
SET SATKT_TicketNo = REPLACE(SATKT_TicketNo, '1120616095', '1120616102');
```

---

#### 2023/10/12

```
SELECT  [SARO_RfidObjectId]
      ,[SARO_CreateDate]
      ,[SARO_CreatorId]
      ,[SARO_ObjectId]
      ,[SARO_TagId]
      ,[SARO_UpdateDate]
      ,[SARO_UpdaterId]
      ,[SARO_Type]
      ,[SARO_Remark]
  FROM [AMIS_KH].[dbo].[SA_RfidObject]
```

```
UPDATE [AMIS_KH].[dbo].[SA_RfidObject] SET [SARO_TagId]='a43c6ee6' WHERE [SARO_RfidObjectId]='SARO_85DJWP0W0';
```

# 2023/06/19

Web 有一種表單是問卷，但為了靈活一點，
有較高權限的使用者，可以到 Dashboard 
1. 建立 多選 + 文字輸入的項目 並保存
2, 然後在建立問卷時，可以把在步驟1建立好像選項來與問題連結, 這樣就可以重覆利用這些多選項
3. 但在後端，要有重覆利用的多選功能，
在設計 Data model 時，
若直覺得 建立 Option, 裡面只有 Primary key(INT), name(String), checked(Boolean),
再用 foreign key 去關聯許多 options，感覺不是一個好的設計！

不知 Data model 若要在這種情況下有效率，學弟有好像建議嗎？
我目前用過往經驗，有想到 把  name(String), checked(Boolean), 
這些放在一個 JSON Array, 中，最後轉成 String 儲到資料庫中



---

## 學弟給的有用 options

### 1. SQL
被 reference 的 table, 其內容通常是不太會變動的
以訂單為例, 被 reference 的 item, customer 都是建立好的, 再被訂單 reference

### 2. No SQL 
不用事先定義 schema
適合 dynamic 應用
但它給的 index 空間有限

### 3. Question, Option
在需求與操作上, Query 會以 Question 相關
例如 畫作、照片的汙損

---

## Item1:

### Q1
about the questionnaire web form 

in a questionnaire, 
I can create a section that consists of one or more qurestion.
For a single question, I can create or re-use a multiple selection module 

to create the multiple selection module
i can add one or more options where each option has a name (type: string), a value (type: boolean)
here i called it a "Multi-selection option" model 

my intuition is to use the SQL data base; 
I can create a table for the multiple selection in which 
a column (type: string) is used to keep the JSON string.
the JSON string represents a JSON array that consists of one or more above-mentioned "Multi-selection option"

do you know the better way to build the created multiple selection 

### A1:

Storing the options for multiple selection in a separate table 
rather than using a JSON string in a single column 
can provide better flexibility and query capabilities. 
Here's an alternative approach:

Create a table for the questionnaire sections:

```
CREATE TABLE QuestionnaireSection (
    Id INT PRIMARY KEY,
    Name VARCHAR(255)
);
```

Create a table for the multiple selection options:

```
CREATE TABLE MultiSelectionOption (
    Id INT PRIMARY KEY,
    SectionId INT,
    Name VARCHAR(255),
    Value BIT,
    FOREIGN KEY (SectionId) REFERENCES QuestionnaireSection(Id)
);
```

When creating a questionnaire section, 
you can insert the relevant options into the ``MultiSelectionOption`` table, 
linking them to the corresponding section using the ``SectionId`` foreign key.
This approach allows you to query and manipulate the options more efficiently. 
You can perform SQL operations, such as filtering, sorting, and joining, 
directly on the ``MultiSelectionOption`` table. 
It also ensures __data integrity through the use of foreign key constraints__.

Using a separate table for options 
provides a structured and normalized data model, 
making it easier to maintain and extend the system in the future. 
It also allows for easier integration with 
other parts of your application 
that may need to interact with these options independently.

While storing the options as a JSON string could work, 
it may limit the capabilities of your database, 
as it cannot easily perform operations 
on individual elements within the JSON array without additional effort. 
Additionally, it may be more challenging 
to enforce data integrity and relationships 
between the questionnaire sections and options.

---

## Item2

### Q2:

the sample you provided above, 
in my understand, 
a "MultiSelectionOption" row is associated with a QuestionnaireSection.
if I want to re-usee the a set of "MultiSelectionOption",
I seems only can re-use the "QuestionnaireSection" 
rather than the set of "MultiSelectionOption"

### A2:

If you want to reuse a set of multiple selection options 
across different questionnaire sections, 
you can __introduce an additional table__ 
to __represent the set of options__ and 
__establish a many-to-many relationship 
between the questionnaire sections and the option sets__. 
Here's an updated approach:


Create a table for the multiple selection option sets:

```
CREATE TABLE OptionSet (
    Id INT PRIMARY KEY,
    Name VARCHAR(255)
);
```

Create a table to associate the option sets with questionnaire sections:

```
CREATE TABLE QuestionnaireSectionOptionSet (
    SectionId INT,
    OptionSetId INT,
    PRIMARY KEY (SectionId, OptionSetId),
    FOREIGN KEY (SectionId) REFERENCES QuestionnaireSection(Id),
    FOREIGN KEY (OptionSetId) REFERENCES OptionSet(Id)
);
```

Create a table for the multiple selection options:
```
CREATE TABLE MultiSelectionOption (
    Id INT PRIMARY KEY,
    OptionSetId INT,
    Name VARCHAR(255),
    Value BIT,
    FOREIGN KEY (OptionSetId) REFERENCES OptionSet(Id)
);
```

With this structure, 
you can associate multiple selection option sets with questionnaire sections, 
and each option set can contain multiple options. 
This allows you to reuse the same set of options across different sections.

To use a specific set of options in a questionnaire section, 
you would create a new entry in the ``QuestionnaireSectionOptionSet`` table, 
associating the section with the desired option set.

This approach provides flexibility by allowing you to reuse option sets 
while maintaining the relationship between the options and the sections. 
It also enables you to query and 
manage the options and their associations efficiently.

Note: You may need to adjust the table and column names 
according to your specific requirements.

---

# 2023/04/20

## Item1

### Q1:
I have a question about the backend system.
for instance, taking the order system as an example.
when a client summit a order form that includes "products' id", "quantity", "customer's id"
to the backend.
what is best practice for a backend to the backend __must check the validity of products and customer
before it creates a new order in an efficient (performance) way__ 

### A1:

There are several ways you can check the validity of the products and customers 
before creating a new order in an efficient way. Here are some best practices:

1. Check the product and customer IDs against your database: 
Before creating a new order, 
you can check the validity of the product and customer IDs against your database. 
This can be done by querying your database to ensure that the IDs exist and are valid.
2. Use __database constraints__: 
You can use database constraints to ensure that 
the product and customer IDs are valid. For example, 
you can create foreign key constraints that 
reference the product and customer tables to ensure that the IDs exist in those tables.
3. __Use caching__: 
Caching can be used to improve the performance of your backend system. 
You can cache the product and customer data to reduce 
the number of database queries required to check their validity.
4. Validate input data: 
Before creating a new order, you can validate the input data 
to ensure that it is in the correct format and meets the required criteria. 
This can help to prevent invalid data from being inserted into the database.
5. __Use indexing__: 
You can use indexing to improve the performance of your database queries. 
For example, you can create an index on the product and customer ID columns 
to speed up queries that check their validity.

By implementing these best practices, you can ensure that 
your backend system efficiently checks the validity of products and customers 
before creating a new order.

---

## Item2
### Q2:
Does the MS SQL provide the database constraints feature?
or .Net entity framework support using the database constraints feature?

### A2:
Yes, MS SQL Server provides support for database constraints, 
and the .NET Entity Framework can use these constraints to help ensure data integrity.

MS SQL Server supports various types of constraints, 
such as primary key, foreign key, unique, and check constraints. 
These constraints can be added to a table using SQL statements, 
such as ALTER TABLE, or through the use of the SQL Server Management Studio (SSMS) graphical user interface.

In the .NET Entity Framework, 
you can __define constraints using data annotations or the Fluent API__. 
For example, you can use the __``Required`` attribute to specify that 
a property must have a value__, or the __``MaxLength`` attribute 
to specify the maximum length of a string property__. 
The Fluent API can be used to define more complex constraints, 
such as foreign key relationships.

When you use the .NET Entity Framework with a SQL Server database 
that has constraints defined, 
the framework will automatically create the corresponding constraints 
in the database based on your data model. 
This can help to ensure data integrity and consistency between your application and the database.

Overall, the combination of MS SQL Server and the .NET Entity Framework 
provides powerful features for defining and enforcing database constraints, 
helping to ensure the reliability and consistency of your data.

---

## Item3

### Q3:
could you give a code snippet for the order example?

### A3:
Sure, here's an example of using database constraints with 
the .NET Entity Framework to create an Order entity with references to a Customer and Product entity:

```
public class Order
{
    public int Id { get; set; }
    public int Quantity { get; set; }
    
    // Foreign key for customer
    public int CustomerId { get; set; }
    // Navigation property for customer
    public virtual Customer Customer { get; set; }
    
    // Foreign key for product
    public int ProductId { get; set; }
    // Navigation property for product
    public virtual Product Product { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    // Navigation property for orders
    public virtual ICollection<Order> Orders { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    // Navigation property for orders
    public virtual ICollection<Order> Orders { get; set; }
}

public class OrderDbContext : DbContext
{
    public DbSet<Order> Orders { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Product> Products { get; set; }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        // Configure the order entity
        modelBuilder.Entity<Order>()
            .HasKey(o => o.Id)
            .Property(o => o.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        
        modelBuilder.Entity<Order>()
            .HasRequired(o => o.Customer)
            .WithMany(c => c.Orders)
            .HasForeignKey(o => o.CustomerId);
        
        modelBuilder.Entity<Order>()
            .HasRequired(o => o.Product)
            .WithMany(p => p.Orders)
            .HasForeignKey(o => o.ProductId);
        
        // Configure the customer entity
        modelBuilder.Entity<Customer>()
            .HasKey(c => c.Id)
            .Property(c => c.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        
        modelBuilder.Entity<Customer>()
            .Property(c => c.Name)
            .IsRequired()
            .HasMaxLength(50);
        
        // Configure the product entity
        modelBuilder.Entity<Product>()
            .HasKey(p => p.Id)
            .Property(p => p.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        
        modelBuilder.Entity<Product>()
            .Property(p => p.Name)
            .IsRequired()
            .HasMaxLength(50);
    }
}
```

In this example, __the ``Order`` entity has foreign keys for the ``Customer`` and ``Product`` entities, 
and "navigation" properties to access them__. 
The ``Customer`` and ``Product`` entities each have a collection of ``Order`` entities.

The ``OrderDbContext`` class defines the database context and 
the corresponding ``DbSet`` properties for the entities. 
The ``OnModelCreating`` method is used to __configure the entities and their relationships__, 
including __defining the foreign key constraints using the ``HasRequired`` and ``WithMany`` methods__.

With this setup, when you add a new ``Order`` entity to the ``Orders DbSet``, 
the Entity Framework will __ensure that the CustomerId and ProductId properties 
refer to valid Customer and Product entities, respectively__, 
based on the defined foreign key constraints. If an invalid ID is used, an exception will be thrown.

---

## Item 4

### Q4:
if I don't use the foreign key constraints, 
the backend needs to query the product and customer to retrive data
and then use the queried data to create a new order.
in such way, the backend need additional database access on querying data.
was I right?

### A4:
Yes, if you don't use foreign key constraints, 
then you would need to query the database to retrieve the relevant customer and product data 
in order to create a new order. This would require additional database access 
and potentially result in slower performance.

__When using foreign key constraints, 
the database can enforce referential integrity and ensure that 
the customer and product IDs used in the order creation request 
correspond to valid records in the respective tables__. 
This can help to __reduce the likelihood of data inconsistencies and errors__, 
and can also help to __improve performance by reducing the need 
for additional database queries to retrieve related data__.

Overall, using foreign key constraints can be a good practice 
to help ensure data integrity, reduce the likelihood of errors, 
and potentially improve performance, especially when dealing with 
complex relationships between entities in a database.


