In my attendance table I want to update a record with S_ID and C_ID from StudentCourse bridge table but its not updating.
https://imgur.com/a/7ZBItur
Its working when I use it to select and display 2 columns from the StudentCourse table and 1 column from the Attendance table but it doesn't work when I use it to update the attendance table which for now is empty
UPDATE Attendance
SET S_ID = sc.S_ID,
C_ID = sc.C_ID
FROM Attendance a
left outer join StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2
There are two actually four tables:
Student(S_ID(primary key))
Course(C_ID(Primary key))
StudentCourse((S_ID,C_ID(Foreign keys))Bridge table)
and attendance table with (S_ID,C_ID(Foreign keys))
What I am doing is displaying data on datagridview by joining tables and selecting S_ID,S_Name,C_ID and Pre_Abs (Attendance table column)columns from all these tables.
Now, I want to insert the info present in datagridview to the attendance table when I click on button.
I have done this already with simple insert query to attendance table by using datagrdview.rows[i].cell[2] property.
I want to know if there is any better idea to do this so that I can use JOIN instead of using datagridview property with for loop.
For now my attendance table is empty while Student, Course, and StudentCourse tables are filled with the data.
What I want is to display record(S_ID, C_ID) from studentCourse table and (Pre_Abs) from Attendance table and when I submit the attendance; I want it to store Pre_abs record against each S_ID, C_ID in the attendance table.
I don't think I can explain it any further.
You have declared alias for the table Attendance. So you should use alias reference before the column name as the same column name also available in other table. Can you please try this-
UPDATE a
SET a.S_ID = sc.S_ID,
a.C_ID = sc.C_ID
FROM Attendance a
LEFT OUTER JOIN StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2
I have also doubt about you where condition as you are Update table Attendance but filtering records on table StudentCourse. use of wrong filter can resultant in update all rows in table Attendance. Please be careful.
Hmmm. I do not understand the reason why your Attendance table exists in the first place. Your StudentCourse table already seems to contain info about which students are having which courses. I'll just assume the Attendance table has to deal with registering student attendance for each single lecture/lesson within a course.
Anyway, as #Psi already commented, you seem to initially want an INSERT query to create a record in the Attendance table with data based on information in the StudentCourse table. I guess that you are thinking about it too hard. ;-) You may try something like this:
INSERT INTO Attendance (S_ID, C_ID, Pre_Abs)
SELECT S_ID, C_ID, Pre_Abs
FROM StudentCourse
WHERE S_ID = 2 AND C_ID = 2
However, it is currently unclear how field Pre_Abs should be filled... You try to get it from table Attendance, but that would seem to be invalid.
Also, when creating INSERT-queries, make sure that the source data values (in the SELECT or VALUES clause) are in the same order as the target fields (in the INSERT clause). If the field/value order doesn't match, the data gets mixed up (if the query does not fail due to data type errors)!
And last but not least: if both Attendance and StudentCourse tables share the same composite key (S_ID, C_ID), you should use that full key when joining related records of those tables:
FROM
Attendance AS A
LEFT JOIN StudentCourse AS SC ON
SC.S_ID = A.S_ID AND
SC.C_ID = A.C_ID
Hope this helps a little...
Related
I have student table in SQL Server.
In my web application I have textBox, where I can input a date (dd-mm-yyyy). This date means that any(application know which exactly) student was absent this day.
How to save this date in SQL Server?
There is no problem when student is absent just one day per all life, because I can create one additional column in my student table and save there date of absent.
But I don't know how many days student will absent. I can add thousand columns to my student table and write in there dates of absents, but it's very bad solution.
So, how to save dates of absence in SQL?
I wrote my web application in ASP.NET, C#, SQL Server.
You need to have another table to keep track of the dates the student was absent.
Say your current table is as follows:
Student
-------
StudentId [PK]
...
So you would now create another table as:
StudentAbsent
-------
StudentAbsentId [PK]
StudentId [FK, Student.StudentId]
AbsentDate
To get the dates the student with id=5 was absent you'd do something like the following in SQL
SELECT AbsentDate FROM StudentAbsent
WHERE StudentId = 5
Oh and BTW you'd want to read more on relationships. If it's a 1-1 relationship one row of table1 is related to one row of table2.
If it's a 1-n relationship, one row of table1 is related to many rows of table2 (as is the case above)
If it's a n-n relationship, one row of table1 is related to many rows of table2 and vice-versa.
You have to create another table called absents with three columns:
id (primary index and auto_increment)
student_id (should not be unique)
date
The id column is just the id of the absent (it's a good practice to have id for every row on a table). The student_id is a reference to id column of students table, identifying the correct student. And the date column is the date of absent.
Another good practice is to create relationship internally and set triggers to actions like delete (what should you do if the student is deleted?).
I have two tables..
Students
StudentID
Name
SchoolID
School
SchoolID
SchoolName.
This is my scenario, I have created a DetailsView on my page which displays X StudentID and its columns. When displaying the SchoolID (from Students table) column it will display the actual ID as its supposed to.
I would like to know how I can display the actual school name instead of the schoolID, what I did first was simply have a query inside the first query which displays the name of the school like...
SELECT StudentID, (SELECT SchoolName FROM Schools
WHERE Schools.SchoolID = Students.SchoolID)
FROM Students WHERE StudentID=1
Although this works this creates issues when trying to edit the record via GridView, it will not save the correct data in those fields.
In the edit step in GridView I'm able to bind the SchoolID key with SchoolName, however this will NOT work when having a query within a query.
So my question really is how can I display foreign key data within Visual Studio without changing the SQL command?
The solution previously was to edit the Select command (after the SQLDatasource was created) to perform a inner join on the table containing the meaningful field name.
Check Here
1.Link
2.Link
You could do that with join query, example:
select students.StudentId,students.Name,students.SchoolID,school.SchoolName from students join school on students.SchoolID=school.SchoolID
And, for better understanding of "JOINS" here is useful link: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
I'm trying to merge tables from different databases, ServerDB and ClientDB and save to ClientDB where the client's tables hold the master's tables records plus any records the user might add. On the other hand server tables could also be updated (new records inserted).
The database has relationships. The column in TableA I want to INSERT INTO values is a ForeignKey linking to TableB's PrimaryKey which is an auto-incremental column.
I'm saving all records from both databases in a merged dataset, and due to database design restrictions, I need to clear both tables on the client before inserting the merged tables from the dataset.
I first update TableB (the one with the PrimaryKey auto-increment column), but now the values of this column have nothing to do with the ForeignKey on TableA, so I update TableA and temporary inserting in the ForeignKey column the value of the first record of TableB's PK. Now I need to update TableA foreignKey column with the correct values from TableA PK column. Theres also a third column on each table that have the same values.
What the syntax of the sql statement should be? If I don't make much sense let me know and I'll post a better description.
It was a confussing question but i think you are talking about inserting values from table 1 to table 2 where table 1 value is equal to table 2 values the sql query for this operation is
INSERT INTO emp (empno,ename)
SELECT t2.deptno,
t2.dname
FROM dept t2
LEFT JOIN emp t1
ON t2.deptno = t1.deptno
in this query table 1 (emp) will insert 2 values into columns(empno and ename) from
table 2 (dept) and join is on (deptno) which is present in both tables.
You can further ask if this was not helpful.
Thank you all for your help. After struggling for a while with the "OledbException Operation must use an updateable query" I found out a solution in a similar topic: SQL Update woes in MS Access - Operation must use an updateable query
Thats the query that did the trick:
UPDATE DISTINCTROW PlaylistsSongs
INNER JOIN PlaylistsNames ON PlaylistNames.PlaylistName = PlaylistsSongs.PlaylistName
SET PlaylistID = PlaylistNames.ID
Hello I am making a reservation system and I am having troubles with my table can someone help me with my problem.
I have a table customer and a table reservation.
My customer table handles all the customers personal info and the reservation handles the reservation details.
My customer table has a customer ID which is a primary key and auto increment. and
my reservation table has a reservation ID which is a primary key and auto increment.
MY PROBLEM IS... How could I connect the two?
I have a big problem on how to join them in my select statement because I dont know what values will I join.
*Note: Btw, I'm using c# winform and I seperated the add customer and add reservation details. I am also wondering if I can include the 2 insert statement in one button or add them seperately..
Use the SQL JOIN statement in your SELECT QUERY:
SELECT c.CustomerName, r.ReservationTime
FROM reservation r
JOIN customer c ON r.CustomerId = c.CustomerId
Edit: It sounds like you are having trouble understanding how to get a newly created CustomerId in order to include it in the Reservation table. The key here is LAST_INSERT_ID(). After your insert into Customer, get the value from LAST_INSERT_ID() - that is the newly created CustomerId. You can then use that value when you insert into Reservation. Eg:
INSERT INTO Customer (CustomerName)
VALUES ('Joe Schmoe');
INSERT INTO Reservation (ReservationTime, CustomerId)
VALUES ('2011-09-12 18:30:00', LAST_INSERT_ID());
Pardon syntax errors - SQL Server is my primary language, if you hadn't gathered that already.
Well, your reservation table should have a customer ID as well.
As I understand you, that's not the case yet.
But it should be like that, because every reservation belongs to a customer.
Then, you can join both tables on the customer ID.
I have a local report that I created in my application. It displays the contents of a table in my access database called "History". One of the columns is "ID" and it refers to the primary key in another table "Employees".
My report shows the employee's ID when it is refreshed but of course I want it to display the employee's name instead.
In short, I need to be able to perform a lookup on another table in my rldc file. How can I go about that?
I assume you fetch data from the database through a dataadapter and a typed dataset. something like this?
TestDataSetTableAdapters.CategoryTableAdapter ca = new TestDataSetTableAdapters.CategoryTableAdapter();
this.ds1 = new TestDataSet();
ca.Fill(this.ds1.Category);
Then you go to the dataset and modify the query in your tableadapter to something like this
select h.*,e.EmployeeName from history h
inner join Employees e on e.ID = h.UserID
Then the datset will be modified to include the column EmployeeName on all rows in the History table. Then the column "employeename" is directly available in yyour report
Here is an example of how to join your employee and history tables with SQL. My Access is rusty, so the syntax might not be perfect, but this should demonstrate the concept. Obviously, I made up the column names, so you will have to change the columns to whatever you need.
SELECT EmployeeID,
EmployeeName,
TimeIn,
TimeOut
FROM Employee Emp
INNER JOIN History History
ON Emp.EmployeeID = History.EmployeeID