Checking if the table is present in the database - c#

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

Related

Insert one table data into another table

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

Connecting Three SQL Server Tables Together to Display Images

I am trying to create a Photo Album/Collection for each student in my ASP.NET website, using SQL Server tables.
Here are my table structure:
I want to store the image name within the Student_Images folder. Then each image will be linked to a Photo Collection. Each Photo Collection is linked to a group.
Then I want to display each group, any photo collections which are associated with that group, and then any images associated with that collection.
I have been able to display images related to each student, but I don't know how to add the 'Photo Collection' table between the Students and Images tables.
Here is how I would like it to display:
Here is how the tables would look populated:
Can anyone advise me as to how to go about making this connection?
There's an idea of a foreign key - where a row in a table refers to a row in another table. You definitely want to do this for a number of reasons, to ensure data integrity and to document your relationships for whoever works on the site.
You don't quite have enough information to get where you want to get. You don't have any linkage between photo albums and the images they contain. You'll need another table for that.
I kinda recommend that you use column names that don't repeat the table name. You'll find you can then see patterns more clearly in your schema. For example Photo_Collection.Photo_Collection_Id should probably be just Photo_Collection.Id. It's more concise, and it's obvious which Id you're talking about because you always use the table name when referring to that column.
So, to get you nearer to where you need to be, I'd recommend something like this:
create table dbo.Students
(
ID
int not null identity( 1, 1 )
constraint [Students.ID.PrimaryKey]
primary key clustered,
Name
nvarchar( 50 ) --> not very generous :-)
)
go
create index [Students.Name.Index] on dbo.Students( Name ) --> maybe unique?
go
create table dbo.Student_Images
(
ID
int not null identity( 1, 1 )
constraint [Student_Images.ID.PrimaryKey]
primary key clustered,
Student_ID
int not null
constraint [Student_Images.to.Student]
foreign key references dbo.Students( ID )
Filename
nvarchar( 250 ) null, --> null? really? each image should have a unique file name, dont you think?
Description
nvarchar( 250 ) null
)
go
create index [Student_Images.to.Students.Index] on dbo.Student_Images( Student_ID )
go
create table dbo.Photo_Collection
(
ID
int not null identity( 1, 1 )
constraint [Photo_Collection.ID.PrimaryKey]
primary key clustered,
Name
nvarchar( 250 ) null --> null? hmmm...could be hard to use
)
go
create index [Photo_Collection.Name.Index] on dbo.Photo_Collection( Name ) --> consider unique??
go
create table dbo.Photo_Collection_Images
(
Photo_Collection_ID
int not null
constraint [Photo_Collection_Images.to.Photo_Collection]
foreign key references dbo.Photo_Collection( ID ),
Student_Image_ID
int not null
constraint [Photo_Collection_Images.to.Student_Images]
foreign key references dbo.Student_Images( ID )
)
You didn't really describe your groups...and there are lots of questions still unanswered. For example, are Photo_Collections something that a student makes? If so, there should probably be a Student_ID in Photo_Collection with a foreign key to Students.
The table I added called Photo_Collection_Images relates your photo collections to the images. You'd include this table in any query that needs to display all the images in a given photo collection. I think that's the main missing bit for you. You'd do something similar for groups.
Also - FYI, pasting images of your text is kinda aggravating. Consider just pasting the text in and indenting it 4 spaces to get it to format correctly.
Edit:
To select, for example, all the images in a photo collection by student, you could do:
select
pc.Name PhotoCollectionName,
si.FileName FileName,
si.Description FileDescription,
s.Name StudentName
from
dbo.Photo_Collection pc
inner join
dbo.Photo_Collection_Images pci
on
pc.Id = pci.Photo_Collection_ID
inner join
dbo.Student_Image si
on
pci.Student_Image_ID = si.ID
inner join
dbo.Students s
on
si.Student_Id = s.ID
This is pretty much the base query for everything.
Make some changes in your Photo_Collection table, create an FK within your Photo_Collection of Student_ID, then you'll be able to show the photo collection of all the students because through Student_ID, you can have access to student table as well as Student_Images.
Hope it helps.

What is the best database design for storing multiple subjects?

Recently I'm working on a project, in which I need save multiples subjects in my database, and numbers of subject will be change as per student's class. e.g: class "VII" has 6 subject (included 1 option subject), class "VIII" has 8 subjects (1 included option subject) and so on. Even the option subject can be different according to student's choice.
I'm using CheckedListBox control. Here is a screenshot.
I'm using MS Sql Server 2012. I'm facing difficulty to design database table. I can't understand should i create all the subject columns in my student table or ????(best way). Please help me, thanks in advance.
What you are describing is something that is called a Many to Many relationship. Many Classes have Many students.
You will have a students table, a subjects table and then a table student_subject table which links both the primary keys of the students and subjects table.
Reference: Many to Many
If a Student can have multiple Subjects, and a Subject can have multiple Students, then that's a many-to-many relationship. Which means there would need to be an intermediary table to link them.
Start with the tables for the objects themselves:
Student
----------
ID
Name
etc.
and
Subject
----------
ID
Name
etc.
Since these are independent aggregate root objects in the domain and neither one owns another one, their tables just contain information which describes that object. A Subject, for example, could have a name and a description, maybe a list of pre-requisites, etc.
Then you create the relationship table between them. A common practice is to just combine the names:
StudentSubject
----------
StudentID
SubjectID
If no two pairs of Student and Subject can be repeated, then the two foreign keys together can make the primary key. If they can be repeated (if the same Student can have the same Subject more than once) then you'd probably want to add a separate primary key to the table.
At its simplest, all this table does is create the relationships between the records in the main tables. However, this can potentially grow to be a domain entity in and of itself. If there are any attributes which describe the relationship between a Student and a Subject, those would be added to this table.
Your database schema should look something like this......
Students
Student_ID (PK)
Student_Name
etc .......
Subject
Subject_ID (PK)
Fee
etc......
Student_Subjects
Student_ID FK Students(Student_ID)
Subject_ID FK Subject(Subject_ID)
DateStarted
etc.....

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

ASP.NET C# problem with Table and SQL logic

Hey I have 3 tables called STUDENT, COURSE and ENROLLMENT. STUDENT has a PK of S_ID(student ID) and COURSE has PK of C_ID(course ID). In enrollment it only has S_ID and C_ID. I have an object data source to show all the students name (in text, and S_ID as the value) in a drop down menu and it will show which courses he is registered in when clicked, using a datagrid and another object data source. I wont to have the student to have multiple courses to be registered too, but I cant do that because you cannot have the same ID in the COURSE table, so every student is only registered to one course.
Is there some sort of option to have same ID's in a table?
If not, then I must some how manipulate the string in C_ID in the COURSE table because all the courses start with ISDVXXX or ITSXXXX or HGFXXXX. This may be hard to understand but hopefully someone will help.
An example may help
So if a student named Joe with a S_ID of 123 is registered to ISDV, he will be registered to all the courses that start with ISDV. But my problem is that my COURSE table has to have unique ID for each course such as ISDV123, ISDV346, ISDV395 etc... so this also ruins my enrollment table because I cannot simply have ISDV in there, it needs a specific course but he is registered to all of them. Any more clarification will be given :P Thanks...
What you're trying to solve is a multi-valued attribute problem. Basically, you have two tables where one (students) has a primary key which is a foreign key in another table (classes). You don't want to have multiples of the same class in the classes table, but you do want a student to be able to have multiple classes.
So, there is a very simple fix, you create another table which contains at least these two columns: student_id and class_id. This way, you could have a single class that multiple students are linked to, and also multiple classes to which a student can be linked to.
What you are looking for is a many to many relationship - i.e. a single student can have multiple courses, and a single course can have multiple students. So your link table (is this what you intended enrollment for?) should have two columns, one for the course ID and one for the student ID.
So if you had student 123 and student 234, and courses ABC and XYZ, your table would look something like:
S_ID C_ID
123 ABC
123 XYZ
234 ABC
Now, for your PK on enrollments you could either use a composite key, or add a unique integer RowId (Identity or HiLo algorithm).
In that case, your enrollment table would look something like this:
S_ID C_ID RowID
123 ABC 1
123 XYZ 2
234 ABC 3
Then, to see what classes a student was in, you could do something like
Select * from courses c
inner join enrollments e
on c.C_ID = e.C_ID
AND e.S_ID = #StudentId
Your Enrollment table stands for the m:n relationship between the Studend and Course tables.
For instance your student with S_ID = 69 is Enrolled to the Courses with C_ID = ISDVXXX , C_ID = ITSXXXX, C_ID = HGFXXXX
A Student can be Enrolled to more Courses and more Students can be Enrolled to the same Course, that's why you have your Enrollment table. In our example, the Enrollment will have the following rows:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX).
If later a student with the S_ID = 96 joins to the Course with C_ID = ISDVXXX, the following will be the new rows of the Enrollment table:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX), (96, ISDVXXX).
The important thing to understand here is that each row in the Enrollment table stands for a Student Enrolled to a Course and there is no need to other fields than the ID of the Student and the ID of the Course, for these two fields together identify an Enrollment.

Categories