When working with database schemas, it's crucial to ensure data integrity. MySQL allows you to add NOT NULL constraints to existing columns to prevent storing null values.
Issue: You have a table named Person with columns P_Id, LastName, and FirstName. However, you omitted the NOT NULL constraint on P_Id.
Syntax Error: Attempting to add the NOT NULL constraint using the following queries may result in a syntax error:
<code class="sql">ALTER TABLE Person MODIFY (P_Id NOT NULL); ALTER TABLE Person ADD CONSTRAINT NOT NULL NOT NULL (P_Id);</code>
Solution:
The correct syntax to add a NOT NULL constraint to an existing column is:
<code class="sql">ALTER TABLE Person MODIFY P_Id INT(11) NOT NULL;</code>
Note:
By following these steps, you can effectively modify existing columns and add NOT NULL constraints, ensuring the integrity of your data in MySQL tables.
The above is the detailed content of How to Add a NOT NULL Constraint to an Existing MySQL Column?. For more information, please follow other related articles on the PHP Chinese website!