If I have a table with only one column, how could I delete a particular row from it? I know the most logical answer would be to have another column in the table with a number that automatically increments, but in a table with only one column, how would I go about doing this? Is there something I can add in the WHERE clause to say row = 3 or something similar?
Please try the following...
DELETE FROM tblTable
WHERE fieldName = targetValue;
If you have any questions or comments, then please feel free to post a Comment accordingly.
You can do:
delete t from t
where col = #value;
However, that will delete all rows with the value. If the value is unique, you should declare the column to be either unique or a primary key.
I should add that you can delete just one row with the value by adding limit 1:
delete t from t
where col = #value
limit 1;
Related
Let's say I have a table with 10 columns, and I have 1 row too.
I want to update only one row and not the others
If I update just one, the other ones will have the default value which is the expected behavior
Is there a way so that I change only a row and still have all the other rows preserve the old values?
I don't want to use a lot of resources for this thing so I wish there's something built-in or something
TL;DR:- How can I update a row and have the other rows stay the same?
I'm using MySQL
An update statement lets you control both the rows being updated and the columns. Usually the rows are defined by the where clause and the columns by the set:
update t
set col4 = <some value>
where col1 = <some value>;
If the where clause is using the table's primary key with an equality condition, then only one row is updated, and only the specified columns in that row change.
IF EXISTS (SELECT * FROM X )
UPDATE [Table] SET...
ELSE
INSERT INTO [Table]
I have a column in MySQL database that store the images names/IDs something like "523523525.jpg".
I want to update this column by adding the folder before the image name/ID to be like "101/523523525.jpg". I want to update specific records not all the column, for example update from record 1 to 1000 by adding "101/...." and records from 1001 to 2000 by adding "102/....".
Look for your ideas.
Add an id column.
ALTER TABLE your_table ADD
COLUMN id INT AUTO_INCREMENT
PRIMARY KEY FIRST;
use concat to update rows based on id
UPDATE your_table SET
Column=CONCAT("101/",column)
WHERE id BETWEEN 1 AND 1000;
for first 1 to 1000 to use LIMIT in update statement.
For next 1001-2000 you need to write anonymous block or procedure using cursor to update records.
How about using the CONCAT function? Assuming you have a seperate column for the id, you can execute an update query to add the path to the existing value.
UPDATE your_table SET path_col=CONCAT('101/', path_col) WHERE id between 1 AND 1000;
Hope it helps! Feel free to ask if you need to know anything.
How can I update only the nth row from a table?
To update the second row for example, i tried using UPDATE and LIMIT, but it is giving me an error.
UPDATE database_table
SET field = 'new_value'
LIMIT 0, 1
Is there a way to do it?
If you have a primary key and a column you'd like to order by (to get the nth row), you could do something like:
UPDATE database_table
SET field = 'new_value'
WHERE primary_key IN (
SELECT primary_key
FROM database_table
ORDER BY some_date_column
LIMIT n - 1, 1
)
Edit: I should probably add a caveat. This answer might be technically correct, but you're likely wrong to use it. I can't imagine too many scenarios where you'd actually want to update the nth row of a table. You should generally only be updating tables based on primary keys. Updating the nth row will likely break your app if multiple users (or even multiple sessions with the same user) are using it at the same time.
The real answer is you should probably change your code to update based on primary key.
You would need some sort of id, and then do something like this:
UPDATE database_table SET field = 'new_value' WHERE id = 2
I have a MySQL table called settings. It has multiple columns, where every column is an item with a single value. So it has only one row and no id column. The design is final (I don't plan to add more columns).
How can I update the value in a single column (change one setting's value)?
What's the problem with using this --> http://dev.mysql.com/doc/refman/5.0/en/update.html
?
UPDATE table1 SET column1 = value
In case you have more than one row, you can add:
WHERE table1.column = matching_value;
making sure the match criteria is only the row you need.
to update a value of certain item column you must specify the row contains the value to be updated, since you don't have a primary key, you can depend on the nature of item values to act as row identifier and i don't recommend that.. the best way is to update your design and add column for the primary key
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1. For example, column_1 and column_2 should be UNIQUE only when active = 1. Any rows that contain active = 0 can share values for column_1 and column_2 with another row, regardless of what the other row's value for active is. But rows where active = 1 cannot share values of column_1 or column_2 with another row that has active = 1.
What I mean by "share" is two rows having the same value(s) in the same column(s). Example: row1.a = row2.a AND row1.b = row2.b. Values would be shared only if both columns in row1 matched the other two columns in row2.
I hope I made myself clear. :\
You can try to make multi-column UNIQUE index with column_1, column_2 and active, and then set active=NULL for the rows where uniqueness not required. Alternatively, you can use triggers (see MySQL trigger syntax)
and check for each inserted/updated row if such values are already in the table - but I think it would be rather slow.
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1
You can set the value of "another column" to a unique value that does not equal to 1. for example the id of a record.
Then the unique index constraint could be applied to all three columns including the "another column". Let's call the "another column" columnX.
Set the value of columnX to 1 if you want to apply the unique constraint to a record. Set the value of columnX to a unique value if you don't want to apply the unique constraint.
Then no extra work/triggers needed. The unique index to all three columns could solve your problem.
I am not sure about MySQL syntax, but it should have pretty much the same thing that SQL Server has:
CREATE UNIQUE INDEX [UNQ_Column1Column2OnActive_MyTable]
ON dbo.[MyTable]([column1,column2)
WHERE ([active] = 1);
This index will make sure if active=1 then column1 and column2 combination is unique across the table.
In SQL Server this could be accomplished with check constraints, however I do not know if MySQL supports anything similar.
What will work on any database, is that you can split the table in two. If the records where active =0 are just history records, and will never become active again, you could just move them to another table, and set a simple unique constraint on the original table.
I am not sure I understand you 100% but lets say you have a table that has a status column and you want to make sure there is only one raw with a status of 'A' (Active). You are OK with many rows with statuses of 'I' or 'Z' or anything else. Only one row is allowed with status of 'A'.
This will do the trick.
CREATE UNIQUE INDEX [Idx_EvalHeaderOnlyOneActive]
ON [dbo].[EvalHeader]([Hdr Status])
WHERE [Hdr Status] = 'A';
indexes are agnostic of external influences. This kind of constraint would have to be implemented outside your database.