SQL Server Database value duplication - sql

I have a user editing table in my admin page in my website.
I want to check if theres a duplication with the username and the email when i update a row. Every row prefers to a different user and has her own id. I want that if in a certain row there a username and email values they cant be duplicated (every id has its own stats). How can i check the duplication ? (I work with myadohelper)
Hope to a quick answer, thanks

The best way to do this is by setting up a unique constraint/index in the database.
alter table t add constraint unq_t_username_email on t(username, email);
An attempt to add a row that already exists will result in an error.

Related

Oracle APEX: Assign primary key as interactive grid ROWID, Use select Interactive Grid ROWID in SQL query

To preface -- I am as green as at gets.
I am tasked with building an app for internal org use. We have a DB with patient data, and in interface with a hospital electronic medical records system. patient data entered into the EMR is sent to us via interface to update the patient profile in our database. Partial matches require manual intervention.
Message is received in a table within a staging schema
attempts to match to existing patient
if there are only 'partial matches' a status is set to 'mismatch'
I want to:
Display a summary of all 'mismatch' status records. I want to use an interactive grid to select individual records.
Set ROWID of interactive grid rows to the respective primary key of the record in the staging table.
Use the selected Interactive Grid ROWID (user selects a specific record in the interactive grid) to select the matching primary key of the corresponding record in staging table to create SQL query to find potential matches within our DB. Potential matches are displayed in a second table.
Questions:
How do I set the rowID of an Interactive grid to the unique key column of the staging table?
--Some research shows I need a hidden item, is this correct?
How do I access a rowID that has been selected in the Interactive grid to use in a SQL query?
My humble thanks and appreciation
So, your question is a bit confusing, but as far as I understand it. You are getting some data from table A, trying to match it with table B. If it matches, it irrelevant for us. If a match is not found you want to show it so that it can be manually matched.
In apex in order to update a table, you need to select what is the primary key by which it will update the data. That is usually a column in the table, but it can also be rowid(just include it in the SQL like any other column).
What I would suggest for you from what I understand of your situation.
Display the mismatched rows in an interactive grid, with rowid as primary key. Then you will need to have a column by which you match, if these entries already have some sort of key by which you tried to match but failed, display that. And have that column be a PopupLOV so the user can edit what value is in that field and set it to the appropriate match. One thing you will need to be careful about. You are editing a Unique key, or perhaps even Primary key, you might get conflicts here. Even if you only display unmatched data in the LOV, you can still have a user editing multiple rows and trying to match two rows to the same value, that will fail with an error that isnt particularly user friendly.

You cannot add or update record (MS Access)

Can someone help me? I am really new to access and vb . net. Every time i try to enforce referential integrity in my database relationship and add records or update a record I a always get a message of "You cannot add or update record because a related record is required in the table tbProducts"
Here is my database relationship.Database Relationship
The problem would likely be that a Foreign Key reference is required in your tbProducts table, the only one I can see from your image is UserID. (from the tbStaff table)
If you're trying to insert a record into the tbProducts, make sure that there is an existing UserID value to match what your inserting.

Update column value modified from another user

I have a column called Note, roleName and there are two roles, admin and engineer.
The engineer updates the notes and saves it , at the same time when admin logs in and modifies the same record (notes), he should be able to do it. How can I achieve this using case sql?
If two users update the same field in the same table, then the user which updates last, will win. If you are trying to do something like Google Docs, where many users can update the same data, that is going to be more than just a update to a table.

use triggers to keep history of relational tables

say I have 6 tables.
Workstation
Workstation_CL
Location
Location_CL
Features
Features_CL
I am currently using triggers to do inserts into the "_CL" version of each table with an additional field that denotes whether the change was an "UPDATE", "INSERT" or "DELETE".
the workstation table keeps track of the "modified_by" user. if a user updates the location of a "Workstation" object, the "Location" table gets updated as well as the "Workstation" table. the only modification to the Workstation table is the "modified_by" field so that I will know who made the change.
The problem I am having is when I think about pulling an audit report. How will I link records in the "Location_CL" to the ones in the "Workstation_CL" both are populated by separate triggers.
somehow my question portion was erased. sorry about that.
Question: how can I pull some type of unique identifier to have in both the "Workstation_CL" and the "Location_CL" so that I can identify each revision? for instance, when I pull all records from the "Location_CL" and I see all location changes, pulling the username from the "Workstation_CL" that made the location change?
Give each revision a GUID generated by the trigger. Populate a field (RevisionId) in both tables with the value.
You need 2, maybe 3 columns on each audit table.
1) Timestamp, so you know when the changes were made.
2) User changed, so you can track who made the changes - I assume that Location can change independently of Workstation.
3) You might need an identifier for the transaction, too. I THINK you can get an id from the DB, though I'm not sure.
I don't think you can have an effective report without timestamps and users, though, and I don't think you just have the user on one table.
During the trigger event, I was able to exec the following:
SELECT #trans_id=transaction_id FROM sys.dm_tran_current_transaction
which gives me the transaction id for the current operation.
with that, I am able to insert it in to the corresponding _CL table and then perform selects that will match the auto-gen id's.

INSERT INTO a table from another table, but with the correct user ID for each comment a user adds

I have two tables - a 'users' and a 'comments'. They both have the column 'IDUsers'. When a user adds a comment I want their user ID to come from the 'users' table and go into the 'comments' table into the 'IDUsers' column from the 'IDUsers' column.
However, at the same time a comment is being added from the user - so I also am using the INSERT INTO for the new information. I'm also using ColdFusion - if that makes much difference.
Hope everyone understands what I'm saying....and thanks for the help.
What exactly is the problem? When a user is posting a comment, you know their user ID. So use that UserID to do the insert into the comments table.
If you're generating the user object on the fly - as a part of the same transaction (make sure it's a single transaction! It's important - synchronization issues!!!) - use something like mysql_insert_id() equivalent in ColdFusion (http://www.coldfusionmuse.com/index.cfm/2005/8/8/identity%20after%20insert). After you do the insert into the user table, get the ID of the inserted record; then use that ID as the foreign key into the comments record.
You will know the userId while a user is adding comments. Use that particular userId in the insert statement. And to make sure that only userId from users table comes in comments table, you can set userId in comments table as a foreign key from users table.