简体   繁体   中英

How can I add a constraint to an already existing table in mySQL?

I have already created a database called contact_info in mySQL and I am trying to add a constraint to the fields firstname and lastname saying that they cannot be NULL. This is the line I am attempting to use at the moment:

ALTER TABLE contact_info MODIFY firstname, lastname DATATYPE NOT NULL;

You can not alter two columns at once. Use separate commands:

ALTER TABLE contact_info MODIFY firstname DATATYPE NOT NULL;
ALTER TABLE contact_info MODIFY lastname DATATYPE NOT NULL;

Or combine them into one command with two specifications:

ALTER TABLE contact_info
    MODIFY firstname DATATYPE NOT NULL,
    MODIFY lastname DATATYPE NOT NULL;
ALTER IGNORE TABLE mytbl ADD not null (columnName);

you can try this:

ALTER IGNORE TABLE table_name ADD not null (columnName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM