如何删除非空约束?
假设创建了一个表,如下所示:
create table testTable ( colA int not null )
如何删除非空约束?我正在寻找一些与
ALTER TABLE testTable ALTER COLUMN colA DROP NOT NULL;
我使用 PostgreSQL 类似的东西。令我惊讶的是,据我所知,MySQL 文档、Google 甚至 Stackoverflow(尽管有数十个或数百个与 NULL 相关的问题)似乎都没有导致单个简单的 SQL 语句这将完成这项工作。
Let's say there's a table created as follows:
create table testTable ( colA int not null )
How would you drop the not null constraint? I'm looking for something along the lines of
ALTER TABLE testTable ALTER COLUMN colA DROP NOT NULL;
which is what it would look like if I used PostgreSQL. To my amazement, as far as I've been able to find, the MySQL docs, Google and yes, even Stackoverflow (in spite of dozens or hundreds of NULL-related questions) don't seem to lead towards a single simple SQL statement which will do the job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我会尝试这样的事情
I would try something like this
在 MySQL 中,可空性是数据类型的一部分,而不是约束。所以:
In MySQL, nullability is a part of the datatype, not a constraint. So:
语法实际上很接近:
The syntax was close its actually:
尝试
Try
这对我有用
This works for me
当您在 Mysql Workbench 中修改表约束或数据类型时,它会显示它将执行以完成请求的代码。
这是我从那个盒子里得到的查询。
但这里的问题是你不能将主键设置为空,你必须将其设置为唯一。
When you modify table constraint or datatype in Mysql workbench then it shows the code it is going to execute to complete the request.
And this is the query I got from that box.
But the catch here is that you can't have the primary key as null you have to set it unique instead.
这在 postgres 中对我有用:
This worked for me in postgres: