Insert one table data into another table - c#

In my database currently, have 2 tables with data called student and subject.
In my web application have a new requirement, so I have to add additional mapping table called StudentSubject. However now I need to insert data to that table(StudentSubject) as follows. How can I write a script to achieve it? And may I know should I need to write Pre-Deployment or Post-Deployment Script to do this? If yes how can I do it?

Simple by cross Join.
INSERT INTO StudentSubject
SELECT StudentId, SubjectId
FROM Student, Subject
--OR
INSERT INTO StudentSubject
SELECT StudentId, SubjectId
FROM Student
CROSS JOIN Subject

Related

Entity Framework with table join to another database with Code First

I have an EF code first application. I have a column PersonId that I need to join with another database (warehouse) table Persons which contains all the people in our organization.
I have read that one solution is to use a view then make my entity model from the view but this seems quite painful if we have a PersonId column in many tables.
How best to return back all rows from table A joined with a table B from another database on key column PersonId, really to return PersonDisplayName from table B?
There has got to be a best practice for this situation?

Checking if the table is present in the database

I am designing a feedback page for my college which collects feedback from 60 students
about their faculty. I also want to hide the details of the student who provides the
feedback. So the code will be creating table of each students at the run time. I'm stuck at
this point. I am not able find the logic on how the table is created during run time and
what will be the table names and how it will differentiate with one another?
Having one table per student would be really impractical, use 1 table for every student instead with a different studentID per student.
Say you want to have the student's last and surname stored you could use this table structure:
CREATE TABLE Students (
StudentID INT(11) NOT NULL AUTO_INCREMENT,
lastname TEXT NOT NULL,
surname TEXT NOT NULL,
PRIMARY KEY (StudentID)
)
then to add students you could INSERT them:
INSERT INTO Students VALUES("mylastname", "mysurname")
As for how to interact with a MySQL database using C# check the documentation on the MySQLDataClient:
http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL
http://dev.mysql.com/downloads/connector/net

Multiple users but storing data in same tables

Hi I am new to databases in general, so forgive my noob question below, but I really do need help.
I have designed a database with 4 tables. I have created an application in C# which will store some values in these 4 tables during the course of the application running. However, this is all working fine when there is only one user of the application, but if more that one user is going to use the same application running in an ASP.NET page, then they will be accessing and altering the data in the 4 tables, and problems will soon arise.
My question is, how do I prevent this from happening? I want each user to have their own unique username and then use that to differentiate them in the tables but my knowledge of databases is limited to know how to achieve this. Please help?
Supposing you have the following table today:
FavoriteFood
—————————————
FoodId
FoodName
And it lists all of your favorite foods. But then you decide you'll let me use your database to store my favorite foods too. Since you don't care about my favorite foods, and I don't care about yours, you need a way of keeping them separate. First, you'll create a User table:
User
—————————
UserId
UserName
FirstName
LastName
Then, you need to relate the User table to the FavoriteFood table. One way of doing this would be to add a Foreign Key to the FavoriteFood table. Give it a new field called UserId:
FavoriteFood
—————————————
FoodId
UserId
FoodName
Then you can get just the food for a single user by adding a WHERE clause to your SQL code:
SELECT FoodName
FROM FavoriteFood
WHERE UserId = #UserId
That could be ok, but I'm not satisfied with it. This database is not normalized! Suppose, you later want to store calorie information about your foods. You add a field called calories to your FavoriteFoods table. As you are populating that field with data, you notice that you are putting in the same data multiple times. All of your users like bananas, so you have as many entries in your table for bananas as you have users. You have to enter the exact same calorie information over and over again. Instead, you should have all the information for a food in the table just once, and use a completely separate table to map food to users:
Food
—————————
FoodId
FoodName
Calories
FavoriteFood
—————————————
FoodId
UserId
Use a join to get the favorite food for a user:
SELECT f.FoodName
,f.Caloires
FROM Food f
JOIN FavoriteFood a ON a.FoodId = f.FoodId
WHERE a.UserId = #UserId

SQL: Joining tables on primary key AND returning the primary key

I'm writing a C# application and am using an Access .mdb. I have a table with email messages and a table with message relations (each email msg can be assigned to several teams of workers), so the rows in the relations table have "msgId" and "teamName" fields.
I want to to get all messages from the first table which are assigned to a specified team. I'm using the following query:
"SELECT * FROM Mails INNER JOIN MailAssignments ON Mails.msgId = MailAssignments.msgId"
But it doesn't return the msgId for me, I guess, because the tables are joined on this field, but then I
m not able to identify messages in my C# code.
How can I make the query return the msgId for me?
It is enough specify the fields name in the selection, or add the table name where you want to get all the fields, try with this selection list :
SELECT Mails.*
FROM Mails INNER JOIN MailAssignments
ON Mails.msgId = MailAssignments.msgId
It should appear twice in the resultset, for Mails.msgId and MailAssignments.msgId respectively. However, you should not expect two columns named msgId. Rather, the DBMS should disambiguate the columns. This is a requirement of Standards SQL, BTW.
IIRC Access will rename both columns in order to disambiguate them. This would explain why there is no column named msgId in the result.

inserting multiple data into different tables using linq to entities?

I'm using AdventureWorks sample database in a project. I can display the Customer's (being various stores) information using vStorewithDemographics. It's a view retrieving different pieces of data from different tables in database.
I want my program to insert a new customer into the database using linq to entities and I'm not sure how to go about this. Inserting into the view just gives me errors, which I thought would happen because it's not a table.
Any way to go about this?
yes it is possible follow following steps
--> Create a instead of trigger and using it you can perform it
Eg, I have two table customer and customerContacts latter contain the phone number of customer and a view name customer details will bring upon all the details of customer as below
create view [dbo].[CustomerDetails] as
select c.*,cc.PhoneNumber from customer c inner join customerContact cc on c.CustomerId = cc.CustomerId
For inserting in have create a instead of trigger
Create TRIGGER trgInsteadOfUpdate ON dbo.CustomerDetails
INSTEAD OF INSERT
AS
Declare #Id int
-- Insert into Customer
INSERT INTO customer SELECT CustomerName,CustomerAddress,State,Country FROM inserted
Set #Id = SCOPE_IDENTITY()
-- Insert into CustomerContact
INSERT INTO customerContact SELECT PhoneNumber,#Id FROm inserted
GO
When for inserting in the linq i can user any of the following two ways
//Insert into view
var newCusContact = new CustomerDetail{ Country="India", CustomerAddress="bbb", CustomerName="Tested", PhoneNumber="7879654", State="Delhi" };
db.CustomerDetails.InsertOnSubmit(newCusContact);
db.SubmitChanges();
OR
string insertStatement = "insert into CustomerDetails(CustomerName,CustomerAddress,State,Country,PhoneNumber) values('DummyValue','DummyValue','Delhi','India','123459')";
db.ExecuteQuery(insertStatement);
db.SubmitChanges();
Hope this helps you

Categories