将MySQL列更改为具有NOT NULL约束
要更新约束,请使用MODIFY命令。以下是语法-
alter table yourTableName modify yourExistingColumnName yourExistingDataType NOT NULL;
让我们首先创建一个表-
mysql> create table DemoTable-> (
-> UserId int NOT NULL AUTO_INCREMENT,
-> UserFirstName varchar(100),
-> UserLastName varchar(100),
-> UserEmailId varchar(100),
-> UserPassword varchar(100),
-> PRIMARY KEY(UserId)
-> );
Query OK, 0 rows affected (0.91 sec)
以下是将列的约束更改为NOT NULL的查询-
mysql> alter table DemoTable modify UserFirstName varchar(100) NOT NULL;Query OK, 0 rows affected (2.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
让我们使用desc命令检查列是否具有NOT NULL约束-
mysql> desc DemoTable;
输出结果
这将产生以下输出。现在UserFirstName列具有NOT NULL约束-
+---------------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| UserId | int(11) | NO | PRI | NULL | auto_increment |
| UserFirstName | varchar(100) | NO | | NULL | |
| UserLastName | varchar(100) | YES | | NULL | |
| UserEmailId | varchar(100) | YES | | NULL | |
| UserPassword | varchar(100) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
5 rows in set (0.17 sec)
以上是 将MySQL列更改为具有NOT NULL约束 的全部内容, 来源链接: utcz.com/z/322567.html