将空值条目阻止到数据库中 [英] Blocking null value entries into database

查看:104
本文介绍了将空值条目阻止到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个脚本来阻止将null值输入数据库.我可以通过对代码进行一些更改来做到这一点,但我的意图不是在代码中对其进行更改,而是编写可以运行以完成此任务的脚本

I want a script to block the entry of null valued into a database . i am able to do this by some changes in the code , but my intention is not change it in the code rather write a script which can be run to accomplish this task

例如表"book"具有author_name,price和title. 我不想允许任何具有空author_name的数据库条目

for example table " book " has author_name , price , title . i don't want to allow any entry into the database that has null author_name

推荐答案

您需要在列的定义中使用NOT NULL子句.例如-

You need to use NOT NULL clause in column's definition. For example -

CREATE TABLE book (
  author_name varchar(50) NOT NULL,
  price decimal(19, 2) DEFAULT NULL,
  title varchar(255) DEFAULT NULL
);

这就像约束一样.

一种限制空白值的解决方法-

A workaround to constraint blank values -

CREATE TRIGGER trigger1
  AFTER INSERT
  ON book
  FOR EACH ROW
BEGIN
  IF author_name = '' THEN
    SIGNAL SQLSTATE VALUE '02001'
      SET MESSAGE_TEXT = 'Blank value is not allowed'; -- Raise error
  END IF;
END

文档- SIGNAL 语句.

这篇关于将空值条目阻止到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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