必填字段mysql [英] Mandatory field mysql

查看:98
本文介绍了必填字段mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保该字段为必填字段?这就是我的意思

How to make sure that field is mandatory ? Here is what I mean

我具有以下mysql表结构:

I have the following mysql table structure:

CREATE TABLE `new` (
  `id` int(11) DEFAULT NULL,
  `name` int(11) DEFAULT NULL,
  `phone` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

这里是没有电话数据的查询

Here is query with no data for phone

insert into new values(1, 'm', '');

但是查询运行正常.如果没有电话字段数据,应该怎么做才能使mysql返回错误?我可以通过php进行验证,但我很好奇如何在mysql中进行验证.

But the query runs fine. What should be done so that mysql returns an error if there is no data for phone field? I can do that validation by php, but I'm curious how to do that in mysql.

推荐答案

INSERT INTO new VALUES(1,'m',NULL)

将导致错误.
如果要检查电话号码字段是否为空字符串,
您可以在MySQL中使用触发器.

will cause error.
If you want to check whether is the phone number field is a blank string,
you can use a trigger in MySQL.

这篇关于必填字段mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆