Database Search that Compares Results for Mutiple Seach Keywords - sql

Fist, let me say I know very little about SQL language and am trying to learn (albeit very slowly). I have created a database table with columns for
ECOREGION_ID
ECOREGION_NAME
SPECIES_NAME
CLASS
so that there is one row for each species name in each ecoregion. My end goal is to create a form in which I can enter in multiple species names and search for the ecoregions they share. For example, if I enter into the 4 different search boxes "Tiger", "Red Panda", "Sloth Bear", and "Rhino" it would bring up a list of all the Ecoregions in which these four species share. I am wondering a few things:
Is my data set up in the correct way in order to do this or is there a more efficient way to set i t up?
What statement should I use to create an sql statement to perform the search I want?
What is the technical term for what I am wanting to do? I have tried many different searches on different forums and can't seem to find what I am looking for, mostly because I probably don't know what to search, lol.
Thanks,
-Drew

You have ECORegion_ID and ECORegion_Name in the same table. I would suggest create a separate table to hold ECORegions. This table would have both an ID and Name. The search table would then only have the ECORegion_ID. This process is called normalization. It basically reduces redundant data in your database.
You are looking for a SELECT statement, which is used to pull data out of one or more tables. The statement has a WHERE option to restrict which rows you bring back and an IN expression as part of the WHERE to allow you to look for multiple keywords.
Search for Normalization to see why to put region name in a separate table. Look up SQL Select to get syntax for the select statement you should get off to a good start

Related

Create SQL String in Microsoft Access Dynamically

I apologize if this is an easy one, but I can't find this on the web anywhere!
I have a list of tables and queries, and a full list of fields from each table and query.
I want to choose my fields in an Access form, and then, on another form, choose a query/table in one column (along with a field) and join it to a table/query in another column (along with the field.) My form for the joins would look like this:
Object1 Field1 Object2 Field2
and so on. I would want to be able to choose my fields through combo boxes, THEN make the SQL string dynamically. I can't use a where clause -- it has to be a join.
The problem is, the structure will change every time. Access gets funny about putting in parentheses. Also, if an object is chosen more than once, Access will want to join it (assuming I do it right) in a different manner than T-SQL.
Is there a way to write a query in T-SQL, and quickly convert to Access? Does anyone know the FROM clause algorithm Access uses to construct the FROM clause? I am stuck here, and I cannot find a solution to save my soul! Thank you in advance, David

Access 2010 Database Clenup

I have problems with my records within my database, so I have a template with about 260,000 records and for each record they have 3 identification columns to determine what time period the record is from and location: one for year, one for month, and one for region. Then the information for identifying the specific item is TagName, and Description. The Problem I am having is when someone entered data into this database they entered different description for the same device, I know this because the tag name is the same. Can I write code that will go through the data base find the items with the same tag name and use one of the descriptions to replace the ones that are different to have a more uniform database. Also some devices do not have tag names so we would want to avoid the "" Case.
Also moving forward into the future I have added more columns to the database to allow for more information to be retrieved, is there a way that I can back fill the data to older records once I know that they have the same tag name and Description once the database is cleaned up? Thanks in advance for the information it is much appreciated.
I assume that this will have to be done with VBA of some sort to modify records by looking for the first record with that description and using a variable to assign that description to all the other items with the same tag name? I just am not sure of the correct VBA syntax to go about this. I assume a similar method would be used for the backfilling process?
Your question is rather broad and multifaceted, so I'll answer key parts in steps:
The Problem I am having is when someone entered data into this
database they entered different description for the same device, I
know this because the tag name is the same.
While you could fix up those inconsistencies easily enough with a bit of SQL code, it would be better to avoid those inconsistencies being possible in the first place:
Create a new table, let's call it 'Tags', with TagName and TagDescription fields, and with TagName set as the primary key. Ensure both fields have their Required setting to True and Allow Zero Length to False.
Populate this new table with all possible tags - you can do this with a one-off 'append query' in Access jargon (INSERT INTO statement in SQL).
Delete the tag description column from the main table.
Go into the Relationships view and add a one-to-many relation between the two tables, linking the TagName field in the main table to the TagName field in the Tags table.
As required, create a query that aggregates data from the two tables.
Also some devices do not have tag names so we would want to avoid the
"" Case.
In Access, the concept of an empty string ("") is different from the concept of a true blank or 'null'. As such, it would be a good idea to replace all empty strings (if there are any) with nulls -
UPDATE MyTable SET TagName = Null WHERE TagName = '';
You can then set the TagName field's Allow Zero Length property to False in the table designer.
Also moving forward into the future I have added more columns to the
database to allow for more information to be retrieved
Think less in terms of more columns than more tables.
I assume that this will have to be done with VBA of some sort to modify records
Either VBA, SQL, or the Access query designers (which create SQL code behind the scenes). In terms of being able to crunch through data the quickest, SQL is best, though pure VBA (and in particular, using the DAO object library) can be easier to understand and follow.

How to check if parts of a string match to a column data in sql table

I want to write a query which checks the input provided by the user with the column data of my table. i tried using LIKE and CONTAINS but it didnt work for me. eg my table data contains "Bar B Q Tonight" and if user enters "BBQ Tonight", how to deal with it? I have enabled full text-indexing for my table too. I am working on sql server 2005 in visual studio. can anybody guide me?
One approach to deal with is to create separate column on your table which will contain popular search tags or keywords, based on that you can show the results to the user.
Synonyms and acronyms are a challenge for information retrieval systems (IR).
Perhaps implement a tiny Acronym Expansion System. Acronyms and expanded terms.
When a user enters such words you need to provide a few suggestions or expanded terms. Like in Google do suggestions or you can show couple of similar terms in return for the query and let the user decide which is relevant.
you need minimum three table as I envisage it but depends on context.
one table contains stop words so you parse each word in the
entered phrase and remove all stop words.
then search individual term in acronyms table and if an
acronym has more than one definitions you can substitute acronym
for each definition and pass it back to user asking which is
relevant. if this is web environment it means list of hyperlinks.

Searching in PL/SQL /Oracle Forms

This is with respect to search of a text in a table
Table_Name:
Details
Columns:
Fname,Mname,Lname,NName
This table contains nearly one lakh records
We are using Oracle forms for some querying option
The user input one name the form searches the table for the name and based on the name either(Fname/Mname/Lname/NName) in which column its is present further actions are proceeeded.
The search is taking a long time since we have huge amount of data present in the table.
I tried with Functional indexes for the table but t did not work its also taking more time
Later i tried with something like this
concatenated all the names into one name and put it into a cursor.
Using the cursor output i tried with Instring but it is hanging
I also tried with searching for building a dynamic cursor but it did not work.
My database is Oracle
Can u help me to out to find an effective solution or please help me if i have missed something.
Thanks
First of all, 1 lakh (100,000) records is not in itself a large table.
The problem I can see is the query appears to be doing an OR against the Fname/Mname/Lname/NName columns.
This means the query will be doing at least one full-table scan to obtain the results.
You may wish to use debug to obtain the query it is firing against the database and attempt to tune this at the SQL prompt using auto trace.
You may need to clarify if the search is also doing something like a LIKE against these columns rather than an EQUALS. As a LIKE will impact the query further and affect indexes.
Certainly the use of INSTR will disable indexes on your searched column.
It is not clear if what your block is based on ie. table, view, query, procedure
You may want to try using the hint on the block properties of the form FIRST_ROWS.

SQL Split functions and imported data

Ok, I've got a database table where data gets dumped by this horrid little program that I despise, but can't change at the moment. It has merchant data in there, names, addresses, and a set of categories that are pipe-delimited. What I need is a clean way to split these out, so I have one row for each merchant/category pair. From there, I can easily get it into the new data structure. This will need to be a repeatable process for a short period of time. I realize the optimal solution is to rid myself of this structure, but I've wracked my brain trying to figure out how to do this cleanly in sql.
I already have a function in the database that will split a delimited string and return a table.
This is in sql server 2008, btw.
Edit (for clarity_
Basically, the following might be a merchant (with the categories attached - other fields redacted for simplicity. Using commas for field delimiters here).
Jimbo's Bait Shoppe, Bait|Sports Gear|Sandwiches
What I need is:
Jimbo's Bait Shoppe, Bait
Jimbo's Bait Shoppe, Sports Gear
Jimbo's Bait Shoppe, Sandwiches
If you have already written a function that splits the string and returns the table you can use a trigger.
Create a trigger on INSERT on the table where the "horrid" program spits the data. The trigger will then take the unformatted data and populate two clean tables (I think in your case you should have two tables: one is a merchant, and another one is products, that are linked using one-to-many relationship using MerchantID).
In this case you can use the table with unformatted data as a "dirty" table. You can cleanse straight after the "horrid" program imported a file.
Please comment if you need help with the triggers