Prevent Duplicate Inserts SQL in PhpMyadmin - sql

I have a Table called users which structure looks like this:
user_id
google_id
google_name
google_email
When I try to insert a record to this table I need to check both user_id and google_id for duplicate data. When we take one by one these two columns I should be able to insert data with out an issue. I mean that a record can contain the same value in one of user_id or google_id already in the table. But not in both. I tried many things an still could not make this work. Can some one guide me? I am using Phpmyadmin to mange DB

Create a unique index on the two columns:
create unique index users_ids on users(user_id,google_id)
If someone tries to insert another row with the same user_id and google_id, the unique index will cause an error.

Related

How to use indexes in sql database?

I have one table that contains 3 columns. Primary id, uuid, date(last login). When users are loging in I run query to database to check if user with this uuid exists. This table should work super fast for ~5mln users. How to make query on table like that faster? Will it help if I add another column for ex. country, and use it as a index?
If you want to check a particular uuid, then you want an index on that column:
create index idx_table_uuid on table(uuid);
This should be fast enough for your purposes.

Assign unique ID to duplicates in Access

I had a very big excel spreadsheet that I moved into Access to try to deal with it easier. I'm very much a novice. I'm trying to use SQL via Access.
I need to assign a unique identifier to duplicates. I've seen people use DENSE_RANK in SQL but I can't get it to work in Access.
Here's what I'm trying to do: I have a large amount of patient and sample data (20k rows). My columns are called FULL_NAME, SAMPLE_NUM, and DATE_REC. Some patients have come in more than once and have multiple samples. I want to give each patient a unique ID that I want to call PATIENT_ID.
I can't figure out how to do this, aside from typing it out on each row. I would greatly appreciate help as I really don't know what I'm doing and there is no one at my work who can help.
To illustrate the previous answers' textual explanation, consider the following SQL action queries which can be run in an Access query window one by one or as VBA string queries with DAO's CurrentDb.Execute or DoCmd.RunSQL. The ALTER statements can be done in MSAcecss.exe.
Create a Patients table (make-table query)
SELECT DISTINCT s.FULL_NAME INTO myPatientsTable
FROM mySamplesTable s
WHERE s.FULL_NAME IS NOT NULL;
Add an autonumber field to new Patients table as a Primary Key
ALTER TABLE myPatientsTable ADD COLUMN PATIENT_ID AUTOINCREMENT NOT NULL PRIMARY KEY;
Add a blank Patient_ID column to Samples table
ALTER TABLE mySamplesTable ADD COLUMN PATIENT_ID INTEGER;
Update Patient_ID Column in Samples table using FULL_NAME field
UPDATE mySamplesTable s
INNER JOIN myPatientsTable p
ON s.[FULL_NAME] = p.[FULL_NAME]
SET s.PATIENT_ID = p.PATIENT_ID;
Maintain third-norm principles of relational databases and remove FULL_NAME field from Samples table
ALTER TABLE mySamplesTable DROP COLUMN FULL_NAME;
Then in a separate query, add a foreign key constraint on PATIENT_ID
ALTER TABLE mySamplesTable
ADD CONSTRAINT PatientRelationship
FOREIGN KEY (PATIENT_ID)
REFERENCES myPatientsTable (PATIENT_ID);
Sounds like FULL_NAME is currently the unique identifier. However, names make very poor unique identifiers and name parts should be in separate fields. Are you sure you don't have multiple patients with same name, e.g. John Smith?
You need a PatientInfo table and then the SampleData table. Do a query that pulls DISTINCT patient info (apparently this is only one field - FULL_NAME) and create a table that generates unique ID with autonumber field. Then build a query that joins tables on the two FULL_Name fields and updates a new field in SampleData called PatientID. Delete the FULL_Name field from SampleData.
The command to number rows in your table is [1]
ALTER TABLE MyTable ADD COLUMN ID AUTOINCREMENT;
Anyway as June7 pointed out it might not be a good idea to combine records just based on patient name as there might be duplicates. Better way will be treat each record as unique patient for now and have a way to fix patient ID when patient comes back. I would suggest to go this way:
create two new columns in your samples table
ID with autoincrement as per query above
patientID where you will copy values from ID column - for now they will be same. But in future they will diverge
copy columns patientID and patientName into separate table patients
now you can delete patientName column from samples table
add column imported to patients table to indicate, that there might be some other records that belong to this patient.
when patients come back you open his record, update all other info like address, phone, ... and look for all possible samples record that belong to him. If so, then fix patient id in those records.
Now you can switch imported indicator because this patient data are up to date.
After fixing patientID for samples records. You will end up with patients with no record in samples table. So you can go and delete them.
Unless you already have a natural key you will be corrupting this data when you run the distinct query and build a key from it. From your posting I would guess a natural key would be SAMPLE_NUM. Another problem is that if you roll up by last name you will almost certainly be combining different patients into one.

Avoiding duplicate rows being inserted in where unizue rows are obtained from two tables

I have a two tables such as customer_name and customer_phone, but the unique customer is identified from the combination of all the four columns from two of the tables.
Since we have multiple souce systems inserting into the below tables at the same time, in all those jobs we validate before insert using a function to check if the customer already exist using (f_name,l_name,area_code,phone_num) this combination. However we still see duplicates getting inserted, because the validation happens while other job has already inserted but not yet commited. Is there any solution to avoid duplicates ?
customer_name
Col: ID, First_name, last_name
cutomer_phone
col: ID,area_code, Phone_number
Yes. Don't do the checking in the application. Let the database do the checking by using unique indexes/constraints. If I had to guess on the constraints you want:
create unique index idx_customer_name_2 on customer_name(first_name, last_name);
create unique index idx_customer_phone_2 on customer_phone(customer_id, phone_number);
The database will then do the checking and you don't have to worry about duplicates -- although you should check for errors on the insert.

Import Excel table to SQL by overwritting

I have an Excel file that maps exactly to a table in SQL Server. I have tried to import but I get the UNIQUE KEY error.
How can I overwrite the exisitng values in the database table with those in the excel file? I can convert to csv. if this is any help. Is there a statement I can write to do this?
Any guidance would be much appreciated.
Thank you
Create a second table using the same DDL as that table (in the database).
Afterward, import the Excel file into that newly created table.
Delete from the actual table (not the one just created above) all rows where the columns making up the unique key constraint match that of the the rows newly inserted into the newly created table.
Then insert into the actual table all rows that exist in the second, newly created and populated table. Because you just deleted the rows with the values you want to overwrite, you will no longer violate the unique constraint.
If you provide the column names of your table and the column(s) that make up the unique constraint I can help you write the query for #3 if needed.
Step 3 would be something like:
delete from table_name
where unique_id in
(select unique_id from newly_created_table_in_step1_and_2)
Please display the whole ERRO. if you have UNIQUE KEY Error then you have to create update query using primary key. you might have to write TSQL query.

How to only insert unique data into an access database

I have an access database that I need to update only if my information is unique. Is there a simple sql statement to accomplish this? Will 'insert ignore' work with access?
Example: I have the info stored in an array ('bob','34','hair'). If my database contains a record that matches on those three columns I would not want it to be inserted. If it was found to be unique I would like it to be inserted.
I am writing this in cold fusion but just cant seem to get the sql right.
Before doing an insert, do a select for those values. If you don't get a record back then you know it is safe to insert. Just use two separate queries, one for the check, and if no record found, then the insert.
A unique index is always a good idea if a field or set of fields should be unique. If you have a unique index in Access on the three fields, an insert will fail.
It is possible is to create a single query that only inserts a record where a matched record is not found, for example:
INSERT INTO Shows (ForeName,Reviews,Musical)
SELECT "bob" As ForeName,"34" As Reviews,"hair" As Musical
FROM (SELECT Count(*) As ExistsCount
FROM Shows
WHERE ForeName = "bob",Reviews = "34",Musical = "hair") AS e
WHERE e.ExistsCount=0