Dynamic or Static column? - mysql

I have some tables in databases. The table contains more than 150 columns for some custom field operations which may/may not be used by all. Instead of creating static 150 unused column fields, it can be created dynamically using add column.
Can somebody justify, which one is better? When to use dynamic, when static and why?

You can look for below normalization for maintaining custom fields, I have used in multiple web/window application successfully.
FormMasterTable
FormId, Name: this will be identifier and will be passed from Form to DB query to identify itself
datatypeMaster Table
datatypeId, datatypeName
The abobe will define all custom fields you are going to support i.e. customer can create to customize a form.
Table for Form-Customer-CustomFieldMapping say Custom Field Master Table for managing column definition and mapping with customer.
customerId, FormName/FormId, FieldName, customFieldId, datatypeId(FK:datatypeMaster), length ....
Next step is to create all tables to store all datatypes defined in datatypeMaster table i.e. one table for each datatype, some of the examples I can think of....
Custom field detail table for numeric say CustomNumberFields
customFieldId FieldValue(numeric(max))
Custom field detail table for string say CustomTextFields
customFieldId FieldValue(varchar(max))
Custom field detail table for Dates say CustomDateFields
customFieldId FieldValue(datetime)
and so on....
Now you can use all in one query with Inner Join to get all custom columns of a customer.
Sample Query to get customfields fields
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomNumberFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomTextFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomDateFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>

Related

splitting values and compare with tables using SQL Server Join

I have two tables called Components and PDF_MSDS. The Component table contains columns f_chem_name, f_component_id and f_chem_name and it contains below sample data.
Components table
SELECT F_Cas_Number, F_Component_Id, F_Chem_Name
FROM Components
WHERE F_Chem_Name = 'CHMNM_17816'
OUTPUT
F_Cas_Number F_Component_Id F_Chem_Name
-------------------------------------------
CAS_5861 PRD1000826 CHMNM_17816
Sample data of PDF_MSDS table
F_PRODUCT F_CAS_NUMBERS F_COMPONENT_IDS
----------------------------------------------------------------------
360 CAS_5779¿CAS_5861¿CAS_2614¿ 3E000685¿3E002268¿3E004960¿3E005217¿PRD1000826¿
Now I want display the f_product value from the PDF_MSDS table by comparing F_Cas_Number and F_Component_Id in Components table with F_CAS_NUMBERS and F_COMPONENT_IDS in the PDF_MSDS table for the given f_chem_name in Components table.
But I am not able compare directly using join with F_Cas_Number in Components table with F_CAS_NUMBERS in PDF_MSDS table and also F_Component_Id in Components table with F_COMPONENT_IDS in PDF_MSDS because multiple cas_numbers and component_ids are stored in F_CAS_NUMBERS and F_COMPONENT_IDS columns in PDF_MSDS table.
How can I search and compare cas_numbers and component_ids and select particular record.Please help.F_Chem_Name is input parameter for Procedure.
You can use LIKE in your JOIN conditions.
JOIN TableB
ON TableA.Column LIKE '%'+TableB.Column+'%'

mysql query using column values as parameter in query phpMyAdmin

I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.

Merging two tables in Access?

I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;

Database design: How to query multiple to multiple table

I have a system used for exhibition in Website.
The exhibition data maybe from multi-data in difference table.
Like this design:
Table [ExhibitionType] used for differentiate category.
Table [ExhibitionBase] used for link extra Sub-Type table & data in different table.
My website use MySQL and PHP
Now question is I DON'T KNOW how to query it. or this design have a flaw.
EDIT 1:
I'm sorry I didn't express my intentions.
Example: I hope query a data list. first i need filter ExhibitionType like 1.
So i need create a SQL like :SELECT * FROM ExhibitionBase WHERE ExhibitionTypeId = 1
And second step i must read ExhibitionDataType and ExhibitionDataId each row data. And according to the different types of ExhibitionData data out to merge into the ExhibitionBase Sub-tables.
HOW TO DO use SQL query it.
This my question.
SELECT b.*, dt.Name, di.name
FROM ExhibitionBase AS b
JOIN ExhibitionDataType AS dt ON dt.id = b.ExhibitionDataType
JOIN ExhibitionDataId AS di ON di.id = b.ExhibitionDataId
WHERE ExhibitionTypeId = 1
Style suggestions:
Do not prefix column and table names with the database name; it clutters things. ("Exhibition")
Do not have a column name the same as its table name.

SQL Join for exporting member details

I have two tables that have member data in them, the reason for this is that I am using Expression Engine and by default it has a standard member data table that includes Email etc. To add custom fields to my register I had to create a secondary table with additional information. I now need to do an export of all member details in a specific order.
Could anyone help me, I have an email table field from the exp_members table that I need to join to the exp_member_data table. I would like the email field added as the third field in the join, this is for an export of member details. I hope this is clear, thanks.
I don't know which fields you want to select and what there names are, but basic structure is as below:
SELECT
exp_members.field_so_and_so,
exp_members.field_so_and_so,
exp_members.email,
...,
exp_members_data.field_so_and_so,
...
FROM exp_members
INNER JOIN exp_members_data ON exp_members.email = exp_members_data.email
ORDER BY exp_members.field_so_and_so, ...