I want to store emergency help line contacts in database from ASP.NET web site . . .this contacts will be assign to particular locations of different cities. . .
For eg: City=Mumbai and then for Mumbai City there will be various locations like Dadar, Andheri, Borivali , etc .
I have designed a form
now, the problem is that , there can be one airport for multiple locations of a particular city then also i have to enter the entire information for each and every location . . . So is there any way to avoid multiple entries ?
You can use following table schema, and design you form accordingly
Location Table
LocationID (Primary Key)
CountryID (Foreign Key)
CityID (Foreign Key)
ContactID (Foreign Key)
Location
City Table
CityID(Primary Key)
City
Country Table
CountryID(Primary Key)
Country
Service Table
ServiceID(Primary Key)
Service --Airport/Ambulance/fire...
Contact Table
ContactID (Primary Key)
ServiceID (Foreign Key)
Description --AirportName/HospitalName
Contact --Contact Number
UPDATE (Form Design Tips)
Form to add records to City/Country/Service Tables (They are quite simple you just need to check duplicate entry/ Required fields.
For Contact Table use Drop Down for Service that is populated from Service Table
For Location use Drop Downs for Country,City and Contact that are populated from respective tables.
Use INNER JOINS to join the tables and display the desired Results
Related
I am looking to select data from multiple tables and insert into respective text boxes. I can have one table that hold all the data, but as I want to apply normalization to the data sets, I will have to split the data.
I already know that I can use :
SELECT *
FROM users
WHERE Username = UserNametxt.Text.Trim()
and then addWithValue etc.
How would I do this using a PK from one table linked to other tables as a FK?
Table users:
id (pk)
email
password
last_login
Table address:
id (fk to users.id)
houseNumber
StreetName
City
Table postcode:
id (fk to users.id)
PostCode
The above is a rough sample of what the data tables will be.
First of all, you should change the structure of your tables. In order to properly normalize DB, tables address and postcode should have their own id (PK), which does not depend on data from other tables.
If you leave it that way, you won't get rid of data duplication (which is one of the main reasons to normalize DB)
So it would look like that:
Table addresses:
id (PK)
userId (FK)
houseNumber
streetName
city
Table postcodes:
id (PK)
userId (FK)
postCode
And then you need to get information. You need to use JOIN to somehow connect tables and get information you need
SELECT * FROM users
LEFT JOIN addresses ON adresses.UserId=users.Id
LEFT JOIN postcodes ON postcodes.UserId=users.Id
WHERE ...
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 wonder why it is not working.
I am using ASP.NET MVC having dbcontext to contact with database.
I have two tables.
Table A and Table B
Table A has multiple columns from that there are three columns i.e. Email, Year and Month which are primary keys / composite keys.
Table B has a 5 columns from which one column is the TableAid that is use to store the auto incremented first column from the TableA without relationship as a foreign key, just store the id.
I pass the details from the view to controller as a one object i.e. TableA class and TableA has a list collection of TableB having multiple values.
Now I store the first TableA data first and than by taking id of the TableA and store TableB's data one by one.
Here in above process I got a exception while storing the details in TableB.
i.e.
Invalid column name 'Email'.
Invalid column name 'Year'.
Invalid column name 'Month'.
Which are composite keys.
I am not able to store child tables details.
Can anyone having any solutions for this?
public class TableA
{
//all fields get set
virtual public ICollection<TableB> TableB { get; set; }
}
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
I am sorry if this is a bit broad but I've been working on a structure for a couple of days and I can't seem to figure this out the cleanest and most efficient way to do this. I could share the tables that I've created for now but I really think it's not close to how a proper diagram should be.
Let me describe my problem a bit:
I have Stores, Countries, Districts, Categories.
Each store could belong to different Countries/Districts enabling Store Branch manipulation.
Of course a store could belong to multiple Categories too, for example Store X could be under both Food and Beverages and Night Clubs.
A Country will have multiple Districts and Stores, and a Store could have many Countries and Districts.
I am writing my application using C# and I don't have problems creating data-layer objects and classes. But I need the proper MSSQL structure to manipulate and filter down data based on given criteria.
The most important criteria would be: Going through Countries as a first step, then locating Stores within that Country as a global view, then it's important to sort Stores based on Districts and/or Categories within that Country.
Please let me know if you need me to share what I have for now, but since I'm on Stack Overflow asking this question you can guess that I'm doing this the wrong way.
Anyway, if you could shed some light on this issue and explain how things should be done properly I would highly appreciate it.
Thanks in advance.
Whenever you have many-to-many relationships (e.g. one district may contain many stores; one store may be contained by many districts), you are going to need to use cross-reference tables between those entities.
I'm assuming that a particular district may only be contained by one single country. Here is how I would model out your schenario:
countries(country_id [PK], name, ...)
districts(district_id [PK], country_id [FK], name, ...)
districts_has_stores(district_id [PK], store_id [PK])
stores(store_id [PK], name, ...)
categories_has_stores(category_id [PK], store_id [PK])
categories(category_id [PK], name, ...)
In ER:
districts_has_stores and categories_has_stores are the cross-reference tables representing the many-to-many relationships between your entities.
Based off of this model, you can retrieve all stores within a particular country, and order the stores by district name using the following SQL:
SELECT
c.*
FROM
districts a
INNER JOIN
districts_has_stores b ON a.district_id = b.district_id
INNER JOIN
stores c ON b.store_id = c.store_id
WHERE
a.country_id = <country_id here>
ORDER BY
a.name
Retrieving the count of stores in each country:
SELECT
a.country_id,
COUNT(*) AS store_count
FROM
districts a
INNER JOIN
districts_has_stores b ON a.district_id = b.district_id
GROUP BY
a.country_id
Edit: As per your comment to this answer, here's an example of how you can retrieve all stores that have a category_id of 1:
SELECT
b.*
FROM
categories_has_stores a
INNER JOIN
stores b ON a.store_id = b.store_id
WHERE
a.category_id = 1
Retrieving all stores in a particular category_id (1) and filtering the result to only include those stores within either districts 4 or 5.
SELECT DISTINCT
b.*
FROM
categories_has_stores a
INNER JOIN
stores b ON a.store_id = b.store_id
INNER JOIN
districts_has_stores c ON b.store_id = c.store_id
WHERE
a.store_id = 1 AND
c.district_id IN (4,5)
It's very easy, you have StoryCategory to solve the "many stores can have many categories" problem. The District and Country tables allow you to store the location of your store.
I advise you to look at the following documentation to expand your database design knowledge:
Many To Many
10 useful articles
I think below should work for you.
Countries
=========
CountryId, CountryName
Districts
=========
DistrictId, CountryId, DistrictName
Stores
=========
StoreId, DistrictId, Storename
Categories
=========
CategoryId, Categoryname
StoreCategories
=========
Storecategoryid, StoreId, CategoryId
You would have a country table with an identity column. Each store can "be-a" Category, you'd have to have an association table which is a child table of the store table which would have a store ID and the ID from the Category table. Each store would belong to a country so your store table would store the ID of the country from the Country table. Similar to the Category, your District table would have a child table which stores the ID of the District and of the Country (since each district can have multiple countries)
Store Table
ID INT IDENTITY PK
Category FK
Country FK (where it actually lives, a 1 to 1)
District FK
Category Table
ID INT IDENTITY PK
StoreCategory Table
StoreID FK
CategoryID FK
Country Table
ID INT IDENTITY PK
District Table
ID INT IDENTITY PK
DistrictCountry Table
DistrictID FK
CountryID FK