Deleting a single row from a table that has multiple entries? - database

So here's my situation.
I have a table in my database, it has multiple entries, but I need only one to be deleted.
When trying to google this, all I could find was questions referring to deleting all except one of these entries, while I need to delete ONLY ONE and leave the rest.
I'm completely confused on this, and don't really understand how to do it.

You need to use a where clause to identify the row that you want to delete, preferably by the primary key of the table. For example:
delete from MyTable where id = 1
If you post your table's schema, we can help with better syntax.

The row you are trying to delete should have a unique way to identify it, typically the primary key. You would then use the WHERE clause to specify your query to DELETE it:
DELETE FROM sparkles WHERE primaryKey = 1
Alternatively, if there are no keys, you would filter by criteria that matches all values of the record you wish to delete.
DELETE FROM sparkles WHERE col1 = 5 AND col2 = 15 AND col3 = 51

If you are trying to achieve this in Java, you would most probably be using JDBC however if this is just a onetime effort you can directly run the delete query with appropriate where clause.

Well I don't see how this is tagged as Java. But it sounds like you want to use limit operator, like this:
DELETE FROM somedatabase WHERE someentry = 5 LIMIT 1;
The response two above me is good to but leaves out LIMIT.

Step by step for doing it in phpMyAdmin....
Go to phpMyAdmin interface
Select the database
Select the table
Click Browse
Look for the row you are looking to delete and click the delete Icon
Confirm deletion of that row
Done.

If you have 5 Fields in table and you want to delete only one field for particular instance then you should fire update query giving the null effect to that filed...
Suppose If table is
Id | Name | MobNum
And you want to delete MobNum of record 5 ...then query can be ike this>>>
update tlbName set MobNum=null where Id='5'

Related

How can I get one of my foreign key outputs to repeat in a merge transformation in SSIS?

I tried asking this question before and it seemed to have gotten swept under the rug.
First thing first, here are these two pictures to show the table structure and the current output I get in SSIS.
Table Diagram
Current Output
So in table three, there is only one entry. This entry (name) applies to the other foreign keys though. What I want the final output to look like is like my current output, but instead of the NULLS, there should just be ones.
I was able to get this far on my own through researching and learning about the merge transformations but I can't seem to find anything on manipulating the data in the way that I want.
I greatly appreciate any tips or advice you can offer.
EDIT: Since the images can't be seen apparently, I will try and describe them.
The table diagram has four tables, the top one in the waterfall has a primary key formed from the three foreign keys for the three different tables.
Trying to accomplish filling out this table in SSIS, my output has each foreign key id from the first two tables, but only one in the third table. The rest from the third foreign key are all NULLS. I believe this is because there is only one entry in that table for now, but this entry applies to all of the foreign key ids and so it should be repeating.
It should look like this:
ID1 ID2 ID3
1 1 1
2 2 1
3 3 1
But instead, I am only getting nulls in the ID3 field after the first record. How do I make the single id repeat in ID3?
EDIT 2: Some additional screenshots of my data flow and merge transformation as requested.
[![SSIS Dataflow][3]][3]
After working on this for a few weeks, and with a tips from a colleague, a solution to this question was found. Surprisingly, it was quite simple and I'm slightly shocked that no one on here could provide the answer.
The solution was simply this; Using a data source, write the following SQL code in the data access mode (SQL Command):
SELECT a.T1ID,
b.T2ID,
c.T3ID
FROM Table1 AS a join
Table2 AS b
On a.T1ID = b.T2ID,
Table3 AS c
ORDER BY a.[T1ID] ASC
If Table3 will always have just a single row, the simplest solution would be to use an Execute SQL task to save the T3id to a variable (Control Flow), then use a Derived Column task (Data Flow) to add the variable as a new column.
If that won't work for you (or your data), you can take a look here to see how to fudge the Merge Join task to do what you want.

Executing Sql Command only once

I have a Database DB with a table name population but with no Primary Key. Means it can have duplication of data. For example: I have 5 families (f1,f2,f3,f4,f5) with different members inside it (and members may have same name). So I can have exactly same type of record in more than 1 row. Now I want to edit just 1 member of the family, but it is editing all the duplicate records. What I want to do is, I just want to Update my Database once and only once. In other words, I want to use UPDATE command to execute just once. How to do it?
I am using Sql Server Express, VS2010, C# 4.0 (if this info matters).
I am pretty sure my problem may sound stupid to some people (probably many). But it is just a dummy of my problem. Any help or suggestion will be greatly appreciated. Thanks
I know it's not exactly what you're asking but seriously, the easiest option is to alter the database to have a primary key and use that. Perhaps an Identity key....
Without that, you could update just one record, but you have no guarantee of which record. This is why primary keys are such as fundamental concept. I suppose this doesn't really matter if they are all the same, so....
If you really want to proceed without a primary key, you need to use the TOP keyword as shown here: How do I update n rows in a table?
Set it to
UPDATE TOP 1 ....
Add an identity column, ID int with auto increment on. Then update using the id's.
CREATE TABLE dbo.Family
(
Id int NOT NULL IDENTITY (1, 1),
FamilyName varchar(50) NULL
)
update dbo.Family set FamilyName = 'xxx' where Id = y
In case you can't add an identity column for some reason:
UPDATE TOP ( 1 ) Families
SET Whatever = 'Your Value', ...
WHERE <Your where clause>
The real answer is: Fix your database design. Every record should have some unique identifier. Add an auto-increment field or something.
To directly answer your question, you can say "update top (1) ..." to only update one record. But without some unique identifier, how do you know which record to update? The program will essentially update a random record. Which takes me back to point 1.
Edit: Whoops, my original answer was for a different engine. Corrected above.

ADOQuery and DBNavigator

OK. I have simple Database ,DBGrid and DBNavigator . I use this code for my adoquery
and view in this in my DBGrid
select Count(*)As 1 , 2 3 from Table1
Group by 1, 2
The problem is that if i have two or more the same article from 1 and i try to delete it from DBnavigator it send me
Key column information is insufficient or incorrect.Too many rows were affected by update.
I want to delete two or more rows.Not only one.
How to fix that ?
I guess it won't be possible using DBNavigator in a usual way (without override delete button event), because the dataset holds more than one record in a DBGrid row and IMHO the grid doesn't take care if you have data in a group. I would say if you select some grouped row and try to delete it, "DBNavigator" checks if there is some primary (unique) key for a row, but in this case, there is no one.
But it's just my notion, I can't verify it now.

Select last updated row ID and last deleted row ID in trigger before they occur in SQL Server

I need to get the id of the updated row in a table to use it to update another table via trigger
Also need to get the id of the deleted row in a table to use it to update another table via trigger
How can I do this?
Is there any built in functions in SQL Server?
If not what kind of trick that can help to accomplish this
Within your trigger, if you want to know the OLD value that was either updated / deleted
SELECT idColumnName FROM deleted
Where idColumnName is the column that contains the ID that you are interested in.
You can then use this ID value to then perform whatever processing that you need.
Additionally. if you want to use the NEW value being updated, the below query gives you that. This is useful especially in case of Updates where you want to compare old / new values of certain fields. In your case, since its an ID column, this will probably not be relevant
SELECT idColumnName FROM inserted

SQL how to increase or decrease one for a int column in one command

I have an Orders table which has a Quantity column. During check in or check out, we need to update that Quantity column by one. Is there a way to do this in one action or we have to get the existing value and then add or minus one on top of it?
Another question is when we insert a new row, do we need to check if same data existing then insert if not, which is two steps, or is there a better way to do this?
thanks,
To answer the first:
UPDATE Orders SET Quantity = Quantity + 1 WHERE ...
To answer the second:
There are several ways to do this. Since you did not specify a database, I will assume MySQL.
INSERT INTO table SET x=1, y=2 ON DUPLICATE KEY UPDATE x=x+1, y=y+2
REPLACE INTO table SET x=1, y=2
They both can handle your question. However, the first syntax allows for more flexibility to update the record rather than just replace it (as the second one does).
Keep in mind that for both to exist, there has to be a UNIQUE key defined...
The single-step answer to the first question is to use something like:
update TBL set CLM = CLM + 1 where key = 'KEY'
That's very much a single-instruction way of doing it.
As for the second question, you shouldn't need to resort to DBMS-specific SQL gymnastics (like UPSERT) to get the result you want. There's a standard method to do update-or-insert that doesn't require a specific DBMS.
try:
insert into TBL (key,val) values ('xyz',0)
catch:
do nothing
update TBL set val = val + 1 where key = 'xyz'
That is, you try to do the creation first. If it's already there, ignore the error. Otherwise you create it with a 0 value.
Then do the update which will work correctly whether or not:
the row originally existed.
someone updated it between your insert and update.
It's not a single instruction and yet, surprisingly enough, it's how we've been doing it successfully for a long long time.
UPDATE Orders Order
SET Order.Quantity = Order.Quantity - 1
WHERE SomeCondition(Order)
As far as I know there is no build-in support for INSERT-OR-UPDATE in SQL. I suggest to create a stored procedure or use a conditional query to achiev this. Here you can find a collection of solutions for different databases.
If my understanding is correct, updates should be pretty simple. I would just do the following.
UPDATE TABLE SET QUANTITY = QUANTITY + 1 and
UPDATE TABLE SET QUANTITY = QUANTITY - 1 where QUANTITY > 0
You may need additional filters to just update a single row instead of all the rows.
For inserts, you can cache some unique id related to your record locally and check against this cache and decide whether to insert or not. The alternative approach is to always insert and check for PK violation error and ignore since this is a redundant insert.
to answer the second:
make the column unique and catch the exception if it's set to the same value.
#dotjoe
It is cheaper to update and check ##rowcount, do an insert after then fact.
Exceptions are expensive && updates are more frequent
Suggestion: If you want to be uber performant in your DAL, make the front end pass in a unique ID for the row to be updated, if null insert.
The DALs should be CRUD, and not need to worry about being stateless.
If you make it stateless, With good indexes, you will not see a diff with the following SQL vs 1 statement.
IF (select top 1 * form x where PK=#ID)
Insert
else
update

Resources