23

I have one table. The table name is employee. I used below query.

delete department,name,bloodgroup from employee where employeeid=2;

But I am not able to delete this record alone. It is showing error. And I don't want to use update statement.

3
  • It can not be standart sql. Normally Delete command cant do this. Commented Jan 4, 2012 at 11:25
  • 2
    Why don't you want to use the update statement? It's the only way to set specific column values to null Commented Jan 4, 2012 at 12:09
  • As others have pointed out, using SQL you can delete rows, but you can't delete values. Instead, you can (possibly) update values to NULL or to some other sensible value, such as zero. Commented Jan 4, 2012 at 20:07

4 Answers 4

37

You can't delete single column entries with the delete SQL command. Only complete rows.

You can use the update command for that:

update employee 
set department = null, name = null, bloodgroup = null
where employeeid=2;
Sign up to request clarification or add additional context in comments.

6 Comments

Or, if the OP actually wants to delete the whole record... DELETE employee WHERE employeeid = 2
@Dems: DELETE FROM employee ...
What if department has a not-null constraint e.g.?
@s.k: If a record without a department is not allowed by design then you can not set it null. That is why the restriction was made by the one creating the table design
@s.k: There is no universal default value. Maybe you should ask a specific question about your problem with some example data.
|
3

The above solution won't work until NOT NULL constraint is dropped(removed) to do this:

ALTER TABLE employee;
ALTER COLUMN department DROP NOT NULL;

After this You can UPDATE the table as above:

UPDATE employee SET department = null WHERE employeeid =2;

Hope this one helps Thank you!

Comments

0

We cannot DELETE a particular column value in SQL, we can UPDATE it.

Comments

-1

You can delete a particular column full value of a table in the database using this command.

UPDATE 'table_name' SET 'column_name' = NULL;

Using this command you can clear all the values of a column to null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.