decompose source data into new, many-to-many schema - sql

I am using MS Access 2010 to do some transformations of data. Specifically, I need to create the data structure for a many-to-many relationship between concept (summarized by rxnconso.rxcui) and word (summarized by drugwords.id. Note that each value of drugwords.id needs to correspond with a unique value of name from the words table in the images below.). To accomplish this, I need to create two tables, drugwords and drugwordsConsoJunction, and also decompose the contents of an existing table words into the drugwords and drugwordsConsoJunction tables. The structure of the destination tables is:
drugwords table: (this table needs to be created)
id (autonumber pk needs to be created from distinct values of words.name)
name
drugwordsConsoJunction: (this table needs to be created)
word_id (fk to drugwords.id)
rxcui (fk to rxnconso.rxcui)
rxnconso (this table already exists):
rxcui
...other fields
The source table for this transformation is called words and has two columns; a value for rxcui, and a value for name. As you can see from the images below, there can be many name values for a given rxcui value. And the second image below shows that there can be many rxcui values for a given name value.
How do I write the SQL to transform words into drugwords and drugwordsConsoJunction, as per the above specifications?
I have uploaded a copy of the database to a file sharing site. You can download it at this link.

If the proposed [drugwords] table is already going to have unique values in its [name] column then you don't need an AutoNumber ID column, you can just use the [name] field as a Primary Key. In that case, the table that maps "words" to the corresponding [rxcui] values could be created by simply doing
SELECT DISTINCT rxcui, [name] INTO drugwordsConsoJunction FROM words
Then you can use the "words" themselves instead of introducing another layer of mapping from (distinct) "words" to (distinct) "IDs".

Related

SQL Referencing a particular table chosen from a row value in another table

I've got a database called SimpleCredentials in which there is a table called dbo.Properties which has (PK) UserID and then some other attributes like Name, Date of Birth etc. There is another Primary Key attribute called ExtendedCredentials which is a string dbo.UserID. So, for example the user with UserID = S-1-5-21-2177 will have the string dbo.S15212177 in their ExtendedCredentials column.
I've got another database called ExtendedCredentials. For every User there is a unique table in that database. Using the previous example, there will be a table called dbo.S15212177.
So, if I have 100 users there will be 100 rows in the dbo.Properties table in the SimpleCredentials database, and there will be 100 unique tables incorporating their UserID in the ExtendedCredentials database.
I want to create an entity relationship diagram, and eventually a MS SQL schema, but how do I represent the multiplicity of dbo.UserIDs and their relationship to their dbo.UserID string attribute in dbo.Properties?
Am I getting something fundamentally wrong here?
You may ask why I don't have a single database called ExtendedProperties with a single table in which each row is the UserID PK and the various extended properties are contained in columns. The simple answer is that some properties are themselves tables. Not every user has the same attributes in those tables. And I can't know ahead of time (a priori) what the full set of user extended property attributes is. So each user gets a table of their own.
Is there a better way to do this?

Passing a record value as an input to pull data from another table whose name contains that input value

I have a master table, fc_master that contains a column called fc_id. This column has values such as fc_id1,fc_id2 and so on.
I also have corresponding detail tables for each of these fc_ids in different categories, namely
category_1_fc_id1, category_2_fc_id1, category_1_fc_id2,
category_2_fc_id2.
This brings my total number of tables to 5, i.e fc_master, category_1_fc_id1, category_2_fc_id1, category_1_fc_id2, category_2_fc_id2.
I want to generate an Oracle SQL query that would allow you me provide a single fc_id as an input to automatically pull from the appropriate fc_id tables. How can I achieve this?
Please simplify/correct your table names...why use fc_id1 at one place
and fc_id_1 at another place? Can you use fc_id1 in both places? same
for 2.
Try to keep table names and column names independent of the data in
fc_id and category_id so that code is static instead of dynamic SQL.
Please redesign to use fc_master (single master) with fc_id and
fc_detail (single detail) with fc_id and category_id. Repeat the
primary keys of master table in detail table so that a single detail
table is sufficient instead of 4.
Optional - Use Partitioning as needed on fc_id (primary) as well as
category_id (secondary) if data is too large.

How do I move a field to a different table and maintain relationships?

I figure this has to be easy, I'm just not sure how to ask the question.
I have thousands of records I imported from a Excel Spreadsheet in a Microsoft Access table with a field that I want to extract into a new table. How do I move the data from the field in the existing table to a new table and maintain the relationships to the record?
The goal is to move the existing data from the one field into a new table that will have a one-to-many relationship with the existing parent table.
For example, in a table called tblProperties I have the following fields:
Property_Address | Property_Owner | UtilityMeter_Number
I want to maintain a history of utility meters on properties as they are replaced, so I want to move the UtilityMeter_Number field from tblProperties into a new table called tblMeters and create a one-many relationship between the two so I can have multiple meter records for each property record.
How do I move all the existing data from the UtilityMeter_Number field in tblProperties into tblMeters, and maintain the relationship?
What is what I'm trying to do called, and how do I do it?
This is called normalizing data structure.
Use a SELECT DISTINCT query to create unique records. Use that dataset as source to create a new table. Something like:
SELECT DISTINCT CustID, LName, FName, MName INTO Customers FROM Orders;
Now delete unnecessary LName, FName, MName fields from Orders table.
Tables are related on the common CustID fields. An autonumber primary key is not utilized. If you do want a relationship on autonumber PK, then continue with following steps:
add an autonumber field in new table
create a number field in original table
run an UPDATE action SQL to populate new number field with autonumber value from new table - join tables on common CustID fields
also delete CustID field from original table

Trying to avoid polymorphic association in a schema for dynamic fields

I want to create a dynamic fields system. The idea is that the owner will be able to create dynamic fields for let's say, the customers of his company. The problem is that with the database structure that I came up with, requires the use of polymorphic association.
My structure is the following:
The fields table that consists of the following columns:
ID, FieldName, FieldType (The field type can be avoided, probably)
The field value tables (There are multiple value tables, one for every data type of the dynamic fields ex. A table to store the values that are DATETIMES, a table that stores the values that are DECIMALS and so on.).These tables have identical structure but with a different data type for their value column! They consist of the following columns:
ID, FieldID, CustomerID, FieldValue
Now, in order to get the field value I have to do a bunch of LEFT JOINs between the Value Tables and the Fields Table and keep only the value column that its value is not NULL, since that only one value column if any will have a value! Of course this isn't efficient at all and I am trying to avoid it. Any suggestions even if they require a completely different database structure at all are welcome. I am also using MySQL along with EntityFrameworkCore.

How to refer to a specific table column from an other DB record?

I want to define extra properties for the columns in the tables of my MYSQL database.
For example I want to define the measurement units of the columns of my database.
To do that I currently plan to create this table :
COLUMN_UNIT
id
{table_name, column_name} unique constraint
unit_id
(I am not using a composite PK because my framework required an auto incremented ID field)
The other tables that I could have are :
UNIT
id
unit_name
WEATHER_DATA
temperature
wind_speed
...
SENSOR_DATA
intensity
frequency
...
Is using the column name and the table name the best way to refer to other column?
Thanks!
UPDATE :
In my case all the records in a column will have the same property value. It is a column wise property. So for the temperature, all the values will be in degree C for example.
you need to use multiple foreign keys to the UNIT table.. for example, your WEATHER_DATA table could be
WEATHER_DATA
temperature
temperature_unit_id (FK to UNIT)
wind_speed
wind_unit_id (FK to UNIT)
i don't know if your framework allows multiple foreign keys on the same table... If it doesn't, i guess that you could create another table containing the name of the table, the nane of the column and the unit_id.. but that would be =S ugly
Hope it helps
EDIT:
you could use a meta table that are just tables that store extra information of your table.. mysql uses his own metatables to store the number of rows, the column types, etc. Some frameworks use their own meta tables (like Django).. But you could create your own meta tables,they are just like any other table, in your case it could be something like
WEATHER_META
temperature_unit_id FK
wind_unit_id FK
and the WEATHER table would be
WEATHER_DATA
id
temperature
wind_speed
...
Or if you want to, you could create a single Meta table for all your tables. Something like
META_TABLES
table_name
column_name
unit_id