I am working with Access database for a C# winform application.I want to create software, which stores the patient's details.I have assigned a patient_Id for each patient, Patient_Id is of "AutoNumber" datatype so that it will automatically increment. My problem is when same patient comes again then New Patient_Id automatically generated to store his new current data, but for same patient I want only one ID.
Use some unique data as a field in your patient table (such as social security) and when a patient comes, look up this unique number and determine if he is a new patient or a returning one. This is a simple SELECT query something like:
"SELECT patient_Id FROM tPatients WHERE SSN=#sNum"
Related
We have a table Products{{ProductID(PK) ,name, Description} } which store name of products
Table Products_Manufacturing {ProductID(FK) ,A,B,C,D,E} stores fields value of multiple products ,But not all product have all columns.
For example
Product_A have {A,B,C}
Product_B have {A,D,E}
we are making a one table with same field like Products_Manufacturing [ProductID(FK) ,A,B,C,D,E]
Table(ProductsFieldSelection) {ProductID(FK),A,B,C,D,E}
ProductID is foriegn key (unique)[Products]
When we create new product then system will shows
all fields of table(ProductsFieldSelection) fields and user will selects
required fields per product. System will saves these field name in ProductsFieldSelection table .
A,B,C,D,E ==>dataType[bool]
Add new product Product_A
Name ==(varchar)
Description==(varchar)
A(bool) ==true
B(bool) ==true
C(bool) ==true
D(bool) ==false
E(bool) ==false
In this situation ,i want design a database system which remember fields of product . Is it correct correct way to handle this situation or any other idea ??
I am developing a windows application, where multiple users can uses the same database.
My problem is when insert a new record to database table, I need to display the new customerID in the customer registration form. For that I get the last customer id and increment by one and display the customer ID of the new customer. In a multi-user environment, if two people are trying to add a new customer at a same time, then there will be problem displaying the new customer id. And when two users accessing and updating the same record at a same time.
What to do?
You could output the inserted values into a table variable and then return that value
e.g.
DECLARE #output TABLE
(Col1 VARCHAR(10));
INSERT targetTable
(Col1)
OUTPUT inserted.Col1
INTO #output
VALUES ('ACC1001')
SELECT Col1 FROM #output;
Don't get the Last Customer Id and auto increment it by one, it's not safe for the reason you mentioned.
After the insert statement of the new record just select the inserted id and display this to the customer registration form with this line of code:
SELECT SCOPE_IDENTITY()
Or check this select statement which does the equivalent:
SELECT #RecordId= MAX([RecordId])
FROM [dbo].[CustomerTbl]
I have a Local SQL Database.
My first table is Customers which stores:
customerID (Primary Key, Identity, Auto Incremented),
firstName,
lastName,
address,
email,
postcode,
phoneNumber.
My second table is Groups which stores:
groupID (Primary Key, Identity, Auto Incremented),
groupName.
I have a third table Customer_Groups to link those two tables which stores:
customerID (Foreign Key),
groupID(Foreign Key).
(this third table was used to solve the many to many relationship)
On my New Customer C# Windows Application Form there are Text Boxes to insert a:
first name,
last name,
address,
email,
postcode,
phone number,
group name.
On my submit button click event, how would I insert these values into those three SQL tables above including inserting the ID's into the Customer_Groups table? Any help would be appreciated. Please let me know if I could be more specific.
So i can show u so many help for SQL insert into local table Help
There videos contains so many tricks
U want insert data in all tables at same time. So u have 3 tables then u have to write 3 method for inserting.
Use PetaPoco, available using NuGet. It allows you to set up some simple DTOs, and when you write one into the DB, the DTO is updated with the new id, so you can use that (within a transaction) to write the next item.
It's a pretty simple micro-ORM
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
I am writing a desktop application that connects to a MySQl database on a remote server.
I have two tables in a MySQL database.
One Table called Client contains the Contacts Id and other basic contact information.
The other table is called Property and contains the property type and address information and also has a Client_Id field that is the same as the Id field in the Client table.
Currently I open a connection to the database.
Then I use a Select query to get a List from the Property table for all the properties whose field call PropertyType = 'House'.
Then I take that List and enumerate through it using ANOTHER SELECT query that gets each Client based on their Id that natched the Id in the list.
Then I close the connection.
So I am only opening and closing the connection one time.
This takes a long time for just 400 records, I am just learning mySQL and am sure there is a better way. Can anyone offer any advice as to how I would query a table to get all the fields that match a certain criteria and then use a filed (Client_Id) to search another table to get clients based up their Id matching the Client_Id?
Depending on which data you want to see, try something like:
select p.address, c.name, c.phone
from Property p
join Clients c on p.client_id = c.id
where p.PropertyType = 'House'
You can reduce that to a single Query with something like
SELECT * FROM Client WHERE Client_id IN (SELECT Client_id FROM Property WHERE PropertyType='House');
Additionally make sure, you have all your indices in place.