Creating tables at Runtime [closed] - database

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have 2 tables User and User_Event
User
id PK BIGINT
User_Event
event_id PK BIGINT
user_id FK References User.id
Should I create above tables before runtime
OR
should I create table "User_Event" for each Existing user at runtime (In this case table name would be like this
User_Event_user001,User_Event_user002....)
Now my questions is
1. Which design is better?
2. which implementation is faster?

For both questions, the best answer is to have an invariant database structure.
Modifying/creating the tables is a lot of work for databases, as they are designed for managing DATA inside the defined structures (tables, views).
It is very rare that changing the structure on the fly is pertinent, and even less effective.
--> create tables before runtime !

I don't know what is purpose of your system but classical implementation is to have one table User_Event and store data about all users in this table. If you want to get info for one user you should use query:
SELECT event_id FROM User_Event where user_id=<your user_id>;

Related

How to overcome save data in table same time by two users [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am implementing a seat booking application. I have a problem. There is a booking table. If two users try to book a same seat exact same time it will be a problem. How can I overcome this problem. I am using Java 8 and PostgreSQL.
A database system is designed to handle concurrent queries like in your situation by itself.
You however need to make sure, that a seat can only be booked once.
Solution #1 If you insert a new entry in the booking table for each booked seat
INSERT INTO booking (booking_id, seat_id, customer_id)
SELECT ?,?,?
WHERE NOT EXISTS (SELECT seat_id FROM booking WHERE seat_id=?)
This solution will check if there is already a booking entry in the database with the seat_id you are trying to book. This can be also achieved by putting an unique constraint on the seat_id attribute in your table. However in this case your query will fail with an error - since the unique constraint is violated when inserting a duplicated value - while in the solution above the query will execute sucessfully and just not insert any value. This solution is prefered.
Solution #2 If you have one row for every seat in your database and just need to update if its booked
UPDATE booking
SET booking_id=?, booked=true
WHERE seat_id=? AND booked = false
Depending on your JDBC library you are able to replace the ? with your real values.
The database system will handle concurrent queries and only allow one query to pass.

Suggestions about a Database conception [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am on a project to create a new Java EE application (JSF, Hibernate, Spring Security, Informix database). This application will automate the entry of notes for the annual interview of bank employees.
At the very beginning, everything was entered in an Excel file which then generated a report with the various performance graphs (according to the notes entered from 0 to 4).
Now I want to do a fairly optimized database design. I thought of creating the following tables:
Interview with columns (interview_id, interview_date),
Competency with columns (competency_id, competency_group, competency_name),
Interview_note with columns (interview_note_id, employee_id (FK), interview_id (FK) , competency_id (FK))
However, I have some doubts about how to keep it compact and logical. Is this the right way of doing things? Are there any improvements to take into account for more optimization?
In your narrative and draft database schema, I find the following identified entities: Employee, Competency, Interview and Interview_note.
In this regard, only the Employee table is missing, but I'm sure you have it somewhere. Moreover, your design is very flexible, since it allows for several Interview_notes of the same interview, competence and employee. What is perhaps missing therefore, is the id of who made the notes. Alternatively, if there's only one set of notes for an interview, you could consider to identify the interviewer in Interview.
A part from that, and maybe some missing data for the note (points, percentage of satisfaction, or some textual annotations?) your design seems to fulfil its purposes.
The database engine will very well optimize all the joins you'll have to do. Maybe facilitate its job by defining the _id as primary key, if you didn't do it.
I can't see other optimizations: each table clearly represent a different relation (in the relational algebra meaning of the term) and merging any of them would inevitably result in a suboptimal redundant schema.

How to enforce a one to many relationship when existing tables cannot be altered [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For background, my situation is I have a database that is missing a lot of foreign key relationships. One in particular, let's call it Orders, which represents orders with a composite primary key of OrderID and LocationID. The other table we'll call OrderDetails which has an OrderID but no LocationID. In reality, it is impossible to have an order in two locations at once, so it was assumed that there was no need to have LocationID in the details table. I didn't design it, and I can't change that.
We also have to work under the assumption there will be no support to add location id to the details table for various reasons. We are also working with Oracle and a high volume database with many concurrent users in many locations. Finally, there will be minimal time to change any applications that use this table.
So my question is: is this solution is feasible, or is there anything else I should try?
Say I create an intersection table, for lack of a better name AllOrders or whatever with primary key OrderID. Now we link Order.OrderID to AllOrders.OrderID and link OrderDetails.OrderID to AllOrders.OrderID. Would it be reasonable then to fill in AllOrders via a trigger on each insert to Orders to enforce the integrity? I am assuming all applications are inserting details after orders or the changes to enforce would be minimal and allowed.
Are there any better solutions? I understand we would do this differently if in charge of designing or given more leeway for fixing, but I'm trying to make the most given the constraints.
Edit --
To clarify what I am looking to accomplish, I want to treat all orders with the same ID as an equivalence class modulo location and ensure that if any order is deleted it requires all orders with the same id deleted and all child order details to be deleted. With primary importance of no orphan details. This has to be done with minimal application changes if possible and no redesign of existing tables if possible.
Create a new table to handle the mapping going forward.
Table: Tb_order_orderdetails
Columns: OrderID, LocationID, OrderDetailsID

Naming my DB tables - Suggestions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a couple of properties in my sql server SLS.Customer table which are references to other tables. I can't think of proper names for those tables. Help please :)
Table1: Contains the "How did you hear about us" values. (Example records: Through a friend, Advertisement, Seminar x, conference y, etc.)
Table2: Contains various reasons a customer refused to buy our product.
Table3: Contains the industry/business type a customer belongs to. (I thought of CustomerIndustry or just Industry but it sounds strange!!
Table4: Contains contact info of the person(s) related to a customer entity (specially if the customer is a company, rather than an individual, I need contact info of a person in charge). This is different from the tables PartyAddress and PartyPhones
As the question is suggestion based. I just put some suggestions here.
Table1:
RefererInfo
RefererDetails
Table2:
backlogReasons
backlogInfo
backlogDetails
Table3:
CustomerCategory
CustomerBackground
Table5:
PersonInChargeForParty
ContactsForParty

related categories - database design [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
I'm looking for a little database design advice...
I have a spreadsheet with a few columns in it. Column 1 being a list of categories and the rest being related categories(to the category in column 1). I'm trying to figure out what the best way to setup the tables would be... My thought so far is to have a table that just lists the categories then have a table with 2 columns that holds the id of the category and the id of a related category.... Would this be the best way to do this? Any better ideas?
A self referencing table (parentId to childId) is how most people implement a hierarchical structure like the one you are describing.
Your way is the best. Academics would call it resolving a reflexive many-to-many relationship with an associative entity, but it's nothing more than what you described.
m:n relations should always (maybe there are exceptions) be made with extra table for relations. So it should be the most flexible solution

Resources