如何在mySQL中为已存在的表添加约束?

时间:2022-05-27 08:01:03

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:

我已经在mySQL中创建了一个名为contact_info的数据库,我试图在字段firstname和lastname中添加一个约束,表示它们不能为NULL。这是我此刻试图使用的那条线:

ALTER TABLE contact_info MODIFY firstname, lastname DATATYPE NOT NULL;

3 个解决方案

#1


1  

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;

#2


0  

ALTER IGNORE TABLE mytbl ADD not null (columnName);

#3


0  

you can try this:

你可以试试这个:

ALTER IGNORE TABLE table_name ADD not null (columnName);

ALTER IGNORE TABLE table_name ADD not null(columnName);

#1


1  

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;

#2


0  

ALTER IGNORE TABLE mytbl ADD not null (columnName);

#3


0  

you can try this:

你可以试试这个:

ALTER IGNORE TABLE table_name ADD not null (columnName);

ALTER IGNORE TABLE table_name ADD not null(columnName);