Should I create a view or another table? - c#

Firstly, as a disclaimer, I'm learning a little about SQL/LINQ indirectly through C#. I'm very green in both really. I've quickly grown tired of using sample databases full of data and doing queries set in the most simple of situations. The information is presented this way because the focus is on C# and visual programming and not the hairies of SQL or even LINQ for that matter.
Even so, I decided to convert a simple application I wrote using text (CSV) files for storage. The application keeps track of three possible payables where 0 - 3 records will exist for a given date. I was able to create the database and separate tables for each contract and build an application that inserts the existing records into the database using LINQ to SQL classes.
Now I have another application that is used to add entries via a contract calculator or directly through a BindingSourceNavigator and DataGridView. Each table has in common four columns - Date, GrossPay, TaxAmount, and NetPay, Date being the primary key. I'd like to view the records by date where I have TotalGross, TotalTax, TotalNet, and a column for the GrossPay each contract table for that date.
What would correct approach to this - a view, LINQ query, separate table, or other? It seems a table would be the "easiest", at least in terms of my ability, but seems like an unnecessary copying of records. I tried to "link" the tables but no other column is guaranteed to be unique or primary.
Any suggestions would be great.
Clarification:
Three tables have the format:
| Date | GrossPay | TaxAmount | NetPay | ...each have others not in common... |
** Each table has specific data used to calculate the common columns based on contract type
I would like to view all records "grouped" by date such that each are represented like:
| Date | TotalGross | TotalTax | TotalNet | Table1Gross | Table2Gross | Table3Gross |
** "Total" columns are sums of the respective columns of all records sharing the date.
** One or two of the "Table(n)Gross" may be zero

I think you are asking if you can select records from three different tables by date for the columns they have in common?
If so, you need to do a union.
In your case it may look something like this in SQL. note that I have made a dummy column to denote the source of the record (which you may want)
SELECT Date, GrossPay, TaxAmount, NetPay, 'Table1' as Source FROM Table1
UNION
SELECT Date, GrossPay, TaxAmount, NetPay, 'Table2' as Source FROM Table2
UNION
SELECT Date, GrossPay, TaxAmount, NetPay, 'Table3' as Source FROM Table3
WHERE Date = '2013-05-05'
I wouldn't bother with a view and definitely don't replicate your data with a seperate table.

Related

Difference of master-details tables by a table with more data fields

As far as i know when you want to separate related data you have to use master-detail tables.
In a situation I have a "Statute" table which store clauses of a statute and their footnotes of each clause.
Table Fields:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Statute_ID | Statute_Text | CanEdit | CanDelete | FootNote_Sort | ParentClause | Parent_Sort +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
You can see that all data (Clauses and thier Notes) Stores in a table. In a C# project the project manager says that this will be better than master-details tables but i believe that separating Notes form Clauses would be better.
Could anyone told me that which scenario will be better? and what is the benefit?
Thank you
Master detail tables are used where you have one to many relationship between the data.
For Example, In business applications, usually the transactions have a master record which holds the date and serial number of the transaction while it has a detail record which can hold a number of entries with each detail having a reference to the master(usually made via Foreign Key relation between master table and detail table in SQL).
In your case you have a single entry about the statue, i.e. Clauses and their Notes will be same for a single statue so you won't be needing a master detail relation

Oracle : How to immediately get the first results from slow queries?

I'm writing a C# application that should run an Oracle-Select query and perform some calculations for each line.
The select query is very big and takes a long time.
In the current application design, I should wait until the query finishes retrieving all the data from the database in order to start the required computations on each row.
I was wondering if there is a way to get the first query results as the database engine find them.
Means that : Instead of waiting for the database engine to find all the rows that correspond to my query and return them, get the result since the first row found by the database engine.
At the end the computation required for each line will start as long as the first line found in the database and hence the total run time will be less.
The idea here is not about how to speed up an Oracle query or adding any index. It's more about getting overlapping computations to optimize more the computations.
Sorry if it's a dump question and thank you in advance.
I'm using Oracle 11g and the Query may just be as simple as (but returns hundreds of thousands of rows)
Select * from Table Where Condition1;
I run the explain plan for my query :
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 251 | 122K| 656K (1)| 00:07:40 |
|* 1 | TABLE ACCESS FULL| TABLE1 | 251 | 122K| 656K (1)| 00:07:40 |
-----------------------------------------------------------------------------
Oracle has an all rows strategy and a first rows strategy.
Usually, Oracle will, when possible, do a first rows strategy when possible. The simplest example of that would be something like:
select * from emp;
Here, there is no join, there's no sorting, etc, so, Oracle will begin to return rows immediately, as it reads through the EMP table.
On the other hand, this is a simple example of an all rows strategy:
select * from emp order by surname;
Here, we're asking for sort on SURNAME, so, we cannot begin to immediately return results. The table must be read in its entirety, and then sorted, before we can return the first row.
There are other factors as well. If you're joining tables, a NESTED LOOPS join will execute with a first rows strategy, whereas a HASH JOIN will (necessarily) employ an all rows strategy.
Ultimately, which is better, which you will want, is going to be dependent on your application. If you're doing stuff that the user directly interacts with, you'll probably want first rows, to not keep the user waiting. For batch jobs, all rows is (probably) better.
Finally, the optimizer can be influenced with the ALL_ROWS and FIRST_ROWS_n hints.

Qualifying bulk copy table mappings MSSQL

I was wondering if there was a way to qualify the table mappings when using sqlbulkcopy in c#?
Currently, I have a table that contains Stock Codes and then columns associated with range of weekly bucks.
example:
Stock Code | 11-2013 | 12-2013| 13-2013 | 14-2013 etc etc.
I have a query that returns quantities for the given stock code and the week number in which they occurred.
example:
part a | 20 | 11-2013
part b | 10 | 14-2013
Ideally, there would be a way to set the columnmappings.add method and specify that I would like to map the date column of the table to the resulting date in the return row of the query. I would show what I have; however, I have no idea if this is even possible. Any suggestions or alternative ideas would be great.
Thanks
Not directly possible. Your source data has to match to your destination data. The SqlBulkCopy class isn't going to do that for you.
Create a sql query from your source data that matches the table schema of your destination table. Then you can use the SqlBulkCopy class.

C# filtered read of several tables with multiple relations

I have a quite large database with several tables and complex relations and thousands of records. Just to simplify the problem, say I have got a MySQL database as follows:
[table1]
id
table2_id
table3_id
name_id
[table2]
id
name_id
[table3]
id
name_id
[name]
id
text
Then there are relations:
table1.name_id -> name.id
table1.table2_id -> table2.id
table1.table3_id -> table3.id
table2.name_id -> name.id
table3.name_id -> name.id
Tables contain:
50,000 records in table1
10,000 records in table2
5,000 records in table3
25,000 records in name
I need to display table1 in a DataGridView showing in columns text names.
I need a filter to be applied to every table1 request where I can include a name search keyword for each table: table1, table2, table3.
I have been trying manually fill DataSet using DataAdapters and adding DataRelations and found some problems along:
it takes ages and a lot of memory to get DataSet filled with all the data. Obviously I do not need everything at once, I would be happy if I could limit table1 to 5,000 records with a filter applied
considering DataAdapters and DataSets, I could not manage to set WHERE conditions to child rows in order to retrieve filtered table1 data - for example: I want to get a list of table1 where table2 name text = "a" and table3 text = "b"
I would not really want to build 'SELECT ... LEFT JOIN' or 'SELECT .... table1, table2, table3' queries as like I mentioned before, I deal with more than 6 tables with nested relations (eg. a related table is in relation with another one etc.)
I would not want to retrieve all table1 records and then filter them at C# side as like I said before - it takes a long time and there is no need to display all records on the screen. All records MUST be considered only in terms of filtering records then result can be limited to 5,000 items.
I understand using DataSet and DataAdapters may be wrong in this scenario. Any other ideas are very welcome and I am sure will be helpful as well. THANK YOU.
Could someone please point me in the right direction?

Fetching column data from some table

I got these 3 tables
Ques Table
Ques |Ans |QuesID|LangID
__________________________________________
Ques1|Ques1's Answer|1 |1
Ques2|Ques2's Answer|2 |2
LangID is a Foreign Key that References LangID in table Languages
Languages Table
LangID |Description|LanguageID
______________________________________
1 |French |1
2 |English |2
Language Table is for website's contents' language
EditorLanguage table
LanguageID|Value|Desc
______________________
1 |fr-FR|French
2 |en-US|English US
EditorLanguage Table is for RadEditor's Language
When a new Ques is added:-
The InsertUpdateQues Stored Procedure inserts the following values in Ques Table:-
QuesID, Ques, Ans, LangID
Now on my Ques Page, I have the following:-
-A drop down list box to select language (this drop down is bound to EditorLanguage Table's Desc column)
-A Text Box for Ques
-Telerik RadEditor for Ques's Answer
- Submit cancel buttons
Now lets say the langauage selected from the drop down is French whose language ID=1, Value=fr-FR, desc=French
Now how do I pass this LangID value to InsertUpdateQues Stored proceedure. I mean how it should get this value?
I am confused because of this Foreign Key thing..new too ms sql
How will the logic be formed? What is even going on here ?
so ok it will get LangID somehow but how and what good will that do ? I am suddenly feeling lost.
Why are Languages and EditorLanguage separate tables? Based on what you're showing us, it looks like they would be better as a single table. The table names and key names are unintuitive as well.
As it stands right now, you're going to have to translate the LanguageID that the form is posting into a LangID that the Ques table needs. This can be done in code by hitting the database (or a cached lookup) to select LangID from Languages where LanguageID equals the value you have. Or you can do this inside of the stored procedure you're calling (so it's only a single trip to the database) by either selecting the value into a variable and using the variable in your insert/update, or by using a subquery in your insert/update to select the value.
Note that this may get clouded if there's any kind of one-to-many relationship between Languages and EditorLanguage.

Categories

Resources