Update TAG Relational table from select match on main table with keywords field and tag table with individual keywords - sql

I imported the Software PAD File Database from http://paddatabase.net/download.html into Microsoft Access in a table called main:
MAIN
-----
ID
ProgramName
Keywords
.
.
I created two new tables: Tags and TagSoftwareRel.
Tags
--------
ID
Tag
TagSoftwareRel
--------------
ID
SoftwareID <- (MainTable)
TagID <- tags table
I extracted all the keywords from the field Keywords as individual words in the Tags table. They Keywords field from Main looks like this:
Keywords
Paul, animated, moving monk, paulaner
Image,Imaging,Graphics,Rotate,Resize,Effects,
Sharpen,Blur,JPEG,TIFF,BMP,WBMP,PSD,GIF,PDF,Format,ICM,ICC,CMYK,
thumbnail,Convert,Display,AJAX,AVI,red-eye removal,lossless JPEG transform
correction, rich,internet,applications,ebooks,webmaster,authoring,
What I want to do is create a SQL Query which Inserts the tagID from the tags table into tagsoftwarerel.tagid and the related softwareID from the main table into tagsoftwarerel.softwareid by using a where tags.tag like main.keywords
I'm sort of at loss of where to start with this.
As it is a public DB I can provide a copy of the database to anyone interested.
Any assistance would be most appreciated. Thank you.

I assume that the field ID from the TagSoftwareRel is an autovalue or identity. That means the value is created automaticly by inserting a new row:
I understand that you already filled the Tags-Table.
Here is a query which would fill the TagSoftarerel-Table:
Insert into TagSoftarerel (SoftwareID, TagID)
Select m.Id,
(Select T.TagId from Tag T where T.Tag = m.Keyword) as TagId
from MAIN m
Advice:
I think you could find a better solution. The Tag-information is redundant in your solution.
If you would just add the Tag.Id to the main-table you could get rid of the Keyword column and store the tag-information solely in the tag table, where it belongs to.

Related

How to modify languageid column in a SQLite FTS table?

In SQLite FTS tables there is a hidden languageid column that I want to make use of: I need to populate my FTS4 table with rows in two different languages, which I will then distinguish by languageid column.
I create my table with a command like this:
CREATE VIRTUAL TABLE `texts` USING FTS4(`text`, tokenize=unicode61, languageid=`lid`)
Now how can I INSERT a row with a specified languageid if it is a hidden column? Or is there some other way to specify the language used in a row?
So, I had to explicitly specify the languageid column like this (here lid is the name of languageid column):
INSERT INTO `texts` (`text`,`lid`) VALUES (?,?)
Sidenote: I used Python in IntelliJ IDEA for this and the IDE gave me a Unable to resolve column 'lid' error, but the code still worked.

SQL return rows if matches keywords

I have a large list of strings (outside of a PostgreSQL database), and I want to go through that list of strings (in a loop) and want to check to see if any part of the string belongs in a table.
For example, I have the two following tables:
table name: trigger_keyword
id (int)
keyword (text)
and table 2:
table name: trigger_message
id int
message text
trigger_keyword_id int /*this is a fk to id on trigger_keyword */
Let's assume we have key word "weather good" in the trigger_keyword table, and we have a message in trigger_message linked to the id of that keyword.
so below is an example of how things look in our tables
trigger_keyword table
id keyword
-----------------------------------------------------
1 weather good
trigger_message table
id message trigger_keyword_id
-----------------------------------------------------
1 yes, the weather is good 1
in one of our strings outside of the SQL database we have the following sentence
"is the weather good in Alaska?"
What SQL can I write to return "yes, the weather is good" because "weather good" is in our trigger_keyword table? do I need to use LIKE for this? I only need help with the SQL portion.
That sounds like you should use full text search:
SELECT m.message
FROM trigger_keyword AS k
JOIN trigger_message AS m ON k.id = m.trigger_keyword_id
WHERE to_tsvector('english', 'is the weather good in Alaska?')
## phraseto_tsquery('english', k.keyword);

Add 1 or more email with append structure

We have a table that is filled by a program with a web service. This table has customer data like custno, name tele1, tele2, addr etc. They want to add emails but they do not know how many fields each customer has. They told me that we can handle this by adding an Append Structure to the table. I look in the internet but I can't find a way to do this with Append Structure.
What they want is to have 1 record for Elias with all his data and under this to put his 3 email.
Can we do it with Append Structure and how.
Thanks in advance
PS. This is an important correction. This is not a DB table but it is a structure. This Structure ws_customer is used as IMPORT in order to pass the customer data for creating or updating the sap customer. So they asked from me to add an APPEND STRUCTURE and add dynamically as much emails as the customer has. For example the customer ELIAS with CUSTNO 145 ADDRESS BROWN 6 has emails asimof#gmail.com, asimof#hol.gr and asimof#greece.gr. The Append Structure will have 1 field and for the record of this customer we will have 3 emails.
Is it possible to do this?
why don't you add a table of email adresses to this structure? in my oppinion, you could do it just the way JozsefSzikszai described it.
When you have your structure, you can append a table to this structure and then you can always add as much email adresses to this table as you want
a example could look like this:
TYPES BEGIN OF your_structure,
field1 TYPE string,
field2 TYPE string,
email TYPE STANDARD TABLE OF string,
END OF your_structure.
when you then have your variable of the type your_structure, you can add email adresses like this:
DATA your_variable TYPE your_structure.
INSERT 'email#adress.com' INTO TABLE your_variable-email.
When your_structure is a DDIC structure and not defined by code, you could append the table to the DDIC structure and use it just the same way

How to create a table from the attributes of another table that contain a key word?

I have a SQL table called SCUBA_CLASSES with attributes ID and NAME. In the table are different kinds of scuba diving classes from beginner to advanced levels. There is a second table called REQUIRED_BOOKS that has the attributes ID and TITLE that contain all the books required by different scuba classes.
I'd like to create a new table called INTRO_REQUIREMENTS with an attribute ID. It will contain a list of all the books required by scuba classes whose name starts with the word "Introduction". How would you create this?
So far I have:
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID;
I can generate a list of classes with required books but can't figure out how to add the requirement that the name of the class has to start with "Introduction".
add one more condition in where clause using using AND and Search name using LIKE.
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID
AND name LIKE "introduction%";

increase Ids in table

I would like to increase all ids in my table by 1000 cause I need to insert there data from other table with excactly the same ids. What is the best way to do that?
update dbo.table set id = id + 1000
go
The best way to go is to not do that. You have to change all related records as well and if you are using identities it gets even more complicated. If you do anything wrong you will seriousl mess up your data integrity. I would suggest that the data you want to insert is the data that needs to have the values changed and if you need to relate back to the data in another tbale, store the original ID in a new field in the table called something like table2id or database2id. If you can't change the existing table, then you can use a lookup table that has both the old id value and the new one.
Under no circumstances should you attempt something of this nature without taking a backup first.
First as HLGEM it seems to be a bad id (think about your foreign keys on id's you must add 1000 to them to).
Second dbo.table has become sys.tables in Server 2008.
Finally you'll need to find the foreign keys columns with this request :
SELECT name,OBJECT_NAME(object_id)
FROM sys.columns
WHERE name like '%id' or name like 'id%'
--depends on where is 'id' in your columns names
name : the column name, OBJECT_NAME : the table name
And update the whole thing (with a tricky request that should looks like this one, but i didn't test with the "update" command) :
CREATE TABLE #TablesWithIds (
columnName varchar(100),
tableName varchar(100)
)
Insert into #TablesWithIds
SELECT name as columnName,OBJECT_NAME(object_id) as tableName
FROM sys.columns
WHERE name like '%id%'
update #TablesWithIds.tableName set #TablesWithIds.columnName = #TablesWithIds.columnName +1000
drop table #TablesWithIds