Insert values into column when the two corresponding column values are not there - sql

I am creating a table with having user profiles associated with various events. Various variables of user profile change at various intervals ranging from daily to more than a year. Hence, once the user is associated with an event, his profile cannot change for that event.
While Inserting the values into the table through a Select query I need to ensure that there is no more than one record for one user ID and Event ID. How can I achieve this in the best optimal way? Thank You in advance.
e.g.
event_id
user_id
age
gender
abc
1
24
M
xyz
5
31
F
xyz
5
31
F
abc
5
31
F
In the above table, the event_id = xyz and user_id = 5 has been entered twice. I need to ensure that the these type of value do not get inserted again from the query that I am retreiving the data.

You would use a unique constraint or index:
alter table t add constraint unq_t_event_id_user_id
unique (event_id, user_id);

Related

Count number of entries per user_id in BigQuery

I have a table consisting of 33 distinct user_id that has a row of data for each day. There should be 31 days (rows) for each user. I want to run a query to confirm this, where the output is a table of unique user_id in column 1 and number of entries in column 2.
I know I can display all rows for each user_id using the following query. But I don't want to do that 33 times just to find out how many entries per user there are in the table.
SELECT *
FROM table
WHERE user_id = xxxx
I'm very new to using SQL and BigQuery. Thank you.

Handling CustomField Insert - Which option is efficient and easier to maintain

We have the following Table structure:
User Table
UserId CompanyId FullName Email
1 1 Alex alex#alex.com
2 1 Sam sam#sam.com
3 2 Rohit rohit#rohit.com
CustomField Table
CustomFieldId CompanyId Name Type
1 1 DOB Datetime
2 1 CompanySize Number
3 2 LandingPage Text
CustomFieldValue Table
UserId CustomFieldId DatetimeValue NumberValue TextValue
1 2 01-01-2020
1 2 10
1 3 Home
2 1
2 2 20
2 3 Product
Please consider the following facts:
There are millions of users in a particular CompanyId
When displaying a particular user in the UI we need to show all the Custom Fields that an end customer can fill up.
How to handle CustomFieldValues table in this case? We are considering the following options
When a new CustomField row is created for a particular CompanyId have a After Insert Trigger to create all corresponding rows in CustomFieldValue table for all users.
This I think would have an initial cost of creating so many rows for each Custom Field in the CustomFieldValue Table. (This may also lock up the table and users of the application would have to wait till all the inserts are done).
Same issue for deleting all CustomFieldValue rows when a CustomField row is deleted from a Company
But easier for UI and backend developers as they don't need to worry about whether a CustomFieldValue doesn't have an entry for a Custom Field that has been created for a Company
Don't create CustomFieldValue rows when a CustomField is added to the Company. Create the CustomFieldValue whenever user fills up the relevant input field in the UI view
This would have negligible insert cost and users would not have to wait for insert or delete to complete in CustomFieldValue table for all the users in a particular company.
The downside is that developers would have to make sure that relevant CustomFields are displayed in the frontend even though no relevant records yet exist in the CustomFieldValue table.
On each Custom Field input update by the end user, the developers would have to first check if a corresponding CustomFieldValue row exits, if so - store the updated value, if not - create the CustomFieldValue row.
Kindly suggest a solution which is efficient and easier to maintain.

get the values mentioned in reference column

I want to join two tables(Table1,Table2) which should return the status of the references mentioned in table1 reference column(12,14,18,19). Table2 has information of the ID which are mapped to the reference in individual rows as shown below.
Table1:
Datatype of columns are: ID is of type integer,Name is character varying,reference is character varying,status is character varying.
ID Name reference status
10 PAX11 12,14,18 Undelivered
11 PAX193 Undelivered
12 ASD1 delivered
14 PAA delivered
90 PQA 19 Undelivered
18 PX Halt
19 ONA delivered
Table2:
Below 3 columns are of integer type.
sno ID reference
1 4 100
2 10 12
3 10 14
4 10 18
5 90 19
Your data model is wrong. You should put your effort into fixing it. The correct fix is to have another table, with one row per id and per sno. This is called a junction or association table.
What is wrong with the data model? The issue is the reference column. Let me count the ways:
A column should contain a single value, not multiple values.
The data type should be correct for a value, so a number should not be stored in a character column.
Foreign keys should be properly defined. You cannot do that with a string list.
Relational databases have relatively weak string processing capabilities.
Queries that use reference cannot make use of indexing, and statistics might be misleading.
Relational databases have these great constructs for storing lists. They are called tables, not strings.

Automatic id assign

i'm a student and im having problems using the automatic increment because when i delete a row it will continue to increment. explaining:
i want to increment id automaticly
so:
id name age
1 michael 18
2 katy 17
3 jack 20
now i delete row 3 and when i click in the button new it'll go to the id 4 instead of id 3
i'v tried rows.count and refresh the textbox but nothing
some adicional info
ds= dataset
maxrows = ds.Tables("virtualtable").Rows.Count
idcliTextBox.Text = maxrows
how do i make it set id to the real last row?
It is the correct behavior and it is not a problem. Usually the autoincrement columns in a database are never reset to accomodate for empty holes caused by deletion of previous inserted records.
The autoincrement column is usually used as primary key to uniquely identify a single record in your table.
Suppose that your table represents students where the ID field value is used as foreign key for another table examresults. In this table you store the exam result of your students. Your student Katy (2) has two records in the examresults table for the graduation in math and geography.
If you delete the record with ID=2 from the table students and the related records from examresults changing the record for Jack from 3 to 2 means that you need to change also the related records for examresults of Jack. This is very impractical and useless if you think about it.

How to replicate a table 100 times and add it back to the original table

I just started using SQL and need to perform the following task.
1) First, I need to dupicate a table 100 times. For all these duplicated tables, I want to keep its content unchanged. But I also want to update the primary key by one.
2) Secend, I want to concatenate all these duplicated tables together by row, and also concatenate them to the original table by row.
Example: The original table looks like:
ID CATEGORY
1 A
2 B
… …
26 Z
And I want to duplicate this table, and concatenate it to the original one. I want to maintain the colums other than the primary key (ID here) unchanged, and update the primary key by one each time. I want to get:
ID CATEGORY
1 A
2 B
...
26 Z
27 A
...
52 Z
How to do this? Thanks!