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.
Related
I'm working with Entity Framework Core (6.0.7) on a legacy database, that isn't under my control, so I can't change any of the database structure unfortunately.
The database structure that I've inherited has quite a few tables in the following format. (this isn't the actual structure, I've simplified it for this question)
id int (pk)
name varchar
item_model varchar
item_id int
...
It's probably irrelevant but I'm using MySQL.
Where the item_model contains the name of a table, and the item_id contains the id of the primary key for that table. So the item_id can point to many different tables.
Although I don't like this pattern, it is used throughout the DB, and in some cases, with only one or two distinct item_models.
Some example data might be
id name item_model item_id
1 Item1 ships 5
2 Item2 cruises 2
3 Item3 ships 3
...
I would prefer the table to look like... as I would then be able to define the foreign keys correctly, and allow the navigation field to work correctly.
id name ship_id cruise_id
1 Item1 5 null
2 Item2 null 2
3 Item3 3 null
...
But as I mentioned before, I can't change the DB structure
How can I tell EF that if item_model=ships that the item_id refers to a ship_ip on the ships table?
I was thinking of creating a virtual field on the model which has either a c# or SQL case statement on it, but I can't see how I can do this. i.e. something like case when item_model='ships' then item_id else null end as ship_id
I guess the bottom line is that I want to keep the logic as simple as possible for anybody utilising the model/repository, and just being able to specify tab.Cruises.CruiseLaunchDate rather that having to add logic to each query in the repository.
ps. I'm relatively new to EF, so please forgive me if I've missed something obvious.
I have this situation:
I have two tables:
Table A
Staging_Table A
Both tables contain those common columns:
Code
Description
Into Table A I also have a column Version which identifies the last version of corresponding column Code.
My problem is how to update the column Version once a new Description is stored for the same Code (I fill up the Staging_Table with a bulk Insert from C#. I have a flow of data that change once a week).
I need to insert the new row into Table A which contain the same Code, but a different Description, without deleting the old one.
I insert the rows from Staging table to table A with MINUS operation and I have this mechanism within a stored procedure because I also fill up the staging table with a Bulk Insert from C#.
The result I need to obtain is the following:
TABLE A:
Id Code Description Version End_date
-- ----------------- ------- --------
1 8585 Red Car 1 26-mag-2015
2 8585 Red Car RRRR 2 01-giu-2015
How can I do that?
I hope the issue is clear
If I understand correctly process work like that:
1. Data is loaded to staging table Staging_table_A
2. Data is inserted from Staging_table_A itno Table_A with additional column version.
I would do:
with cnt as (select count(*) c, code from Table_A group by code)
Insert into Table_A (select sta.*, nvl(cnt.c,0) + 1 as version
from Staging_table_A sta left outer join cnt on (sta.code = cnt.code));
This is based on condition that in Table_A versions contains no duplicates.
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
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.
I'm putting data from website (json) to sql base.
In db i have these rows.
ID | PostId | Name | Message
Id is auto-increment primary key.
PostId also has a unique values.
Name and Message are nothing special.
When I run my script / click on the button in form / ... , the program saves all the values into database (lets say there are 25).
Next time I'm going to press the button there will be added 25 more records (all duplicates), and so on...
Is there a way that the program can check through 'PostIds' if the value already exists before adding it to the db?
Thanks
Another way is to use merge statement, this can also update the duplicate rows if you like and is easier than useing an if statement
if not exists(select * from table where PostId =#PostId)
Begin
//add
End
You have many option
Simple one is that you ask Your data base about that PostId
SELECT count(PostId) FROM Table where PostId = #PostId;