219

I have table name called "Person" with following column names

P_Id(int),
LastName(varchar),
FirstName (varchar).

I forgot to give NOT NULL Constraint to P_Id.

Now I tried with following query to add NOT NULL Constraint to existing column called P_Id,

1. ALTER TABLE  Person MODIFY  (P_Id NOT  NULL);
2. ALTER TABLE Person ADD CONSTRAINT NOT  NULL NOT NULL (P_Id);

I am getting syntax error....

3 Answers 3

346

Just use an ALTER TABLE... MODIFY... query and add NOT NULL into your existing column definition. For example:

ALTER TABLE Person MODIFY P_Id INT(11) NOT NULL;

A word of caution: you need to specify the full column definition again when using a MODIFY query. If your column has, for example, a DEFAULT value, or a column comment, you need to specify it in the MODIFY statement along with the data type and the NOT NULL, or it will be lost. The safest practice to guard against such mishaps is to copy the column definition from the output of a SHOW CREATE TABLE YourTable query, modify it to include the NOT NULL constraint, and paste it into your ALTER TABLE... MODIFY... query.

Sign up to request clarification or add additional context in comments.

10 Comments

@Positive why did you write INT(11) and not just INT? What is the effect of the 11 ?
The 11 is just an example, it sets the length of the P_Id. i am not sure if it is necessary to add it again even though you might not even want to change it.
@Pacerier with change you modify the column's name
@Victor MODIFY is also supported by Oracle. And PostgreSQL doesn't support CHANGE instead it provides an ALTER [COLUMN] statement.
note to self: structured query language isn't a standard query language...
|
26

Try this, you will know the difference between change and modify,

ALTER TABLE table_name CHANGE curr_column_name new_column_name new_column_datatype [constraints]

ALTER TABLE table_name MODIFY column_name new_column_datatype [constraints]
  • You can change name and datatype of the particular column using CHANGE.
  • You can modify the particular column datatype using MODIFY. You cannot change the name of the column using this statement.

Hope, I explained well in detail.

4 Comments

You perform MODIFY operation to change but reverse is not possible ?
Oddly I was getting an error (MySQL 5.6, Workbench 6.3) changing/modifying a column I'd named null_heart_rate_count, error was #1138, Invalid use of NULL value. I had to drop and add the column instead.
@NavrattanYadav I think he meant to say rename, not reverse
@WilliamT.Mallard I was getting the same error. In my case it was because the column was already containing a NULL value in one row. After changing values to somenthing other than NULL, the query performed successfully.
19

Would like to add:

After update, such as

ALTER TABLE table_name modify column_name tinyint(4) NOT NULL;

If you get

ERROR 1138 (22004): Invalid use of NULL value

Make sure you update the table first to have values in the related column (so it's not null)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.