1

I have the following column in table room Now I want to change the definition of column property_id from NULL to NOT NULL How can I do this following Query is not working

ALTER TABLE `room` CHANGE `property_id` `property_id` bigint(20)  NOT NULL;

Actual definition of Column is :

 `property_id` bigint(20) DEFAULT NULL,
6
  • 2
    do you have any rows contain NULL before your change the table definition? Commented Jul 13, 2017 at 12:32
  • Yes it have , Should I delete those rows? Commented Jul 13, 2017 at 12:33
  • Yes before changing the column condition Commented Jul 13, 2017 at 12:33
  • @SFAH You don't need to delete them, per se. But they cannot be NULL when you change the column definition. What you need to do with your data depends on your data and how you use it. Commented Jul 13, 2017 at 12:34
  • @SFAH, then you cannot do that, you have to treat those NULL first otherwise if will conflict the column properties Commented Jul 13, 2017 at 12:34

2 Answers 2

3

In mysql you can achieve this as,

ALTER TABLE room MODIFY property_id bigint(20) not null;
Sign up to request clarification or add additional context in comments.

2 Comments

In other dbms, such as oracle, it will not allow you alter a nonempty column, but mysql automatically casts things around for you when you alter columns
@SFAH, But you kept two 'property_id' s in ur query. Check once.
1

My Query was perfectly fine , The only mistake is that there are some rows that contains null record so that why it is creating an issue

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.